diff --git a/Patches/ScrollSyncPatches.cs b/Patches/ScrollSyncPatches.cs new file mode 100644 index 0000000..a729dd4 --- /dev/null +++ b/Patches/ScrollSyncPatches.cs @@ -0,0 +1,85 @@ +using Aki.Reflection.Patching; +using EFT.UI; +using EFT.UI.Ragfair; +using HarmonyLib; +using System.Reflection; +using UnityEngine; +using UnityEngine.UI; + +namespace UIFixes +{ + public class ScrollSyncPatches + { + private static float StashScrollPosition = 1f; + + public static void Enable() + { + new SimpleStashPanelPatch().Enable(); + new TraderDealScreenPatch().Enable(); + new AddOfferWindowPatch().Enable(); + } + + private static void UpdateScrollPosition(Vector2 position) + { + StashScrollPosition = position.y; + } + + private static void SynchronizeScrollRect(UIElement element, ScrollRect scrollRect = null) + { + if (!Settings.SynchronizeStashScrolling.Value || element == null || (scrollRect ??= element.GetComponentInChildren()) == null) + { + return; + } + + scrollRect.verticalNormalizedPosition = StashScrollPosition; + scrollRect.onValueChanged.AddListener(UpdateScrollPosition); + } + + private class SimpleStashPanelPatch : ModulePatch + { + protected override MethodBase GetTargetMethod() + { + return AccessTools.Method(typeof(SimpleStashPanel), "Show"); + } + + [PatchPostfix] + private static void Postfix(SimpleStashPanel __instance) + { + SynchronizeScrollRect(__instance); + } + } + + public class TraderDealScreenPatch : ModulePatch + { + protected override MethodBase GetTargetMethod() + { + return AccessTools.Method(typeof(TraderDealScreen), "method_3"); + } + + // TraderDealScreen is a monstrosity that loads multiple times and isn't done loading when Show() is done + // method_3 shows the stash grid, if method_5() returned true + [PatchPostfix] + private static void Postfix(TraderDealScreen __instance, ScrollRect ____stashScroll) + { + if (__instance.method_5()) + { + SynchronizeScrollRect(__instance, ____stashScroll); + } + } + } + + public class AddOfferWindowPatch : ModulePatch + { + protected override MethodBase GetTargetMethod() + { + return AccessTools.Method(typeof(AddOfferWindow), "Show"); + } + + [PatchPostfix] + private static void Postfix(AddOfferWindow __instance) + { + SynchronizeScrollRect(__instance); + } + } + } +} diff --git a/Plugin.cs b/Plugin.cs index b0f6f81..bcca119 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -23,6 +23,7 @@ namespace UIFixes DialogPatches.Enable(); ItemPanelResizePatches.Enable(); ProductionPanelPatches.Enable(); + ScrollSyncPatches.Enable(); } } } diff --git a/Settings.cs b/Settings.cs index 1675505..9520beb 100644 --- a/Settings.cs +++ b/Settings.cs @@ -40,6 +40,7 @@ namespace UIFixes // Inventory public static ConfigEntry SwapItems { get; set; } public static ConfigEntry SwapImpossibleContainers { get; set; } + public static ConfigEntry SynchronizeStashScrolling { get; set; } public static ConfigEntry MergeFIRMoney { get; set; } public static ConfigEntry MergeFIRAmmo { get; set; } public static ConfigEntry MergeFIROther { get; set; } @@ -126,6 +127,15 @@ namespace UIFixes null, new ConfigurationManagerAttributes { }))); + configEntries.Add(SynchronizeStashScrolling = config.Bind( + InventorySection, + "Synchronize Stash Scroll Position", + false, + new ConfigDescription( + "Remember your scroll position all the places you see your stash - inventory, trading screen, mail screen, etc.", + null, + new ConfigurationManagerAttributes { }))); + configEntries.Add(MergeFIRMoney = config.Bind( InventorySection, "Autostack Money with FiR Money",