using System;
using BizHawk.Client.Common;
using BizHawk.Common;
namespace BizHawk.Client.ApiHawk
{
///
/// This class holds a joypad for any type of console
///
public sealed class Joypad
{
#region Fields
private JoypadButton _pressedButtons;
private float _analogX;
private float _analogY;
private int _player;
#endregion
#region cTor(s)
///
/// Initialize a new instance of
///
/// What this is used for
/// Which player this controller is assigned to
/// not in range 1..max where max is .
internal Joypad(SystemInfo system, int player)
{
if (!1.RangeTo(system.MaxControllers).Contains(player))
{
throw new InvalidOperationException($"{player} is invalid for {system.DisplayName}");
}
System = system;
_player = player;
}
#endregion
#region Methods
///
/// Add specified input to current ones
///
/// Input to add
public void AddInput(JoypadButton input)
{
input &= System.AvailableButtons;
_pressedButtons |= input;
}
///
/// Clear inputs
///
public void ClearInputs()
{
_pressedButtons = 0;
}
///
/// Remove specified input to current ones
///
/// Input to remove
public void RemoveInput(JoypadButton input)
{
_pressedButtons ^= input;
}
#endregion
#region Properties
///
/// Gets or sets X value for Analog stick
///
/// The value you get will always be rounded to 0 decimal
public float AnalogX
{
get => (float)Math.Round(_analogX, 0);
set => _analogX = value;
}
///
/// Gets or sets Y value for Analog stick
///
/// The value you get will always be rounded to 0 decimal
public float AnalogY
{
get => (float)Math.Round(_analogY, 0);
set => _analogY = value;
}
///
/// Gets or sets inputs
/// If you pass inputs unavailable for current system, they'll be removed
///
/// It overrides all existing inputs
public JoypadButton Inputs
{
get => _pressedButtons;
set
{
value &= System.AvailableButtons;
_pressedButtons = value;
}
}
///
/// Gets for current
///
public SystemInfo System { get; }
#endregion
}
}