Opentk/Source/OpenTK/Input/GamePadState.cs

246 lines
8.1 KiB
C#
Raw Normal View History

#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
{
/// <summary>
2014-01-03 00:58:16 +00:00
/// Describes the current state of a <see cref="GamePad"/> device.
/// </summary>
2013-12-22 21:01:04 +00:00
public struct GamePadState : IEquatable<GamePadState>
{
const float RangeMultiplier = 1.0f / (short.MaxValue + 1);
2013-01-17 01:59:52 +00:00
Buttons buttons;
2014-01-02 18:24:15 +00:00
int packet_number;
2013-12-22 21:32:18 +00:00
short left_stick_x;
short left_stick_y;
short right_stick_x;
short right_stick_y;
2013-12-24 00:36:44 +00:00
byte left_trigger;
byte right_trigger;
bool is_connected;
#region Public Members
2014-01-03 00:58:16 +00:00
/// <summary>
/// Gets a <see cref="GamePadThumbSticks"/> structure describing the
/// state of the <c>GamePad</c> thumb sticks.
/// </summary>
2013-12-22 21:32:18 +00:00
public GamePadThumbSticks ThumbSticks
{
2013-12-22 21:32:18 +00:00
get { return new GamePadThumbSticks(left_stick_x, left_stick_y, right_stick_x, right_stick_y); }
}
2014-01-03 00:58:16 +00:00
/// <summary>
/// Gets a <see cref="GamePadButtons"/> structure describing the
/// state of the <c>GamePad</c> buttons.
/// </summary>
public GamePadButtons Buttons
{
get { return new GamePadButtons(buttons); }
}
2014-01-03 00:58:16 +00:00
/// <summary>
/// Gets a <see cref="GamePadDPad"/> structure describing the
/// state of the <c>GamePad</c> directional pad.
/// </summary>
public GamePadDPad DPad
{
get { return new GamePadDPad(buttons); }
}
2014-01-03 00:58:16 +00:00
/// <summary>
/// Gets a <see cref="GamePadTriggers"/> structure describing the
/// state of the <c>GamePad</c> triggers.
/// </summary>
public GamePadTriggers Triggers
{
get { return new GamePadTriggers(left_trigger, right_trigger); }
}
2014-01-03 00:58:16 +00:00
/// <summary>
/// Gets a value indicating whether this <c>GamePad</c> instance is connected.
/// </summary>
/// <value><c>true</c> if this instance is connected; otherwise, <c>false</c>.</value>
public bool IsConnected
{
get { return is_connected; }
}
2014-01-03 00:58:16 +00:00
/// <summary>
/// Gets the packet number for this <c>GamePadState</c> instance.
/// Use the packet number to determine whether the state of a
/// <c>GamePad</c> device has changed.
/// </summary>
2014-01-02 18:24:15 +00:00
public int PacketNumber
{
get { return packet_number; }
}
2014-01-03 00:58:16 +00:00
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="OpenTK.Input.GamePadState"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="OpenTK.Input.GamePadState"/>.</returns>
2013-12-22 21:01:04 +00:00
public override string ToString()
{
return String.Format(
2014-09-02 22:10:21 +00:00
"{{Sticks: {0}; Triggers: {1}; Buttons: {2}; DPad: {3}; IsConnected: {4}}}",
ThumbSticks, Triggers, Buttons, DPad, IsConnected);
2013-12-22 21:01:04 +00:00
}
2014-01-03 00:58:16 +00:00
/// <summary>
/// Serves as a hash function for a <see cref="OpenTK.Input.GamePadState"/> object.
/// </summary>
/// <returns>A hash code for this instance that is suitable for use in hashing algorithms and data structures such as a
/// hash table.</returns>
2013-12-22 21:01:04 +00:00
public override int GetHashCode()
{
2013-12-22 21:32:18 +00:00
return
ThumbSticks.GetHashCode() ^ Buttons.GetHashCode() ^
DPad.GetHashCode() ^ IsConnected.GetHashCode();
2013-12-22 21:01:04 +00:00
}
2014-01-03 00:58:16 +00:00
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to the current <see cref="OpenTK.Input.GamePadState"/>.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with the current <see cref="OpenTK.Input.GamePadState"/>.</param>
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to the current
/// <see cref="OpenTK.Input.GamePadState"/>; otherwise, <c>false</c>.</returns>
2013-12-22 21:01:04 +00:00
public override bool Equals(object obj)
{
return
obj is GamePadState &&
Equals((GamePadState)obj);
}
#endregion
#region IEquatable<GamePadState> Members
2014-01-03 00:58:16 +00:00
/// <summary>
/// Determines whether the specified <see cref="OpenTK.Input.GamePadState"/> is equal to the current <see cref="OpenTK.Input.GamePadState"/>.
/// </summary>
/// <param name="other">The <see cref="OpenTK.Input.GamePadState"/> to compare with the current <see cref="OpenTK.Input.GamePadState"/>.</param>
/// <returns><c>true</c> if the specified <see cref="OpenTK.Input.GamePadState"/> is equal to the current
/// <see cref="OpenTK.Input.GamePadState"/>; otherwise, <c>false</c>.</returns>
2013-12-22 21:01:04 +00:00
public bool Equals(GamePadState other)
{
return
2013-12-22 21:32:18 +00:00
ThumbSticks == other.ThumbSticks &&
2013-12-22 21:01:04 +00:00
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;
}
2013-12-22 21:32:18 +00:00
if ((axis & GamePadAxes.LeftY) != 0)
{
left_stick_y = value;
}
2013-12-22 21:32:18 +00:00
if ((axis & GamePadAxes.RightX) != 0)
{
right_stick_x = value;
}
2013-12-22 21:32:18 +00:00
if ((axis & GamePadAxes.RightY) != 0)
{
right_stick_y = value;
}
2013-12-22 21:32:18 +00:00
if ((axis & GamePadAxes.LeftTrigger) != 0)
{
// Adjust from [-32768, 32767] to [0, 255]
2013-12-30 12:49:26 +00:00
left_trigger = (byte)((value - short.MinValue) >> 8);
}
if ((axis & GamePadAxes.RightTrigger) != 0)
{
// Adjust from [-32768, 32767] to [0, 255]
2013-12-30 12:49:26 +00:00
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;
}
2014-01-02 18:24:15 +00:00
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
}
}