From 8884996c067b6390c745fadc8e2a93872aa9e99e Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Sun, 24 Nov 2024 21:25:51 -0500 Subject: [PATCH] button events and stuff properly work --- MiraAPI.Example/ExamplePlugin.cs | 2 +- MiraAPI/Events/Mira/MiraButtonClickEvent.cs | 20 ++++++++++++++++++++ MiraAPI/Events/MiraEventManager.cs | 20 ++++++++++++++++++++ MiraAPI/Hud/CustomActionButton.cs | 9 +++++---- MiraAPI/Hud/CustomButtonManager.cs | 6 ++++++ 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/MiraAPI.Example/ExamplePlugin.cs b/MiraAPI.Example/ExamplePlugin.cs index 06adad0..eb4402c 100644 --- a/MiraAPI.Example/ExamplePlugin.cs +++ b/MiraAPI.Example/ExamplePlugin.cs @@ -29,7 +29,7 @@ public partial class ExamplePlugin : BasePlugin, IMiraPlugin { Logger.Warning("Freeze button clicked!"); e.Cancel(); - e.Button.SetTimer(5f); + e.Button.SetTimer(15f); }); MiraEventManager.RegisterEventHandler>(e=> diff --git a/MiraAPI/Events/Mira/MiraButtonClickEvent.cs b/MiraAPI/Events/Mira/MiraButtonClickEvent.cs index c517f5c..9dc58eb 100644 --- a/MiraAPI/Events/Mira/MiraButtonClickEvent.cs +++ b/MiraAPI/Events/Mira/MiraButtonClickEvent.cs @@ -22,3 +22,23 @@ public sealed class MiraButtonClickEvent : MiraCancelableEvent where T : Cust Button = button; } } + +/// +/// Button click event for Mira Buttons only. Do not use for vanilla buttons. +/// +public sealed class MiraButtonClickEvent : MiraCancelableEvent +{ + /// + /// Gets Mira Button that was clicked. + /// + public CustomActionButton Button { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The Mira Button that was clicked. + public MiraButtonClickEvent(CustomActionButton button) + { + Button = button; + } +} diff --git a/MiraAPI/Events/MiraEventManager.cs b/MiraAPI/Events/MiraEventManager.cs index e76f512..b2fa3a1 100644 --- a/MiraAPI/Events/MiraEventManager.cs +++ b/MiraAPI/Events/MiraEventManager.cs @@ -31,6 +31,26 @@ public static class MiraEventManager } } + /// + /// Invoke an event and use a specific type to find the handlers. + /// + /// The event instance. + /// The type to use for handler lookup. + public static void InvokeEvent(MiraEvent eventInstance, Type type) + { + EventHandlers.TryGetValue(type, out var handlers); + if (handlers == null) + { + Logger.Warning("No handlers for event " + type.Name); + return; + } + + foreach (var handler in handlers) + { + handler.DynamicInvoke(eventInstance); + } + } + /// /// Register an event. /// diff --git a/MiraAPI/Hud/CustomActionButton.cs b/MiraAPI/Hud/CustomActionButton.cs index c1abbb6..970eea3 100644 --- a/MiraAPI/Hud/CustomActionButton.cs +++ b/MiraAPI/Hud/CustomActionButton.cs @@ -101,13 +101,14 @@ public abstract class CustomActionButton pb.OnClick = new Button.ButtonClickedEvent(); pb.OnClick.AddListener((UnityAction)(() => { - var eventType = typeof(MiraButtonClickEvent<>).MakeGenericType(GetType()); + var eventType = CustomButtonManager.ButtonEventTypes[GetType()]; var @event = (MiraCancelableEvent)Activator.CreateInstance(eventType, this)!; - MiraEventManager.InvokeEvent(@event); + MiraEventManager.InvokeEvent(@event, eventType); if (@event.IsCancelled) { - var cancelledEvent = new MiraButtonCancelledEvent(this); - MiraEventManager.InvokeEvent(cancelledEvent); + var cancelEventType = CustomButtonManager.ButtonCancelledEventTypes[GetType()]; + var cancelEvent = (MiraEvent)Activator.CreateInstance(cancelEventType, this)!; + MiraEventManager.InvokeEvent(cancelEvent, cancelEventType); } else { diff --git a/MiraAPI/Hud/CustomButtonManager.cs b/MiraAPI/Hud/CustomButtonManager.cs index 61c5ab6..d2accef 100644 --- a/MiraAPI/Hud/CustomButtonManager.cs +++ b/MiraAPI/Hud/CustomButtonManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using MiraAPI.Events.Mira; using MiraAPI.PluginLoading; using Reactor.Utilities; @@ -9,6 +10,8 @@ namespace MiraAPI.Hud; internal static class CustomButtonManager { internal static readonly List CustomButtons = []; + internal static readonly Dictionary ButtonEventTypes = []; + internal static readonly Dictionary ButtonCancelledEventTypes = []; internal static void RegisterButton(Type buttonType, MiraPluginInfo pluginInfo) { @@ -29,5 +32,8 @@ internal static class CustomButtonManager typeof(CustomButtonSingleton<>).MakeGenericType(buttonType) .GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic)! .SetValue(null, button); + + ButtonEventTypes.Add(buttonType, typeof(MiraButtonClickEvent<>).MakeGenericType(buttonType)); + ButtonCancelledEventTypes.Add(buttonType, typeof(MiraButtonCancelledEvent<>).MakeGenericType(buttonType)); } }