[Input] Implemented JoystickHat GamePad mapping
authorthefiddler <stapostol@gmail.com>
Fri, 25 Jul 2014 21:26:03 +0000 (23:26 +0200)
committerthefiddler <stapostol@gmail.com>
Fri, 25 Jul 2014 21:26:03 +0000 (23:26 +0200)
Source/OpenTK/Input/ConfigurationType.cs
Source/OpenTK/Input/GamePadConfiguration.cs
Source/OpenTK/Input/GamePadConfigurationSource.cs
Source/OpenTK/Platform/MappedGamePadDriver.cs

index 0cbb649..abfe15e 100644 (file)
@@ -35,7 +35,8 @@ namespace OpenTK.Input
     {
         Unmapped = 0,
         Axis,
-        Button
+        Button,
+        Hat
     }
 }
 
index 54600e7..c7e9877 100644 (file)
@@ -175,8 +175,11 @@ namespace OpenTK.Input
                     return new GamePadConfigurationSource(ParseButton(item));
 
                 case 'h':
-                    throw new NotImplementedException();
-                    //return new MapItem(ParseHat(item));
+                    {
+                        HatPosition position;
+                        JoystickHat hat = ParseHat(item, out position);
+                        return new GamePadConfigurationSource(hat, position);
+                    }
 
                 default:
                     throw new InvalidOperationException("[Input] Invalid GamePad configuration value");
@@ -193,12 +196,38 @@ namespace OpenTK.Input
 
         static JoystickButton ParseButton(string item)
         {
-            // item is in the format "b#" where # a zero-based integer nubmer
+            // item is in the format "b#" where # a zero-based integer number
             JoystickButton button = JoystickButton.Button0;
             int id = Int32.Parse(item.Substring(1));
             return button + id;
         }
 
+        static JoystickHat ParseHat(string item, out HatPosition position)
+        {
+            // item is in the format "h#.#" where:
+            // - the 1st # is the zero-based hat id
+            // - the 2nd # is a bit-flag defining the hat position
+            JoystickHat hat = JoystickHat.Hat0;
+            int id = Int32.Parse(item.Substring(1, 1));
+            int pos = Int32.Parse(item.Substring(3));
+
+            position = HatPosition.Centered;
+            switch (pos)
+            {
+                case 1: position = HatPosition.Up; break;
+                case 2: position = HatPosition.Right; break;
+                case 3: position = HatPosition.UpRight; break;
+                case 4: position = HatPosition.Down ; break;
+                case 6: position = HatPosition.DownRight; break;
+                case 8: position = HatPosition.Left; break;
+                case 9: position = HatPosition.UpLeft; break;
+                case 12: position = HatPosition.DownLeft; break;
+                default: position = HatPosition.Centered; break;
+            }
+
+            return hat + id;
+        }
+
         #endregion
     }
 }
index c1b25b8..c312f52 100644 (file)
@@ -34,8 +34,10 @@ namespace OpenTK.Input
     struct GamePadConfigurationSource
     {
         ConfigurationType map_type;
-        Nullable<JoystickButton> map_button;
-        Nullable<JoystickAxis> map_axis;
+        JoystickButton? map_button;
+        JoystickAxis? map_axis;
+        JoystickHat? map_hat;
+        HatPosition? map_hat_position;
 
         public GamePadConfigurationSource(JoystickAxis axis)
             : this()
@@ -51,6 +53,14 @@ namespace OpenTK.Input
             Button = button;
         }
 
+        public GamePadConfigurationSource(JoystickHat hat, HatPosition pos)
+            : this()
+        {
+            Type = ConfigurationType.Hat;
+            Hat = hat;
+            map_hat_position = pos;
+        }
+
         public ConfigurationType Type
         {
             get { return map_type; }
@@ -68,5 +78,17 @@ namespace OpenTK.Input
             get { return map_button.Value; }
             private set { map_button = value; }
         }
+
+        public JoystickHat Hat
+        {
+            get { return map_hat.Value; }
+            private set { map_hat = value; }
+        }
+
+        public HatPosition HatPosition
+        {
+            get { return map_hat_position.Value; }
+            private set { map_hat_position = value; }
+        }
     }
 }
index 7924540..e6902af 100644 (file)
@@ -117,6 +117,28 @@ namespace OpenTK.Platform
                                 }
                             }
                             break;
+
+                        case ConfigurationType.Hat:
+                            {
+                                // JoystickHat -> Buttons/GamePadAxes mapping
+                                JoystickHat source_hat = map.Source.Hat;
+                                bool pressed = joy.GetHat(source_hat).Position == map.Source.HatPosition;
+
+                                switch (map.Target.Type)
+                                {
+                                    case ConfigurationType.Axis:
+                                        // Todo: if SDL2 GameController config is ever updated to
+                                        // distinguish between negative/positive axes, then update
+                                        // the following line to support both.
+                                        pad.SetAxis(map.Target.Axis, pressed ? short.MaxValue : (short)0);
+                                        break;
+
+                                    case ConfigurationType.Button:
+                                        pad.SetButton(map.Target.Button, pressed);
+                                        break;
+                                }
+                            }
+                            break;
                     }
                 }
             }