#region usings using System; using System.Collections.Generic; using OpenTK; using OpenTK.Input; using SM.Base.Window; #endregion namespace SM.Base.Controls { /// /// Mouse controller /// public class Mouse { private static MouseState? _mouseState; private static List _lastButtonsPressed = new List(); private static Vector2 _inScreen; /// /// Gets or sets the current position of the mouse in the screen. /// public static Vector2 InScreen { get => _inScreen; set { _inScreen = value; UpdateNormalized(SMRenderer.CurrentWindow); } } /// /// The current position of the mouse in the screen from 0..1. /// public static Vector2 InScreenNormalized { get; private set; } /// /// This returns true, if the left mouse button was pressed. /// Its pretty much: IsDown(MouseButton.Left, true) /// public static bool LeftClick => IsDown(MouseButton.Left, true); /// /// This returns true, if the right mouse button was pressed. /// Its pretty much: IsDown(MouseButton.Right, true) /// public static bool RightClick => IsDown(MouseButton.Right, true); /// /// If true, it disables the tracking of the mouse, allowing you to change the value, without the system replacing it again. /// public static bool StopTracking { get; set; } private static void UpdateNormalized(IGenericWindow window) { InScreenNormalized = new Vector2(_inScreen.X / (float)window.Width, _inScreen.Y / (float)window.Height); } /// /// The event to update the values. /// /// The event args. /// The window where the mouse is checked internal static void MouseMoveEvent(MouseMoveEventArgs mmea, IGenericWindow window) { if (StopTracking) return; InScreen = new Vector2(mmea.X, mmea.Y); UpdateNormalized(window); } internal static void SetState() { if (_mouseState.HasValue) { _lastButtonsPressed = new List(); foreach (object o in Enum.GetValues(typeof(MouseButton))) if (_mouseState.Value[(MouseButton) o]) _lastButtonsPressed.Add((MouseButton) o); } _mouseState = OpenTK.Input.Mouse.GetState(); } /// /// Checks if the mouse is pressed. /// /// /// If true, it will not get called, when it was pressed in the last update. public static bool IsDown(MouseButton button, bool once = false) { return _mouseState?[button] == true && !(once && _lastButtonsPressed.Contains(button)); } /// /// Checks if the mouse is not pressed. /// /// /// If true, it will not get called, when it was not pressed in the last update. public static bool IsUp(MouseButton button, bool once = false) { return _mouseState?[button] == false && !(once && !_lastButtonsPressed.Contains(button)); } } }