diff --git a/Patches/ContextMenuShortcutPatch.cs b/Patches/ContextMenuShortcutPatch.cs index f160189..4848d92 100644 --- a/Patches/ContextMenuShortcutPatch.cs +++ b/Patches/ContextMenuShortcutPatch.cs @@ -1,69 +1,97 @@ using Aki.Reflection.Patching; using EFT.InventoryLogic; using EFT.UI; +using EFT.UI.DragAndDrop; using HarmonyLib; +using System; using System.Reflection; +using UnityEngine.EventSystems; namespace UIFixes { - public class ContextMenuShortcutPatch : ModulePatch + public static class ContextMenuShortcutPatches { - protected override MethodBase GetTargetMethod() + public static void Enable() { - return AccessTools.Method(typeof(ItemUiContext), nameof(ItemUiContext.Update)); + new ItemUiContextPatch().Enable(); + new HideoutItemViewRegisterContextPatch().Enable(); } - [PatchPostfix] - public static void Postfix(ItemUiContext __instance) + public class ItemUiContextPatch : ModulePatch { - ItemContextAbstractClass itemContext = __instance.R().ItemContext; - if (itemContext == null) + protected override MethodBase GetTargetMethod() { - return; + return AccessTools.Method(typeof(ItemUiContext), nameof(ItemUiContext.Update)); } - if (Settings.InspectKeyBind.Value.IsDown()) + [PatchPostfix] + public static void Postfix(ItemUiContext __instance) { - __instance.GetItemContextInteractions(itemContext, null).ExecuteInteraction(EItemInfoButton.Inspect); - return; - } - - if (Settings.OpenKeyBind.Value.IsDown()) - { - __instance.GetItemContextInteractions(itemContext, null).ExecuteInteraction(EItemInfoButton.Open); - return; - } - - if (Settings.TopUpKeyBind.Value.IsDown()) - { - __instance.GetItemContextInteractions(itemContext, null).ExecuteInteraction(EItemInfoButton.TopUp); - return; - } - - if (Settings.UseKeyBind.Value.IsDown()) - { - __instance.GetItemContextInteractions(itemContext, null).ExecuteInteraction(EItemInfoButton.Use); - } - - if (Settings.UseAllKeyBind.Value.IsDown()) - { - var interactions = __instance.GetItemContextInteractions(itemContext, null); - if (!interactions.ExecuteInteraction(EItemInfoButton.UseAll)) + // Need an item context to operate on, and ignore these keypresses if there's a focused textbox somewhere + ItemContextAbstractClass itemContext = __instance.R().ItemContext; + if (itemContext == null || EventSystem.current.currentSelectedGameObject != null) { - interactions.ExecuteInteraction(EItemInfoButton.Use); + return; + } + + if (Settings.InspectKeyBind.Value.IsDown()) + { + __instance.GetItemContextInteractions(itemContext, null).ExecuteInteraction(EItemInfoButton.Inspect); + return; + } + + if (Settings.OpenKeyBind.Value.IsDown()) + { + __instance.GetItemContextInteractions(itemContext, null).ExecuteInteraction(EItemInfoButton.Open); + return; + } + + if (Settings.TopUpKeyBind.Value.IsDown()) + { + __instance.GetItemContextInteractions(itemContext, null).ExecuteInteraction(EItemInfoButton.TopUp); + return; + } + + if (Settings.UseKeyBind.Value.IsDown()) + { + __instance.GetItemContextInteractions(itemContext, null).ExecuteInteraction(EItemInfoButton.Use); + } + + if (Settings.UseAllKeyBind.Value.IsDown()) + { + var interactions = __instance.GetItemContextInteractions(itemContext, null); + if (!interactions.ExecuteInteraction(EItemInfoButton.UseAll)) + { + interactions.ExecuteInteraction(EItemInfoButton.Use); + } + } + + if (Settings.FilterByKeyBind.Value.IsDown()) + { + __instance.GetItemContextInteractions(itemContext, null).ExecuteInteraction(EItemInfoButton.FilterSearch); + return; + } + + if (Settings.LinkedSearchKeyBind.Value.IsDown()) + { + __instance.GetItemContextInteractions(itemContext, null).ExecuteInteraction(EItemInfoButton.LinkedSearch); + return; } } + } - if (Settings.FilterByKeyBind.Value.IsDown()) + // HideoutItemViews don't register themselves with ItemUiContext for some reason + public class HideoutItemViewRegisterContextPatch : ModulePatch + { + protected override MethodBase GetTargetMethod() { - __instance.GetItemContextInteractions(itemContext, null).ExecuteInteraction(EItemInfoButton.FilterSearch); - return; + return AccessTools.Method(typeof(HideoutItemView), nameof(HideoutItemView.OnPointerEnter)); } - if (Settings.LinkedSearchKeyBind.Value.IsDown()) + [PatchPostfix] + public static void Postfix(HideoutItemView __instance) { - __instance.GetItemContextInteractions(itemContext, null).ExecuteInteraction(EItemInfoButton.LinkedSearch); - return; + ItemUiContext.Instance.RegisterCurrentItemContext(__instance.ItemContext); } } } diff --git a/Patches/FleaPrevSearchPatches.cs b/Patches/FleaPrevSearchPatches.cs index 8ed6513..9250872 100644 --- a/Patches/FleaPrevSearchPatches.cs +++ b/Patches/FleaPrevSearchPatches.cs @@ -269,7 +269,7 @@ namespace UIFixes } [PatchPrefix] - public static void Prefix(RagfairScreen __instance, ISession session, DefaultUIButton ____addOfferButton, ref PreviousFilterButton __state) + public static void Prefix(DefaultUIButton ____addOfferButton, ref PreviousFilterButton __state) { // Create previous button if (!Settings.EnableFleaHistory.Value) @@ -361,7 +361,7 @@ namespace UIFixes } [PatchPostfix] - public static async void Postfix(OfferViewList __instance, Task __result, LightScroller ____scroller, EViewListType ___eviewListType_0) + public static async void Postfix(OfferViewList __instance, Task __result, EViewListType ___eviewListType_0) { await __result; diff --git a/Patches/HideoutSearchPatches.cs b/Patches/HideoutSearchPatches.cs index dd81a96..aba3494 100644 --- a/Patches/HideoutSearchPatches.cs +++ b/Patches/HideoutSearchPatches.cs @@ -6,6 +6,8 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; +using UnityEngine; +using UnityEngine.EventSystems; using UnityEngine.UI; namespace UIFixes @@ -14,18 +16,21 @@ namespace UIFixes { private static readonly Dictionary LastSearches = []; + private static float LastAbsoluteDownScrollPosition = -1f; + public static void Enable() { - new FixHideoutSearchPatch().Enable(); + new LazyLoadPatch().Enable(); new RestoreHideoutSearchPatch().Enable(); new SaveHideoutSearchPatch().Enable(); new CloseHideoutSearchPatch().Enable(); new FastHideoutSearchPatch().Enable(); new FixHideoutSearchAgainPatch().Enable(); + new CancelScrollOnMouseWheelPatch().Enable(); } // Deactivate ProduceViews as they lazy load if they don't match the search - public class FixHideoutSearchPatch : ModulePatch + public class LazyLoadPatch : ModulePatch { protected override MethodBase GetTargetMethod() { @@ -43,6 +48,27 @@ namespace UIFixes { view.GameObject.SetActive(false); } + + // As the objects load in, try to restore the old scroll position + if (LastAbsoluteDownScrollPosition >= 0f) + { + ScrollRect scrollRect = view.GetComponentInParent(); + if (scrollRect != null) + { + LayoutRebuilder.ForceRebuildLayoutImmediate(scrollRect.RectTransform()); + float currentAbsoluteDownScrollPosition = (1f - scrollRect.verticalNormalizedPosition) * (scrollRect.content.rect.height - scrollRect.viewport.rect.height); + if (LastAbsoluteDownScrollPosition > currentAbsoluteDownScrollPosition + 112f) // 112 is about the height of each item + { + scrollRect.verticalNormalizedPosition = 0f; + } + else + { + // Last one, try to set it exactly + scrollRect.verticalNormalizedPosition = 1f - (LastAbsoluteDownScrollPosition / (scrollRect.content.rect.height - scrollRect.viewport.rect.height)); + LastAbsoluteDownScrollPosition = -1f; + } + } + } } } @@ -130,6 +156,14 @@ namespace UIFixes { LastSearches[__instance.AreaData.ToString()] = ____searchInputField.text; + ScrollRect scrollRect = __instance.GetComponentInParent(); + if (scrollRect != null) + { + // Need to save the absolute DOWN position, because that's the direction the scrollbox will grow. + // Subtract the viewport height from content heigh because that's the actual RANGE of the scroll position + LastAbsoluteDownScrollPosition = (1f - scrollRect.verticalNormalizedPosition) * (scrollRect.content.rect.height - scrollRect.viewport.rect.height); + } + // Reset the default behavior AreaScreenSubstrate areaScreenSubstrate = __instance.GetComponentInParent(); LayoutElement layoutElement = areaScreenSubstrate.R().ContentLayout; @@ -149,6 +183,21 @@ namespace UIFixes public static void Postfix() { LastSearches.Clear(); + LastAbsoluteDownScrollPosition = -1f; + } + } + + public class CancelScrollOnMouseWheelPatch : ModulePatch + { + protected override MethodBase GetTargetMethod() + { + return AccessTools.Method(typeof(ScrollRectNoDrag), nameof(ScrollRectNoDrag.OnScroll)); + } + + [PatchPostfix] + public static void Postfix() + { + LastAbsoluteDownScrollPosition = -1f; } } } diff --git a/Patches/LoadAmmoInRaidPatches.cs b/Patches/LoadAmmoInRaidPatches.cs index 72b8c54..e1453a1 100644 --- a/Patches/LoadAmmoInRaidPatches.cs +++ b/Patches/LoadAmmoInRaidPatches.cs @@ -1,8 +1,10 @@ using Aki.Reflection.Patching; +using Aki.Reflection.Utils; using EFT.InventoryLogic; using EFT.UI; using EFT.UI.DragAndDrop; using HarmonyLib; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -22,11 +24,12 @@ namespace UIFixes { protected override MethodBase GetTargetMethod() { - return AccessTools.Method(typeof(GClass3052), nameof(GClass3052.IsActive)); + Type type = PatchConstants.EftTypes.Single(t => t.GetProperty("IsOwnedByPlayer") != null); + return AccessTools.Method(type, "IsActive"); } [PatchPrefix] - public static bool Prefix(GClass3052 __instance, EItemInfoButton button, ref bool __result, Item ___item_0) + public static bool Prefix(EItemInfoButton button, ref bool __result, Item ___item_0) { if (button != EItemInfoButton.LoadAmmo || !Plugin.InRaid() || !Settings.EnableLoadAmmo.Value) { diff --git a/Plugin.cs b/Plugin.cs index ded791f..b45f0c5 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -46,7 +46,7 @@ namespace UIFixes new GridWindowButtonsPatch().Enable(); new LoadMagPresetsPatch().Enable(); KeepWindowsOnScreenPatches.Enable(); - new ContextMenuShortcutPatch().Enable(); + ContextMenuShortcutPatches.Enable(); new OpenSortingTablePatch().Enable(); LoadAmmoInRaidPatches.Enable(); }