From 2bf024a912e6aae1d69d1caee372102402a323ba Mon Sep 17 00:00:00 2001 From: thefiddler Date: Thu, 19 Dec 2013 16:28:20 +0100 Subject: [PATCH] Implemented new GamePad interface (WIP) --- Source/OpenTK/Input/GamePad.cs | 32 +++++-- Source/OpenTK/Input/GamePadAxis.cs | 8 +- Source/OpenTK/Input/GamePadCapabilities.cs | 67 +++++++++++++++ Source/OpenTK/Input/GamePadDPad.cs | 90 ++++++++++++++++++++ Source/OpenTK/Input/GamePadState.cs | 99 ++++++++++++++++++++++ Source/OpenTK/OpenTK.csproj | 5 +- 6 files changed, 290 insertions(+), 11 deletions(-) create mode 100644 Source/OpenTK/Input/GamePadCapabilities.cs create mode 100644 Source/OpenTK/Input/GamePadDPad.cs diff --git a/Source/OpenTK/Input/GamePad.cs b/Source/OpenTK/Input/GamePad.cs index 303f4516..7a2fc066 100644 --- a/Source/OpenTK/Input/GamePad.cs +++ b/Source/OpenTK/Input/GamePad.cs @@ -30,17 +30,39 @@ using System; namespace OpenTK.Input { /// - /// Provides access to GamePad devices. Note: this API is not implemented yet. + /// Provides access to GamePad devices. /// 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(); + + /// + /// Retrieves a GamePadCapabilities structure describing the + /// capabilities of a gamepad device. + /// + /// The zero-based index of a gamepad device. + /// A GamePadCapabilities structure describing the capabilities of the gamepad device. + public static GamePadCapabilities GetCapabilities(int index) { - throw new NotImplementedException(); + if (index < 0) + throw new IndexOutOfRangeException(); + + return driver.GetCapabilities(index); } - #endregion + /// + /// Retrieves the GamePadState for the specified gamepad device. + /// + /// The zero-based index of a gamepad device. + /// A GamePadState structure describing the state of the gamepad device. + public static GamePadState GetState(int index) + { + return driver.GetState(index); + } } } diff --git a/Source/OpenTK/Input/GamePadAxis.cs b/Source/OpenTK/Input/GamePadAxis.cs index c1c96c53..dd6b3192 100644 --- a/Source/OpenTK/Input/GamePadAxis.cs +++ b/Source/OpenTK/Input/GamePadAxis.cs @@ -25,11 +25,11 @@ // THE SOFTWARE. using System; -namespace OpenTK +namespace OpenTK.Input { public enum GamePadAxis - { - /// The first axis of the gamepad. + { + /// The first axis of the gamepad. Axis0 = 0, /// The second axis of the gamepad. Axis1, @@ -49,8 +49,6 @@ namespace OpenTK Axis8, /// The tenth axis of the gamepad. Axis9, - /// The last axis of the gamepad. - LastAxis } } diff --git a/Source/OpenTK/Input/GamePadCapabilities.cs b/Source/OpenTK/Input/GamePadCapabilities.cs new file mode 100644 index 00000000..23c56cd3 --- /dev/null +++ b/Source/OpenTK/Input/GamePadCapabilities.cs @@ -0,0 +1,67 @@ +// #region License +// +// GamePadCapabilities.cs +// +// Author: +// Stefanos A. +// +// 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; } + } + } +} + diff --git a/Source/OpenTK/Input/GamePadDPad.cs b/Source/OpenTK/Input/GamePadDPad.cs new file mode 100644 index 00000000..72389b00 --- /dev/null +++ b/Source/OpenTK/Input/GamePadDPad.cs @@ -0,0 +1,90 @@ +// #region License +// +// GamePadDPad.cs +// +// Author: +// Stefanos A. +// +// 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; + } + } + } +} diff --git a/Source/OpenTK/Input/GamePadState.cs b/Source/OpenTK/Input/GamePadState.cs index 21e3aa91..d45787bb 100644 --- a/Source/OpenTK/Input/GamePadState.cs +++ b/Source/OpenTK/Input/GamePadState.cs @@ -34,6 +34,105 @@ namespace OpenTK.Input /// public struct GamePadState /*: IEquatable*/ { + 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 } } diff --git a/Source/OpenTK/OpenTK.csproj b/Source/OpenTK/OpenTK.csproj index 7bddfcb0..723b4412 100644 --- a/Source/OpenTK/OpenTK.csproj +++ b/Source/OpenTK/OpenTK.csproj @@ -738,7 +738,6 @@ - @@ -777,6 +776,10 @@ + + + +