[Common] Moved axis translation to common
authorStefanos A <stapostol@gmail.com>
Fri, 17 Jan 2014 18:10:41 +0000 (19:10 +0100)
committerthefiddler <stapostol@gmail.com>
Thu, 11 Sep 2014 10:51:46 +0000 (12:51 +0200)
Source/OpenTK/Platform/Common/Hid.cs
Source/OpenTK/Platform/MacOS/HIDInput.cs

index 0d3c102..fc0c432 100644 (file)
 
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Text;
+using OpenTK.Input;
 
 namespace OpenTK.Platform.Common
 {
+    class HidHelper
+    {
+        /// <summary>
+        /// Scales the specified value linearly between min and max.
+        /// </summary>
+        /// <param name="value">The value to scale</param>
+        /// <param name="value_min">The minimum expected value (inclusive)</param>
+        /// <param name="value_max">The maximum expected value (inclusive)</param>
+        /// <param name="result_min">The minimum output value (inclusive)</param>
+        /// <param name="result_max">The maximum output value (inclusive)</param>
+        /// <returns>The value, scaled linearly between min and max</returns>
+        public static int ScaleValue(int value, int value_min, int value_max,
+            int result_min, int result_max)
+        {
+            if (value_min >= value_max || result_min >= result_max)
+                throw new ArgumentOutOfRangeException();
+            MathHelper.Clamp(value, value_min, value_max);
+            
+            int range = result_max - result_min;
+            int half_range = range >> 1;
+            long temp = (value - value_min) * range; // need long to avoid overflow
+            return (int)(temp / (value_max - value_min) + half_range);
+        }
+
+        public static JoystickAxis TranslateJoystickAxis(HIDPage page, int usage)
+        {
+            switch (page)
+            {
+                case HIDPage.GenericDesktop:
+                    switch ((HIDUsageGD)usage)
+                    {
+                        case HIDUsageGD.X:
+                            return JoystickAxis.Axis0;
+                        case HIDUsageGD.Y:
+                            return JoystickAxis.Axis1;
+
+                        case HIDUsageGD.Z:
+                            return JoystickAxis.Axis2;
+                        case HIDUsageGD.Rz:
+                            return JoystickAxis.Axis3;
+
+                        case HIDUsageGD.Rx:
+                            return JoystickAxis.Axis4;
+                        case HIDUsageGD.Ry:
+                            return JoystickAxis.Axis5;
+
+                        case HIDUsageGD.Slider:
+                            return JoystickAxis.Axis6;
+                        case HIDUsageGD.Dial:
+                            return JoystickAxis.Axis7;
+                        case HIDUsageGD.Wheel:
+                            return JoystickAxis.Axis8;
+                    }
+                    break;
+
+                case HIDPage.Simulation:
+                    switch ((HIDUsageSim)usage)
+                    {
+                        case HIDUsageSim.Rudder:
+                            return JoystickAxis.Axis9;
+                        case HIDUsageSim.Throttle:
+                            return JoystickAxis.Axis10;
+                    }
+                    break;
+            }
+
+            Debug.Print("[Input] Unknown axis with HID page/usage {0}/{1}", page, usage);
+            return 0;
+        }
+    }
+
     enum HIDPage : ushort
     {
         Undefined = 0x00,
index cccd920..e4a4614 100755 (executable)
@@ -756,7 +756,7 @@ namespace OpenTK.Platform.MacOS
                         case HIDUsageGD.Dial:
                         case HIDUsageGD.Wheel:
                             short offset = GetJoystickAxis(val, elem);
-                            JoystickAxis axis = TranslateJoystickAxis(usage);
+                            JoystickAxis axis = HidHelper.TranslateJoystickAxis(page, usage);
                             if (axis >= JoystickAxis.Axis0 && axis <= JoystickAxis.Last)
                             {
                                 joy.State.SetAxis(axis, offset);
@@ -780,7 +780,7 @@ namespace OpenTK.Platform.MacOS
                         case HIDUsageSim.Rudder:
                         case HIDUsageSim.Throttle:
                             short offset = GetJoystickAxis(val, elem);
-                            JoystickAxis axis = TranslateJoystickAxis(usage);
+                            JoystickAxis axis = HidHelper.TranslateJoystickAxis(page, usage);
                             if (axis >= JoystickAxis.Axis0 && axis <= JoystickAxis.Last)
                             {
                                 joy.State.SetAxis(axis, offset);
@@ -807,51 +807,7 @@ namespace OpenTK.Platform.MacOS
             int max = NativeMethods.IOHIDElementGetLogicalMax(element).ToInt32();
             int min = NativeMethods.IOHIDElementGetLogicalMin(element).ToInt32();
             int offset = NativeMethods.IOHIDValueGetIntegerValue(val).ToInt32();
-            if (offset < min)
-                offset = min;
-            if (offset > max)
-                offset = max;
-
-            const int range = short.MaxValue - short.MinValue + 1;
-            const int half_range = short.MaxValue + 1;
-            return (short)((offset - min) * range / (max - min) + half_range);
-        }
-
-        static JoystickAxis TranslateJoystickAxis(int usage)
-        {
-            switch (usage)
-            {
-                case (int)HIDUsageGD.X:
-                    return JoystickAxis.Axis0;
-                case (int)HIDUsageGD.Y:
-                    return JoystickAxis.Axis1;
-
-                case (int)HIDUsageGD.Z:
-                    return JoystickAxis.Axis2;
-                case (int)HIDUsageGD.Rz:
-                    return JoystickAxis.Axis3;
-
-                case (int)HIDUsageGD.Rx:
-                    return JoystickAxis.Axis4;
-                case (int)HIDUsageGD.Ry:
-                    return JoystickAxis.Axis5;
-
-                case (int)HIDUsageGD.Slider:
-                    return JoystickAxis.Axis6;
-                case (int)HIDUsageGD.Dial:
-                    return JoystickAxis.Axis7;
-                case (int)HIDUsageGD.Wheel:
-                    return JoystickAxis.Axis8;
-
-                case (int)HIDUsageSim.Rudder:
-                    return JoystickAxis.Axis9;
-                case (int)HIDUsageSim.Throttle:
-                    return JoystickAxis.Axis10;
-
-                default:
-                    Debug.Print("[Mac] Unknown axis with HID usage {0}", usage);
-                    return 0;
-            }
+            return (short)HidHelper.ScaleValue(offset, min, max, short.MinValue, short.MaxValue);
         }
 
         static bool GetJoystickButton(IOHIDValueRef val, IOHIDElementRef element)