Updated internal IGamePadDriver interface

This commit is contained in:
thefiddler 2013-12-19 16:27:26 +01:00
parent ee65f81f56
commit 484af18673
4 changed files with 71 additions and 28 deletions

View file

@ -6,19 +6,11 @@ namespace OpenTK.Input
{ {
interface IGamePadDriver interface IGamePadDriver
{ {
/// <summary>
/// Retrieves the combined <see cref="OpenTK.Input.GamePadState"/> for all gamepad devices.
/// </summary>
/// <returns>A <see cref="OpenTK.Input.GamePadState"/> structure containing the combined state for all gamepad devices.</returns>
GamePadState GetState();
/// <summary>
/// Retrieves the <see cref="OpenTK.Input.GamePadState"/> for the specified gamepad device.
/// </summary>
/// <param name="index">The index of the keyboard device.</param>
/// <returns>A <see cref="OpenTK.Input.GamePadState"/> structure containing the state of the gamepad device.</returns>
GamePadState GetState(int index); GamePadState GetState(int index);
GamePadCapabilities GetCapabilities(int index);
/// <summary> /// <summary>
/// Retrieves the device name for the gamepad device. /// Retrieves the device name for the gamepad device.
/// </summary> /// </summary>
@ -26,6 +18,6 @@ namespace OpenTK.Input
/// <returns>A <see cref="System.String"/> with the name of the specified device or <see cref="System.String.Empty"/>.</returns> /// <returns>A <see cref="System.String"/> with the name of the specified device or <see cref="System.String.Empty"/>.</returns>
/// <remarks> /// <remarks>
/// <para>If no device exists at the specified index, the return value is <see cref="System.String.Empty"/>.</para></remarks> /// <para>If no device exists at the specified index, the return value is <see cref="System.String.Empty"/>.</para></remarks>
string GetDeviceName(int index); string GetName(int index);
} }
} }

View file

@ -34,18 +34,32 @@ namespace OpenTK.Platform.SDL2
{ {
class Sdl2JoystickDriver : IJoystickDriver, IGamePadDriver, IDisposable class Sdl2JoystickDriver : IJoystickDriver, IGamePadDriver, IDisposable
{ {
const float RangeMultiplier = 1.0f / 32768.0f;
struct Sdl2JoystickDetails struct Sdl2JoystickDetails
{ {
public IntPtr Handle { get; set; } public IntPtr Handle { get; set; }
public float RangeMultiplier { get { return 1.0f / 32768.0f; } }
public int HatCount { get; set; } public int HatCount { get; set; }
public int BallCount { get; set; } public int BallCount { get; set; }
public bool IsConnected { get; set; } public bool IsConnected { get; set; }
} }
class Sdl2GamePad
{
public IntPtr Handle { get; private set; }
public GamePadState State { get; set; }
public Sdl2GamePad(IntPtr handle)
{
Handle = handle;
}
}
readonly List<JoystickDevice> joysticks = new List<JoystickDevice>(); readonly List<JoystickDevice> joysticks = new List<JoystickDevice>();
readonly Dictionary<int, Sdl2GamePad> controllers = new Dictionary<int, Sdl2GamePad>();
IList<JoystickDevice> joysticks_readonly; IList<JoystickDevice> joysticks_readonly;
bool disposed = false; bool disposed;
public Sdl2JoystickDriver() public Sdl2JoystickDriver()
{ {
@ -94,6 +108,11 @@ namespace OpenTK.Platform.SDL2
return id >= 0 && id < joysticks.Count; return id >= 0 && id < joysticks.Count;
} }
bool IsControllerValid(int id)
{
return controllers.ContainsKey(id);
}
#endregion #endregion
#region Public Members #region Public Members
@ -145,7 +164,7 @@ namespace OpenTK.Platform.SDL2
if (IsJoystickValid(id)) if (IsJoystickValid(id))
{ {
JoystickDevice<Sdl2JoystickDetails> joystick = (JoystickDevice<Sdl2JoystickDetails>)joysticks[id]; JoystickDevice<Sdl2JoystickDetails> joystick = (JoystickDevice<Sdl2JoystickDetails>)joysticks[id];
float value = ev.Value * joystick.Details.RangeMultiplier; float value = ev.Value * RangeMultiplier;
joystick.SetAxis((JoystickAxis)ev.Axis, value); joystick.SetAxis((JoystickAxis)ev.Axis, value);
} }
else else
@ -203,12 +222,27 @@ namespace OpenTK.Platform.SDL2
switch (ev.Type) switch (ev.Type)
{ {
case EventType.CONTROLLERDEVICEADDED: case EventType.CONTROLLERDEVICEADDED:
if (SDL.IsGameController(id))
{
IntPtr handle = SDL.GameControllerOpen(id);
if (handle != IntPtr.Zero)
{
Sdl2GamePad pad = new Sdl2GamePad(handle);
pad.State.SetConnected(true);
controllers.Add(id, pad);
}
}
break; break;
case EventType.CONTROLLERDEVICEREMOVED: case EventType.CONTROLLERDEVICEREMOVED:
if (IsControllerValid(id))
{
controllers[id].State.SetConnected(false);
}
break; break;
case EventType.CONTROLLERDEVICEREMAPPED: case EventType.CONTROLLERDEVICEREMAPPED:
// Todo: what should we do in this case?
break; break;
} }
} }
@ -216,15 +250,27 @@ namespace OpenTK.Platform.SDL2
public void ProcessControllerEvent(ControllerAxisEvent ev) public void ProcessControllerEvent(ControllerAxisEvent ev)
{ {
int id = ev.Which; int id = ev.Which;
if (IsControllerValid(id))
{
controllers[id].State.SetAxis((GamePadAxis)ev.Axis, ev.Value);
}
else
{
Debug.Print("[SDL2] Invalid game controller handle {0} in {1}", id, ev.Type);
}
} }
public void ProcessControllerEvent(ControllerButtonEvent ev) public void ProcessControllerEvent(ControllerButtonEvent ev)
{ {
int id = ev.Which; int id = ev.Which;
if (IsControllerValid(id))
{
controllers[id].State.SetButton((Buttons)ev.Button, ev.State == State.Pressed);
}
else
{
Debug.Print("[SDL2] Invalid game controller handle {0} in {1}", id, ev.Type);
}
} }
#endregion #endregion
@ -248,9 +294,9 @@ namespace OpenTK.Platform.SDL2
#region IGamePadDriver Members #region IGamePadDriver Members
public GamePadState GetState() public GamePadCapabilities GetCapabilities(int index)
{ {
return new GamePadState(); return new GamePadCapabilities();
} }
public GamePadState GetState(int index) public GamePadState GetState(int index)
@ -263,7 +309,7 @@ namespace OpenTK.Platform.SDL2
return new GamePadState(); return new GamePadState();
} }
public string GetDeviceName(int index) public string GetName(int index)
{ {
return String.Empty; return String.Empty;
} }

View file

@ -428,8 +428,9 @@ namespace OpenTK.Platform.Windows
#endregion #endregion
//HACK implement #region IGamePadDriver Members
public GamePadState GetState()
public GamePadCapabilities GetCapabilities(int index)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -439,9 +440,11 @@ namespace OpenTK.Platform.Windows
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string GetDeviceName(int index) public string GetName(int index)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
#endregion
} }
} }

View file

@ -259,21 +259,23 @@ namespace OpenTK.Platform.X11
#endregion #endregion
//HACK implement #region IGamePadDriver Members
public GamePadState GetState()
public GamePadCapabilities GetCapabilities(int index)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public GamePadState GetState(int index) public GamePadState GetState(int index)
{ {
Poll();
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string GetDeviceName(int index) public string GetName(int index)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
#endregion
} }
} }