using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; using StardewModdingAPI.Framework; namespace StardewModdingAPI.Events { /// Events raised when the player uses a controller, keyboard, or mouse. public static class ControlEvents { /********* ** Events *********/ /// Raised when the changes. That happens when the player presses or releases a key. public static event EventHandler KeyboardChanged; /// Raised when the player presses a keyboard key. public static event EventHandler KeyPressed; /// Raised when the player releases a keyboard key. public static event EventHandler KeyReleased; /// Raised when the changes. That happens when the player moves the mouse, scrolls the mouse wheel, or presses/releases a button. public static event EventHandler MouseChanged; /// The player pressed a controller button. This event isn't raised for trigger buttons. public static event EventHandler ControllerButtonPressed; /// The player released a controller button. This event isn't raised for trigger buttons. public static event EventHandler ControllerButtonReleased; /// The player pressed a controller trigger button. public static event EventHandler ControllerTriggerPressed; /// The player released a controller trigger button. public static event EventHandler ControllerTriggerReleased; /********* ** Internal methods *********/ /// Raise a event. /// Encapsulates monitoring and logging. /// The previous keyboard state. /// The current keyboard state. internal static void InvokeKeyboardChanged(IMonitor monitor, KeyboardState priorState, KeyboardState newState) { monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.KeyboardChanged)}", ControlEvents.KeyboardChanged?.GetInvocationList(), null, new EventArgsKeyboardStateChanged(priorState, newState)); } /// Raise a event. /// Encapsulates monitoring and logging. /// The previous mouse state. /// The current mouse state. /// The previous mouse position on the screen adjusted for the zoom level. /// The current mouse position on the screen adjusted for the zoom level. internal static void InvokeMouseChanged(IMonitor monitor, MouseState priorState, MouseState newState, Point priorPosition, Point newPosition) { monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.MouseChanged)}", ControlEvents.MouseChanged?.GetInvocationList(), null, new EventArgsMouseStateChanged(priorState, newState, priorPosition, newPosition)); } /// Raise a event. /// Encapsulates monitoring and logging. /// The keyboard button that was pressed. internal static void InvokeKeyPressed(IMonitor monitor, Keys key) { monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.KeyPressed)}", ControlEvents.KeyPressed?.GetInvocationList(), null, new EventArgsKeyPressed(key)); } /// Raise a event. /// Encapsulates monitoring and logging. /// The keyboard button that was released. internal static void InvokeKeyReleased(IMonitor monitor, Keys key) { monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.KeyReleased)}", ControlEvents.KeyReleased?.GetInvocationList(), null, new EventArgsKeyPressed(key)); } /// Raise a event. /// Encapsulates monitoring and logging. /// The player who pressed the button. /// The controller button that was pressed. internal static void InvokeButtonPressed(IMonitor monitor, PlayerIndex playerIndex, Buttons button) { monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.ControllerButtonPressed)}", ControlEvents.ControllerButtonPressed?.GetInvocationList(), null, new EventArgsControllerButtonPressed(playerIndex, button)); } /// Raise a event. /// Encapsulates monitoring and logging. /// The player who released the button. /// The controller button that was released. internal static void InvokeButtonReleased(IMonitor monitor, PlayerIndex playerIndex, Buttons button) { monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.ControllerButtonReleased)}", ControlEvents.ControllerButtonReleased?.GetInvocationList(), null, new EventArgsControllerButtonReleased(playerIndex, button)); } /// Raise a event. /// Encapsulates monitoring and logging. /// The player who pressed the trigger button. /// The trigger button that was pressed. /// The current trigger value. internal static void InvokeTriggerPressed(IMonitor monitor, PlayerIndex playerIndex, Buttons button, float value) { monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.ControllerTriggerPressed)}", ControlEvents.ControllerTriggerPressed?.GetInvocationList(), null, new EventArgsControllerTriggerPressed(playerIndex, button, value)); } /// Raise a event. /// Encapsulates monitoring and logging. /// The player who pressed the trigger button. /// The trigger button that was pressed. /// The current trigger value. internal static void InvokeTriggerReleased(IMonitor monitor, PlayerIndex playerIndex, Buttons button, float value) { monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.ControllerTriggerReleased)}", ControlEvents.ControllerTriggerReleased?.GetInvocationList(), null, new EventArgsControllerTriggerReleased(playerIndex, button, value)); } } }