forked from craftworkgames/MonoGame.Extended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MouseStateExtended.cs
101 lines (86 loc) · 4.59 KB
/
MouseStateExtended.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
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace MonoGame.Extended.Input
{
public struct MouseStateExtended
{
private readonly MouseState _currentMouseState;
private readonly MouseState _previousMouseState;
public MouseStateExtended(MouseState currentMouseState, MouseState previousMouseState)
{
_currentMouseState = currentMouseState;
_previousMouseState = previousMouseState;
}
public int X => _currentMouseState.X;
public int Y => _currentMouseState.Y;
public Point Position => _currentMouseState.Position;
public bool PositionChanged => _currentMouseState.Position != _previousMouseState.Position;
public int DeltaX => _previousMouseState.X - _currentMouseState.X;
public int DeltaY => _previousMouseState.Y - _currentMouseState.Y;
public Point DeltaPosition => new Point(DeltaX, DeltaY);
public int ScrollWheelValue => _currentMouseState.ScrollWheelValue;
public int DeltaScrollWheelValue => _previousMouseState.ScrollWheelValue - _currentMouseState.ScrollWheelValue;
public ButtonState LeftButton => _currentMouseState.LeftButton;
public ButtonState MiddleButton => _currentMouseState.MiddleButton;
public ButtonState RightButton => _currentMouseState.RightButton;
public ButtonState XButton1 => _currentMouseState.XButton1;
public ButtonState XButton2 => _currentMouseState.XButton2;
public bool IsButtonDown(MouseButton button)
{
// ReSharper disable once SwitchStatementMissingSomeCases
switch (button)
{
case MouseButton.Left: return IsPressed(m => m.LeftButton);
case MouseButton.Middle: return IsPressed(m => m.MiddleButton);
case MouseButton.Right: return IsPressed(m => m.RightButton);
case MouseButton.XButton1: return IsPressed(m => m.XButton1);
case MouseButton.XButton2: return IsPressed(m => m.XButton2);
}
return false;
}
public bool IsButtonUp(MouseButton button)
{
// ReSharper disable once SwitchStatementMissingSomeCases
switch (button)
{
case MouseButton.Left: return IsReleased(m => m.LeftButton);
case MouseButton.Middle: return IsReleased(m => m.MiddleButton);
case MouseButton.Right: return IsReleased(m => m.RightButton);
case MouseButton.XButton1: return IsReleased(m => m.XButton1);
case MouseButton.XButton2: return IsReleased(m => m.XButton2);
}
return false;
}
public bool WasButtonJustDown(MouseButton button)
{
// ReSharper disable once SwitchStatementMissingSomeCases
switch (button)
{
case MouseButton.Left: return WasJustPressed(m => m.LeftButton);
case MouseButton.Middle: return WasJustPressed(m => m.MiddleButton);
case MouseButton.Right: return WasJustPressed(m => m.RightButton);
case MouseButton.XButton1: return WasJustPressed(m => m.XButton1);
case MouseButton.XButton2: return WasJustPressed(m => m.XButton2);
}
return false;
}
public bool WasButtonJustUp(MouseButton button)
{
// ReSharper disable once SwitchStatementMissingSomeCases
switch (button)
{
case MouseButton.Left: return WasJustReleased(m => m.LeftButton);
case MouseButton.Middle: return WasJustReleased(m => m.MiddleButton);
case MouseButton.Right: return WasJustReleased(m => m.RightButton);
case MouseButton.XButton1: return WasJustReleased(m => m.XButton1);
case MouseButton.XButton2: return WasJustReleased(m => m.XButton2);
}
return false;
}
private bool IsPressed(Func<MouseState, ButtonState> button) => button(_currentMouseState) == ButtonState.Pressed;
private bool IsReleased(Func<MouseState, ButtonState> button) => button(_currentMouseState) == ButtonState.Released;
private bool WasJustPressed(Func<MouseState, ButtonState> button) => button(_previousMouseState) == ButtonState.Released && button(_currentMouseState) == ButtonState.Pressed;
private bool WasJustReleased(Func<MouseState, ButtonState> button) => button(_previousMouseState) == ButtonState.Pressed && button(_currentMouseState) == ButtonState.Released;
}
}