]> git.r.bdr.sh - rbdr/super-polarity/blobdiff - SuperPolarityMac/InputController.cs
Removes spaces.
[rbdr/super-polarity] / SuperPolarityMac / InputController.cs
diff --git a/SuperPolarityMac/InputController.cs b/SuperPolarityMac/InputController.cs
new file mode 100644 (file)
index 0000000..c92bb9c
--- /dev/null
@@ -0,0 +1,248 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Microsoft.Xna.Framework.Input;
+
+namespace SuperPolarity
+{
+    static class InputController
+    {
+        static Dictionary<string, List<Action<float>>> Listeners;
+        static Dictionary<string, List<Keys>> RegisteredKeys;
+        static Dictionary<string, List<Buttons>> RegisteredButtons;
+        static List<string> BlockedKeys;
+        static List<string> BlockedButtons;
+
+        static GamePadState InputGamePadState;
+        static KeyboardState InputKeyboardState;
+
+        /*
+         * Registered Events.
+         * 
+         * You register: name of the event (ie. attack) and a key associated with it.
+         * or button... Left Stick /always/ dispatches move event.
+         */
+
+        static InputController()
+        {
+            Listeners = new Dictionary<string,List<Action<float>>>();
+            RegisteredButtons = new Dictionary<string, List<Buttons>>();
+            RegisteredKeys = new Dictionary<string, List<Keys>>();
+            BlockedKeys = new List<string>();
+            BlockedButtons = new List<string>();
+            InputKeyboardState = new KeyboardState();
+            InputGamePadState = new GamePadState();
+        }
+
+        public static void UpdateInput()
+        {
+            DispatchMoveEvents();
+            DispatchRegisteredEvents();
+        }
+
+        public static void UpdateInput(bool highPriorityOnly) 
+        {
+            Poll();
+            DispatchPauseEvent();
+            if (!highPriorityOnly)
+            {
+                UpdateInput();
+            }
+        }
+
+        public static void DispatchPauseEvent()
+        {
+            // OK THIS IS ALL KINDS OF WRONG. THIS IS A PLACEHOLDER BECAUSE DEMO!
+            var keyPressed = false;
+            if ((InputKeyboardState.IsKeyDown(Keys.Enter) || InputGamePadState.IsButtonDown(Buttons.Start))) {
+                keyPressed = true;
+                if(!BlockedButtons.Contains("pause") && !BlockedKeys.Contains("pause"))
+                {
+                    BlockedButtons.Add("pause");
+                    BlockedKeys.Add("pause");
+                    Console.WriteLine("Dispatch");
+                    Dispatch("pause", 0);
+                }
+            }
+
+            if (!keyPressed)
+            {
+                BlockedButtons.Remove("pause");
+                BlockedKeys.Remove("pause");
+            }
+        }
+
+        private static void Poll()
+        {
+            InputGamePadState = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One);
+            InputKeyboardState = Keyboard.GetState();
+        }
+
+        public static void RegisterEventForKey(string eventName, Keys key)
+        {
+            List<Keys> newKeyList;
+            if (!RegisteredKeys.ContainsKey(eventName))
+            {
+                newKeyList = new List<Keys>();
+                RegisteredKeys.Add(eventName, newKeyList);
+            }
+
+            RegisteredKeys.TryGetValue(eventName, out newKeyList);
+
+            newKeyList.Add(key);
+        }
+
+        public static void RegisterEventForButton(string eventName, Buttons button)
+        {
+            List<Buttons> newButtonList;
+            if (!RegisteredButtons.ContainsKey(eventName))
+            {
+                newButtonList = new List<Buttons>();
+                RegisteredButtons.Add(eventName, newButtonList);
+            }
+
+            RegisteredButtons.TryGetValue(eventName, out newButtonList);
+
+            newButtonList.Add(button);
+        }
+
+        private static void DispatchRegisteredEvents()
+        {
+            var keyFired = false;
+
+            foreach (KeyValuePair<string,List<Keys>> entry in RegisteredKeys) {
+                keyFired = false;
+                foreach (Keys key in entry.Value)
+                {
+                    if (InputKeyboardState.IsKeyDown(key))
+                    {
+                        if (!BlockedKeys.Contains(entry.Key))
+                        {
+                            BlockedKeys.Add(entry.Key);
+                            Dispatch(entry.Key, 1);
+                        }
+                        keyFired = true;
+                        break;
+                    }
+                }
+
+                if (!keyFired)
+                {
+                    BlockedKeys.Remove(entry.Key);
+                }
+            }
+
+            foreach (KeyValuePair<string, List<Buttons>> entry in RegisteredButtons)
+            {
+                keyFired = false;
+                foreach (Buttons button in entry.Value)
+                {
+                    if (InputGamePadState.IsButtonDown(button))
+                    {
+                        if (!BlockedButtons.Contains(entry.Key))
+                        {
+                            BlockedButtons.Add(entry.Key);
+                            Dispatch(entry.Key, 1);
+                        }
+                        keyFired = true;
+                        break;
+                    };
+                }
+
+                if (!keyFired)
+                {
+                    BlockedButtons.Remove(entry.Key);
+                }
+            }
+        }
+
+        private static void DispatchMoveEvents()
+        {
+            float xMovement = 0.0f;
+            float yMovement = 0.0f;
+            // Dispatch the moveX / MoveY events every frame.
+
+            xMovement = InputGamePadState.ThumbSticks.Left.X;
+            yMovement = -InputGamePadState.ThumbSticks.Left.Y;
+
+            if (InputKeyboardState.IsKeyDown(Keys.Left))
+            {
+                xMovement = -1.0f;
+            }
+
+            if (InputKeyboardState.IsKeyDown(Keys.Right))
+            {
+                xMovement = 1.0f;
+            }
+
+            if (InputKeyboardState.IsKeyDown(Keys.Up))
+            {
+                yMovement = -1.0f;
+            }
+
+            if (InputKeyboardState.IsKeyDown(Keys.Down))
+            {
+                yMovement = 1.0f;
+            }
+
+            Dispatch("moveX", xMovement);
+            Dispatch("moveY", yMovement);
+        }
+
+        public static void Bind(string eventName, Action<float> listener)
+        {
+            List<Action<float>> newListenerList;
+            List<Action<float>> listenerList;
+            bool               foundListeners;
+
+            if (!Listeners.ContainsKey(eventName)) {
+               newListenerList = new List<Action<float>>();
+               Listeners.Add(eventName, newListenerList);
+            }
+
+            foundListeners = Listeners.TryGetValue(eventName, out listenerList);
+
+            listenerList.Add(listener);
+        }
+
+        public static void Unbind(string eventName, Action<float> listener)
+        {
+            List<Action<float>> listenerList;
+            bool foundListeners;
+
+            if (!Listeners.ContainsKey(eventName))
+            {
+                return;
+            }
+
+            foundListeners = Listeners.TryGetValue(eventName, out listenerList);
+
+            listenerList.Remove(listener);
+        }
+
+        public static void Dispatch(string eventName, float value)
+        {
+            List<Action<float>> listenerList;
+            bool foundListeners;
+
+            foundListeners = Listeners.TryGetValue(eventName, out listenerList);
+
+            if (!foundListeners)
+            {
+                return;
+            }
+
+            for (var i = listenerList.Count - 1; i >= 0; i--)
+            {
+                listenerList[i](value);
+            }
+        }
+
+        public static void Unlock(string eventName)
+        {
+            BlockedButtons.Remove(eventName);
+            BlockedKeys.Remove(eventName);
+        }
+    }
+}