basekeybind class, vanilla keybindings

This commit is contained in:
xChipseq committed 2025-09-08 18:37:09 +02:00
1 parent af3d99eafe
commit 6872ca6866
8 files changed
+164 -116

No files matched your search

+4 -2
View File
@@ -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
/// <summary>
/// Gets the keybind for this button. If null, no keybind will be added.
/// </summary>
public virtual MiraKeybind? Keybind => null;
public virtual BaseKeybind? Keybind => null;
/// <summary>
/// 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<MiraApiPlugin>.Instance.MiraConfig!.ShowKeybinds.Value);
PluginSingleton<MiraApiPlugin>.Instance.MiraConfig!.ShowKeybinds.Value &&
Keybind.CurrentKey != KeyboardKeyCode.None);
}
if (Timer >= 0)
+67
View File
@@ -0,0 +1,67 @@
using System;
using Rewired;
namespace MiraAPI.Keybinds;
/// <summary>
/// Base keybind class.
/// </summary>
public class BaseKeybind
{
/// <summary>
/// Gets the Rewired <see cref="InputAction"/> assinged for this keybind.
/// </summary>
public InputAction? RewiredInputAction { get; internal set; }
/// <summary>
/// Gets the unique identifier for this keybind. Used in Rewired.
/// </summary>
public string Id { get; }
/// <summary>
/// Gets the currently assigned keycode.
/// </summary>
public KeyboardKeyCode CurrentKey => KeybindUtils.GetKeycodeByKeybind(this);
/// <summary>
/// Gets or sets the handler of the keybind. Invoked when the keybind is activated.
/// </summary>
protected Action Handler { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="BaseKeybind"/> class.
/// </summary>
/// <param name="id">The unique identifier for this keybind.</param>
public BaseKeybind(string id)
{
Id = id;
Handler = () => { };
}
/// <summary>
/// Triggers the keybind, invoking its handler.
/// </summary>
public void Invoke()
{
Handler.Invoke();
}
/// <summary>
/// Adds an <see cref="Action"/> that will be invoked when the keybind is used.
/// </summary>
/// <param name="action">The <see cref="Action"/> to add.</param>
public void OnActivate(Action action)
{
Handler += action;
}
/// <summary>
/// Removes an <see cref="Action"/> that would be invoked when the keybind is used.
/// Not recommended to use - add checks in the method added in <see cref="OnActivate"/> itself rather than removing it from the keybind.
/// </summary>
/// <param name="action">The <see cref="Action"/> to remove.</param>
public void RemoveOnActivate(Action action)
{
Handler -= action;
}
}
+4 -9
View File
@@ -16,8 +16,7 @@ public static class KeybindManager
/// Gets a list of all registered keybinds.
/// </summary>
public static List<MiraKeybind> Keybinds { get; } = new();
internal static List<VanillaKeybind> VanillaKeybinds { get; } = new();
internal static Dictionary<Type, VanillaKeybind> 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);
}
}
+23 -4
View File
@@ -18,11 +18,11 @@ public static class KeybindUtils
/// </summary>
/// <param name="keybind">The keybind to get the keycode from.</param>
/// <returns>The currently assigned keycode.</returns>
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
/// <param name="actionId">The action ID.</param>
/// <returns>The keyboard keycode.</returns>
public static KeyboardKeyCode GetKeycodeByActionId(int actionId)
{
return GetActionElementMap(actionId)?.keyboardKeyCode ?? KeyboardKeyCode.None;
}
/// <summary>
/// Gets a <see cref="ActionElementMap"/> by id with ReInput.
/// </summary>
/// <param name="actionId">The action ID.</param>
/// <returns>The keyboard keycode.</returns>
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);
}
/// <summary>
/// Gets an <see cref="InputAction"/> by id.
/// </summary>
/// <param name="id">The action identifier name.</param>
/// <returns>The keyboard keycode.</returns>
public static InputAction? GetInputActionById(int id)
{
return RewiredInputManager?.userData?.GetActionById(id);
}
/// <summary>
+6 -57
View File
@@ -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;
/// <summary>
/// Represents a registered keybind.
/// </summary>
public class MiraKeybind()
public class MiraKeybind : BaseKeybind
{
/// <summary>
/// Gets name of the keybind
/// </summary>
public string Name { get; }
/// <summary>
/// Gets the unique identifier for this keybind. Used in Rewired.
/// </summary>
public string Id { get; }
/// <summary>
/// Gets the default keycode of this keybind.
/// </summary>
public KeyboardKeyCode DefaultKey { get; }
/// <summary>
/// Gets a value indicating whether this keybind should be checked for conflicts with other exclusive keybinds using the same key.
/// </summary>
public bool Exclusive { get; }
/// <summary>
/// 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; }
/// <summary>
/// Gets the Rewired <see cref="InputAction"/> assinged for this keybind.
/// Gets a value indicating whether this keybind should be checked for conflicts with other exclusive keybinds using the same key.
/// </summary>
public InputAction? RewiredInputAction { get; internal set; }
/// <summary>
/// Gets the currently assigned keycode.
/// </summary>
public KeyboardKeyCode CurrentKey => KeybindUtils.GetKeycodeByKeybind(this) ?? KeyboardKeyCode.None;
/// <summary>
/// Gets or sets the handler of the keybind. Invoked when the keybind is activated.
/// </summary>
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<ModifierKey>();
ModifierKeys = modifierKeys ?? [];
Exclusive = exclusive;
Handler = () => { };
}
/// <inheritdoc/>
@@ -79,31 +55,4 @@ public class MiraKeybind()
{
return $"{Name} ({Id})";
}
/// <summary>
/// Triggers the keybind, invoking its handler.
/// </summary>
public void Invoke()
{
Handler.Invoke();
}
/// <summary>
/// Adds an <see cref="Action"/> that will be invoked when the keybind is used.
/// </summary>
/// <param name="action">The <see cref="Action"/> to add.</param>
public void OnActivate(Action action)
{
Handler += action;
}
/// <summary>
/// Removes an <see cref="Action"/> that would be invoked when the keybind is used.
/// Not recommended to use - add checks in the method added in <see cref="OnActivate"/> itself rather than removing it from the keybind.
/// </summary>
/// <param name="action">The <see cref="Action"/> to remove.</param>
public void RemoveOnActivate(Action action)
{
Handler -= action;
}
}
+16 -9
View File
@@ -1,18 +1,25 @@
using Rewired;
using TMPro;
using UnityEngine;
namespace MiraAPI.Keybinds;
public class VanillaKeybind
/// <summary>
/// Represents a vanilla keybind.
/// </summary>
public class VanillaKeybind : BaseKeybind
{
public AbilityButton Ability { get; }
public InputAction RewiredInputAction { get; }
public KeyboardKeyCode CurrentKey => KeybindUtils.GetKeycodeByActionId(RewiredInputAction.id);
/// <summary>
/// Gets the <see cref="ActionButton"/> this keybind is binded to.
/// </summary>
public ActionButton? Button { get; }
public VanillaKeybind(AbilityButton ability, InputAction inputAction)
/// <summary>
/// Initializes a new instance of the <see cref="VanillaKeybind"/> class.
/// </summary>
/// <param name="button">The keybind action button.</param>
/// <param name="action">The rewired input action.</param>
public VanillaKeybind(ActionButton button, InputAction action) : base(action.name)
{
Ability = ability;
RewiredInputAction = inputAction;
Button = button;
RewiredInputAction = action;
}
}
+9 -7
View File
@@ -1,15 +1,17 @@
using System.Linq;
namespace MiraAPI.Keybinds;
namespace MiraAPI.Keybinds;
public static class VanillaKeybindSingleton<T> where T : AbilityButton
/// <summary>
/// Singleton for getting a vanilla keybind by <see cref="ActionButton"/>.
/// </summary>
/// <typeparam name="T">ActionButton type.</typeparam>
public static class VanillaKeybinding<T> where T : ActionButton
{
internal static VanillaKeybind _instance;
private static VanillaKeybind? _instance;
/// <summary>
/// Gets the instance of the option group.
/// </summary>
#pragma warning disable CA1000
public static VanillaKeybind Instance => _instance;
public static VanillaKeybind Instance => _instance ??= KeybindManager.VanillaKeybinds[typeof(T)];
#pragma warning restore CA1000
}
}
+35 -28
View File
@@ -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<GameObject, int>
{
{ __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<TextMeshPro>(), actionId);
KeybindManager.RegisterVanillaKeybind(buttonObj.GetComponent<ActionButton>(), 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<GameObject, int>
{
{ __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<TextMeshPro>(), actionId);
KeybindManager.RegisterVanillaKeybind();
}
}
/// <summary>
@@ -169,11 +170,10 @@ public static class HudManagerPatches
PluginSingleton<MiraApiPlugin>.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();
}
}
}
}