Opentk/Source/OpenTK/Input/GamePadDPad.cs

224 lines
8.2 KiB
C#
Raw Normal View History

// #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
2013-12-22 21:32:24 +00:00
using System;
namespace OpenTK.Input
{
2014-01-03 00:58:16 +00:00
/// <summary>
/// Describes the state of a <see cref="GamePad"/> directional pad.
/// </summary>
2013-12-22 21:01:04 +00:00
public struct GamePadDPad : IEquatable<GamePadDPad>
{
[Flags]
enum DPadButtons : byte
{
Up = Buttons.DPadUp,
Down = Buttons.DPadDown,
Left = Buttons.DPadLeft,
Right = Buttons.DPadRight
}
DPadButtons buttons;
2014-01-03 00:58:16 +00:00
#region Internal Members
2013-12-22 21:01:04 +00:00
internal GamePadDPad(Buttons state)
{
// DPad butons are stored in the lower 4bits
// of the Buttons enumeration.
buttons = (DPadButtons)((int)state & 0x0f);
}
2014-01-03 00:58:16 +00:00
#endregion
#region Public Members
/// <summary>
/// Gets the <see cref="ButtonState"/> for the up button.
/// </summary>
/// <value><c>ButtonState.Pressed</c> if the up button is pressed; otherwise, <c>ButtonState.Released</c>.</value>
public ButtonState Up
{
get { return IsUp ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets the <see cref="ButtonState"/> for the down button.
/// </summary>
/// <value><c>ButtonState.Pressed</c> if the down button is pressed; otherwise, <c>ButtonState.Released</c>.</value>
public ButtonState Down
{
get { return IsDown ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets the <see cref="ButtonState"/> for the left button.
/// </summary>
/// <value><c>ButtonState.Pressed</c> if the left button is pressed; otherwise, <c>ButtonState.Released</c>.</value>
public ButtonState Left
{
get { return IsLeft ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets the <see cref="ButtonState"/> for the right button.
/// </summary>
/// <value><c>ButtonState.Pressed</c> if the right button is pressed; otherwise, <c>ButtonState.Released</c>.</value>
public ButtonState Right
{
get { return IsRight ? ButtonState.Pressed : ButtonState.Released; }
}
/// <summary>
/// Gets a value indicating whether the up button is pressed.
/// </summary>
/// <value><c>true</c> if the up button is pressed; otherwise, <c>false</c>.</value>
public bool IsUp
{
get { return (buttons & DPadButtons.Up) != 0; }
internal set { SetButton(DPadButtons.Up, value); }
}
2014-01-03 00:58:16 +00:00
/// <summary>
/// Gets a value indicating whether the down button is pressed.
/// </summary>
/// <value><c>true</c> if the down button is pressed; otherwise, <c>false</c>.</value>
public bool IsDown
{
get { return (buttons & DPadButtons.Down) != 0; }
internal set { SetButton(DPadButtons.Down, value); }
}
2014-01-03 00:58:16 +00:00
/// <summary>
/// Gets a value indicating whether the left button is pressed.
/// </summary>
/// <value><c>true</c> if the left button is pressed; otherwise, <c>false</c>.</value>
public bool IsLeft
{
get { return (buttons & DPadButtons.Left) != 0; }
internal set { SetButton(DPadButtons.Left, value); }
}
2014-01-03 00:58:16 +00:00
/// <summary>
/// Gets a value indicating whether the right button is pressed.
/// </summary>
/// <value><c>true</c> if the right button is pressed; otherwise, <c>false</c>.</value>
public bool IsRight
{
get { return (buttons & DPadButtons.Right) != 0; }
internal set { SetButton(DPadButtons.Right, value); }
}
2014-01-03 00:58:16 +00:00
/// <param name="left">A <see cref="GamePadDPad"/> instance to test for equality.</param>
/// <param name="right">A <see cref="GamePadDPad"/> instance to test for equality.</param>
2013-12-22 21:01:04 +00:00
public static bool operator ==(GamePadDPad left, GamePadDPad right)
{
return left.Equals(right);
}
2014-01-03 00:58:16 +00:00
/// <param name="left">A <see cref="GamePadDPad"/> instance to test for inequality.</param>
/// <param name="right">A <see cref="GamePadDPad"/> instance to test for inequality.</param>
2013-12-22 21:01:04 +00:00
public static bool operator !=(GamePadDPad left, GamePadDPad right)
{
return !left.Equals(right);
}
2014-01-03 00:58:16 +00:00
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="OpenTK.Input.GamePadDPad"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="OpenTK.Input.GamePadDPad"/>.</returns>
2013-12-22 21:01:04 +00:00
public override string ToString()
{
return String.Format(
2013-12-22 21:07:30 +00:00
"{{{0}{1}{2}{3}}}",
IsUp ? "U" : String.Empty,
IsLeft ? "L" : String.Empty,
IsDown ? "D" : String.Empty,
IsRight ? "R" : String.Empty);
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.GamePadDPad"/> 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()
{
return buttons.GetHashCode();
}
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.GamePadDPad"/>.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with the current <see cref="OpenTK.Input.GamePadDPad"/>.</param>
/// <returns><c>true</c> if the specified <see cref="System.Object"/> is equal to the current
/// <see cref="OpenTK.Input.GamePadDPad"/>; otherwise, <c>false</c>.</returns>
2013-12-22 21:01:04 +00:00
public override bool Equals(object obj)
{
return
obj is GamePadDPad &&
Equals((GamePadDPad)obj);
}
#endregion
#region Private Members
void SetButton(DPadButtons button, bool value)
{
if (value)
{
buttons |= button;
}
else
{
buttons &= ~button;
}
}
2013-12-22 21:01:04 +00:00
#endregion
#region IEquatable<GamePadDPad> Members
2014-01-03 00:58:16 +00:00
/// <summary>
/// Determines whether the specified <see cref="OpenTK.Input.GamePadDPad"/> is equal to the current <see cref="OpenTK.Input.GamePadDPad"/>.
/// </summary>
/// <param name="other">The <see cref="OpenTK.Input.GamePadDPad"/> to compare with the current <see cref="OpenTK.Input.GamePadDPad"/>.</param>
/// <returns><c>true</c> if the specified <see cref="OpenTK.Input.GamePadDPad"/> is equal to the current
/// <see cref="OpenTK.Input.GamePadDPad"/>; otherwise, <c>false</c>.</returns>
2013-12-22 21:01:04 +00:00
public bool Equals(GamePadDPad other)
{
return buttons == other.buttons;
}
#endregion
}
}