From ed2dff20f6ddc7b504f106ef9410a616c15a4cbc Mon Sep 17 00:00:00 2001 From: Tyfon <29051038+tyfon7@users.noreply.github.com> Date: Mon, 3 Jun 2024 02:49:16 -0700 Subject: [PATCH] Add snap buttons and keybinds to container grids --- Patches/GridWindowButtonsPatch.cs | 100 ++++++++++++++++++++++++++ Patches/InspectWindowResizePatches.cs | 16 +++-- Plugin.cs | 1 + R.cs | 2 +- Settings.cs | 10 +++ 5 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 Patches/GridWindowButtonsPatch.cs diff --git a/Patches/GridWindowButtonsPatch.cs b/Patches/GridWindowButtonsPatch.cs new file mode 100644 index 0000000..c34173a --- /dev/null +++ b/Patches/GridWindowButtonsPatch.cs @@ -0,0 +1,100 @@ +using Aki.Reflection.Patching; +using EFT.InventoryLogic; +using EFT.UI; +using HarmonyLib; +using System.Reflection; +using UnityEngine; +using UnityEngine.UI; + +namespace UIFixes +{ + public class GridWindowButtonsPatch : ModulePatch + { + protected override MethodBase GetTargetMethod() + { + return AccessTools.DeclaredMethod(typeof(GridWindow), nameof(GridWindow.Show)); + } + + [PatchPostfix] + public static void Postfix(GridWindow __instance) + { + if (Settings.AddContainerButtons.Value) + { + Transform closeButton = __instance.transform.Find("Caption Panel/Close Button"); + Image sortBackground = __instance.transform.Find("Caption Panel/Sort Button")?.GetComponent(); + + // Left button + Button leftButton = CreateButton(closeButton, sortBackground.sprite, EItemAttributeId.RecoilBack); + leftButton.onClick.AddListener(() => SnapLeft(__instance)); + __instance.R().UI.AddDisposable(() => leftButton.onClick.RemoveAllListeners()); + + // Right button + Button rightButton = CreateButton(closeButton, sortBackground.sprite, EItemAttributeId.RecoilBack); + rightButton.transform.Find("X").Rotate(0f, 180f, 0f); + rightButton.onClick.AddListener(() => SnapRight(__instance)); + __instance.R().UI.AddDisposable(() => rightButton.onClick.RemoveAllListeners()); + + // Put close back on the end + closeButton.SetAsLastSibling(); + } + + // Keybinds + LeftRightKeybind leftRightKeybind = __instance.GetOrAddComponent(); + leftRightKeybind.Init(__instance); + } + + private static Button CreateButton(Transform template, Sprite backgroundSprite, EItemAttributeId attributeIcon) + { + Transform transform = UnityEngine.Object.Instantiate(template, template.parent, false); + + Image background = transform.GetComponent(); + background.sprite = backgroundSprite; + + Image icon = transform.Find("X").GetComponent(); + icon.sprite = EFTHardSettings.Instance.StaticIcons.GetAttributeIcon(attributeIcon); + icon.overrideSprite = null; + icon.SetNativeSize(); + + Button button = transform.GetComponent