2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework.Input;
7 namespace SuperPolarity
9 static class InputController
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;
17 static GamePadState InputGamePadState;
18 static KeyboardState InputKeyboardState;
23 * You register: name of the event (ie. attack) and a key associated with it.
24 * or button... Left Stick /always/ dispatches move event.
27 static InputController()
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();
38 public static void UpdateInput()
41 DispatchRegisteredEvents();
44 public static void UpdateInput(bool highPriorityOnly)
48 if (!highPriorityOnly)
54 public static void DispatchPauseEvent()
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))) {
60 if(!BlockedButtons.Contains("pause") && !BlockedKeys.Contains("pause"))
62 BlockedButtons.Add("pause");
63 BlockedKeys.Add("pause");
64 Console.WriteLine("Dispatch");
71 BlockedButtons.Remove("pause");
72 BlockedKeys.Remove("pause");
76 private static void Poll()
78 InputGamePadState = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One);
79 InputKeyboardState = Keyboard.GetState();
82 public static void RegisterEventForKey(string eventName, Keys key)
84 List<Keys> newKeyList;
85 if (!RegisteredKeys.ContainsKey(eventName))
87 newKeyList = new List<Keys>();
88 RegisteredKeys.Add(eventName, newKeyList);
91 RegisteredKeys.TryGetValue(eventName, out newKeyList);
96 public static void RegisterEventForButton(string eventName, Buttons button)
98 List<Buttons> newButtonList;
99 if (!RegisteredButtons.ContainsKey(eventName))
101 newButtonList = new List<Buttons>();
102 RegisteredButtons.Add(eventName, newButtonList);
105 RegisteredButtons.TryGetValue(eventName, out newButtonList);
107 newButtonList.Add(button);
110 private static void DispatchRegisteredEvents()
112 var keyFired = false;
114 foreach (KeyValuePair<string,List<Keys>> entry in RegisteredKeys) {
116 foreach (Keys key in entry.Value)
118 if (InputKeyboardState.IsKeyDown(key))
120 if (!BlockedKeys.Contains(entry.Key))
122 BlockedKeys.Add(entry.Key);
123 Dispatch(entry.Key, 1);
132 BlockedKeys.Remove(entry.Key);
136 foreach (KeyValuePair<string, List<Buttons>> entry in RegisteredButtons)
139 foreach (Buttons button in entry.Value)
141 if (InputGamePadState.IsButtonDown(button))
143 if (!BlockedButtons.Contains(entry.Key))
145 BlockedButtons.Add(entry.Key);
146 Dispatch(entry.Key, 1);
155 BlockedButtons.Remove(entry.Key);
160 private static void DispatchMoveEvents()
162 float xMovement = 0.0f;
163 float yMovement = 0.0f;
164 // Dispatch the moveX / MoveY events every frame.
166 xMovement = InputGamePadState.ThumbSticks.Left.X;
167 yMovement = -InputGamePadState.ThumbSticks.Left.Y;
169 if (InputKeyboardState.IsKeyDown(Keys.Left))
174 if (InputKeyboardState.IsKeyDown(Keys.Right))
179 if (InputKeyboardState.IsKeyDown(Keys.Up))
184 if (InputKeyboardState.IsKeyDown(Keys.Down))
189 Dispatch("moveX", xMovement);
190 Dispatch("moveY", yMovement);
193 public static void Bind(string eventName, Action<float> listener)
195 List<Action<float>> newListenerList;
196 List<Action<float>> listenerList;
199 if (!Listeners.ContainsKey(eventName)) {
200 newListenerList = new List<Action<float>>();
201 Listeners.Add(eventName, newListenerList);
204 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
206 listenerList.Add(listener);
209 public static void Unbind(string eventName, Action<float> listener)
211 List<Action<float>> listenerList;
214 if (!Listeners.ContainsKey(eventName))
219 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
221 listenerList.Remove(listener);
224 public static void Dispatch(string eventName, float value)
226 List<Action<float>> listenerList;
229 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
236 for (var i = listenerList.Count - 1; i >= 0; i--)
238 listenerList[i](value);
242 public static void Unlock(string eventName)
244 BlockedButtons.Remove(eventName);
245 BlockedKeys.Remove(eventName);