From 0277490e8a3b9621c577d5855ea200b7e45e133f Mon Sep 17 00:00:00 2001 From: Tyfon <29051038+tyfon7@users.noreply.github.com> Date: Sat, 13 Jul 2024 15:54:28 -0700 Subject: [PATCH] fix bsg bug that loses context on search, fixing multiselect drawing when mouse starts over unsearched item that becomes searched --- .vscode/tasks.json | 1 + Multiselect/MultiSelect.cs | 4 +- Multiselect/MultiSelectDebug.cs | 30 +++++++++++---- Patches/MultiSelectPatches.cs | 66 +++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 10 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 0b4d6cd..7a89f44 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -13,6 +13,7 @@ "presentation": { "echo": true, "reveal": "always", + "revealProblems": "onProblem", "focus": false, "panel": "shared", "showReuseMessage": false, diff --git a/Multiselect/MultiSelect.cs b/Multiselect/MultiSelect.cs index db9b4a3..8ef887e 100644 --- a/Multiselect/MultiSelect.cs +++ b/Multiselect/MultiSelect.cs @@ -196,12 +196,12 @@ public class MultiSelect SecondaryItems.Clear(); } - public static IEnumerable ItemContexts + public static IEnumerable ItemContexts { get { return SelectedItems.Keys; } } - public static IEnumerable SecondaryContexts + public static IEnumerable SecondaryContexts { get { return SecondaryItems.Keys; } } diff --git a/Multiselect/MultiSelectDebug.cs b/Multiselect/MultiSelectDebug.cs index 2660b8a..90cef72 100644 --- a/Multiselect/MultiSelectDebug.cs +++ b/Multiselect/MultiSelectDebug.cs @@ -1,5 +1,7 @@ using System.Linq; using System.Text; +using EFT.InventoryLogic; +using EFT.UI; using UnityEngine; namespace UIFixes; @@ -32,22 +34,18 @@ public class MultiSelectDebug : MonoBehaviour builder.Append("MultiSelect\n"); builder.AppendFormat("Active: {1}\n", MultiSelect.Active ? "green" : "red", MultiSelect.Active); + builder.AppendFormat("Hovered: {0}\n", FormatItemContext(ItemUiContext.Instance.R().ItemContext)); builder.AppendFormat("Items: {0}\n", MultiSelect.Count); - foreach (DragItemContext itemContext in MultiSelect.SortedItemContexts()) + foreach (MultiSelectItemContext itemContext in MultiSelect.SortedItemContexts()) { - LocationInGrid location = itemContext.ItemAddress is GridItemAddress gridAddress ? MultiGrid.GetGridLocation(gridAddress) : null; - builder.AppendFormat("x{0} {1} {2} {3}\n", - itemContext.Item.StackObjectsCount, - itemContext.ItemAddress.Container.ID, - location != null ? $"({location.x}, {location.y})" : "(slot)", - itemContext.Item.Name.Localized()); + builder.AppendFormat("{0}\n", FormatItemContext(itemContext)); } if (MultiSelect.SecondaryContexts.Any()) { builder.AppendFormat("Secondary Items: {0}\n", MultiSelect.SecondaryCount); - foreach (DragItemContext itemContext in MultiSelect.SecondaryContexts) + foreach (MultiSelectItemContext itemContext in MultiSelect.SecondaryContexts) { builder.AppendFormat("x{0} {1}\n", itemContext.Item.StackObjectsCount, itemContext.Item.ToString()); } @@ -59,4 +57,20 @@ public class MultiSelectDebug : MonoBehaviour GUI.Box(guiRect, guiContent, guiStyle); } + + private string FormatItemContext(ItemContextAbstractClass itemContext) + { + if (itemContext == null) + { + return "null"; + } + + ItemAddress address = itemContext is DragItemContext dragItemContext ? dragItemContext.ItemAddress : itemContext.Item.CurrentAddress; + LocationInGrid location = address is GridItemAddress gridAddress ? + itemContext is MultiSelectItemContext ? MultiGrid.GetGridLocation(gridAddress) : gridAddress.LocationInGrid : + null; + string locationString = location != null ? $"({location.x}, {location.y})" : "(slot)"; + + return $"x{itemContext.Item.StackObjectsCount} {address.Container.ID} {locationString} {itemContext.Item.Name.Localized()}"; + } } diff --git a/Patches/MultiSelectPatches.cs b/Patches/MultiSelectPatches.cs index fbefba2..af99c0c 100644 --- a/Patches/MultiSelectPatches.cs +++ b/Patches/MultiSelectPatches.cs @@ -54,6 +54,7 @@ public static class MultiSelectPatches // Workarounds new DisableSplitPatch().Enable(); new DisableSplitTargetPatch().Enable(); + new FixSearchedContextPatch().Enable(); // Actions new ItemViewClickPatch().Enable(); @@ -425,6 +426,71 @@ public static class MultiSelectPatches } } + public class FixSearchedContextPatch : ModulePatch + { + private static string CurrentContextSearchingId = null; + + protected override MethodBase GetTargetMethod() + { + return AccessTools.Method(typeof(GridView), nameof(GridView.OnItemAdded)); + } + + [PatchPrefix] + public static void Prefix(GridView __instance, GEventArgs2 eventArgs, TraderControllerClass ___traderControllerClass, ItemUiContext ___itemUiContext_0) + { + if (eventArgs.To.Container != __instance.Grid || eventArgs.Status != CommandStatus.Begin) + { + return; + } + + Item item = eventArgs.Item; + if (___itemUiContext_0.ItemContextAbstractClass?.Item != eventArgs.Item) + { + return; + } + + LocationInGrid locationInGrid = ((GridItemAddress)eventArgs.To).LocationInGrid; + + bool searchable = !(___traderControllerClass != null && ___traderControllerClass.CanSearch && ___traderControllerClass.ID != eventArgs.OwnerId); + if (!searchable || !locationInGrid.isSearched) + { + return; + } + + ItemView itemView = __instance.method_9(item, iv => !iv.IsSearched); + if (itemView != null) + { + CurrentContextSearchingId = item.Id; + } + } + + [PatchPostfix] + public static void Postfix(GridView __instance, GEventArgs2 eventArgs, ItemUiContext ___itemUiContext_0, Dictionary ___dictionary_0) + { + if (eventArgs.To.Container != __instance.Grid) + { + return; + } + + if (CurrentContextSearchingId == eventArgs.Item.Id) + { + if (eventArgs.Status == CommandStatus.Succeed) + { + if (___dictionary_0.TryGetValue(eventArgs.Item.Id, out ItemView itemView)) + { + ___itemUiContext_0.RegisterCurrentItemContext(itemView.ItemContext); + } + + CurrentContextSearchingId = null; + } + else if (eventArgs.Status == CommandStatus.Failed) + { + CurrentContextSearchingId = null; + } + } + } + } + public class GridViewCanAcceptPatch : ModulePatch { protected override MethodBase GetTargetMethod()