From 2237d3a2dfbd41909f3f29b4a34b416b37c0520a Mon Sep 17 00:00:00 2001 From: ang-xd Date: Thu, 28 Aug 2025 12:33:19 -0400 Subject: [PATCH] Additional changes to keybind icons --- .../Buttons/Freezer/FreezeButton.cs | 2 -- MiraAPI/Hud/CustomActionButton.cs | 36 ++++++++++++++----- MiraAPI/Hud/CustomButtonManager.cs | 1 + MiraAPI/Patches/HudManagerPatches.cs | 30 ++++++++++++++++ MiraAPI/Utilities/Extensions.cs | 2 +- MiraAPI/Utilities/Helpers.cs | 22 ++++++++++++ 6 files changed, 82 insertions(+), 11 deletions(-) diff --git a/MiraAPI.Example/Buttons/Freezer/FreezeButton.cs b/MiraAPI.Example/Buttons/Freezer/FreezeButton.cs index 1f10c6f..726c16f 100644 --- a/MiraAPI.Example/Buttons/Freezer/FreezeButton.cs +++ b/MiraAPI.Example/Buttons/Freezer/FreezeButton.cs @@ -17,8 +17,6 @@ public class FreezeButton : CustomActionButton public override float Cooldown => OptionGroupSingleton.Instance.FreezeDuration; - public override int MaxUses => (int)OptionGroupSingleton.Instance.FreezeUses; - public override LoadableAsset Sprite => ExampleAssets.ExampleButton; public override KeyboardKeyCode? DefaultKeybind => KeyboardKeyCode.T; public override ModifierKey Modifier1 => ModifierKey.Control; diff --git a/MiraAPI/Hud/CustomActionButton.cs b/MiraAPI/Hud/CustomActionButton.cs index 1400f4c..ffaa315 100644 --- a/MiraAPI/Hud/CustomActionButton.cs +++ b/MiraAPI/Hud/CustomActionButton.cs @@ -126,7 +126,17 @@ public abstract class CustomActionButton /// /// Gets the gameObject used for the keybind icon. /// - public GameObject KeybindIcon { get; set; } + public GameObject? KeybindIcon { get; private set; } + + /// + /// Gets or sets the keybind entry for the button. + /// + private KeybindManager.KeybindEntry? KeybindEntry { get; set; } + + /// + /// Gets or sets the keybind icon text. + /// + private TextMeshPro? KeybindText { get; set; } /// /// The method used to create the button. @@ -200,13 +210,18 @@ public abstract class CustomActionButton } })); - KeybindIcon = Object.Instantiate(Button.usesRemainingSprite.gameObject, Button.transform); - KeybindIcon.GetComponent().sprite = MiraAssets.KeybindButton.LoadAsset(); - KeybindIcon.transform.GetChild(0).GetComponent().text = - KeybindManager.GetEntries().First(x => x.Id == $"{Name}_Keybind").Key.ToString(); - KeybindIcon.name = "KeybindIcon"; - KeybindIcon.transform.localPosition = new(0.5f, 0.5f, -0.1f); - Button.usesRemainingSprite.transform.localPosition = new(-0.341f, 0.5f, -0.1f); + if (DefaultKeybind != KeyboardKeyCode.None) + { + KeybindEntry = KeybindManager.GetEntries().First(x => x.Id == $"{Name}_Keybind"); + KeybindIcon = + Helpers.CreateKeybindIcon( + Button.gameObject, + KeybindEntry.Key, + new Vector3(MaxUses <= 0 ? -0.4f : 0.4f, 0.45f, -9f) + ); + KeybindText = KeybindIcon.transform.GetChild(0).GetComponent(); + Button.usesRemainingSprite.transform.localPosition = new(-0.341f, 0.45f, -0.1f); + } } /// @@ -465,6 +480,11 @@ public abstract class CustomActionButton /// The local PlayerControl. public virtual void FixedUpdateHandler(PlayerControl playerControl) { + if (KeybindText != null && KeybindEntry != null) + { + KeybindText.text = KeybindEntry.Key.ToString(); + } + if (Timer >= 0) { if (!TimerPaused) diff --git a/MiraAPI/Hud/CustomButtonManager.cs b/MiraAPI/Hud/CustomButtonManager.cs index e576595..8e57c40 100644 --- a/MiraAPI/Hud/CustomButtonManager.cs +++ b/MiraAPI/Hud/CustomButtonManager.cs @@ -4,6 +4,7 @@ using System.Collections.ObjectModel; using System.Reflection; using MiraAPI.Events.Mira; using MiraAPI.PluginLoading; +using UnityEngine; namespace MiraAPI.Hud; diff --git a/MiraAPI/Patches/HudManagerPatches.cs b/MiraAPI/Patches/HudManagerPatches.cs index ce244db..fa848de 100644 --- a/MiraAPI/Patches/HudManagerPatches.cs +++ b/MiraAPI/Patches/HudManagerPatches.cs @@ -1,11 +1,14 @@ using System; using System.Collections; +using System.Collections.Generic; using HarmonyLib; using MiraAPI.Hud; using MiraAPI.Keybinds; +using MiraAPI.Utilities; using Reactor.Utilities; using Reactor.Utilities.Extensions; using Rewired; +using TMPro; using UnityEngine; using Object = UnityEngine.Object; @@ -22,6 +25,8 @@ public static class HudManagerPatches public static Transform? BottomRight { get; private set; } public static Transform? Buttons { get; private set; } + private static Dictionary VanillaKeybindIcons = new(); + /* /// /// Trigger hudstart on current custom gamemode @@ -96,6 +101,26 @@ public static class HudManagerPatches gridArrange.ArrangeChilds(); aspectPosition.AdjustPosition(); + + 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 = Helpers.GetKeybindByActionId(actionId); + var icon = Helpers.CreateKeybindIcon(buttonObj, key, keybindIconPos); + VanillaKeybindIcons.Add(icon.transform.GetChild(0).GetComponent(), actionId); + } } /// @@ -132,6 +157,11 @@ public static class HudManagerPatches [HarmonyPostfix] public static void StartPostfix() { + foreach (var btnIcon in VanillaKeybindIcons) + { + btnIcon.Key.text = Helpers.GetKeybindByActionId(btnIcon.Value).ToString(); + } + foreach (var entry in KeybindManager.GetEntries()) { var player = ReInput.players.GetPlayer(0); diff --git a/MiraAPI/Utilities/Extensions.cs b/MiraAPI/Utilities/Extensions.cs index 863df73..e5f991f 100644 --- a/MiraAPI/Utilities/Extensions.cs +++ b/MiraAPI/Utilities/Extensions.cs @@ -94,7 +94,7 @@ public static class Extensions /// The current timer value. /// The maximum timer value. /// The format string to use for the timer text. - public static void SetCooldownFormat(this ActionButton button, float timer, float maxTimer, string format = "0") + public static void SetCooldownFormat(this ActionButton? button, float timer, float maxTimer, string format = "0") { var num = Mathf.Clamp(timer / maxTimer, 0f, 1f); button.isCoolingDown = num > 0f; diff --git a/MiraAPI/Utilities/Helpers.cs b/MiraAPI/Utilities/Helpers.cs index 2bf7b37..6c0bf4c 100644 --- a/MiraAPI/Utilities/Helpers.cs +++ b/MiraAPI/Utilities/Helpers.cs @@ -5,6 +5,7 @@ using System.Globalization; using System.Linq; using Il2CppSystem.IO; using MiraAPI.Keybinds; +using MiraAPI.Utilities.Assets; using Rewired; using TMPro; using UnityEngine; @@ -25,6 +26,27 @@ public static class Helpers { return [.. GameData.Instance.AllPlayers.ToArray().Where(x => !x.IsDead && !x.Disconnected && x.Object).Select(x => x.Object)]; } + internal static GameObject CreateKeybindIcon(GameObject button, KeyboardKeyCode keyCode, Vector3 localPos) + { + var keybindIcon = Object.Instantiate(HudManager.Instance.AbilityButton.usesRemainingSprite.gameObject, button.transform); + keybindIcon.GetComponent().sprite = MiraAssets.KeybindButton.LoadAsset(); + keybindIcon.transform.GetChild(0).GetComponent().text = keyCode.ToString(); + keybindIcon.name = "KeybindIcon"; + keybindIcon.transform.localPosition = localPos; + return keybindIcon; + } + + /// + /// Gets the keybind for an action with ReInput. + /// + /// The action ID. + /// The keyboard key code. + public static KeyboardKeyCode GetKeybindByActionId(int actionId) + { + var player = ReInput.players.GetPlayer(0); + return player.controllers.maps.GetFirstElementMapWithAction(ControllerType.Keyboard, actionId, false) + .keyboardKeyCode; + } /// /// Determines whether a given probability check succeeds.