forked from NeoforceControls/Neoforce-Mono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputSystem.cs
751 lines (629 loc) · 27.8 KB
/
InputSystem.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
////////////////////////////////////////////////////////////////
// //
// Neoforce Controls //
// //
////////////////////////////////////////////////////////////////
// //
// File: InputSystem.cs //
// //
// Version: 0.7 //
// //
// Date: 11/09/2010 //
// //
// Author: Tom Shane //
// //
////////////////////////////////////////////////////////////////
// //
// Copyright (c) by Tom Shane //
// //
////////////////////////////////////////////////////////////////
#region //// Using /////////////
////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.GamerServices;
////////////////////////////////////////////////////////////////////////////
#endregion
namespace TomShane.Neoforce.Controls
{
#region //// Enums /////////////
////////////////////////////////////////////////////////////////////////////
[Flags]
public enum InputMethods
{
None = 0x00,
Keyboard = 0x01,
Mouse = 0x02,
GamePad = 0x04,
All = Keyboard | Mouse | 0x04
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public enum MouseButton
{
None = 0,
Left,
Right,
Middle,
XButton1,
XButton2
}
////////////////////////////////////////////////////////////////////////////
public enum MouseScrollDirection
{
None = 0,
Down = 1,
Up = 2
}
////////////////////////////////////////////////////////////////////////////
public enum GamePadButton
{
None = 0,
Start = 6,
Back,
Up,
Down,
Left,
Right,
A,
B,
X,
Y,
BigButton,
LeftShoulder,
RightShoulder,
LeftTrigger,
RightTrigger,
LeftStick,
RightStick,
LeftStickLeft,
LeftStickRight,
LeftStickUp,
LeftStickDown,
RightStickLeft,
RightStickRight,
RightStickUp,
RightStickDown
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public enum ActivePlayer
{
None = -1,
One = 0,
Two = 1,
Three = 2,
Four = 3
}
////////////////////////////////////////////////////////////////////////////
#endregion
#region //// Structs ///////////
////////////////////////////////////////////////////////////////////////////
public struct GamePadVectors
{
public Vector2 LeftStick;
public Vector2 RightStick;
public float LeftTrigger;
public float RightTrigger;
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public struct InputOffset
{
public int X;
public int Y;
public float RatioX;
public float RatioY;
public InputOffset(int x, int y, float rx, float ry)
{
X = x;
Y = y;
RatioX = rx;
RatioY = ry;
}
}
////////////////////////////////////////////////////////////////////////////
#endregion
#region //// Classes ///////////
////////////////////////////////////////////////////////////////////////////
public class InputSystem: Disposable
{
#region //// Classes ///////////
////////////////////////////////////////////////////////////////////////////
private class InputKey
{
public Keys Key = Keys.None;
public bool Pressed = false;
public double Countdown = RepeatDelay;
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
private class InputMouseButton
{
public MouseButton Button = MouseButton.None;
public bool Pressed = false;
public double Countdown = RepeatDelay;
public InputMouseButton()
{
}
public InputMouseButton(MouseButton button)
{
Button = button;
}
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
private class InputMouse
{
public MouseState State = new MouseState();
public Point Position = new Point(0, 0);
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
private class InputGamePadButton
{
public GamePadButton Button = GamePadButton.None;
public bool Pressed = false;
public double Countdown = RepeatDelay;
public InputGamePadButton()
{
}
public InputGamePadButton(GamePadButton button)
{
Button = button;
}
}
////////////////////////////////////////////////////////////////////////////
#endregion
#region //// Consts ////////////
////////////////////////////////////////////////////////////////////////////
private const int RepeatDelay = 500;
private const int RepeatRate = 50;
private float ClickThreshold = 0.5f;
////////////////////////////////////////////////////////////////////////////
#endregion
#region //// Fields ////////////
////////////////////////////////////////////////////////////////////////////
private List<InputKey> keys = new List<InputKey>();
private List<InputMouseButton> mouseButtons = new List<InputMouseButton>();
private List<InputGamePadButton> gamePadButtons = new List<InputGamePadButton>();
private MouseState mouseState = new MouseState();
private GamePadState gamePadState = new GamePadState();
private Manager manager = null;
private InputOffset inputOffset = new InputOffset(0, 0, 1.0f, 1.0f);
private InputMethods inputMethods = InputMethods.All;
private ActivePlayer activePlayer = ActivePlayer.None;
////////////////////////////////////////////////////////////////////////////
#endregion
#region //// Properties ////////
////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Sets or gets input offset and ratio when rescaling controls in render target.
/// </summary>
public virtual InputOffset InputOffset
{
get { return inputOffset; }
set { inputOffset = value; }
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Sets or gets input methods allowed for navigation.
/// </summary>
public virtual InputMethods InputMethods
{
get { return inputMethods; }
set { inputMethods = value; }
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public virtual ActivePlayer ActivePlayer
{
get { return activePlayer; }
set { activePlayer = value; }
}
////////////////////////////////////////////////////////////////////////////
#endregion
#region //// Events ////////////
////////////////////////////////////////////////////////////////////////////
public event KeyEventHandler KeyDown;
public event KeyEventHandler KeyPress;
public event KeyEventHandler KeyUp;
public event MouseEventHandler MouseDown;
public event MouseEventHandler MousePress;
public event MouseEventHandler MouseUp;
public event MouseEventHandler MouseMove;
/// <summary>
/// Occurs when the mouse is scrolled.
/// </summary>
public event MouseEventHandler MouseScroll;
public event GamePadEventHandler GamePadUp;
public event GamePadEventHandler GamePadDown;
public event GamePadEventHandler GamePadPress;
public event GamePadEventHandler GamePadMove;
////////////////////////////////////////////////////////////////////////////
#endregion
#region //// Constructors //////
////////////////////////////////////////////////////////////////////////////
public InputSystem(Manager manager, InputOffset offset)
{
this.inputOffset = offset;
this.manager = manager;
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public InputSystem(Manager manager): this(manager, new InputOffset(0, 0, 1.0f, 1.0f))
{
}
////////////////////////////////////////////////////////////////////////////
#endregion
#region //// Methods ///////////
////////////////////////////////////////////////////////////////////////////
public virtual void Initialize()
{
keys.Clear();
mouseButtons.Clear();
gamePadButtons.Clear();
#if (!XBOX && !XBOX_FAKE)
foreach (string str in Enum.GetNames(typeof(Keys)))
{
InputKey key = new InputKey();
key.Key = (Keys)Enum.Parse(typeof(Keys), str);
keys.Add(key);
}
foreach (string str in Enum.GetNames(typeof(MouseButton)))
{
InputMouseButton btn = new InputMouseButton();
btn.Button = (MouseButton)Enum.Parse(typeof(MouseButton), str);
mouseButtons.Add(btn);
}
foreach (string str in Enum.GetNames(typeof(GamePadButton)))
{
InputGamePadButton btn = new InputGamePadButton();
btn.Button = (GamePadButton)Enum.Parse(typeof(GamePadButton), str);
gamePadButtons.Add(btn);
}
#else
gamePadButtons.Add(new InputGamePadButton(GamePadButton.None));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.Start));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.Back));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.Up));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.Down));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.Left));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.Right));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.A));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.B));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.X));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.Y));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.BigButton));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.LeftShoulder));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.RightShoulder));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.LeftTrigger));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.RightTrigger));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.LeftStick));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.RightStick));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.LeftStickLeft));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.LeftStickRight));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.LeftStickUp));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.LeftStickDown));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.RightStickLeft));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.RightStickRight));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.RightStickUp));
gamePadButtons.Add(new InputGamePadButton(GamePadButton.RightStickDown));
#endif
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public virtual void SendMouseState(MouseState state, GameTime gameTime)
{
UpdateMouse(state, gameTime);
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public virtual void SendKeyboardState(KeyboardState state, GameTime gameTime)
{
UpdateKeys(state, gameTime);
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public virtual void SendGamePadState(PlayerIndex playerIndex, GamePadState state, GameTime gameTime)
{
UpdateGamePad(playerIndex, state, gameTime);
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
public virtual void Update(GameTime gameTime)
{
if (manager.UseGuide && Guide.IsVisible) return;
#if (!XBOX && !XBOX_FAKE)
MouseState ms = Mouse.GetState();
KeyboardState ks = Keyboard.GetState();
#endif
{
#if (!XBOX && !XBOX_FAKE)
if ((inputMethods & InputMethods.Mouse) == InputMethods.Mouse) UpdateMouse(ms, gameTime);
if ((inputMethods & InputMethods.Keyboard) == InputMethods.Keyboard) UpdateKeys(ks, gameTime);
#endif
if ((inputMethods & InputMethods.GamePad) == InputMethods.GamePad)
{
PlayerIndex index = PlayerIndex.One;
if (Gamer.SignedInGamers.Count > 0 && activePlayer == ActivePlayer.None)
{
int i = 0; // Have to be done this way, else it crashes for player other than one
index = Gamer.SignedInGamers[i].PlayerIndex;
}
else if (activePlayer != ActivePlayer.None)
{
index = (PlayerIndex)activePlayer;
}
GamePadState gs = GamePad.GetState(index);
UpdateGamePad(index, gs, gameTime);
}
}
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
private ButtonState GetVectorState(GamePadButton button, GamePadState state)
{
ButtonState ret = ButtonState.Released;
bool down = false;
float t = ClickThreshold;
switch (button)
{
case GamePadButton.LeftStickLeft: down = state.ThumbSticks.Left.X < -t; break;
case GamePadButton.LeftStickRight: down = state.ThumbSticks.Left.X > t; break;
case GamePadButton.LeftStickUp: down = state.ThumbSticks.Left.Y > t; break;
case GamePadButton.LeftStickDown: down = state.ThumbSticks.Left.Y < -t; break;
case GamePadButton.RightStickLeft: down = state.ThumbSticks.Right.X < -t; break;
case GamePadButton.RightStickRight: down = state.ThumbSticks.Right.X > t; break;
case GamePadButton.RightStickUp: down = state.ThumbSticks.Right.Y > t; break;
case GamePadButton.RightStickDown: down = state.ThumbSticks.Right.Y < -t; break;
case GamePadButton.LeftTrigger: down = state.Triggers.Left > t; break;
case GamePadButton.RightTrigger: down = state.Triggers.Right > t; break;
}
ret = down ? ButtonState.Pressed : ButtonState.Released;
return ret;
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
private void UpdateGamePad(PlayerIndex playerIndex, GamePadState state, GameTime gameTime)
{
GamePadEventArgs e = new GamePadEventArgs(playerIndex);
if (state.ThumbSticks.Left != gamePadState.ThumbSticks.Left ||
state.ThumbSticks.Right != gamePadState.ThumbSticks.Right ||
state.Triggers.Left != gamePadState.Triggers.Left ||
state.Triggers.Right != gamePadState.Triggers.Right)
{
BuildGamePadEvent(state, GamePadButton.None, ref e);
if (GamePadMove != null) GamePadMove.Invoke(this, e);
}
foreach (InputGamePadButton btn in gamePadButtons)
{
ButtonState bs = ButtonState.Released;
if (btn.Button == GamePadButton.None) continue;
else if (btn.Button == GamePadButton.A) bs = state.Buttons.A;
else if (btn.Button == GamePadButton.B) bs = state.Buttons.B;
else if (btn.Button == GamePadButton.Back) bs = state.Buttons.Back;
else if (btn.Button == GamePadButton.Down) bs = state.DPad.Down;
else if (btn.Button == GamePadButton.Left) bs = state.DPad.Left;
else if (btn.Button == GamePadButton.Right) bs = state.DPad.Right;
else if (btn.Button == GamePadButton.Start) bs = state.Buttons.Start;
else if (btn.Button == GamePadButton.Up) bs = state.DPad.Up;
else if (btn.Button == GamePadButton.X) bs = state.Buttons.X;
else if (btn.Button == GamePadButton.Y) bs = state.Buttons.Y;
else if (btn.Button == GamePadButton.BigButton) bs = state.Buttons.BigButton;
else if (btn.Button == GamePadButton.LeftShoulder) bs = state.Buttons.LeftShoulder;
else if (btn.Button == GamePadButton.RightShoulder) bs = state.Buttons.RightShoulder;
else if (btn.Button == GamePadButton.LeftStick) bs = state.Buttons.LeftStick;
else if (btn.Button == GamePadButton.RightStick) bs = state.Buttons.RightStick;
else bs = GetVectorState(btn.Button, state);
bool pressed = (bs == ButtonState.Pressed);
if (pressed)
{
double ms = gameTime.ElapsedGameTime.TotalMilliseconds;
if (pressed) btn.Countdown -= ms;
}
if ((pressed) && (!btn.Pressed))
{
btn.Pressed = true;
BuildGamePadEvent(state, btn.Button, ref e);
if (GamePadDown != null) GamePadDown.Invoke(this, e);
if (GamePadPress != null) GamePadPress.Invoke(this, e);
}
else if ((!pressed) && (btn.Pressed))
{
btn.Pressed = false;
btn.Countdown = RepeatDelay;
BuildGamePadEvent(state, btn.Button, ref e);
if (GamePadUp != null) GamePadUp.Invoke(this, e);
}
else if (btn.Pressed && btn.Countdown < 0)
{
e.Button = btn.Button;
btn.Countdown = RepeatRate;
BuildGamePadEvent(state, btn.Button, ref e);
if (GamePadPress != null) GamePadPress.Invoke(this, e);
}
}
gamePadState = state;
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
private void BuildGamePadEvent(GamePadState state, GamePadButton button, ref GamePadEventArgs e)
{
e.State = state;
e.Button = button;
e.Vectors.LeftStick = new Vector2(state.ThumbSticks.Left.X, state.ThumbSticks.Left.Y);
e.Vectors.RightStick = new Vector2(state.ThumbSticks.Right.X, state.ThumbSticks.Right.Y);
e.Vectors.LeftTrigger = state.Triggers.Left;
e.Vectors.RightTrigger = state.Triggers.Right;
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
private void UpdateKeys(KeyboardState state, GameTime gameTime)
{
#if (!XBOX && !XBOX_FAKE)
KeyEventArgs e = new KeyEventArgs();
e.Caps = (((ushort)NativeMethods.GetKeyState(0x14)) & 0xffff) != 0;
foreach (Keys key in state.GetPressedKeys())
{
if (key == Keys.LeftAlt || key == Keys.RightAlt) e.Alt = true;
else if (key == Keys.LeftShift || key == Keys.RightShift) e.Shift = true;
else if (key == Keys.LeftControl || key == Keys.RightControl) e.Control = true;
}
foreach (InputKey key in keys)
{
if (key.Key == Keys.LeftAlt || key.Key == Keys.RightAlt ||
key.Key == Keys.LeftShift || key.Key == Keys.RightShift ||
key.Key == Keys.LeftControl || key.Key == Keys.RightControl)
{
continue;
}
bool pressed = state.IsKeyDown(key.Key);
double ms = gameTime.ElapsedGameTime.TotalMilliseconds;
if (pressed) key.Countdown -= ms;
if ((pressed) && (!key.Pressed))
{
key.Pressed = true;
e.Key = key.Key;
if (KeyDown != null) KeyDown.Invoke(this, e);
if (KeyPress != null) KeyPress.Invoke(this, e);
}
else if ((!pressed) && (key.Pressed))
{
key.Pressed = false;
key.Countdown = RepeatDelay;
e.Key = key.Key;
if (KeyUp != null) KeyUp.Invoke(this, e);
}
else if (key.Pressed && key.Countdown < 0)
{
key.Countdown = RepeatRate;
e.Key = key.Key;
if (KeyPress != null) KeyPress.Invoke(this, e);
}
}
#endif
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
private Point RecalcPosition(Point pos)
{
return new Point((int)((pos.X - InputOffset.X) / InputOffset.RatioX), (int)((pos.Y - InputOffset.Y) / InputOffset.RatioY));
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
private void AdjustPosition(ref MouseEventArgs e)
{
Rectangle screen = manager.Game.Window.ClientBounds;
if (e.Position.X < 0) e.Position.X = 0;
if (e.Position.Y < 0) e.Position.Y = 0;
if (e.Position.X >= screen.Width) e.Position.X = screen.Width - 1;
if (e.Position.Y >= screen.Height) e.Position.Y = screen.Height - 1;
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
private void BuildMouseEvent(MouseState state, MouseButton button, ref MouseEventArgs e)
{
e.State = state;
e.Button = button;
e.Position = new Point(state.X, state.Y);
AdjustPosition(ref e);
e.Position = RecalcPosition(e.Position);
e.State = new MouseState(e.Position.X, e.Position.Y, e.State.ScrollWheelValue, e.State.LeftButton, e.State.MiddleButton, e.State.RightButton, e.State.XButton1, e.State.XButton2);
Point pos = RecalcPosition(new Point(mouseState.X, mouseState.Y));
e.Difference = new Point(e.Position.X - pos.X, e.Position.Y - pos.Y);
}
////////////////////////////////////////////////////////////////////////////
private void BuildMouseEvent(MouseState state, MouseButton button, MouseScrollDirection direction, ref MouseEventArgs e)
{
BuildMouseEvent(state, button, ref e);
e.ScrollDirection = direction;
}
////////////////////////////////////////////////////////////////////////////
private void UpdateMouse(MouseState state, GameTime gameTime)
{
#if (!XBOX && !XBOX_FAKE)
if ((state.X != mouseState.X) || (state.Y != mouseState.Y))
{
MouseEventArgs e = new MouseEventArgs();
MouseButton btn = MouseButton.None;
if (state.LeftButton == ButtonState.Pressed) btn = MouseButton.Left;
else if (state.RightButton == ButtonState.Pressed) btn = MouseButton.Right;
else if (state.MiddleButton == ButtonState.Pressed) btn = MouseButton.Middle;
else if (state.XButton1 == ButtonState.Pressed) btn = MouseButton.XButton1;
else if (state.XButton2 == ButtonState.Pressed) btn = MouseButton.XButton2;
BuildMouseEvent(state, btn, ref e);
if (MouseMove != null)
{
MouseMove.Invoke(this, e);
}
}
// Mouse wheel position changed
if (state.ScrollWheelValue != mouseState.ScrollWheelValue)
{
MouseEventArgs e = new MouseEventArgs();
MouseScrollDirection direction = state.ScrollWheelValue < mouseState.ScrollWheelValue ? MouseScrollDirection.Down : MouseScrollDirection.Up;
BuildMouseEvent(state, MouseButton.None, direction, ref e);
if (MouseScroll != null)
{
MouseScroll.Invoke(this, e);
}
}
UpdateButtons(state, gameTime);
mouseState = state;
#endif
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
private void UpdateButtons(MouseState state, GameTime gameTime)
{
#if (!XBOX && !XBOX_FAKE)
MouseEventArgs e = new MouseEventArgs();
foreach (InputMouseButton btn in mouseButtons)
{
ButtonState bs = ButtonState.Released;
if (btn.Button == MouseButton.Left) bs = state.LeftButton;
else if (btn.Button == MouseButton.Right) bs = state.RightButton;
else if (btn.Button == MouseButton.Middle) bs = state.MiddleButton;
else if (btn.Button == MouseButton.XButton1) bs = state.XButton1;
else if (btn.Button == MouseButton.XButton2) bs = state.XButton2;
else continue;
bool pressed = (bs == ButtonState.Pressed);
if (pressed)
{
double ms = gameTime.ElapsedGameTime.TotalMilliseconds;
if (pressed) btn.Countdown -= ms;
}
if ((pressed) && (!btn.Pressed))
{
btn.Pressed = true;
BuildMouseEvent(state, btn.Button, ref e);
if (MouseDown != null) MouseDown.Invoke(this, e);
if (MousePress != null) MousePress.Invoke(this, e);
}
else if ((!pressed) && (btn.Pressed))
{
btn.Pressed = false;
btn.Countdown = RepeatDelay;
BuildMouseEvent(state, btn.Button, ref e);
if (MouseUp != null) MouseUp.Invoke(this, e);
}
else if (btn.Pressed && btn.Countdown < 0)
{
e.Button = btn.Button;
btn.Countdown = RepeatRate;
BuildMouseEvent(state, btn.Button, ref e);
if (MousePress != null) MousePress.Invoke(this, e);
}
}
#endif
}
////////////////////////////////////////////////////////////////////////////
#endregion
}
////////////////////////////////////////////////////////////////////////////
#endregion
}