/
MouseManager.cs
170 lines (152 loc) · 4.35 KB
/
MouseManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using Cosmos.HAL;
namespace Cosmos.System
{
/// <summary>
/// The possible states of a mouse.
/// </summary>
[Flags]
public enum MouseState
{
/// <summary>
/// No button is pressed.
/// </summary>
None = 0b0000_0000,
/// <summary>
/// The left mouse button is pressed.
/// </summary>
Left = 0b0000_0001,
/// <summary>
/// The right mouse button is pressed.
/// </summary>
Right = 0b0000_0010,
/// <summary>
/// The middle mouse button is pressed.
/// </summary>
Middle = 0b0000_0100,
/// <summary>
/// The fourth mouse button is pressed.
/// </summary>
FourthButton = 0b0000_1000,
/// <summary>
/// The fifth mouse button is pressed.
/// </summary>
FifthButton = 0b0001_0000
}
/// <summary>
/// Mouse manager class.
/// </summary>
public static class MouseManager
{
private static List<MouseBase> mMouseList = new List<MouseBase>();
/// <summary>
/// The X location of the mouse.
/// </summary>
public static uint X;
/// <summary>
/// The Y location of the mouse.
/// </summary>
public static uint Y;
// /// <summary>
// /// The Point of the mouse.
// /// </summary>
// public static Point GetPoint() {return new Point((int)MouseManager.X, (int)MouseManager.Y);}
/// <summary>
/// The state the mouse is currently in.
/// </summary>
public static MouseState MouseState;
/// <summary>
/// The sensitivity of the mouse, 1.0f is the default.
/// </summary>
public static float MouseSensitivity = 1.0f;
private static uint mScreenWidth;
private static uint mScreenHeight;
/// <summary>
/// The screen width (i.e. max value of X).
/// </summary>
public static uint ScreenWidth
{
get => mScreenWidth;
set
{
mScreenWidth = value;
if (X >= mScreenWidth)
{
X = mScreenWidth - 1;
}
}
}
/// <summary>
/// The screen height (i.e. max value of Y).
/// </summary>
public static uint ScreenHeight
{
get => mScreenHeight;
set
{
mScreenHeight = value;
if (Y >= mScreenHeight)
{
Y = mScreenHeight - 1;
}
}
}
/// <summary>
/// Mouse manager constructor.
/// </summary>
static MouseManager()
{
foreach (var mouse in HAL.Global.GetMouseDevices())
{
AddMouse(mouse);
}
}
/// <summary>
/// Mouse handler.
/// </summary>
/// <param name="aDeltaX">Mouse location change on X axis.</param>
/// <param name="aDeltaY">Mouse location change on Y axis.</param>
/// <param name="aMouseState">Mouse pressed button state</param>
/// <param name="aScrollWheel">unused</param>
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;
}
}
/// <summary>
/// Add mouse to the mouse list.
/// </summary>
/// <param name="aMouse">A mouse to add.</param>
private static void AddMouse(MouseBase aMouse)
{
aMouse.OnMouseChanged = HandleMouse;
mMouseList.Add(aMouse);
}
}
}