using System; using System.Collections.Generic; using Cosmos.HAL; namespace Cosmos.System { /// /// The possible states of a mouse. /// [Flags] public enum MouseState { /// /// No button is pressed. /// None = 0b0000_0000, /// /// The left mouse button is pressed. /// Left = 0b0000_0001, /// /// The right mouse button is pressed. /// Right = 0b0000_0010, /// /// The middle mouse button is pressed. /// Middle = 0b0000_0100, /// /// The fourth mouse button is pressed. /// FourthButton = 0b0000_1000, /// /// The fifth mouse button is pressed. /// FifthButton = 0b0001_0000 } public static class MouseManager { private static List mMouseList = new List(); /// /// The X location of the mouse. /// public static uint X; /// /// The Y location of the mouse. /// public static uint Y; /// /// The state the mouse is currently in. /// public static MouseState MouseState; /// /// The sensitivity of the mouse, 1.0f is the default. /// public static float MouseSensitivity = 1.0f; private static uint mScreenWidth; private static uint mScreenHeight; /// /// The screen width (i.e. max value of X). /// public static uint ScreenWidth { get => mScreenWidth; set { mScreenWidth = value; if (X >= mScreenWidth) { X = mScreenWidth - 1; } } } /// /// The screen height (i.e. max value of Y). /// public static uint ScreenHeight { get => mScreenHeight; set { mScreenHeight = value; if (X >= mScreenHeight) { X = mScreenHeight - 1; } } } static MouseManager() { foreach (var mouse in HAL.Global.GetMouseDevices()) { AddMouse(mouse); } } public static void HandleMouse(int aDeltaX, int aDeltaY, int aMouseState, int aScrollWheel) { int x = (int)(X + MouseSensitivity * aDeltaX); int y = (int)(Y + MouseSensitivity * aDeltaY); MouseState = (MouseState)aMouseState; if (x <= 0) { X = 0; } else if (x >= ScreenWidth) { X = ScreenWidth - 1; } else { X = (uint)x; } if (y <= 0) { Y = 0; } else if (y >= ScreenHeight) { Y = ScreenHeight - 1; } else { Y = (uint)y; } } private static void AddMouse(MouseBase aMouse) { aMouse.OnMouseChanged = HandleMouse; mMouseList.Add(aMouse); } } }