1 | using Microsoft.Xna.Framework; |
---|
2 | using Microsoft.Xna.Framework.Input; |
---|
3 | using System.Collections.Generic; |
---|
4 | using System.Windows.Forms; |
---|
5 | using Keys = System.Windows.Forms.Keys; |
---|
6 | |
---|
7 | namespace CastleMaster.Input |
---|
8 | { |
---|
9 | public class InputHandler |
---|
10 | { |
---|
11 | private class Key |
---|
12 | { |
---|
13 | private bool lastState = false, currentState = false, nextState = false; |
---|
14 | |
---|
15 | public bool IsDown { get { return currentState; } } |
---|
16 | |
---|
17 | public bool HasBeenPressed { get { return !lastState && currentState; } } |
---|
18 | |
---|
19 | public void Update() |
---|
20 | { |
---|
21 | lastState = currentState; |
---|
22 | currentState = nextState; |
---|
23 | } |
---|
24 | |
---|
25 | public void Toggle(bool state) |
---|
26 | { |
---|
27 | nextState = state; |
---|
28 | } |
---|
29 | |
---|
30 | public void Release() |
---|
31 | { |
---|
32 | currentState = false; |
---|
33 | nextState = false; |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | private static Dictionary<Keys, Key> registeredKeys; |
---|
38 | private static Dictionary<MouseButtons, Key> registeredMouseButtons; |
---|
39 | private static int oldMouseScroll = 0, mouseScrollDelta; |
---|
40 | private static Point mousePos = Point.Zero; |
---|
41 | |
---|
42 | private Control windowControl; |
---|
43 | |
---|
44 | public InputHandler(GameWindow window) |
---|
45 | { |
---|
46 | registeredKeys = new Dictionary<Keys, Key>(); |
---|
47 | registeredMouseButtons = new Dictionary<MouseButtons, Key>(); |
---|
48 | |
---|
49 | windowControl = Control.FromHandle(window.Handle); |
---|
50 | windowControl.PreviewKeyDown += new PreviewKeyDownEventHandler(OnKeyDown); |
---|
51 | windowControl.KeyUp += new KeyEventHandler(OnKeyUp); |
---|
52 | windowControl.MouseDown += new MouseEventHandler(OnMouseDown); |
---|
53 | windowControl.MouseUp += new MouseEventHandler(OnMouseUp); |
---|
54 | } |
---|
55 | |
---|
56 | private void OnMouseUp(object sender, MouseEventArgs e) |
---|
57 | { |
---|
58 | Key k; |
---|
59 | if (registeredMouseButtons.TryGetValue(e.Button, out k)) k.Toggle(false); |
---|
60 | } |
---|
61 | |
---|
62 | private void OnMouseDown(object sender, MouseEventArgs e) |
---|
63 | { |
---|
64 | Key k; |
---|
65 | if (registeredMouseButtons.TryGetValue(e.Button, out k)) k.Toggle(true); |
---|
66 | } |
---|
67 | |
---|
68 | private void OnKeyUp(object sender, KeyEventArgs e) |
---|
69 | { |
---|
70 | Key k; |
---|
71 | if (registeredKeys.TryGetValue(e.KeyCode, out k)) k.Toggle(false); |
---|
72 | } |
---|
73 | |
---|
74 | private void OnKeyDown(object sender, PreviewKeyDownEventArgs e) |
---|
75 | { |
---|
76 | Key k; |
---|
77 | if (registeredKeys.TryGetValue(e.KeyCode, out k)) k.Toggle(true); |
---|
78 | } |
---|
79 | |
---|
80 | public void Update() |
---|
81 | { |
---|
82 | foreach (Key k in registeredKeys.Values) |
---|
83 | k.Update(); |
---|
84 | |
---|
85 | MouseState state = Mouse.GetState(); |
---|
86 | mousePos.X = state.X; |
---|
87 | mousePos.Y = state.Y; |
---|
88 | mouseScrollDelta = state.ScrollWheelValue - oldMouseScroll; |
---|
89 | oldMouseScroll = state.ScrollWheelValue; |
---|
90 | |
---|
91 | foreach (Key k in registeredMouseButtons.Values) |
---|
92 | k.Update(); |
---|
93 | } |
---|
94 | |
---|
95 | public void RegisterKeyboardKey(Keys key) |
---|
96 | { |
---|
97 | registeredKeys.Add(key, new Key()); |
---|
98 | } |
---|
99 | |
---|
100 | public void RegisterMouseKey(MouseButtons mb) |
---|
101 | { |
---|
102 | registeredMouseButtons.Add(mb, new Key()); |
---|
103 | } |
---|
104 | |
---|
105 | public void ReleaseAll() |
---|
106 | { |
---|
107 | foreach (Key k in registeredKeys.Values) |
---|
108 | k.Release(); |
---|
109 | |
---|
110 | foreach (Key k in registeredMouseButtons.Values) |
---|
111 | k.Release(); |
---|
112 | } |
---|
113 | |
---|
114 | public static bool IsKeyDown(Keys key) |
---|
115 | { |
---|
116 | Key k; |
---|
117 | return registeredKeys.TryGetValue(key, out k) && k.IsDown; |
---|
118 | } |
---|
119 | |
---|
120 | public static bool HasKeyBeenPressed(Keys key) |
---|
121 | { |
---|
122 | Key k; |
---|
123 | return registeredKeys.TryGetValue(key, out k) && k.HasBeenPressed; |
---|
124 | } |
---|
125 | |
---|
126 | public static bool IsMouseButtonDown(MouseButtons mb) |
---|
127 | { |
---|
128 | Key k; |
---|
129 | return registeredMouseButtons.TryGetValue(mb, out k) && k.IsDown; |
---|
130 | } |
---|
131 | |
---|
132 | public static bool HasMouseButtonBeenPressed(MouseButtons mb) |
---|
133 | { |
---|
134 | Key k; |
---|
135 | return registeredMouseButtons.TryGetValue(mb, out k) && k.HasBeenPressed; |
---|
136 | } |
---|
137 | |
---|
138 | public static int MouseX { get { return mousePos.X; } } |
---|
139 | |
---|
140 | public static int MouseY { get { return mousePos.Y; } } |
---|
141 | |
---|
142 | public static int MouseScrollDelta { get { return mouseScrollDelta; } } |
---|
143 | |
---|
144 | public static Point MousePos { get { return mousePos; } } |
---|
145 | } |
---|
146 | } |
---|