[Common] Moved axis translation to common

This commit is contained in:
Stefanos A 2014-01-17 19:10:41 +01:00 committed by thefiddler
parent 85e6b9f9ad
commit 94476b930e
2 changed files with 76 additions and 47 deletions

View file

@ -29,10 +29,83 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Text; using System.Text;
using OpenTK.Input;
namespace OpenTK.Platform.Common 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 enum HIDPage : ushort
{ {
Undefined = 0x00, Undefined = 0x00,

View file

@ -756,7 +756,7 @@ namespace OpenTK.Platform.MacOS
case HIDUsageGD.Dial: case HIDUsageGD.Dial:
case HIDUsageGD.Wheel: case HIDUsageGD.Wheel:
short offset = GetJoystickAxis(val, elem); short offset = GetJoystickAxis(val, elem);
JoystickAxis axis = TranslateJoystickAxis(usage); JoystickAxis axis = HidHelper.TranslateJoystickAxis(page, usage);
if (axis >= JoystickAxis.Axis0 && axis <= JoystickAxis.Last) if (axis >= JoystickAxis.Axis0 && axis <= JoystickAxis.Last)
{ {
joy.State.SetAxis(axis, offset); joy.State.SetAxis(axis, offset);
@ -780,7 +780,7 @@ namespace OpenTK.Platform.MacOS
case HIDUsageSim.Rudder: case HIDUsageSim.Rudder:
case HIDUsageSim.Throttle: case HIDUsageSim.Throttle:
short offset = GetJoystickAxis(val, elem); short offset = GetJoystickAxis(val, elem);
JoystickAxis axis = TranslateJoystickAxis(usage); JoystickAxis axis = HidHelper.TranslateJoystickAxis(page, usage);
if (axis >= JoystickAxis.Axis0 && axis <= JoystickAxis.Last) if (axis >= JoystickAxis.Axis0 && axis <= JoystickAxis.Last)
{ {
joy.State.SetAxis(axis, offset); joy.State.SetAxis(axis, offset);
@ -807,51 +807,7 @@ namespace OpenTK.Platform.MacOS
int max = NativeMethods.IOHIDElementGetLogicalMax(element).ToInt32(); int max = NativeMethods.IOHIDElementGetLogicalMax(element).ToInt32();
int min = NativeMethods.IOHIDElementGetLogicalMin(element).ToInt32(); int min = NativeMethods.IOHIDElementGetLogicalMin(element).ToInt32();
int offset = NativeMethods.IOHIDValueGetIntegerValue(val).ToInt32(); int offset = NativeMethods.IOHIDValueGetIntegerValue(val).ToInt32();
if (offset < min) return (short)HidHelper.ScaleValue(offset, min, max, short.MinValue, short.MaxValue);
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;
}
} }
static bool GetJoystickButton(IOHIDValueRef val, IOHIDElementRef element) static bool GetJoystickButton(IOHIDValueRef val, IOHIDElementRef element)