Wishlist context menu everywhere

This commit is contained in:
Tyfon
2024-09-26 15:40:14 -07:00
parent 466f5c24b8
commit 5259d4e60c
5 changed files with 127 additions and 39 deletions

View File

@@ -1,4 +1,4 @@
// These shouln't change (unless they do) // These shouldn't change (unless they do)
global using GridItemAddress = ItemAddressClass; global using GridItemAddress = ItemAddressClass;
global using DragItemContext = ItemContextClass; global using DragItemContext = ItemContextClass;
global using InsuranceItem = ItemClass; global using InsuranceItem = ItemClass;

View File

@@ -419,6 +419,38 @@ public class MultiSelect
} }
} }
public static void WishlistAll(ItemUiContext itemUiContext, BaseItemInfoInteractions interactions, bool add, bool allOrNothing)
{
EItemInfoButton interaction = add ? EItemInfoButton.AddToWishlist : EItemInfoButton.RemoveFromWishlist;
if (!allOrNothing || InteractionCount(interaction, itemUiContext) == Count)
{
var taskSerializer = itemUiContext.gameObject.AddComponent<MultiSelectItemContextTaskSerializer>();
taskSerializer.Initialize(ItemContexts.Where(ic => InteractionAvailable(ic, interaction, itemUiContext)),
itemContext =>
{
TaskCompletionSource taskSource = new();
void callback()
{
interactions.RequestRedrawForItem();
taskSource.Complete();
}
if (add)
{
itemUiContext.AddToWishList(itemContext.Item, callback);
}
else
{
itemUiContext.RemoveFromWishList(itemContext.Item, callback);
}
return taskSource.Task;
});
itemUiContext.Tooltip?.Close();
}
}
private static void ShowSelection(GridItemView itemView) private static void ShowSelection(GridItemView itemView)
{ {
GameObject selectedMark = itemView.transform.Find("SelectedMark")?.gameObject; GameObject selectedMark = itemView.transform.Find("SelectedMark")?.gameObject;

View File

@@ -4,6 +4,7 @@ using EFT.UI;
using EFT.UI.DragAndDrop; using EFT.UI.DragAndDrop;
using HarmonyLib; using HarmonyLib;
using SPT.Reflection.Patching; using SPT.Reflection.Patching;
using SPT.Reflection.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -47,6 +48,9 @@ public static class ContextMenuPatches
new EmptyModSlotMenuRemovePatch().Enable(); new EmptyModSlotMenuRemovePatch().Enable();
new EmptySlotMenuPatch().Enable(); new EmptySlotMenuPatch().Enable();
new EmptySlotMenuRemovePatch().Enable(); new EmptySlotMenuRemovePatch().Enable();
new InventoryWishlistPatch().Enable();
new TradingWishlistPatch().Enable();
} }
public class ContextMenuNamesPatch : ModulePatch public class ContextMenuNamesPatch : ModulePatch
@@ -64,65 +68,51 @@ public static class ContextMenuPatches
return; return;
} }
int count = 0;
if (caption == EItemInfoButton.Insure.ToString()) if (caption == EItemInfoButton.Insure.ToString())
{ {
InsuranceCompanyClass insurance = ItemUiContext.Instance.Session.InsuranceCompany; InsuranceCompanyClass insurance = ItemUiContext.Instance.Session.InsuranceCompany;
int count = MultiSelect.ItemContexts.Select(ic => InsuranceItem.FindOrCreate(ic.Item)) count = MultiSelect.ItemContexts.Select(ic => InsuranceItem.FindOrCreate(ic.Item))
.Where(i => insurance.ItemTypeAvailableForInsurance(i) && !insurance.InsuredItems.Contains(i)) .Where(i => insurance.ItemTypeAvailableForInsurance(i) && !insurance.InsuredItems.Contains(i))
.Count(); .Count();
if (count > 0)
{
____text.text += " (x" + count + ")";
}
} }
else if (caption == EItemInfoButton.Equip.ToString()) else if (caption == EItemInfoButton.Equip.ToString())
{ {
int count = MultiSelect.InteractionCount(EItemInfoButton.Equip, ItemUiContext.Instance); count = MultiSelect.InteractionCount(EItemInfoButton.Equip, ItemUiContext.Instance);
if (count > 0)
{
____text.text += " (x" + count + ")";
}
} }
else if (caption == EItemInfoButton.Unequip.ToString()) else if (caption == EItemInfoButton.Unequip.ToString())
{ {
int count = MultiSelect.InteractionCount(EItemInfoButton.Unequip, ItemUiContext.Instance); count = MultiSelect.InteractionCount(EItemInfoButton.Unequip, ItemUiContext.Instance);
if (count > 0)
{
____text.text += " (x" + count + ")";
}
} }
else if (caption == EItemInfoButton.LoadAmmo.ToString()) else if (caption == EItemInfoButton.LoadAmmo.ToString())
{ {
int count = MultiSelect.InteractionCount(EItemInfoButton.LoadAmmo, ItemUiContext.Instance); count = MultiSelect.InteractionCount(EItemInfoButton.LoadAmmo, ItemUiContext.Instance);
if (count > 0)
{
____text.text += " (x" + count + ")";
}
} }
else if (caption == EItemInfoButton.UnloadAmmo.ToString()) else if (caption == EItemInfoButton.UnloadAmmo.ToString())
{ {
int count = MultiSelect.InteractionCount(EItemInfoButton.UnloadAmmo, ItemUiContext.Instance); count = MultiSelect.InteractionCount(EItemInfoButton.UnloadAmmo, ItemUiContext.Instance);
if (count > 0)
{
____text.text += " (x" + count + ")";
}
} }
else if (caption == EItemInfoButton.ApplyMagPreset.ToString()) else if (caption == EItemInfoButton.ApplyMagPreset.ToString())
{ {
int count = MultiSelect.InteractionCount(EItemInfoButton.ApplyMagPreset, ItemUiContext.Instance); count = MultiSelect.InteractionCount(EItemInfoButton.ApplyMagPreset, ItemUiContext.Instance);
if (count > 0)
{
____text.text += " (x" + count + ")";
}
} }
else if (caption == EItemInfoButton.Unpack.ToString()) else if (caption == EItemInfoButton.Unpack.ToString())
{ {
int count = MultiSelect.InteractionCount(EItemInfoButton.Unpack, ItemUiContext.Instance); count = MultiSelect.InteractionCount(EItemInfoButton.Unpack, ItemUiContext.Instance);
if (count > 0) }
{ else if (caption == EItemInfoButton.AddToWishlist.ToString())
____text.text += " (x" + count + ")"; {
} count = MultiSelect.InteractionCount(EItemInfoButton.AddToWishlist, ItemUiContext.Instance);
}
else if (caption == EItemInfoButton.RemoveFromWishlist.ToString())
{
count = MultiSelect.InteractionCount(EItemInfoButton.RemoveFromWishlist, ItemUiContext.Instance);
}
if (count > 0)
{
____text.text += " (x" + count + ")";
} }
} }
} }
@@ -519,7 +509,9 @@ public static class ContextMenuPatches
{ {
protected override MethodBase GetTargetMethod() protected override MethodBase GetTargetMethod()
{ {
return AccessTools.Method(typeof(InteractionButtonsContainer), nameof(InteractionButtonsContainer.SetSubInteractions)).MakeGenericMethod([typeof(InsuranceInteractions.EInsurers)]); return AccessTools.Method(
typeof(InteractionButtonsContainer),
nameof(InteractionButtonsContainer.SetSubInteractions)).MakeGenericMethod([typeof(InsuranceInteractions.EInsurers)]);
} }
// Existing logic tries to place it on the right, moving to the left if necessary. They didn't do it correctly, so it always goes on the left. // Existing logic tries to place it on the right, moving to the left if necessary. They didn't do it correctly, so it always goes on the left.
@@ -530,6 +522,54 @@ public static class ContextMenuPatches
} }
} }
public class InventoryWishlistPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
// R.InventoryActions.Type is only ever referenced by it's child class, which overrides AvailableInteractions
Type type = PatchConstants.EftTypes.First(t => t.BaseType == R.InventoryInteractions.Type);
return AccessTools.DeclaredProperty(type, "AvailableInteractions").GetMethod;
}
[PatchPostfix]
public static void Postfix(ref IEnumerable<EItemInfoButton> __result)
{
if (!Settings.WishlistContextEverywhere.Value)
{
return;
}
var list = __result.ToList();
int index = list.IndexOf(EItemInfoButton.Tag);
list.Insert(index, EItemInfoButton.RemoveFromWishlist);
list.Insert(index, EItemInfoButton.AddToWishlist);
__result = list;
}
}
public class TradingWishlistPatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return AccessTools.DeclaredProperty(R.TradingInteractions.Type, "AvailableInteractions").GetMethod;
}
[PatchPostfix]
public static void Postfix(ref IEnumerable<EItemInfoButton> __result)
{
if (!Settings.WishlistContextEverywhere.Value)
{
return;
}
var list = __result.ToList();
int index = list.IndexOf(EItemInfoButton.Tag);
list.Insert(index, EItemInfoButton.RemoveFromWishlist);
list.Insert(index, EItemInfoButton.AddToWishlist);
__result = list;
}
}
private static void PositionContextMenuFlyout(SimpleContextMenuButton button, SimpleContextMenu flyoutMenu) private static void PositionContextMenuFlyout(SimpleContextMenuButton button, SimpleContextMenu flyoutMenu)
{ {
RectTransform buttonTransform = button.RectTransform(); RectTransform buttonTransform = button.RectTransform();

View File

@@ -315,7 +315,7 @@ public static class MultiSelectPatches
} }
[PatchPrefix] [PatchPrefix]
public static bool Prefix(EItemInfoButton interaction, ItemUiContext ___itemUiContext_1) public static bool Prefix(BaseItemInfoInteractions __instance, EItemInfoButton interaction, ItemUiContext ___itemUiContext_1)
{ {
if (!MultiSelect.Active) if (!MultiSelect.Active)
{ {
@@ -336,6 +336,12 @@ public static class MultiSelectPatches
case EItemInfoButton.Unpack: case EItemInfoButton.Unpack:
MultiSelect.UnpackAll(___itemUiContext_1, false); MultiSelect.UnpackAll(___itemUiContext_1, false);
return false; return false;
case EItemInfoButton.AddToWishlist:
MultiSelect.WishlistAll(___itemUiContext_1, __instance, true, false);
return false;
case EItemInfoButton.RemoveFromWishlist:
MultiSelect.WishlistAll(___itemUiContext_1, __instance, false, false);
return false;
default: default:
return true; return true;
} }

View File

@@ -138,6 +138,7 @@ internal class Settings
public static ConfigEntry<bool> DefaultSortingTableBind { get; set; } // Advanced public static ConfigEntry<bool> DefaultSortingTableBind { get; set; } // Advanced
public static ConfigEntry<bool> ContextMenuOnRight { get; set; } public static ConfigEntry<bool> ContextMenuOnRight { get; set; }
public static ConfigEntry<bool> AddOfferContextMenu { get; set; } public static ConfigEntry<bool> AddOfferContextMenu { get; set; }
public static ConfigEntry<bool> WishlistContextEverywhere { get; set; }
public static ConfigEntry<bool> ShowGPCurrency { get; set; } public static ConfigEntry<bool> ShowGPCurrency { get; set; }
public static ConfigEntry<bool> ShowOutOfStockCheckbox { get; set; } public static ConfigEntry<bool> ShowOutOfStockCheckbox { get; set; }
public static ConfigEntry<SortingTableDisplay> SortingTableButton { get; set; } public static ConfigEntry<SortingTableDisplay> SortingTableButton { get; set; }
@@ -730,7 +731,7 @@ internal class Settings
new ConfigurationManagerAttributes { }))); new ConfigurationManagerAttributes { })));
configEntries.Add(AddOfferContextMenu = config.Bind( configEntries.Add(AddOfferContextMenu = config.Bind(
InputSection, InventorySection,
"Add Offer Context Menu", "Add Offer Context Menu",
true, true,
new ConfigDescription( new ConfigDescription(
@@ -738,6 +739,15 @@ internal class Settings
null, null,
new ConfigurationManagerAttributes { }))); new ConfigurationManagerAttributes { })));
configEntries.Add(WishlistContextEverywhere = config.Bind(
InventorySection,
"Wishlist Context Menu Everywhere",
true,
new ConfigDescription(
"Add/Remove to wishlist available in the context menu on all screens",
null,
new ConfigurationManagerAttributes { })));
configEntries.Add(ShowGPCurrency = config.Bind( configEntries.Add(ShowGPCurrency = config.Bind(
InventorySection, InventorySection,
"Show GP Coins in Currency", "Show GP Coins in Currency",