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 controller button that was pressed. internal static void InvokeButtonPressed(IMonitor monitor, Buttons button) { monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.ControllerButtonPressed)}", ControlEvents.ControllerButtonPressed?.GetInvocationList(), null, new EventArgsControllerButtonPressed(PlayerIndex.One, button)); } /// Raise a event. /// Encapsulates monitoring and logging. /// The controller button that was released. internal static void InvokeButtonReleased(IMonitor monitor, Buttons button) { monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.ControllerButtonReleased)}", ControlEvents.ControllerButtonReleased?.GetInvocationList(), null, new EventArgsControllerButtonReleased(PlayerIndex.One, button)); } /// Raise a event. /// Encapsulates monitoring and logging. /// The trigger button that was pressed. /// The current trigger value. internal static void InvokeTriggerPressed(IMonitor monitor, Buttons button, float value) { monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.ControllerTriggerPressed)}", ControlEvents.ControllerTriggerPressed?.GetInvocationList(), null, new EventArgsControllerTriggerPressed(PlayerIndex.One, button, value)); } /// Raise a event. /// Encapsulates monitoring and logging. /// The trigger button that was pressed. /// The current trigger value. internal static void InvokeTriggerReleased(IMonitor monitor, Buttons button, float value) { monitor.SafelyRaiseGenericEvent($"{nameof(ControlEvents)}.{nameof(ControlEvents.ControllerTriggerReleased)}", ControlEvents.ControllerTriggerReleased?.GetInvocationList(), null, new EventArgsControllerTriggerReleased(PlayerIndex.One, button, value)); } } }