From 14c2e796588cc468ee7bc63ea9eb818a09393256 Mon Sep 17 00:00:00 2001 From: Tyfon <29051038+tyfon7@users.noreply.github.com> Date: Mon, 22 Jul 2024 15:04:00 -0700 Subject: [PATCH] examine keybind; unlock cursor --- GlobalUsings.cs | 1 + Patches/ContextMenuShortcutPatches.cs | 22 ++++++++++++++---- Patches/UnlockCursorPatch.cs | 33 +++++++++++++++++++++++++++ Plugin.cs | 1 + Settings.cs | 20 ++++++++++++++++ 5 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 Patches/UnlockCursorPatch.cs diff --git a/GlobalUsings.cs b/GlobalUsings.cs index 324da0f..19651d1 100644 --- a/GlobalUsings.cs +++ b/GlobalUsings.cs @@ -18,6 +18,7 @@ global using DiscardResult = GClass2799; global using ItemSorter = GClass2772; global using ItemWithLocation = GClass2521; global using SearchableGrid = GClass2516; +global using CursorManager = GClass3034; // State machine states global using FirearmReadyState = EFT.Player.FirearmController.GClass1619; diff --git a/Patches/ContextMenuShortcutPatches.cs b/Patches/ContextMenuShortcutPatches.cs index 61e110c..e2de8f9 100644 --- a/Patches/ContextMenuShortcutPatches.cs +++ b/Patches/ContextMenuShortcutPatches.cs @@ -75,12 +75,12 @@ public static class ContextMenuShortcutPatches if (Settings.UseAllKeyBind.Value.IsDown()) { - TryInteraction(__instance, itemContext, EItemInfoButton.UseAll, EItemInfoButton.Use); + TryInteraction(__instance, itemContext, EItemInfoButton.UseAll, [EItemInfoButton.Use]); } if (Settings.UnloadKeyBind.Value.IsDown()) { - TryInteraction(__instance, itemContext, EItemInfoButton.Unload, EItemInfoButton.UnloadAmmo); + TryInteraction(__instance, itemContext, EItemInfoButton.Unload, [EItemInfoButton.UnloadAmmo]); } if (Settings.UnpackKeyBind.Value.IsDown()) @@ -103,15 +103,27 @@ public static class ContextMenuShortcutPatches MoveToFromSortingTable(itemContext, __instance); } + if (Settings.ExamineKeyBind.Value.IsDown()) + { + TryInteraction(__instance, itemContext, EItemInfoButton.Examine, + [EItemInfoButton.Fold, EItemInfoButton.Unfold, EItemInfoButton.TurnOn, EItemInfoButton.TurnOff, EItemInfoButton.CheckMagazine]); + } + Interactions = null; } - private static void TryInteraction(ItemUiContext itemUiContext, ItemContextAbstractClass itemContext, EItemInfoButton interaction, EItemInfoButton? fallbackInteraction = null) + private static void TryInteraction(ItemUiContext itemUiContext, ItemContextAbstractClass itemContext, EItemInfoButton interaction, EItemInfoButton[] fallbackInteractions = null) { Interactions ??= itemUiContext.GetItemContextInteractions(itemContext, null); - if (!Interactions.ExecuteInteraction(interaction) && fallbackInteraction.HasValue) + if (!Interactions.ExecuteInteraction(interaction) && fallbackInteractions != null) { - Interactions.ExecuteInteraction(fallbackInteraction.Value); + foreach (var fallbackInteraction in fallbackInteractions) + { + if (Interactions.ExecuteInteraction(fallbackInteraction)) + { + return; + } + } } } diff --git a/Patches/UnlockCursorPatch.cs b/Patches/UnlockCursorPatch.cs new file mode 100644 index 0000000..fc67210 --- /dev/null +++ b/Patches/UnlockCursorPatch.cs @@ -0,0 +1,33 @@ +using HarmonyLib; +using SPT.Reflection.Patching; +using System; +using System.Linq; +using System.Reflection; +using UnityEngine; + +namespace UIFixes; + +public class UnlockCursorPatch : ModulePatch +{ + private static readonly FullScreenMode[] WindowedModes = [FullScreenMode.Windowed, FullScreenMode.MaximizedWindow, FullScreenMode.FullScreenWindow]; + + protected override MethodBase GetTargetMethod() + { + return AccessTools.Method(typeof(CursorManager), nameof(CursorManager.SetCursorLockMode)); + } + + [PatchPrefix] + public static bool Prefix(bool cursorVisible, FullScreenMode fullscreenMode, Action ___action_0) + { + Cursor.lockState = cursorVisible ? + Settings.UnlockCursor.Value && WindowedModes.Contains(fullscreenMode) ? CursorLockMode.None : CursorLockMode.Confined : + CursorLockMode.Locked; + + if (___action_0 != null) + { + ___action_0(); + } + + return false; + } +} \ No newline at end of file diff --git a/Plugin.cs b/Plugin.cs index 5e0923c..5072daa 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -67,6 +67,7 @@ public class Plugin : BaseUnityPlugin SortPatches.Enable(); ReloadInPlacePatches.Enable(); BarterOfferPatches.Enable(); + new UnlockCursorPatch().Enable(); } public static bool InRaid() diff --git a/Settings.cs b/Settings.cs index 56c5c8f..7d1cc59 100644 --- a/Settings.cs +++ b/Settings.cs @@ -48,6 +48,7 @@ internal class Settings private const string FleaMarketSection = "6. Flea Market"; // General + public static ConfigEntry UnlockCursor { get; set; } public static ConfigEntry ShowPresetConfirmations { get; set; } public static ConfigEntry ShowTransferConfirmations { get; set; } public static ConfigEntry KeepMessagesOpen { get; set; } @@ -63,6 +64,7 @@ internal class Settings public static ConfigEntry MouseScrollMulti { get; set; } public static ConfigEntry InspectKeyBind { get; set; } public static ConfigEntry OpenKeyBind { get; set; } + public static ConfigEntry ExamineKeyBind { get; set; } public static ConfigEntry TopUpKeyBind { get; set; } public static ConfigEntry UseKeyBind { get; set; } public static ConfigEntry UseAllKeyBind { get; set; } @@ -133,6 +135,15 @@ internal class Settings var configEntries = new List(); // General + configEntries.Add(UnlockCursor = config.Bind( + GeneralSection, + "Unlock Cursor", + true, + new ConfigDescription( + "Unlock cursor in Windowed, Maximized Windowed, and FullScreen Windowed modes. Note that you must alt-tab out of the game and back in for this to take affect.", + null, + new ConfigurationManagerAttributes { }))); + configEntries.Add(ShowPresetConfirmations = config.Bind( GeneralSection, "Show Weapon Preset Confirmation Dialog", @@ -269,6 +280,15 @@ internal class Settings null, new ConfigurationManagerAttributes { }))); + configEntries.Add(ExamineKeyBind = config.Bind( + InputSection, + "Examine/Interact Shortcut", + new KeyboardShortcut(KeyCode.None), + new ConfigDescription( + "Keybind to examine an item, fold it, unfold it, turn it on, turn it off, or check a magazine", + null, + new ConfigurationManagerAttributes { }))); + configEntries.Add(TopUpKeyBind = config.Bind( InputSection, "Top Up Ammo Shortcut",