From 13ef3bdd498faf7d2151de3ee3e70095a8d44ced Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Sat, 21 Jun 2025 19:32:28 -0400 Subject: [PATCH] add format string for cooldown timers --- MiraAPI/Hud/CustomActionButton.cs | 10 +++++-- MiraAPI/Utilities/Extensions.cs | 46 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/MiraAPI/Hud/CustomActionButton.cs b/MiraAPI/Hud/CustomActionButton.cs index 7e0c0e2..42edf39 100644 --- a/MiraAPI/Hud/CustomActionButton.cs +++ b/MiraAPI/Hud/CustomActionButton.cs @@ -3,6 +3,7 @@ using System.Globalization; using MiraAPI.Events; using MiraAPI.Events.Mira; using MiraAPI.Patches; +using MiraAPI.Utilities; using MiraAPI.Utilities.Assets; using UnityEngine; using UnityEngine.Events; @@ -36,6 +37,11 @@ public abstract class CustomActionButton /// public abstract LoadableAsset Sprite { get; } + /// + /// Gets the format string for the cooldown timer. + /// + public virtual string CooldownTimerFormatString => "0"; + /// /// Gets the button's effect duration in seconds. If the button has no effect, set to 0. /// @@ -446,12 +452,12 @@ public abstract class CustomActionButton { Button.SetFillUp(Timer, EffectDuration); - Button.cooldownTimerText.text = Mathf.CeilToInt(Timer).ToString(NumberFormatInfo.InvariantInfo); + Button.cooldownTimerText.text = Timer.ToString(CooldownTimerFormatString, NumberFormatInfo.InvariantInfo); Button.cooldownTimerText.gameObject.SetActive(true); } else { - Button.SetCoolDown(Timer, Cooldown); + Button.SetCooldownFormat(Timer, Cooldown, CooldownTimerFormatString); } } diff --git a/MiraAPI/Utilities/Extensions.cs b/MiraAPI/Utilities/Extensions.cs index 74e5ea2..86e62e9 100644 --- a/MiraAPI/Utilities/Extensions.cs +++ b/MiraAPI/Utilities/Extensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; @@ -41,6 +42,51 @@ public static class Extensions BitConverter.GetBytes(count.Value).AddRangeToArray(BitConverter.GetBytes(chance.Value))); } + /// + /// Sets the cooldown of a button with a formatted string. + /// + /// The ActionButton to set the cooldown for. + /// 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") + { + var num = Mathf.Clamp(timer / maxTimer, 0f, 1f); + button.isCoolingDown = num > 0f; + button.SetCooldownFill(num); + if (button.isCoolingDown) + { + button.cooldownTimerText.text = timer.ToString(format, NumberFormatInfo.InvariantInfo); + button.cooldownTimerText.gameObject.SetActive(true); + return; + } + button.cooldownTimerText.gameObject.SetActive(false); + } + + /// + /// Sets the fill-up variant of a cooldown button with a formatted string. + /// + /// The ActionButton to set the cooldown for. + /// The current timer value. + /// The maximum timer value. + /// The format string to use for the timer text. + public static void SetFillUpFormat(this ActionButton button, float timer, float maxTimer, string format="0") + { + var num = Mathf.Clamp(timer / maxTimer, 0f, 1f); + button.isCoolingDown = num > 0f; + if (button.isCoolingDown && timer < 3f) + { + button.graphic.transform.localPosition = button.position + (Vector3)UnityEngine.Random.insideUnitCircle * 0.05f; + button.cooldownTimerText.text = timer.ToString(format, NumberFormatInfo.InvariantInfo); + button.cooldownTimerText.gameObject.SetActive(true); + } + else + { + button.graphic.transform.localPosition = button.position; + } + button.SetCooldownFill(num); + } + /// /// Gets a PlayerControl from their PlayerVoteArea in a meeting. ///