-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCControlManager.cs
329 lines (303 loc) · 10.3 KB
/
GCControlManager.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Text;
namespace Gamecodeur
{
class ControlMethod
{
public string Name;
public List<Keys> ListKeys;
// Pad
public bool[][] GamePadButton;
// Mouse
public bool MouseButton1;
public bool MouseButton2;
public bool MouseButton3;
// States
public bool isDown;
public bool isUp;
public bool isPressed;
public ControlMethod()
{
ListKeys = new List<Keys>();
GamePadButton = new bool[2][];
GamePadButton[0] = new bool[(int)GCControlManager.GamePadButton._MAX];
GamePadButton[1] = new bool[(int)GCControlManager.GamePadButton._MAX];
}
}
public class GCControlManager
{
KeyboardState OldKB;
MouseState OldMS;
GamePadState[] OldGPS;
List<ControlMethod> ListMethods;
public enum MouseButton
{
Button1,
Button2,
Button3
}
public enum GamePadButton
{
A,
B,
X,
Y,
Left,
Up,
Right,
Down,
_MAX
}
public GCControlManager()
{
Reset();
}
public void Reset()
{
ListMethods = new List<ControlMethod>();
OldGPS = new GamePadState[2];
}
private ControlMethod GetControlMethod(string pMethodName)
{
bool bFoundControl = false;
foreach (var method in ListMethods)
{
if (method.Name == pMethodName)
{
bFoundControl = true;
return method;
}
}
if (!bFoundControl)
{
ControlMethod cm = new ControlMethod();
cm.Name = pMethodName;
ListMethods.Add(cm);
return cm;
}
return null;
}
public void SetMethodPad(string pMethodName, int pPadNumber, GamePadButton pButton)
{
ControlMethod method = GetControlMethod(pMethodName);
method.GamePadButton[pPadNumber][(int)pButton] = true;
}
public void SetMethodMouse(string pMethodName, MouseButton pButton)
{
ControlMethod method = GetControlMethod(pMethodName);
switch (pButton)
{
case MouseButton.Button1:
method.MouseButton1 = true;
break;
case MouseButton.Button2:
method.MouseButton2 = true;
break;
case MouseButton.Button3:
method.MouseButton3 = true;
break;
default:
break;
}
}
public void SetMethodKey(string pMethodName, Keys key)
{
ControlMethod method = GetControlMethod(pMethodName);
bool bFoundKey = false;
for (int i = 0; i < method.ListKeys.Count; i++)
{
Keys k = method.ListKeys[i];
if (k == key)
{
method.ListKeys[i] = key;
bFoundKey = true;
}
}
if (!bFoundKey)
{
method.ListKeys.Add(key);
}
}
public bool Pressed(string pMethodName)
{
foreach (ControlMethod cm in ListMethods)
{
if (cm.Name == pMethodName)
{
bool isPressed = cm.isPressed;
cm.isPressed = false;
return isPressed;
}
}
return false;
}
public bool Down(string pMethodName)
{
foreach (ControlMethod cm in ListMethods)
{
if (cm.Name == pMethodName)
{
return cm.isDown;
}
}
return false;
}
public void Update()
{
KeyboardState NewKB = Keyboard.GetState();
MouseState NewMS = Mouse.GetState();
GamePadState[] NewGPS = new GamePadState[2];
NewGPS[0] = GamePad.GetState(PlayerIndex.One);
NewGPS[1] = GamePad.GetState(PlayerIndex.Two);
ButtonState OldMouseButtonDown = ButtonState.Released;
ButtonState NewMouseButtonDown = ButtonState.Released;
bool OldGamePadButtonDown = false;
bool NewGamePadButtonDown = false;
foreach (ControlMethod cm in ListMethods)
{
cm.isUp = false;
cm.isDown = false;
cm.isPressed = false;
// Pad
for (int i = 0; i < (int)GamePadButton._MAX; i++)
{
if (cm.GamePadButton[0][i])
{
if (i == (int)GamePadButton.A)
{
OldGamePadButtonDown = OldGPS[0].IsButtonDown(Buttons.A);
NewGamePadButtonDown = NewGPS[0].IsButtonDown(Buttons.A);
}
else if (i == (int)GamePadButton.B)
{
OldGamePadButtonDown = OldGPS[0].IsButtonDown(Buttons.B);
NewGamePadButtonDown = NewGPS[0].IsButtonDown(Buttons.B);
}
if (i == (int)GamePadButton.X)
{
OldGamePadButtonDown = OldGPS[0].IsButtonDown(Buttons.X);
NewGamePadButtonDown = NewGPS[0].IsButtonDown(Buttons.X);
}
else if (i == (int)GamePadButton.Y)
{
OldGamePadButtonDown = OldGPS[0].IsButtonDown(Buttons.Y);
NewGamePadButtonDown = NewGPS[0].IsButtonDown(Buttons.Y);
}
else if (i == (int)GamePadButton.Right)
{
OldGamePadButtonDown = OldGPS[0].DPad.Right == ButtonState.Pressed;
NewGamePadButtonDown = NewGPS[0].DPad.Right == ButtonState.Pressed;
}
else if (i == (int)GamePadButton.Down)
{
OldGamePadButtonDown = OldGPS[0].DPad.Down == ButtonState.Pressed;
NewGamePadButtonDown = NewGPS[0].DPad.Down == ButtonState.Pressed;
}
else if (i == (int)GamePadButton.Left)
{
OldGamePadButtonDown = OldGPS[0].DPad.Left == ButtonState.Pressed;
NewGamePadButtonDown = NewGPS[0].DPad.Left == ButtonState.Pressed;
}
else if (i == (int)GamePadButton.Up)
{
OldGamePadButtonDown = OldGPS[0].DPad.Up == ButtonState.Pressed;
NewGamePadButtonDown = NewGPS[0].DPad.Up == ButtonState.Pressed;
}
}
}
if (NewGamePadButtonDown)
{
cm.isDown = true;
if (OldGamePadButtonDown == false)
{
cm.isPressed = true;
}
}
else
{
if (OldGamePadButtonDown == true)
{
cm.isUp = true;
}
else
{
cm.isUp = false;
}
}
// Mouse
bool MouseToTest = false;
if (cm.MouseButton1)
{
MouseToTest = true;
NewMouseButtonDown = NewMS.LeftButton;
OldMouseButtonDown = OldMS.LeftButton;
}
if (cm.MouseButton2)
{
MouseToTest = true;
NewMouseButtonDown = NewMS.RightButton;
OldMouseButtonDown = OldMS.RightButton;
}
if (cm.MouseButton3)
{
MouseToTest = true;
NewMouseButtonDown = NewMS.MiddleButton;
OldMouseButtonDown = OldMS.MiddleButton;
}
if (MouseToTest)
{
if (NewMouseButtonDown == ButtonState.Pressed)
{
cm.isDown = true;
if (OldMouseButtonDown == ButtonState.Released)
{
cm.isPressed = true;
}
}
else
{
if (OldMouseButtonDown == ButtonState.Pressed)
{
cm.isUp = true;
}
else
{
cm.isUp = false;
}
}
}
// Keys
for (int i = 0; i < cm.ListKeys.Count; i++)
{
Keys k = cm.ListKeys[i];
if (NewKB.IsKeyDown(k))
{
cm.isDown = true;
if (!OldKB.IsKeyDown(k))
{
cm.isPressed = true;
}
}
else
{
if (cm.isDown)
{
cm.isUp = true;
}
else
{
cm.isUp = false;
}
}
}
}
OldGPS[0] = NewGPS[0];
OldGPS[1] = NewGPS[1];
OldMS = NewMS;
OldKB = NewKB;
}
}
}