Implemented new GamePad interface (WIP)

This commit is contained in:
thefiddler 2013-12-19 16:28:20 +01:00
parent ecd04a3cad
commit 2bf024a912
6 changed files with 290 additions and 11 deletions

View file

@ -30,17 +30,39 @@ using System;
namespace OpenTK.Input namespace OpenTK.Input
{ {
/// <summary> /// <summary>
/// Provides access to GamePad devices. Note: this API is not implemented yet. /// Provides access to GamePad devices.
/// </summary> /// </summary>
public class GamePad public class GamePad
{ {
#region Constructors internal const int MaxAxisCount = 10;
internal const int MaxButtonCount = 16; // if this grows over 32 then GamePadState.buttons must be modified
internal const int MaxDPadCount = 2;
static GamePad() static readonly IGamePadDriver driver =
Platform.Factory.Default.CreateGamePadDriver();
/// <summary>
/// Retrieves a <c>GamePadCapabilities</c> structure describing the
/// capabilities of a gamepad device.
/// </summary>
/// <param name="index">The zero-based index of a gamepad device.</param>
/// <returns>A <c>GamePadCapabilities</c> structure describing the capabilities of the gamepad device.</returns>
public static GamePadCapabilities GetCapabilities(int index)
{ {
throw new NotImplementedException(); if (index < 0)
throw new IndexOutOfRangeException();
return driver.GetCapabilities(index);
} }
#endregion /// <summary>
/// Retrieves the <c>GamePadState</c> for the specified gamepad device.
/// </summary>
/// <param name="index">The zero-based index of a gamepad device.</param>
/// <returns>A <c>GamePadState</c> structure describing the state of the gamepad device.</returns>
public static GamePadState GetState(int index)
{
return driver.GetState(index);
}
} }
} }

View file

@ -25,7 +25,7 @@
// THE SOFTWARE. // THE SOFTWARE.
using System; using System;
namespace OpenTK namespace OpenTK.Input
{ {
public enum GamePadAxis public enum GamePadAxis
{ {
@ -49,8 +49,6 @@ namespace OpenTK
Axis8, Axis8,
/// <summary>The tenth axis of the gamepad.</summary> /// <summary>The tenth axis of the gamepad.</summary>
Axis9, Axis9,
/// <summary>The last axis of the gamepad.</summary>
LastAxis
} }
} }

View file

@ -0,0 +1,67 @@
// #region License
//
// GamePadCapabilities.cs
//
// Author:
// Stefanos A. <stapostol@gmail.com>
//
// Copyright (c) 2006-2013 Stefanos Apostolopoulos
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// #endregion
using System;
namespace OpenTK.Input
{
public struct GamePadCapabilities
{
byte axis_count;
byte button_count;
byte dpad_count;
byte trackball_count;
public int AxisCount
{
get { return axis_count; }
internal set { axis_count = (byte)value; }
}
public int ButtonCount
{
get { return button_count; }
internal set { button_count = (byte)value; }
}
public int DPadCount
{
get { return dpad_count; }
internal set { dpad_count = (byte)value; }
}
public int TrackballCount
{
get { return trackball_count; }
internal set { trackball_count = (byte)value; }
}
}
}

View file

@ -0,0 +1,90 @@
// #region License
//
// GamePadDPad.cs
//
// Author:
// Stefanos A. <stapostol@gmail.com>
//
// Copyright (c) 2006-2013 Stefanos Apostolopoulos
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// #endregion
using System;
namespace OpenTK.Input
{
public struct GamePadDPad
{
[Flags]
enum DPadButtons : byte
{
Up = Buttons.DPadUp,
Down = Buttons.DPadDown,
Left = Buttons.DPadLeft,
Right = Buttons.DPadRight
}
DPadButtons buttons;
internal GamePadDPad(Buttons state)
{
// DPad butons are stored in the lower 4bits
// of the Buttons enumeration.
buttons = (DPadButtons)((int)state & 0x0f);
}
public bool IsUp
{
get { return (buttons & DPadButtons.Up) != 0; }
internal set { SetButton(DPadButtons.Up, value); }
}
public bool IsDown
{
get { return (buttons & DPadButtons.Down) != 0; }
internal set { SetButton(DPadButtons.Down, value); }
}
public bool IsLeft
{
get { return (buttons & DPadButtons.Left) != 0; }
internal set { SetButton(DPadButtons.Left, value); }
}
public bool IsRight
{
get { return (buttons & DPadButtons.Right) != 0; }
internal set { SetButton(DPadButtons.Right, value); }
}
void SetButton(DPadButtons button, bool value)
{
if (value)
{
buttons |= button;
}
else
{
buttons &= ~button;
}
}
}
}

View file

@ -34,6 +34,105 @@ namespace OpenTK.Input
/// </summary> /// </summary>
public struct GamePadState /*: IEquatable<GamePadState>*/ public struct GamePadState /*: IEquatable<GamePadState>*/
{ {
const float RangeMultiplier = 1.0f / (short.MaxValue + 1);
Buttons buttons;
unsafe fixed short axes[GamePad.MaxAxisCount];
bool is_connected;
#region Public Members
public float GetAxis(GamePadAxis axis)
{
throw new NotImplementedException();
}
public GamePadButtons Buttons
{
get { return new GamePadButtons(buttons); }
}
public GamePadDPad DPad
{
get { return new GamePadDPad(buttons); }
}
public bool IsConnected
{
get { return is_connected; }
}
#endregion
#region Internal Members
internal void SetAxis(GamePadAxis axis, short value)
{
if (IsAxisValid(axis))
{
int index = (int)axis;
unsafe
{
fixed (short *paxes = axes)
{
*(paxes + index) = value;
}
}
}
else
{
throw new ArgumentOutOfRangeException("axis");
}
}
internal void SetButton(Buttons button, bool pressed)
{
if (IsButtonValid(button))
{
int index = (int)button;
Buttons mask = (Buttons)(1 << index);
if (pressed)
{
buttons |= mask;
}
else
{
buttons &= ~mask;
}
}
else
{
throw new ArgumentOutOfRangeException("button");
}
}
internal void SetConnected(bool connected)
{
is_connected = connected;
}
#endregion
#region Private Members
bool IsAxisValid(GamePadAxis axis)
{
int index = (int)axis;
return index >= 0 && index < GamePad.MaxAxisCount;
}
bool IsButtonValid(Buttons button)
{
int index = (int)button;
return index >= 0 && index < GamePad.MaxButtonCount;
}
bool IsDPadValid(int index)
{
return index >= 0 && index < GamePad.MaxDPadCount;
}
#endregion
} }
} }

View file

@ -738,7 +738,6 @@
</EmbeddedResource> </EmbeddedResource>
<Compile Include="Platform\MacOS\HIDInput.cs" /> <Compile Include="Platform\MacOS\HIDInput.cs" />
<Compile Include="IntPtrEqualityComparer.cs" /> <Compile Include="IntPtrEqualityComparer.cs" />
<Compile Include="Input\GamePadButton.cs" />
<Compile Include="Input\GamePadAxis.cs" /> <Compile Include="Input\GamePadAxis.cs" />
<Compile Include="Platform\SDL2\Sdl2DisplayDeviceDriver.cs" /> <Compile Include="Platform\SDL2\Sdl2DisplayDeviceDriver.cs" />
<Compile Include="Platform\SDL2\Sdl2Factory.cs" /> <Compile Include="Platform\SDL2\Sdl2Factory.cs" />
@ -777,6 +776,10 @@
<Compile Include="SlotAttribute.cs" /> <Compile Include="SlotAttribute.cs" />
<Compile Include="RewrittenAttribute.cs" /> <Compile Include="RewrittenAttribute.cs" />
<Compile Include="Graphics\OpenGL\GLObsolete.cs" /> <Compile Include="Graphics\OpenGL\GLObsolete.cs" />
<Compile Include="Input\GamePadCapabilities.cs" />
<Compile Include="Input\GamePadDPad.cs" />
<Compile Include="Input\GamePadButtons.cs" />
<Compile Include="Input\Buttons.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup> <PropertyGroup>