reorg controller and input adapter classes

This commit is contained in:
adelikat 2020-07-03 11:54:10 -05:00
parent 6db1d7e61d
commit c64eff6baf
10 changed files with 152 additions and 159 deletions

View File

@ -1,55 +1,55 @@
using System.Collections.Generic;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
/// <summary>
/// Will hold buttons for 1 frame and then release them.
/// (Calling Click() from your button click is what you want to do)
/// TODO - should the duration be controllable?
/// </summary>
public class ClickyVirtualPadController : IController
{
private readonly HashSet<string> _pressed = new HashSet<string>();
public ControllerDefinition Definition { get; set; }
public bool IsPressed(string button) => _pressed.Contains(button);
public int AxisValue(string name) => 0;
/// <summary>
/// Call this once per frame to do the timekeeping for the hold and release
/// </summary>
public void FrameTick() => _pressed.Clear();
/// <summary>
/// Call this to hold the button down for one frame
/// </summary>
public void Click(string button) => _pressed.Add(button);
public void Toggle(string button)
{
if (IsPressed(button))
{
_pressed.Remove(button);
}
else
{
_pressed.Add(button);
}
}
public void SetBool(string button, bool value)
{
if (value)
{
_pressed.Remove(button);
}
else
{
_pressed.Add(button);
}
}
}
}
using System.Collections.Generic;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
/// <summary>
/// Will hold buttons for 1 frame and then release them.
/// (Calling Click() from your button click is what you want to do)
/// TODO - should the duration be controllable?
/// </summary>
public class ClickyVirtualPadController : IController
{
private readonly HashSet<string> _pressed = new HashSet<string>();
public ControllerDefinition Definition { get; set; }
public bool IsPressed(string button) => _pressed.Contains(button);
public int AxisValue(string name) => 0;
/// <summary>
/// Call this once per frame to do the timekeeping for the hold and release
/// </summary>
public void FrameTick() => _pressed.Clear();
/// <summary>
/// Call this to hold the button down for one frame
/// </summary>
public void Click(string button) => _pressed.Add(button);
public void Toggle(string button)
{
if (IsPressed(button))
{
_pressed.Remove(button);
}
else
{
_pressed.Add(button);
}
}
public void SetBool(string button, bool value)
{
if (value)
{
_pressed.Remove(button);
}
else
{
_pressed.Add(button);
}
}
}
}

View File

@ -1,49 +1,49 @@
using System.Collections.Generic;
using BizHawk.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
/// <summary>
/// A basic implementation of IController
/// </summary>
public class SimpleController : IController
{
public ControllerDefinition Definition { get; set; }
protected WorkingDictionary<string, bool> Buttons { get; private set; } = new WorkingDictionary<string, bool>();
protected WorkingDictionary<string, int> Axes { get; private set; } = new WorkingDictionary<string, int>();
public void Clear()
{
Buttons = new WorkingDictionary<string, bool>();
Axes = new WorkingDictionary<string, int>();
}
public bool this[string button]
{
get => Buttons[button];
set => Buttons[button] = value;
}
public virtual bool IsPressed(string button) => this[button];
public int AxisValue(string name) => Axes[name];
public IDictionary<string, bool> BoolButtons() => Buttons;
public void AcceptNewAxis(string axisId, int value)
{
Axes[axisId] = value;
}
public void AcceptNewAxes(IEnumerable<(string AxisID, int Value)> newValues)
{
foreach (var (axisID, value) in newValues)
{
Axes[axisID] = value;
}
}
}
}
using System.Collections.Generic;
using BizHawk.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
/// <summary>
/// A basic implementation of IController
/// </summary>
public class SimpleController : IController
{
public ControllerDefinition Definition { get; set; }
protected WorkingDictionary<string, bool> Buttons { get; private set; } = new WorkingDictionary<string, bool>();
protected WorkingDictionary<string, int> Axes { get; private set; } = new WorkingDictionary<string, int>();
public void Clear()
{
Buttons = new WorkingDictionary<string, bool>();
Axes = new WorkingDictionary<string, int>();
}
public bool this[string button]
{
get => Buttons[button];
set => Buttons[button] = value;
}
public virtual bool IsPressed(string button) => this[button];
public int AxisValue(string name) => Axes[name];
public IDictionary<string, bool> BoolButtons() => Buttons;
public void AcceptNewAxis(string axisId, int value)
{
Axes[axisId] = value;
}
public void AcceptNewAxes(IEnumerable<(string AxisID, int Value)> newValues)
{
foreach (var (axisID, value) in newValues)
{
Axes[axisID] = value;
}
}
}
}

View File

@ -2,7 +2,7 @@
namespace BizHawk.Client.Common
{
public class AndAdapter : IController
public class AndAdapter : IInputAdapter
{
public ControllerDefinition Definition => Source.Definition;
@ -20,11 +20,11 @@ namespace BizHawk.Client.Common
// this works in the code because SourceOr is the autofire controller
public int AxisValue(string name) => Source.AxisValue(name);
internal IController Source { get; set; }
public IController Source { get; set; }
internal IController SourceAnd { get; set; }
}
public class XorAdapter : IController
public class XorAdapter : IInputAdapter
{
public ControllerDefinition Definition => Source.Definition;
@ -42,11 +42,11 @@ namespace BizHawk.Client.Common
// this works in the code because SourceOr is the autofire controller
public int AxisValue(string name) => Source.AxisValue(name);
internal IController Source { get; set; }
public IController Source { get; set; }
internal IController SourceXor { get; set; }
}
public class ORAdapter : IController
public class ORAdapter : IInputAdapter
{
public ControllerDefinition Definition => Source.Definition;
@ -60,7 +60,7 @@ namespace BizHawk.Client.Common
// this works in the code because SourceOr is the autofire controller
public int AxisValue(string name) => Source.AxisValue(name);
internal IController Source { get; set; }
public IController Source { get; set; }
internal IController SourceOr { get; set; }
}
}

View File

@ -10,4 +10,44 @@ namespace BizHawk.Client.Common
{
IController Source { get; set; }
}
public static class InputAdapterExtensions
{
/// <summary>
/// Creates a new IController that is in a state of a bitwise And of the source and target controllers
/// </summary>
public static IController And(this IController source, IController target)
{
return new AndAdapter
{
Source = source,
SourceAnd = target
};
}
/// <summary>
/// Creates a new IController that is in a state of a bitwise Xor of the source and target controllers
/// </summary>
public static IController Xor(this IController source, IController target)
{
return new XorAdapter
{
Source = source,
SourceXor = target
};
}
/// <summary>
/// Creates a new IController that is in a state of a bitwise Or of the source and target controllers
/// </summary>
public static IController Or(this IController source, IController target)
{
return new ORAdapter
{
Source = source,
SourceOr = target
};
}
}
}

View File

@ -1,44 +0,0 @@
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common.InputAdapterExtensions
{
public static class InputAdapterExtensions
{
/// <summary>
/// Creates a new IController that is in a state of a bitwise And of the source and target controllers
/// </summary>
public static IController And(this IController source, IController target)
{
return new AndAdapter
{
Source = source,
SourceAnd = target
};
}
/// <summary>
/// Creates a new IController that is in a state of a bitwise Xor of the source and target controllers
/// </summary>
public static IController Xor(this IController source, IController target)
{
return new XorAdapter
{
Source = source,
SourceXor = target
};
}
/// <summary>
/// Creates a new IController that is in a state of a bitwise Or of the source and target controllers
/// </summary>
public static IController Or(this IController source, IController target)
{
return new ORAdapter
{
Source = source,
SourceOr = target
};
}
}
}

View File

@ -1,7 +1,5 @@
using System.Collections.Generic;
using BizHawk.Emulation.Common;
using BizHawk.Client.Common.InputAdapterExtensions;
namespace BizHawk.Client.Common
{

View File

@ -6,7 +6,7 @@ using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
public interface IStickyController : IController
public interface IStickyController : IInputAdapter
{
bool IsSticky(string button);
}

View File

@ -7,7 +7,7 @@ namespace BizHawk.Client.Common
/// Filters input for things called Up and Down while considering the client's AllowUD_LR option.
/// This is a bit gross but it is unclear how to do it more nicely
/// </summary>
public class UdlrControllerAdapter : IController
public class UdlrControllerAdapter : IInputAdapter
{
public IController Source { get; set; }

View File

@ -6,7 +6,6 @@ using System.Collections.Generic;
using BizHawk.Emulation.Common;
using BizHawk.Client.Common;
using BizHawk.Client.Common.InputAdapterExtensions;
namespace BizHawk.Client.EmuHawk
{