]> git.r.bdr.sh - rbdr/super-polarity/blame_incremental - SuperPolarityMac/InputController.cs
Works in win, compiles in mac
[rbdr/super-polarity] / SuperPolarityMac / InputController.cs
... / ...
CommitLineData
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Microsoft.Xna.Framework.Input;
6
7namespace SuperPolarity
8{
9 static class InputController
10 {
11 static Dictionary<string, List<Action<float>>> Listeners;
12 static Dictionary<string, List<Keys>> RegisteredKeys;
13 static Dictionary<string, List<Buttons>> RegisteredButtons;
14 static List<string> BlockedKeys;
15 static List<string> BlockedButtons;
16
17 static GamePadState InputGamePadState;
18 static KeyboardState InputKeyboardState;
19
20 /*
21 * Registered Events.
22 *
23 * You register: name of the event (ie. attack) and a key associated with it.
24 * or button... Left Stick /always/ dispatches move event.
25 */
26
27 static InputController()
28 {
29 Listeners = new Dictionary<string,List<Action<float>>>();
30 RegisteredButtons = new Dictionary<string, List<Buttons>>();
31 RegisteredKeys = new Dictionary<string, List<Keys>>();
32 BlockedKeys = new List<string>();
33 BlockedButtons = new List<string>();
34 InputKeyboardState = new KeyboardState();
35 InputGamePadState = new GamePadState();
36 }
37
38 public static void UpdateInput()
39 {
40 DispatchMoveEvents();
41 DispatchRegisteredEvents();
42 }
43
44 public static void UpdateInput(bool highPriorityOnly)
45 {
46 Poll();
47 DispatchPauseEvent();
48 if (!highPriorityOnly)
49 {
50 UpdateInput();
51 }
52 }
53
54 public static void DispatchPauseEvent()
55 {
56 // OK THIS IS ALL KINDS OF WRONG. THIS IS A PLACEHOLDER BECAUSE DEMO!
57 var keyPressed = false;
58 if ((InputKeyboardState.IsKeyDown(Keys.Enter) || InputGamePadState.IsButtonDown(Buttons.Start))) {
59 keyPressed = true;
60 if(!BlockedButtons.Contains("pause") && !BlockedKeys.Contains("pause"))
61 {
62 BlockedButtons.Add("pause");
63 BlockedKeys.Add("pause");
64 Console.WriteLine("Dispatch");
65 Dispatch("pause", 0);
66 }
67 }
68
69 if (!keyPressed)
70 {
71 BlockedButtons.Remove("pause");
72 BlockedKeys.Remove("pause");
73 }
74 }
75
76 private static void Poll()
77 {
78 InputGamePadState = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One);
79 InputKeyboardState = Keyboard.GetState();
80 }
81
82 public static void RegisterEventForKey(string eventName, Keys key)
83 {
84 List<Keys> newKeyList;
85 if (!RegisteredKeys.ContainsKey(eventName))
86 {
87 newKeyList = new List<Keys>();
88 RegisteredKeys.Add(eventName, newKeyList);
89 }
90
91 RegisteredKeys.TryGetValue(eventName, out newKeyList);
92
93 newKeyList.Add(key);
94 }
95
96 public static void RegisterEventForButton(string eventName, Buttons button)
97 {
98 List<Buttons> newButtonList;
99 if (!RegisteredButtons.ContainsKey(eventName))
100 {
101 newButtonList = new List<Buttons>();
102 RegisteredButtons.Add(eventName, newButtonList);
103 }
104
105 RegisteredButtons.TryGetValue(eventName, out newButtonList);
106
107 newButtonList.Add(button);
108 }
109
110 private static void DispatchRegisteredEvents()
111 {
112 var keyFired = false;
113
114 foreach (KeyValuePair<string,List<Keys>> entry in RegisteredKeys) {
115 keyFired = false;
116 foreach (Keys key in entry.Value)
117 {
118 if (InputKeyboardState.IsKeyDown(key))
119 {
120 if (!BlockedKeys.Contains(entry.Key))
121 {
122 BlockedKeys.Add(entry.Key);
123 Dispatch(entry.Key, 1);
124 }
125 keyFired = true;
126 break;
127 }
128 }
129
130 if (!keyFired)
131 {
132 BlockedKeys.Remove(entry.Key);
133 }
134 }
135
136 foreach (KeyValuePair<string, List<Buttons>> entry in RegisteredButtons)
137 {
138 keyFired = false;
139 foreach (Buttons button in entry.Value)
140 {
141 if (InputGamePadState.IsButtonDown(button))
142 {
143 if (!BlockedButtons.Contains(entry.Key))
144 {
145 BlockedButtons.Add(entry.Key);
146 Dispatch(entry.Key, 1);
147 }
148 keyFired = true;
149 break;
150 };
151 }
152
153 if (!keyFired)
154 {
155 BlockedButtons.Remove(entry.Key);
156 }
157 }
158 }
159
160 private static void DispatchMoveEvents()
161 {
162 float xMovement = 0.0f;
163 float yMovement = 0.0f;
164 // Dispatch the moveX / MoveY events every frame.
165
166 xMovement = InputGamePadState.ThumbSticks.Left.X;
167 yMovement = -InputGamePadState.ThumbSticks.Left.Y;
168
169 if (InputKeyboardState.IsKeyDown(Keys.Left))
170 {
171 xMovement = -1.0f;
172 }
173
174 if (InputKeyboardState.IsKeyDown(Keys.Right))
175 {
176 xMovement = 1.0f;
177 }
178
179 if (InputKeyboardState.IsKeyDown(Keys.Up))
180 {
181 yMovement = -1.0f;
182 }
183
184 if (InputKeyboardState.IsKeyDown(Keys.Down))
185 {
186 yMovement = 1.0f;
187 }
188
189 Dispatch("moveX", xMovement);
190 Dispatch("moveY", yMovement);
191 }
192
193 public static void Bind(string eventName, Action<float> listener)
194 {
195 List<Action<float>> newListenerList;
196 List<Action<float>> listenerList;
197 bool foundListeners;
198
199 if (!Listeners.ContainsKey(eventName)) {
200 newListenerList = new List<Action<float>>();
201 Listeners.Add(eventName, newListenerList);
202 }
203
204 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
205
206 listenerList.Add(listener);
207 }
208
209 public static void Unbind(string eventName, Action<float> listener)
210 {
211 List<Action<float>> listenerList;
212 bool foundListeners;
213
214 if (!Listeners.ContainsKey(eventName))
215 {
216 return;
217 }
218
219 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
220
221 listenerList.Remove(listener);
222 }
223
224 public static void Dispatch(string eventName, float value)
225 {
226 List<Action<float>> listenerList;
227 bool foundListeners;
228
229 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
230
231 if (!foundListeners)
232 {
233 return;
234 }
235
236 for (var i = listenerList.Count - 1; i >= 0; i--)
237 {
238 listenerList[i](value);
239 }
240 }
241
242 public static void Unlock(string eventName)
243 {
244 BlockedButtons.Remove(eventName);
245 BlockedKeys.Remove(eventName);
246 }
247 }
248}