mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
Add support for modifier keys
This commit is contained in:
1 parent
dfe124d67a
commit
355eef509b
8 files changed
+56
-9
No files matched your search
@@ -21,6 +21,7 @@ public class FreezeButton : CustomActionButton<PlayerControl>
|
||||
|
||||
public override LoadableAsset<Sprite> Sprite => ExampleAssets.ExampleButton;
|
||||
public override KeyboardKeyCode Defaultkeybind => KeyboardKeyCode.T;
|
||||
public override ModifierKey Modifier1 => ModifierKey.Control;
|
||||
|
||||
protected override void OnClick()
|
||||
{
|
||||
|
||||
@@ -17,6 +17,8 @@ public class MeetingButton : CustomActionButton
|
||||
|
||||
public override LoadableAsset<Sprite> Sprite => ExampleAssets.ExampleButton;
|
||||
public override KeyboardKeyCode Defaultkeybind => KeyboardKeyCode.P;
|
||||
public override ModifierKey Modifier1 => ModifierKey.Control;
|
||||
public override ModifierKey Modifier2 => ModifierKey.Shift;
|
||||
|
||||
public override bool Enabled(RoleBehaviour? role)
|
||||
{
|
||||
|
||||
@@ -58,6 +58,23 @@ public abstract class CustomActionButton
|
||||
/// </summary>
|
||||
public abstract KeyboardKeyCode Defaultkeybind { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The first optional modifier key (e.g., Control, Shift, Alt) that must be held with the main key to activate the button.
|
||||
/// </summary>
|
||||
public virtual ModifierKey Modifier1 => ModifierKey.None;
|
||||
|
||||
/// <summary>
|
||||
/// The second optional modifier key.
|
||||
/// Set to <see cref="ModifierKey.None"/> if unused.
|
||||
/// </summary>
|
||||
public virtual ModifierKey Modifier2 => ModifierKey.None;
|
||||
|
||||
/// <summary>
|
||||
/// The third optional modifier key.
|
||||
/// Set to <see cref="ModifierKey.None"/> if unused.
|
||||
/// </summary>
|
||||
public virtual ModifierKey Modifier3 => ModifierKey.None;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the button's text outline color.
|
||||
/// </summary>
|
||||
|
||||
@@ -34,6 +34,21 @@ namespace MiraAPI.Keybinds
|
||||
/// If true, this keybind will be checked for conflicts with other exclusive keybinds using the same key.
|
||||
/// </summary>
|
||||
public bool Exclusive;
|
||||
|
||||
/// <summary>
|
||||
/// The first optional modifier key (e.g., Control, Shift, Alt) that must be held with the main key.
|
||||
/// </summary>
|
||||
public ModifierKey Modifier1 = ModifierKey.None;
|
||||
|
||||
/// <summary>
|
||||
/// The second optional modifier key, if needed. Set to <c>ModifierKey.None</c> if unused.
|
||||
/// </summary>
|
||||
public ModifierKey Modifier2 = ModifierKey.None;
|
||||
|
||||
/// <summary>
|
||||
/// The third optional modifier key, if needed. Set to <c>ModifierKey.None</c> if unused.
|
||||
/// </summary>
|
||||
public ModifierKey Modifier3 = ModifierKey.None;
|
||||
}
|
||||
|
||||
private static readonly List<KeybindEntry> Registered = new();
|
||||
@@ -46,8 +61,11 @@ namespace MiraAPI.Keybinds
|
||||
/// <param name="key">Default key to use if no rebind exists.</param>
|
||||
/// <param name="handler">Method to invoke when the keybind is pressed.</param>
|
||||
/// <param name="exclusive">If true, conflicts with other exclusive keybinds on the same key.</param>
|
||||
/// <param name="modifier1">Optional first modifier key (e.g., <c>Control</c>, <c>Shift</c>, <c>Alt</c>) that must be held with the main key.</param>
|
||||
/// <param name="modifier2">Optional second modifier key. Use <c>ModifierKey.None</c> if unused.</param>
|
||||
/// <param name="modifier3">Optional third modifier key. Use <c>ModifierKey.None</c> if unused.</param>
|
||||
/// <returns> Returns the key that was registered for this keybind. </returns>
|
||||
public static KeyboardKeyCode Register(string id, string description, KeyboardKeyCode key, Action handler, bool exclusive = true)
|
||||
public static KeyboardKeyCode Register(string id, string description, KeyboardKeyCode key, Action handler, bool exclusive = true, ModifierKey modifier1 = ModifierKey.None, ModifierKey modifier2 = ModifierKey.None, ModifierKey modifier3 = ModifierKey.None)
|
||||
{
|
||||
Registered.Add(new KeybindEntry
|
||||
{
|
||||
@@ -56,6 +74,9 @@ namespace MiraAPI.Keybinds
|
||||
Key = key,
|
||||
Handler = handler,
|
||||
Exclusive = exclusive,
|
||||
Modifier1 = modifier1,
|
||||
Modifier2 = modifier2,
|
||||
Modifier3 = modifier3,
|
||||
});
|
||||
|
||||
return key;
|
||||
|
||||
@@ -138,8 +138,12 @@ public static class HudManagerPatches
|
||||
foreach (var entry in KeybindManager.GetEntries())
|
||||
{
|
||||
var player = ReInput.players.GetPlayer(0);
|
||||
var keyboard = player.controllers.Keyboard;
|
||||
|
||||
if (player.GetButtonDown(entry.Id))
|
||||
if (player.GetButtonDown(entry.Id) &&
|
||||
(entry.Modifier1 == ModifierKey.None || keyboard.GetModifierKey(entry.Modifier1)) &&
|
||||
(entry.Modifier2 == ModifierKey.None || keyboard.GetModifierKey(entry.Modifier2)) &&
|
||||
(entry.Modifier3 == ModifierKey.None || keyboard.GetModifierKey(entry.Modifier3)))
|
||||
{
|
||||
entry.Handler?.Invoke();
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace MiraAPI.Patches.Keybinds
|
||||
{
|
||||
foreach (var entry in KeybindManager.GetEntries())
|
||||
{
|
||||
__instance.userData.RegisterModBind(entry.Id, entry.Description, entry.Key);
|
||||
__instance.userData.RegisterModBind(entry.Id, entry.Description, entry.Key, modifier1: entry.Modifier1, modifier2: entry.Modifier2, modifier3: entry.Modifier3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,8 +137,7 @@ public sealed class MiraPluginManager
|
||||
|
||||
foreach (var button in CustomButtonManager.Buttons)
|
||||
{
|
||||
|
||||
KeybindManager.Register($"{button.Name}_Keybind", $"Keybind for {button.Name}", button.Defaultkeybind, button.ClickHandler);
|
||||
KeybindManager.Register($"{button.Name}_Keybind", $"Keybind for {button.Name}", button.Defaultkeybind, button.ClickHandler, modifier1: button.Modifier1, modifier2: button.Modifier2, modifier3: button.Modifier3);
|
||||
|
||||
Logger<MiraApiPlugin>.Info($"Registered keybind for button '{button.GetType().Name}' with default key {button.Defaultkeybind}.");
|
||||
}
|
||||
|
||||
@@ -559,8 +559,11 @@ public static class Extensions
|
||||
/// <param name="category">Category ID to group actions in Rewired (default is 0).</param>
|
||||
/// <param name="elementIdentifierId">The element identifier ID (default is -1, meaning none specified).</param>
|
||||
/// <param name="type">The <see cref="InputActionType"/> for this action (default is Button).</param>
|
||||
/// <param name="modifier1">The first optional modifier key (e.g., <c>Control</c>, <c>Shift</c>, <c>Alt</c>) that must be held together with the main key.</param>
|
||||
/// <param name="modifier2">The second optional modifier key. Set to <see cref="ModifierKey.None"/> if not used.</param>
|
||||
/// <param name="modifier3">The third optional modifier key. Set to <see cref="ModifierKey.None"/> if not used.</param>
|
||||
/// <returns>The action ID of the newly registered action.</returns>
|
||||
public static int RegisterModBind(this UserData userData, string actionName, string description, KeyboardKeyCode key, int category = 0, int elementIdentifierId = -1, InputActionType type = InputActionType.Button)
|
||||
public static int RegisterModBind(this UserData userData, string actionName, string description, KeyboardKeyCode key, int category = 0, int elementIdentifierId = -1, InputActionType type = InputActionType.Button, ModifierKey modifier1 = ModifierKey.None, ModifierKey modifier2 = ModifierKey.None, ModifierKey modifier3 = ModifierKey.None)
|
||||
{
|
||||
userData.AddAction(category);
|
||||
|
||||
@@ -579,9 +582,9 @@ public static class Extensions
|
||||
_elementType = ControllerElementType.Button,
|
||||
_axisContribution = Pole.Positive,
|
||||
_keyboardKeyCode = key,
|
||||
_modifierKey1 = ModifierKey.None,
|
||||
_modifierKey2 = ModifierKey.None,
|
||||
_modifierKey3 = ModifierKey.None,
|
||||
_modifierKey1 = modifier1,
|
||||
_modifierKey2 = modifier2,
|
||||
_modifierKey3 = modifier3,
|
||||
};
|
||||
userData.keyboardMaps[0].actionElementMaps.Add(map);
|
||||
userData.joystickMaps[0].actionElementMaps.Add(map);
|
||||
|
||||
Reference in new issue
Block a user