From c64eff6baf13e1e20e5bba94eeacebe8c2231849 Mon Sep 17 00:00:00 2001 From: adelikat Date: Fri, 3 Jul 2020 11:54:10 -0500 Subject: [PATCH] reorg controller and input adapter classes --- .../{ => controllers}/AutofireController.cs | 0 .../ClickyVirtualPadController.cs | 110 +++++++++--------- .../SimpleController.cs | 98 ++++++++-------- .../inputAdapters/BitwiseAdapters.cs | 12 +- .../inputAdapters/IInputAdapter.cs | 40 +++++++ .../inputAdapters/InputAdapterExtensions.cs | 44 ------- .../inputAdapters/InputManager.cs | 2 - .../inputAdapters/StickyAdapters.cs | 2 +- .../inputAdapters/UDLRController.cs | 2 +- .../DisplayManager/OSDManager.cs | 1 - 10 files changed, 152 insertions(+), 159 deletions(-) rename src/BizHawk.Client.Common/{ => controllers}/AutofireController.cs (100%) rename src/BizHawk.Client.Common/{inputAdapters => controllers}/ClickyVirtualPadController.cs (95%) rename src/BizHawk.Client.Common/{inputAdapters => controllers}/SimpleController.cs (96%) delete mode 100644 src/BizHawk.Client.Common/inputAdapters/InputAdapterExtensions.cs diff --git a/src/BizHawk.Client.Common/AutofireController.cs b/src/BizHawk.Client.Common/controllers/AutofireController.cs similarity index 100% rename from src/BizHawk.Client.Common/AutofireController.cs rename to src/BizHawk.Client.Common/controllers/AutofireController.cs diff --git a/src/BizHawk.Client.Common/inputAdapters/ClickyVirtualPadController.cs b/src/BizHawk.Client.Common/controllers/ClickyVirtualPadController.cs similarity index 95% rename from src/BizHawk.Client.Common/inputAdapters/ClickyVirtualPadController.cs rename to src/BizHawk.Client.Common/controllers/ClickyVirtualPadController.cs index 7eb943a5d6..0a34b38747 100644 --- a/src/BizHawk.Client.Common/inputAdapters/ClickyVirtualPadController.cs +++ b/src/BizHawk.Client.Common/controllers/ClickyVirtualPadController.cs @@ -1,55 +1,55 @@ -using System.Collections.Generic; -using BizHawk.Emulation.Common; - -namespace BizHawk.Client.Common -{ - /// - /// 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? - /// - public class ClickyVirtualPadController : IController - { - private readonly HashSet _pressed = new HashSet(); - - public ControllerDefinition Definition { get; set; } - - public bool IsPressed(string button) => _pressed.Contains(button); - - public int AxisValue(string name) => 0; - - /// - /// Call this once per frame to do the timekeeping for the hold and release - /// - public void FrameTick() => _pressed.Clear(); - - /// - /// Call this to hold the button down for one frame - /// - 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 +{ + /// + /// 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? + /// + public class ClickyVirtualPadController : IController + { + private readonly HashSet _pressed = new HashSet(); + + public ControllerDefinition Definition { get; set; } + + public bool IsPressed(string button) => _pressed.Contains(button); + + public int AxisValue(string name) => 0; + + /// + /// Call this once per frame to do the timekeeping for the hold and release + /// + public void FrameTick() => _pressed.Clear(); + + /// + /// Call this to hold the button down for one frame + /// + 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); + } + } + } +} diff --git a/src/BizHawk.Client.Common/inputAdapters/SimpleController.cs b/src/BizHawk.Client.Common/controllers/SimpleController.cs similarity index 96% rename from src/BizHawk.Client.Common/inputAdapters/SimpleController.cs rename to src/BizHawk.Client.Common/controllers/SimpleController.cs index e95fa5952d..868dcb92f9 100644 --- a/src/BizHawk.Client.Common/inputAdapters/SimpleController.cs +++ b/src/BizHawk.Client.Common/controllers/SimpleController.cs @@ -1,49 +1,49 @@ -using System.Collections.Generic; - -using BizHawk.Common; -using BizHawk.Emulation.Common; - -namespace BizHawk.Client.Common -{ - /// - /// A basic implementation of IController - /// - public class SimpleController : IController - { - public ControllerDefinition Definition { get; set; } - - protected WorkingDictionary Buttons { get; private set; } = new WorkingDictionary(); - protected WorkingDictionary Axes { get; private set; } = new WorkingDictionary(); - - public void Clear() - { - Buttons = new WorkingDictionary(); - Axes = new WorkingDictionary(); - } - - 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 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 +{ + /// + /// A basic implementation of IController + /// + public class SimpleController : IController + { + public ControllerDefinition Definition { get; set; } + + protected WorkingDictionary Buttons { get; private set; } = new WorkingDictionary(); + protected WorkingDictionary Axes { get; private set; } = new WorkingDictionary(); + + public void Clear() + { + Buttons = new WorkingDictionary(); + Axes = new WorkingDictionary(); + } + + 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 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; + } + } + } +} diff --git a/src/BizHawk.Client.Common/inputAdapters/BitwiseAdapters.cs b/src/BizHawk.Client.Common/inputAdapters/BitwiseAdapters.cs index 9ab08e11ea..114f9ede6b 100644 --- a/src/BizHawk.Client.Common/inputAdapters/BitwiseAdapters.cs +++ b/src/BizHawk.Client.Common/inputAdapters/BitwiseAdapters.cs @@ -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; } } } diff --git a/src/BizHawk.Client.Common/inputAdapters/IInputAdapter.cs b/src/BizHawk.Client.Common/inputAdapters/IInputAdapter.cs index 45e977bd77..9d7aff76e4 100644 --- a/src/BizHawk.Client.Common/inputAdapters/IInputAdapter.cs +++ b/src/BizHawk.Client.Common/inputAdapters/IInputAdapter.cs @@ -10,4 +10,44 @@ namespace BizHawk.Client.Common { IController Source { get; set; } } + + public static class InputAdapterExtensions + { + /// + /// Creates a new IController that is in a state of a bitwise And of the source and target controllers + /// + public static IController And(this IController source, IController target) + { + return new AndAdapter + { + Source = source, + SourceAnd = target + }; + } + + /// + /// Creates a new IController that is in a state of a bitwise Xor of the source and target controllers + /// + public static IController Xor(this IController source, IController target) + { + return new XorAdapter + { + Source = source, + SourceXor = target + }; + } + + + /// + /// Creates a new IController that is in a state of a bitwise Or of the source and target controllers + /// + public static IController Or(this IController source, IController target) + { + return new ORAdapter + { + Source = source, + SourceOr = target + }; + } + } } diff --git a/src/BizHawk.Client.Common/inputAdapters/InputAdapterExtensions.cs b/src/BizHawk.Client.Common/inputAdapters/InputAdapterExtensions.cs deleted file mode 100644 index 8fb59c3509..0000000000 --- a/src/BizHawk.Client.Common/inputAdapters/InputAdapterExtensions.cs +++ /dev/null @@ -1,44 +0,0 @@ -using BizHawk.Emulation.Common; - -namespace BizHawk.Client.Common.InputAdapterExtensions -{ - public static class InputAdapterExtensions - { - /// - /// Creates a new IController that is in a state of a bitwise And of the source and target controllers - /// - public static IController And(this IController source, IController target) - { - return new AndAdapter - { - Source = source, - SourceAnd = target - }; - } - - /// - /// Creates a new IController that is in a state of a bitwise Xor of the source and target controllers - /// - public static IController Xor(this IController source, IController target) - { - return new XorAdapter - { - Source = source, - SourceXor = target - }; - } - - - /// - /// Creates a new IController that is in a state of a bitwise Or of the source and target controllers - /// - public static IController Or(this IController source, IController target) - { - return new ORAdapter - { - Source = source, - SourceOr = target - }; - } - } -} diff --git a/src/BizHawk.Client.Common/inputAdapters/InputManager.cs b/src/BizHawk.Client.Common/inputAdapters/InputManager.cs index 860ddddbb3..7c015c265b 100644 --- a/src/BizHawk.Client.Common/inputAdapters/InputManager.cs +++ b/src/BizHawk.Client.Common/inputAdapters/InputManager.cs @@ -1,7 +1,5 @@ using System.Collections.Generic; - using BizHawk.Emulation.Common; -using BizHawk.Client.Common.InputAdapterExtensions; namespace BizHawk.Client.Common { diff --git a/src/BizHawk.Client.Common/inputAdapters/StickyAdapters.cs b/src/BizHawk.Client.Common/inputAdapters/StickyAdapters.cs index 0a970ef7ce..0d69387514 100644 --- a/src/BizHawk.Client.Common/inputAdapters/StickyAdapters.cs +++ b/src/BizHawk.Client.Common/inputAdapters/StickyAdapters.cs @@ -6,7 +6,7 @@ using BizHawk.Emulation.Common; namespace BizHawk.Client.Common { - public interface IStickyController : IController + public interface IStickyController : IInputAdapter { bool IsSticky(string button); } diff --git a/src/BizHawk.Client.Common/inputAdapters/UDLRController.cs b/src/BizHawk.Client.Common/inputAdapters/UDLRController.cs index b1e0317b09..305d171696 100644 --- a/src/BizHawk.Client.Common/inputAdapters/UDLRController.cs +++ b/src/BizHawk.Client.Common/inputAdapters/UDLRController.cs @@ -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 /// - public class UdlrControllerAdapter : IController + public class UdlrControllerAdapter : IInputAdapter { public IController Source { get; set; } diff --git a/src/BizHawk.Client.EmuHawk/DisplayManager/OSDManager.cs b/src/BizHawk.Client.EmuHawk/DisplayManager/OSDManager.cs index 14a934e361..7da0530076 100644 --- a/src/BizHawk.Client.EmuHawk/DisplayManager/OSDManager.cs +++ b/src/BizHawk.Client.EmuHawk/DisplayManager/OSDManager.cs @@ -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 {