diff --git a/MiraAPI/Hud/CustomActionButton.cs b/MiraAPI/Hud/CustomActionButton.cs index ca7dd6a..c0e1b65 100644 --- a/MiraAPI/Hud/CustomActionButton.cs +++ b/MiraAPI/Hud/CustomActionButton.cs @@ -7,6 +7,7 @@ using MiraAPI.Patches; using MiraAPI.Utilities; using MiraAPI.Utilities.Assets; using Reactor.Utilities; +using Rewired; using TMPro; using UnityEngine; using UnityEngine.Events; @@ -74,7 +75,7 @@ public abstract class CustomActionButton /// /// Gets the keybind for this button. If null, no keybind will be added. /// - public virtual MiraKeybind? Keybind => null; + public virtual BaseKeybind? Keybind => null; /// /// Gets the button's text outline color. @@ -493,7 +494,8 @@ public abstract class CustomActionButton { KeybindText.text = Keybind.CurrentKey.ToString(); KeybindIcon?.SetActive(ActiveInputManager.currentControlType == ActiveInputManager.InputType.Keyboard && - PluginSingleton.Instance.MiraConfig!.ShowKeybinds.Value); + PluginSingleton.Instance.MiraConfig!.ShowKeybinds.Value && + Keybind.CurrentKey != KeyboardKeyCode.None); } if (Timer >= 0) diff --git a/MiraAPI/Keybinds/BaseKeybind.cs b/MiraAPI/Keybinds/BaseKeybind.cs new file mode 100644 index 0000000..21d636e --- /dev/null +++ b/MiraAPI/Keybinds/BaseKeybind.cs @@ -0,0 +1,67 @@ +using System; +using Rewired; + +namespace MiraAPI.Keybinds; + +/// +/// Base keybind class. +/// +public class BaseKeybind +{ + /// + /// Gets the Rewired assinged for this keybind. + /// + public InputAction? RewiredInputAction { get; internal set; } + + /// + /// Gets the unique identifier for this keybind. Used in Rewired. + /// + public string Id { get; } + + /// + /// Gets the currently assigned keycode. + /// + public KeyboardKeyCode CurrentKey => KeybindUtils.GetKeycodeByKeybind(this); + + /// + /// Gets or sets the handler of the keybind. Invoked when the keybind is activated. + /// + protected Action Handler { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// The unique identifier for this keybind. + public BaseKeybind(string id) + { + Id = id; + Handler = () => { }; + } + + /// + /// Triggers the keybind, invoking its handler. + /// + public void Invoke() + { + Handler.Invoke(); + } + + /// + /// Adds an that will be invoked when the keybind is used. + /// + /// The to add. + public void OnActivate(Action action) + { + Handler += action; + } + + /// + /// Removes an that would be invoked when the keybind is used. + /// Not recommended to use - add checks in the method added in itself rather than removing it from the keybind. + /// + /// The to remove. + public void RemoveOnActivate(Action action) + { + Handler -= action; + } +} \ No newline at end of file diff --git a/MiraAPI/Keybinds/KeybindManager.cs b/MiraAPI/Keybinds/KeybindManager.cs index 630fadb..a123647 100644 --- a/MiraAPI/Keybinds/KeybindManager.cs +++ b/MiraAPI/Keybinds/KeybindManager.cs @@ -16,8 +16,7 @@ public static class KeybindManager /// Gets a list of all registered keybinds. /// public static List Keybinds { get; } = new(); - - internal static List VanillaKeybinds { get; } = new(); + internal static Dictionary VanillaKeybinds { get; } = new(); internal static void RewiredInit() { @@ -94,13 +93,9 @@ public static class KeybindManager return all.ToArray(); } - internal static void RegisterVanillaKeybind(AbilityButton button, InputAction inputAction) + internal static void RegisterVanillaKeybind(ActionButton button, int id) { - var keybind = new VanillaKeybind(button, inputAction); - VanillaKeybinds.Add(keybind); - typeof(VanillaKeybindSingleton<>) - .MakeGenericType(button.GetType()) - .GetField("_instance")! - .SetValue(null, keybind); + var keybind = new VanillaKeybind(button, KeybindUtils.GetInputActionById(id)!); + VanillaKeybinds.Add(button.GetType(), keybind); } } diff --git a/MiraAPI/Keybinds/KeybindUtils.cs b/MiraAPI/Keybinds/KeybindUtils.cs index fbca1d6..f4371b3 100644 --- a/MiraAPI/Keybinds/KeybindUtils.cs +++ b/MiraAPI/Keybinds/KeybindUtils.cs @@ -18,11 +18,11 @@ public static class KeybindUtils /// /// The keybind to get the keycode from. /// The currently assigned keycode. - public static KeyboardKeyCode? GetKeycodeByKeybind(MiraKeybind keybind) + public static KeyboardKeyCode GetKeycodeByKeybind(BaseKeybind keybind) { if (keybind.RewiredInputAction == null) { - return null; + return KeyboardKeyCode.None; } return GetKeycodeByActionId(keybind.RewiredInputAction.id); @@ -34,10 +34,29 @@ public static class KeybindUtils /// The action ID. /// The keyboard keycode. public static KeyboardKeyCode GetKeycodeByActionId(int actionId) + { + return GetActionElementMap(actionId)?.keyboardKeyCode ?? KeyboardKeyCode.None; + } + + /// + /// Gets a by id with ReInput. + /// + /// The action ID. + /// The keyboard keycode. + public static ActionElementMap? GetActionElementMap(int actionId) { var player = ReInput.players.GetPlayer(0); - return player.controllers.maps.GetFirstElementMapWithAction(ControllerType.Keyboard, actionId, false)? - .keyboardKeyCode ?? KeyboardKeyCode.None; + return player.controllers.maps.GetFirstElementMapWithAction(ControllerType.Keyboard, actionId, false); + } + + /// + /// Gets an by id. + /// + /// The action identifier name. + /// The keyboard keycode. + public static InputAction? GetInputActionById(int id) + { + return RewiredInputManager?.userData?.GetActionById(id); } /// diff --git a/MiraAPI/Keybinds/MiraKeybind.cs b/MiraAPI/Keybinds/MiraKeybind.cs index a0e2c45..52d64a1 100644 --- a/MiraAPI/Keybinds/MiraKeybind.cs +++ b/MiraAPI/Keybinds/MiraKeybind.cs @@ -1,6 +1,4 @@ -using System; -using System.Globalization; -using Reactor.Utilities; +using System.Globalization; using Rewired; namespace MiraAPI.Keybinds; @@ -8,28 +6,18 @@ namespace MiraAPI.Keybinds; /// /// Represents a registered keybind. /// -public class MiraKeybind() +public class MiraKeybind : BaseKeybind { /// /// Gets name of the keybind /// public string Name { get; } - /// - /// Gets the unique identifier for this keybind. Used in Rewired. - /// - public string Id { get; } - /// /// Gets the default keycode of this keybind. /// public KeyboardKeyCode DefaultKey { get; } - /// - /// Gets a value indicating whether this keybind should be checked for conflicts with other exclusive keybinds using the same key. - /// - public bool Exclusive { get; } - /// /// Gets the modifier keys assigned to this keybind. /// Due to Rewired limitations, there can only be 3 modifier keys. @@ -37,19 +25,9 @@ public class MiraKeybind() public ModifierKey[] ModifierKeys { get; } /// - /// Gets the Rewired assinged for this keybind. + /// Gets a value indicating whether this keybind should be checked for conflicts with other exclusive keybinds using the same key. /// - public InputAction? RewiredInputAction { get; internal set; } - - /// - /// Gets the currently assigned keycode. - /// - public KeyboardKeyCode CurrentKey => KeybindUtils.GetKeycodeByKeybind(this) ?? KeyboardKeyCode.None; - - /// - /// Gets or sets the handler of the keybind. Invoked when the keybind is activated. - /// - private Action Handler { get; set; } + public bool Exclusive { get; } internal string? SourcePluginName { get; set; } @@ -64,14 +42,12 @@ public class MiraKeybind() string name, KeyboardKeyCode? defaultKeycode, ModifierKey[]? modifierKeys = null, - bool exclusive = true) : this() + bool exclusive = true) : base(name.ToLower(CultureInfo.InvariantCulture).Replace(' ', '_')) { Name = name; - Id = name.ToLower(CultureInfo.InvariantCulture).Replace(' ', '_'); DefaultKey = defaultKeycode ?? KeyboardKeyCode.None; - ModifierKeys = modifierKeys ?? Array.Empty(); + ModifierKeys = modifierKeys ?? []; Exclusive = exclusive; - Handler = () => { }; } /// @@ -79,31 +55,4 @@ public class MiraKeybind() { return $"{Name} ({Id})"; } - - /// - /// Triggers the keybind, invoking its handler. - /// - public void Invoke() - { - Handler.Invoke(); - } - - /// - /// Adds an that will be invoked when the keybind is used. - /// - /// The to add. - public void OnActivate(Action action) - { - Handler += action; - } - - /// - /// Removes an that would be invoked when the keybind is used. - /// Not recommended to use - add checks in the method added in itself rather than removing it from the keybind. - /// - /// The to remove. - public void RemoveOnActivate(Action action) - { - Handler -= action; - } } diff --git a/MiraAPI/Keybinds/VanillaKeybind.cs b/MiraAPI/Keybinds/VanillaKeybind.cs index 56a3121..ad1bbb5 100644 --- a/MiraAPI/Keybinds/VanillaKeybind.cs +++ b/MiraAPI/Keybinds/VanillaKeybind.cs @@ -1,18 +1,25 @@ using Rewired; -using TMPro; -using UnityEngine; namespace MiraAPI.Keybinds; -public class VanillaKeybind +/// +/// Represents a vanilla keybind. +/// +public class VanillaKeybind : BaseKeybind { - public AbilityButton Ability { get; } - public InputAction RewiredInputAction { get; } - public KeyboardKeyCode CurrentKey => KeybindUtils.GetKeycodeByActionId(RewiredInputAction.id); + /// + /// Gets the this keybind is binded to. + /// + public ActionButton? Button { get; } - public VanillaKeybind(AbilityButton ability, InputAction inputAction) + /// + /// Initializes a new instance of the class. + /// + /// The keybind action button. + /// The rewired input action. + public VanillaKeybind(ActionButton button, InputAction action) : base(action.name) { - Ability = ability; - RewiredInputAction = inputAction; + Button = button; + RewiredInputAction = action; } } diff --git a/MiraAPI/Keybinds/VanillaKeybindSingleton.cs b/MiraAPI/Keybinds/VanillaKeybindSingleton.cs index 99b34a7..980da77 100644 --- a/MiraAPI/Keybinds/VanillaKeybindSingleton.cs +++ b/MiraAPI/Keybinds/VanillaKeybindSingleton.cs @@ -1,15 +1,17 @@ -using System.Linq; +namespace MiraAPI.Keybinds; -namespace MiraAPI.Keybinds; - -public static class VanillaKeybindSingleton where T : AbilityButton +/// +/// Singleton for getting a vanilla keybind by . +/// +/// ActionButton type. +public static class VanillaKeybinding where T : ActionButton { - internal static VanillaKeybind _instance; + private static VanillaKeybind? _instance; /// /// Gets the instance of the option group. /// #pragma warning disable CA1000 - public static VanillaKeybind Instance => _instance; + public static VanillaKeybind Instance => _instance ??= KeybindManager.VanillaKeybinds[typeof(T)]; #pragma warning restore CA1000 -} \ No newline at end of file +} diff --git a/MiraAPI/Patches/HudManagerPatches.cs b/MiraAPI/Patches/HudManagerPatches.cs index 99dea2b..6183d5c 100644 --- a/MiraAPI/Patches/HudManagerPatches.cs +++ b/MiraAPI/Patches/HudManagerPatches.cs @@ -45,6 +45,33 @@ public static class HudManagerPatches [HarmonyPatch(nameof(HudManager.Start))] public static void StartPostfix(HudManager __instance) { + vanillaKeybindIcons = []; + var keybindIconPos = new Vector3(-0.4f, 0.4f, -9f); + var vanillaButtons = new Dictionary + { + { __instance.KillButton.gameObject, 8 }, + { __instance.UseButton.gameObject, 6 }, + { __instance.ReportButton.gameObject, 7 }, + { __instance.ImpostorVentButton.gameObject, 50 }, + { __instance.SabotageButton.gameObject, 4 }, + }; + + KeybindManager.VanillaKeybinds.Clear(); + foreach (var kvp in vanillaButtons) + { + var buttonObj = kvp.Key; + var actionId = kvp.Value; + + var key = KeybindUtils.GetKeycodeByActionId(actionId); + if (key == KeyboardKeyCode.None) + { + continue; + } + var icon = Helpers.CreateKeybindIcon(buttonObj, key, keybindIconPos); + vanillaKeybindIcons.Add(icon.transform.GetChild(0).GetComponent(), actionId); + KeybindManager.RegisterVanillaKeybind(buttonObj.GetComponent(), actionId); + } + if (Buttons == null) { Buttons = __instance.transform.Find("Buttons"); @@ -100,32 +127,6 @@ public static class HudManagerPatches gridArrange.ArrangeChilds(); aspectPosition.AdjustPosition(); - - vanillaKeybindIcons = []; - var keybindIconPos = new Vector3(-0.4f, 0.4f, -9f); - var vanillaButtons = new Dictionary - { - { __instance.KillButton.gameObject, 8 }, - { __instance.UseButton.gameObject, 6 }, - { __instance.ReportButton.gameObject, 7 }, - { __instance.ImpostorVentButton.gameObject, 50 }, - { __instance.SabotageButton.gameObject, 4 }, - }; - - foreach (var kvp in vanillaButtons) - { - var buttonObj = kvp.Key; - var actionId = kvp.Value; - - var key = KeybindUtils.GetKeycodeByActionId(actionId); - if (key == KeyboardKeyCode.None) - { - continue; - } - var icon = Helpers.CreateKeybindIcon(buttonObj, key, keybindIconPos); - vanillaKeybindIcons.Add(icon.transform.GetChild(0).GetComponent(), actionId); - KeybindManager.RegisterVanillaKeybind(); - } } /// @@ -169,11 +170,10 @@ public static class HudManagerPatches PluginSingleton.Instance.MiraConfig!.ShowKeybinds.Value); } + var player = ReInput.players.GetPlayer(0); foreach (var entry in KeybindManager.Keybinds) { - var player = ReInput.players.GetPlayer(0); var keyboard = player.controllers.Keyboard; - bool modifierKeysPressed = entry.ModifierKeys.All(k => keyboard.GetModifierKey(k)) || entry.ModifierKeys.Length <= 0; if (player.GetButtonDown(entry.Id) && modifierKeysPressed) @@ -181,5 +181,12 @@ public static class HudManagerPatches entry.Invoke(); } } + foreach (var entry in KeybindManager.VanillaKeybinds.Values) + { + if (player.GetButtonDown(entry.Id)) + { + entry.Invoke(); + } + } } }