#region License // // The Open Toolkit Library License // // Copyright (c) 2006 - 2009 the Open Toolkit library. // // 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 { /// /// Describes the current state of a device. /// public struct GamePadState : IEquatable { const float RangeMultiplier = 1.0f / (short.MaxValue + 1); Buttons buttons; int packet_number; short left_stick_x; short left_stick_y; short right_stick_x; short right_stick_y; byte left_trigger; byte right_trigger; bool is_connected; #region Public Members /// /// Gets a structure describing the /// state of the GamePad thumb sticks. /// public GamePadThumbSticks ThumbSticks { get { return new GamePadThumbSticks(left_stick_x, left_stick_y, right_stick_x, right_stick_y); } } /// /// Gets a structure describing the /// state of the GamePad buttons. /// public GamePadButtons Buttons { get { return new GamePadButtons(buttons); } } /// /// Gets a structure describing the /// state of the GamePad directional pad. /// public GamePadDPad DPad { get { return new GamePadDPad(buttons); } } /// /// Gets a structure describing the /// state of the GamePad triggers. /// public GamePadTriggers Triggers { get { return new GamePadTriggers(left_trigger, right_trigger); } } /// /// Gets a value indicating whether this GamePad instance is connected. /// /// true if this instance is connected; otherwise, false. public bool IsConnected { get { return is_connected; } } /// /// Gets the packet number for this GamePadState instance. /// Use the packet number to determine whether the state of a /// GamePad device has changed. /// public int PacketNumber { get { return packet_number; } } /// /// Returns a that represents the current . /// /// A that represents the current . public override string ToString() { return String.Format( "{{Sticks: {0}; Buttons: {1}; DPad: {2}; IsConnected: {3}}}", ThumbSticks, Buttons, DPad, IsConnected); } /// /// Serves as a hash function for a object. /// /// A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a /// hash table. public override int GetHashCode() { return ThumbSticks.GetHashCode() ^ Buttons.GetHashCode() ^ DPad.GetHashCode() ^ IsConnected.GetHashCode(); } /// /// Determines whether the specified is equal to the current . /// /// The to compare with the current . /// true if the specified is equal to the current /// ; otherwise, false. public override bool Equals(object obj) { return obj is GamePadState && Equals((GamePadState)obj); } #endregion #region IEquatable Members /// /// Determines whether the specified is equal to the current . /// /// The to compare with the current . /// true if the specified is equal to the current /// ; otherwise, false. public bool Equals(GamePadState other) { return ThumbSticks == other.ThumbSticks && Buttons == other.Buttons && DPad == other.DPad && IsConnected == other.IsConnected; } #endregion #region Internal Members internal void SetAxis(GamePadAxes axis, short value) { if ((axis & GamePadAxes.LeftX) != 0) { left_stick_x = value; } if ((axis & GamePadAxes.LeftY) != 0) { left_stick_y = value; } if ((axis & GamePadAxes.RightX) != 0) { right_stick_x = value; } if ((axis & GamePadAxes.RightY) != 0) { right_stick_y = value; } if ((axis & GamePadAxes.LeftTrigger) != 0) { // Adjust from [-32768, 32767] to [0, 255] left_trigger = (byte)((value - short.MinValue) >> 8); } if ((axis & GamePadAxes.RightTrigger) != 0) { // Adjust from [-32768, 32767] to [0, 255] right_trigger = (byte)((value - short.MinValue) >> 8); } } internal void SetButton(Buttons button, bool pressed) { if (pressed) { buttons |= button; } else { buttons &= ~button; } } internal void SetConnected(bool connected) { is_connected = connected; } internal void SetTriggers(byte left, byte right) { left_trigger = left; right_trigger = right; } internal void SetPacketNumber(int number) { packet_number = number; } #endregion #region Private Members bool IsAxisValid(GamePadAxes axis) { int index = (int)axis; return index >= 0 && index < GamePad.MaxAxisCount; } bool IsDPadValid(int index) { return index >= 0 && index < GamePad.MaxDPadCount; } #endregion } }