2016-03-26 22:07:44 +00:00
using System ;
using BizHawk.Client.Common ;
2020-01-07 15:25:57 +00:00
using BizHawk.Common ;
2016-03-26 22:07:44 +00:00
namespace BizHawk.Client.ApiHawk
{
/// <summary>
/// This class holds a joypad for any type of console
/// </summary>
public sealed class Joypad
{
#region Fields
2020-01-03 22:37:33 +00:00
private JoypadButton _pressedButtons ;
private float _analogX ;
private float _analogY ;
private int _player ;
2016-03-26 22:07:44 +00:00
#endregion
#region cTor ( s )
/// <summary>
/// Initialize a new instance of <see cref="Joypad"/>
/// </summary>
/// <param name="system">What <see cref="SystemInfo"/> this <see cref="Joypad"/> is used for</param>
/// <param name="player">Which player this controller is assigned to</param>
2019-12-31 22:56:15 +00:00
/// <exception cref="IndexOutOfRangeException"><paramref name="player"/> not in range <c>1..max</c> where <c>max</c> is <paramref name="system"/>.<see cref="SystemInfo.MaxControllers"/></exception>
2016-03-26 22:07:44 +00:00
internal Joypad ( SystemInfo system , int player )
{
2020-01-07 15:25:57 +00:00
if ( ! 1. RangeTo ( system . MaxControllers ) . Contains ( player ) )
2016-03-26 22:07:44 +00:00
{
2019-03-20 05:52:54 +00:00
throw new InvalidOperationException ( $"{player} is invalid for {system.DisplayName}" ) ;
2016-03-26 22:07:44 +00:00
}
2020-01-03 22:37:33 +00:00
System = system ;
_player = player ;
2016-03-26 22:07:44 +00:00
}
#endregion
#region Methods
/// <summary>
/// Add specified input to current ones
/// </summary>
/// <param name="input">Input to add</param>
public void AddInput ( JoypadButton input )
{
2020-01-03 22:37:33 +00:00
input & = System . AvailableButtons ;
_pressedButtons | = input ;
2016-03-26 22:07:44 +00:00
}
/// <summary>
/// Clear inputs
/// </summary>
public void ClearInputs ( )
{
2020-01-03 22:37:33 +00:00
_pressedButtons = 0 ;
2016-03-26 22:07:44 +00:00
}
/// <summary>
/// Remove specified input to current ones
/// </summary>
/// <param name="input">Input to remove</param>
public void RemoveInput ( JoypadButton input )
{
2020-01-03 22:37:33 +00:00
_pressedButtons ^ = input ;
2016-03-26 22:07:44 +00:00
}
#endregion
#region Properties
2016-04-06 20:10:36 +00:00
/// <summary>
/// Gets or sets X value for Analog stick
/// </summary>
2020-01-03 22:37:33 +00:00
/// <remarks>The value you get will always be rounded to 0 decimal</remarks>
2016-04-06 20:10:36 +00:00
public float AnalogX
{
2020-01-03 22:37:33 +00:00
get = > ( float ) Math . Round ( _analogX , 0 ) ;
set = > _analogX = value ;
2016-04-06 20:10:36 +00:00
}
/// <summary>
/// Gets or sets Y value for Analog stick
/// </summary>
2020-01-03 22:37:33 +00:00
/// <remarks>The value you get will always be rounded to 0 decimal</remarks>
2016-04-06 20:10:36 +00:00
public float AnalogY
{
2020-01-03 22:37:33 +00:00
get = > ( float ) Math . Round ( _analogY , 0 ) ;
set = > _analogY = value ;
2016-04-06 20:10:36 +00:00
}
2016-03-26 22:07:44 +00:00
/// <summary>
/// Gets or sets inputs
/// If you pass inputs unavailable for current system, they'll be removed
/// </summary>
/// <remarks>It overrides all existing inputs</remarks>
public JoypadButton Inputs
{
2020-01-03 22:37:33 +00:00
get = > _pressedButtons ;
2016-03-26 22:07:44 +00:00
set
{
2020-01-03 22:37:33 +00:00
value & = System . AvailableButtons ;
_pressedButtons = value ;
2016-03-26 22:07:44 +00:00
}
}
/// <summary>
/// Gets <see cref="SystemInfo"/> for current <see cref="Joypad"/>
/// </summary>
2020-01-03 22:37:33 +00:00
public SystemInfo System { get ; }
2016-03-26 22:07:44 +00:00
#endregion
}
}