mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
Merge pull request #64 from All-Of-Us-Mods/vanilla-keybind-bindings
Vanilla keybindings
This commit is contained in:
9 files changed
+174
-67
No files matched your search
@@ -8,6 +8,7 @@ using MiraAPI.Patches;
|
||||
using MiraAPI.Utilities;
|
||||
using MiraAPI.Utilities.Assets;
|
||||
using Reactor.Utilities;
|
||||
using Rewired;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
@@ -75,7 +76,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.
|
||||
@@ -494,7 +495,8 @@ public abstract class CustomActionButton
|
||||
{
|
||||
KeybindText.text = Keybind.CurrentKey.ToString();
|
||||
KeybindIcon?.SetActive(ActiveInputManager.currentControlType == ActiveInputManager.InputType.Keyboard &&
|
||||
LocalSettingsTabSingleton<MiraApiSettings>.Instance.ShowKeybinds.Value);
|
||||
LocalSettingsTabSingleton<MiraApiSettings>.Instance.ShowKeybinds.Value &&
|
||||
Keybind.CurrentKey != KeyboardKeyCode.None);
|
||||
}
|
||||
|
||||
if (Timer >= 0)
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
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; protected set; }
|
||||
|
||||
/// <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 = () => { };
|
||||
}
|
||||
|
||||
public BaseKeybind()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Il2CppInterop.Runtime.Attributes;
|
||||
using MiraAPI.Utilities;
|
||||
using Reactor.Utilities;
|
||||
using Rewired;
|
||||
@@ -17,6 +18,9 @@ public static class KeybindManager
|
||||
/// </summary>
|
||||
public static List<MiraKeybind> Keybinds { get; } = new();
|
||||
|
||||
[HideFromIl2Cpp]
|
||||
internal static Dictionary<Type, VanillaKeybind> VanillaKeybinds { get; set; } = [];
|
||||
|
||||
internal static void RewiredInit()
|
||||
{
|
||||
try
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Reactor.Utilities;
|
||||
using System.Globalization;
|
||||
using Rewired;
|
||||
|
||||
namespace MiraAPI.Keybinds;
|
||||
@@ -8,48 +6,28 @@ 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 assinged to this keybind.
|
||||
/// Gets the modifier keys assigned to this keybind.
|
||||
/// Due to Rewired limitations, there can only be 3 modifier keys.
|
||||
/// </summary>
|
||||
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)
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Rewired;
|
||||
|
||||
namespace MiraAPI.Keybinds;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a vanilla keybind.
|
||||
/// </summary>
|
||||
public class VanillaKeybind : BaseKeybind
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ActionButton"/> this keybind is binded to.
|
||||
/// </summary>
|
||||
public ActionButton? Button { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="VanillaKeybind"/> class.
|
||||
/// </summary>
|
||||
/// <param name="id">The keybind id.</param>
|
||||
public VanillaKeybind(int id)
|
||||
{
|
||||
RewiredInputAction = KeybindUtils.GetInputActionById(id);
|
||||
Id = RewiredInputAction?.name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace MiraAPI.Keybinds;
|
||||
|
||||
/// <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
|
||||
{
|
||||
private static VanillaKeybind? _instance;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the instance of the option group.
|
||||
/// </summary>
|
||||
#pragma warning disable CA1000
|
||||
public static VanillaKeybind Instance => _instance ??= KeybindManager.VanillaKeybinds[typeof(T)];
|
||||
#pragma warning restore CA1000
|
||||
}
|
||||
@@ -99,7 +99,6 @@ public static class HudManagerPatches
|
||||
|
||||
gridArrange.Start();
|
||||
gridArrange.ArrangeChilds();
|
||||
|
||||
aspectPosition.AdjustPosition();
|
||||
|
||||
vanillaKeybindIcons = [];
|
||||
@@ -111,6 +110,7 @@ public static class HudManagerPatches
|
||||
{ __instance.ReportButton.gameObject, 7 },
|
||||
{ __instance.ImpostorVentButton.gameObject, 50 },
|
||||
{ __instance.SabotageButton.gameObject, 4 },
|
||||
{ __instance.AbilityButton.gameObject, 49 },
|
||||
};
|
||||
|
||||
foreach (var kvp in vanillaButtons)
|
||||
@@ -125,6 +125,8 @@ public static class HudManagerPatches
|
||||
}
|
||||
var icon = Helpers.CreateKeybindIcon(buttonObj, key, keybindIconPos);
|
||||
vanillaKeybindIcons.Add(icon.transform.GetChild(0).GetComponent<TextMeshPro>(), actionId);
|
||||
var comp = buttonObj.GetComponent<ActionButton>();
|
||||
KeybindManager.VanillaKeybinds[comp.GetType()].Button = comp;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,11 +171,10 @@ public static class HudManagerPatches
|
||||
LocalSettingsTabSingleton<MiraApiSettings>.Instance.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 +182,12 @@ public static class HudManagerPatches
|
||||
entry.Invoke();
|
||||
}
|
||||
}
|
||||
foreach (var entry in KeybindManager.VanillaKeybinds.Values)
|
||||
{
|
||||
if (player.GetButtonDown(entry.Id))
|
||||
{
|
||||
entry.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using HarmonyLib;
|
||||
using MiraAPI.Keybinds;
|
||||
using MonoMod.Utils;
|
||||
using Rewired;
|
||||
|
||||
namespace MiraAPI.Patches.Keybinds;
|
||||
@@ -20,5 +21,16 @@ public static class KeybindMenuPatch
|
||||
KeybindUtils.RewiredInputManager = __instance;
|
||||
KeybindManager.RewiredInit();
|
||||
_registered = true;
|
||||
|
||||
// Set these here right after input manager initializes
|
||||
KeybindManager.VanillaKeybinds = new()
|
||||
{
|
||||
{ typeof(KillButton), new VanillaKeybind(8) },
|
||||
{ typeof(UseButton), new VanillaKeybind(6) },
|
||||
{ typeof(ReportButton), new VanillaKeybind(7) },
|
||||
{ typeof(VentButton), new VanillaKeybind(50) },
|
||||
{ typeof(SabotageButton), new VanillaKeybind(4) },
|
||||
{ typeof(AbilityButton), new VanillaKeybind(49) },
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user