From dbeb279bf02c02aa05764931ff9f080e8410b24b Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Mon, 15 Jun 2020 01:32:40 +1000 Subject: [PATCH] Add an extension method to help w/ ControllerDefinition init of axes --- src/BizHawk.Emulation.Common/Extensions.cs | 61 +++++++++++++++ .../Atari/A7800Hawk/A7800HawkControllers.cs | 10 +-- .../Consoles/Belogic/Uzem.cs | 14 +--- .../Consoles/Coleco/ColecoControllers.cs | 22 ++---- .../GCE/Vectrex/VectrexHawkControllers.cs | 10 +-- .../Controllers/IntellivisionControllers.cs | 12 +-- .../Consoles/Nintendo/GBA/MGBAHawk.cs | 20 +---- .../Nintendo/GBHawk/GBHawkControllers.cs | 10 +-- .../Consoles/Nintendo/NDS/MelonDS.cs | 7 +- .../Consoles/Nintendo/NES/NESControllers.cs | 37 ++++----- .../Nintendo/SNES/LibsnesControllerDeck.cs | 78 ++++++------------- .../Consoles/Nintendo/SNES9X/Snes9x.cs | 57 ++++---------- .../Consoles/Sega/SMS/SMS.Input.cs | 33 +++----- .../Sega/gpgx64/GPGXControlConverter.cs | 41 ++++------ .../Consoles/Sony/PSX/Octoshock.cs | 20 ++--- .../Libretro/LibretroCore.cs | 6 +- .../Display/InputDisplayTests.cs | 12 +-- .../Client.Common/Movie/LogGeneratorTests.cs | 12 +-- 18 files changed, 182 insertions(+), 280 deletions(-) diff --git a/src/BizHawk.Emulation.Common/Extensions.cs b/src/BizHawk.Emulation.Common/Extensions.cs index df4e64eabc..6d521efa01 100644 --- a/src/BizHawk.Emulation.Common/Extensions.cs +++ b/src/BizHawk.Emulation.Common/Extensions.cs @@ -8,6 +8,8 @@ using System.Runtime.CompilerServices; using BizHawk.Common.PathExtensions; using BizHawk.Common.StringExtensions; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Common { public static class EmulatorExtensions @@ -386,5 +388,64 @@ namespace BizHawk.Emulation.Common var pass2 = Path.GetFileName(pass1).RemoveInvalidFileSystemChars(); return Path.Combine(filesystemDir, pass2.RemoveSuffix('.')); // trailing '.' would be duplicated when file extension is added } + + /// + /// Adds an axis to the receiver , and returns it. + /// The new axis will appear after any that were previously defined. + /// + /// identical reference to ; the object is mutated + public static ControllerDefinition AddAxis(this ControllerDefinition def, string name, int min, int mid, int max, bool isReversed = false) + { + def.AxisControls.Add(name); + def.AxisRanges.Add(new AxisRange(min, mid, max, isReversed)); + return def; + } + + /// + /// Adds an X/Y pair of axes to the receiver , and returns it. + /// The new axes will appear after any that were previously defined. + /// + /// format string e.g. "P1 Left {0}" (will be used to interpolate "X" and "Y") + /// identical reference to ; the object is mutated + public static ControllerDefinition AddXYPair(this ControllerDefinition def, string nameFormat, AxisPairOrientation pDir, int minBoth, int midBoth, int maxBoth) + { + def.AxisControls.Add(string.Format(nameFormat, "X")); + def.AxisControls.Add(string.Format(nameFormat, "Y")); + def.AxisRanges.AddRange(CreateAxisRangePair(minBoth, midBoth, maxBoth, pDir)); + return def; + } + + /// + /// Adds an X/Y pair of axes to the receiver , and returns it. + /// The new axes will appear after any that were previously defined. + /// + /// format string e.g. "P1 Left {0}" (will be used to interpolate "X" and "Y") + /// identical reference to ; the object is mutated + public static ControllerDefinition AddXYPair(this ControllerDefinition def, string nameFormat, AxisPairOrientation pDir, (int Min, int Mid, int Max) rangeX, (int Min, int Mid, int Max) rangeY) + { + def.AxisControls.Add(string.Format(nameFormat, "X")); + def.AxisControls.Add(string.Format(nameFormat, "Y")); + def.AxisRanges.Add(new AxisRange(rangeX.Min, rangeX.Mid, rangeX.Max, ((byte) pDir & 2) != 0)); + def.AxisRanges.Add(new AxisRange(rangeY.Min, rangeY.Mid, rangeY.Max, ((byte) pDir & 1) != 0)); + return def; + } + + /// + /// Adds an X/Y/Z triple of axes to the receiver , and returns it. + /// The new axes will appear after any that were previously defined. + /// + /// format string e.g. "P1 Tilt {0}" (will be used to interpolate "X", "Y", and "Z") + /// identical reference to ; the object is mutated + public static ControllerDefinition AddXYZTriple(this ControllerDefinition def, string nameFormat, int minAll, int midAll, int maxAll) + { + def.AxisControls.Add(string.Format(nameFormat, "X")); + def.AxisControls.Add(string.Format(nameFormat, "Y")); + def.AxisControls.Add(string.Format(nameFormat, "Z")); + var range = new AxisRange(minAll, midAll, maxAll); + def.AxisRanges.Add(range); + def.AxisRanges.Add(range); + def.AxisRanges.Add(range); + return def; + } } } \ No newline at end of file diff --git a/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800HawkControllers.cs b/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800HawkControllers.cs index 18d479d6e9..60d1cc701d 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800HawkControllers.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800HawkControllers.cs @@ -4,6 +4,8 @@ using System.Linq; using BizHawk.Common; using BizHawk.Emulation.Common; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Cores.Atari.A7800Hawk { /// @@ -272,12 +274,8 @@ namespace BizHawk.Emulation.Cores.Atari.A7800Hawk Definition = new ControllerDefinition { Name = "Light Gun Controller", - BoolButtons = BaseDefinition - .Select(b => "P" + PortNum + " " + b) - .ToList(), - AxisControls = { "P" + PortNum + " X", "P" + PortNum + " Y" }, - AxisRanges = { new ControllerDefinition.AxisRange(1, 160, 320), new ControllerDefinition.AxisRange(1, 121, 242) } - }; + BoolButtons = BaseDefinition.Select(b => $"P{PortNum} {b}").ToList() + }.AddXYPair($"P{PortNum} {{0}}", AxisPairOrientation.RightAndUp, (1, 160, 320), (1, 121, 242)); //TODO verify direction against hardware } public int PortNum { get; } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Belogic/Uzem.cs b/src/BizHawk.Emulation.Cores/Consoles/Belogic/Uzem.cs index 2f7f453cf8..39cf7fb554 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Belogic/Uzem.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Belogic/Uzem.cs @@ -2,6 +2,8 @@ using BizHawk.Emulation.Cores.Waterbox; using System; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Cores.Consoles.Belogic { [Core("uzem", "David Etherton", true, true, "", "", false)] @@ -59,16 +61,8 @@ namespace BizHawk.Emulation.Cores.Consoles.Belogic private static readonly ControllerDefinition Mouse = new ControllerDefinition { Name = "SNES Controller", - BoolButtons = - { - "P1 Mouse Left", "P1 Mouse Right", "Power" - }, - AxisControls = - { - "P1 Mouse X", "P1 Mouse Y" - }, - AxisRanges = ControllerDefinition.CreateAxisRangePair(-127, 0, 127, ControllerDefinition.AxisPairOrientation.RightAndUp) //TODO verify direction against hardware - }; + BoolButtons = { "P1 Mouse Left", "P1 Mouse Right", "Power" } + }.AddXYPair("P1 Mouse {0}", AxisPairOrientation.RightAndUp, -127, 0, 127); //TODO verify direction against hardware private static readonly string[] PadBits = { diff --git a/src/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoControllers.cs b/src/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoControllers.cs index 14a57745ef..4c7e1dde60 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoControllers.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoControllers.cs @@ -6,6 +6,8 @@ using System.Linq; using BizHawk.Common; using BizHawk.Emulation.Common; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Cores.ColecoVision { /// @@ -130,14 +132,8 @@ namespace BizHawk.Emulation.Cores.ColecoVision public ColecoTurboController(int portNum) { PortNum = portNum; - Definition = new ControllerDefinition - { - BoolButtons = BaseBoolDefinition - .Select(b => "P" + PortNum + " " + b) - .ToList(), - AxisControls = { "P" + PortNum + " Disc X", "P" + PortNum + " Disc Y" }, - AxisRanges = ControllerDefinition.CreateAxisRangePair(-127, 0, 127, ControllerDefinition.AxisPairOrientation.RightAndUp) //TODO verify direction against hardware - }; + Definition = new ControllerDefinition { BoolButtons = BaseBoolDefinition.Select(b => $"P{PortNum} {b}").ToList() } + .AddXYPair($"P{PortNum} Disc {{0}}", AxisPairOrientation.RightAndUp, -127, 0, 127); //TODO verify direction against hardware } public int PortNum { get; } @@ -235,14 +231,8 @@ namespace BizHawk.Emulation.Cores.ColecoVision public ColecoSuperActionController(int portNum) { PortNum = portNum; - Definition = new ControllerDefinition - { - BoolButtons = BaseBoolDefinition - .Select(b => "P" + PortNum + " " + b) - .ToList(), - AxisControls = { "P" + PortNum + " Disc X", "P" + PortNum + " Disc Y" }, - AxisRanges = ControllerDefinition.CreateAxisRangePair(-127, 0, 127, ControllerDefinition.AxisPairOrientation.RightAndUp) //TODO verify direction against hardware - }; + Definition = new ControllerDefinition { BoolButtons = BaseBoolDefinition.Select(b => $"P{PortNum} {b}").ToList() } + .AddXYPair($"P{PortNum} Disc {{0}}", AxisPairOrientation.RightAndUp, -127, 0, 127); //TODO verify direction against hardware } public int PortNum { get; } diff --git a/src/BizHawk.Emulation.Cores/Consoles/GCE/Vectrex/VectrexHawkControllers.cs b/src/BizHawk.Emulation.Cores/Consoles/GCE/Vectrex/VectrexHawkControllers.cs index 4690c793f8..dd3ed74757 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/GCE/Vectrex/VectrexHawkControllers.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/GCE/Vectrex/VectrexHawkControllers.cs @@ -4,6 +4,8 @@ using System.Linq; using BizHawk.Common; using BizHawk.Emulation.Common; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Cores.Consoles.Vectrex { /// @@ -78,12 +80,8 @@ namespace BizHawk.Emulation.Cores.Consoles.Vectrex Definition = new ControllerDefinition { Name = "Vectrex Analog Controller", - BoolButtons = BaseDefinition - .Select(b => "P" + PortNum + " " + b) - .ToList(), - AxisControls = { "P" + PortNum + " Stick X", "P" + PortNum + " Stick Y" }, - AxisRanges = ControllerDefinition.CreateAxisRangePair(-128, 0, 127, ControllerDefinition.AxisPairOrientation.RightAndUp) - }; + BoolButtons = BaseDefinition.Select(b => $"P{PortNum} {b}").ToList() + }.AddXYPair($"P{PortNum} Stick {{0}}", AxisPairOrientation.RightAndUp, -128, 0, 127); } public int PortNum { get; } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Controllers/IntellivisionControllers.cs b/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Controllers/IntellivisionControllers.cs index fa0c940a1a..3e6e5095a6 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Controllers/IntellivisionControllers.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Controllers/IntellivisionControllers.cs @@ -6,6 +6,8 @@ using System.Linq; using BizHawk.Common; using BizHawk.Emulation.Common; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Cores.Intellivision { /// @@ -139,14 +141,8 @@ namespace BizHawk.Emulation.Cores.Intellivision public FakeAnalogController(int portNum) { PortNum = portNum; - Definition = new ControllerDefinition - { - BoolButtons = BaseBoolDefinition - .Select(b => "P" + PortNum + " " + b) - .ToList(), - AxisControls = { "P" + PortNum + " Disc X", "P" + PortNum + " Disc Y" }, - AxisRanges = ControllerDefinition.CreateAxisRangePair(-127, 0, 127, ControllerDefinition.AxisPairOrientation.RightAndUp) //TODO verify direction against hardware - }; + Definition = new ControllerDefinition { BoolButtons = BaseBoolDefinition.Select(b => $"P{PortNum} {b}").ToList() } + .AddXYPair($"P{PortNum} Disc {{0}}", AxisPairOrientation.RightAndUp, -127, 0, 127); //TODO verify direction against hardware } public int PortNum { get; } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.cs index 5316562699..61d63a4afc 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.cs @@ -250,25 +250,11 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA return baseTime + increment; } - private static readonly ControllerDefinition.AxisRange TiltRange = new ControllerDefinition.AxisRange(-32767, 0, 32767); - public static readonly ControllerDefinition GBAController = new ControllerDefinition { Name = "GBA Controller", - BoolButtons = - { - "Up", "Down", "Left", "Right", "Start", "Select", "B", "A", "L", "R", "Power" - }, - AxisControls = - { - "Tilt X", "Tilt Y", "Tilt Z", - "Light Sensor" - }, - AxisRanges = - { - TiltRange, TiltRange, TiltRange, - new ControllerDefinition.AxisRange(0, 100, 200), - } - }; + BoolButtons = { "Up", "Down", "Left", "Right", "Start", "Select", "B", "A", "L", "R", "Power" } + }.AddXYZTriple("Tilt {0}", -32767, 0, 32767) + .AddAxis("Light Sensor", 0, 100, 200); } } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawkControllers.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawkControllers.cs index fe47f7c13d..787305d914 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawkControllers.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawkControllers.cs @@ -5,6 +5,8 @@ using System.Linq; using BizHawk.Common; using BizHawk.Emulation.Common; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Cores.Nintendo.GBHawk { /// @@ -114,12 +116,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk Definition = new ControllerDefinition { Name = "Gameboy Controller + Tilt", - BoolButtons = BaseDefinition - .Select(b => "P" + PortNum + " " + b) - .ToList(), - AxisControls = { "P" + PortNum + " Tilt X", "P" + PortNum + " Tilt Y" }, - AxisRanges = ControllerDefinition.CreateAxisRangePair(-45, 0, 45, ControllerDefinition.AxisPairOrientation.RightAndUp) //TODO verify direction against hardware - }; + BoolButtons = BaseDefinition.Select(b => $"P{PortNum} {b}").ToList() + }.AddXYPair($"P{PortNum} Tilt {{0}}", AxisPairOrientation.RightAndUp, -45, 0, 45); //TODO verify direction against hardware } public int PortNum { get; } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.cs index 90433d41cf..4650538163 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.cs @@ -3,6 +3,8 @@ using System.Runtime.InteropServices; using System.IO; using BizHawk.Emulation.Common; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS { [Core("MelonDS", "Arisotura", false, false, null, null, true)] @@ -92,10 +94,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS ControllerDefinition.BoolButtons.Add("Power"); ControllerDefinition.BoolButtons.Add("Touch"); - ControllerDefinition.AxisControls.Add("TouchX"); - ControllerDefinition.AxisRanges.Add(new ControllerDefinition.AxisRange(0, 128, 255)); - ControllerDefinition.AxisControls.Add("TouchY"); - ControllerDefinition.AxisRanges.Add(new ControllerDefinition.AxisRange(0, 96, 191)); + ControllerDefinition.AddXYPair("Touch{0}", AxisPairOrientation.RightAndUp, (0, 128, 255), (0, 96, 191)); //TODO verify direction against hardware CoreComm = comm; _resampler = new SpeexResampler(SpeexResampler.Quality.QUALITY_DEFAULT, 32768, 44100, 32768, 44100); diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NESControllers.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NESControllers.cs index 0dddca8254..a97519f7ea 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NESControllers.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NESControllers.cs @@ -5,6 +5,8 @@ using BizHawk.Emulation.Common; using BizHawk.Common; using Newtonsoft.Json; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Cores.Nintendo.NES { /* @@ -182,9 +184,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES ser.EndSection(); } - internal static readonly ControllerDefinition.AxisRange ArkanoidPaddleRange = new ControllerDefinition.AxisRange(0, 80, 160); - - internal static readonly List ZapperRanges = new List { new ControllerDefinition.AxisRange(0, 128, 255), new ControllerDefinition.AxisRange(0, 120, 239) }; + internal static readonly AxisRange ArkanoidPaddleRange = new AxisRange(0, 80, 160); } public class UnpluggedNES : INesPort @@ -540,6 +540,12 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES } } + internal static class NESControllerDefExtensions + { + public static ControllerDefinition AddZapper(this ControllerDefinition def, string nameFormat) + => def.AddXYPair(nameFormat, AxisPairOrientation.RightAndUp, (0, 128, 255), (0, 120, 239)); //TODO verify direction against hardware + } + // Dummy interface to indicate zapper behavior, used as a means of type checking for zapper functionality public interface IZapper { @@ -553,12 +559,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES /// public LightgunDelegate PPUCallback { get; set; } - static ControllerDefinition Definition = new ControllerDefinition - { - BoolButtons = { "0Fire" }, - AxisControls = { "0Zapper X", "0Zapper Y" }, - AxisRanges = NesDeck.ZapperRanges - }; + private static readonly ControllerDefinition Definition + = new ControllerDefinition { BoolButtons = { "0Fire" } }.AddZapper("0Zapper {0}"); public void Strobe(StrobeInfo s, IController c) { @@ -606,12 +608,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES bool resetting = false; uint latchedvalue = 0; - static ControllerDefinition Definition = new ControllerDefinition - { - BoolButtons = { "0Fire" }, - AxisControls = { "0Zapper X", "0Zapper Y" }, - AxisRanges = NesDeck.ZapperRanges - }; + private static readonly ControllerDefinition Definition + = new ControllerDefinition { BoolButtons = { "0Fire" } }.AddZapper("0Zapper {0}"); void Latch(IController c) { @@ -1006,12 +1004,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES public class OekaKids : IFamicomExpansion { - static ControllerDefinition Definition = new ControllerDefinition - { - BoolButtons = { "0Click", "0Touch" }, - AxisControls = { "0Pen X", "0Pen Y" }, - AxisRanges = NesDeck.ZapperRanges // why would a tablet have the same resolution as a CRT monitor? --yoshi - }; + private static readonly ControllerDefinition Definition + = new ControllerDefinition { BoolButtons = { "0Click", "0Touch" } } + .AddZapper("0Pen {0}"); // why would a tablet have the same resolution as a CRT monitor? --yoshi bool resetting; int shiftidx; diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesControllerDeck.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesControllerDeck.cs index 8df1246845..8854a081f6 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesControllerDeck.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesControllerDeck.cs @@ -5,6 +5,8 @@ using System.Linq; using BizHawk.Emulation.Common; using BizHawk.Common.NumberExtensions; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Cores.Nintendo.SNES { public class LibsnesControllerDeck @@ -20,13 +22,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES Payload } - /// - /// problem: when you're in 240 line mode, the limit on Y needs to be 240. when you're in 224 mode, it needs to be 224. - /// perhaps the deck needs to account for this... - /// for reference Snes9x is always in 224 mode - /// - public static readonly List LightGunRanges = new List { new ControllerDefinition.AxisRange(0, 128, 256), new ControllerDefinition.AxisRange(0, 0, 240) }; - private static ILibsnesController Factory(ControllerType t, LibsnesCore.SnesSyncSettings ss) { switch (t) @@ -89,6 +84,17 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES } } + internal static class SNESControllerDefExtensions + { + /// + /// problem: when you're in 240 line mode, the limit on Y needs to be 240. when you're in 224 mode, it needs to be 224. + /// perhaps the deck needs to account for this... + /// for reference Snes9x is always in 224 mode + /// + public static ControllerDefinition AddLightGun(this ControllerDefinition def, string nameFormat) + => def.AddXYPair(nameFormat, AxisPairOrientation.RightAndUp, (0, 128, 256), (0, 0, 240)); //TODO verify direction against hardware + } + public interface ILibsnesController { /// @@ -279,20 +285,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES { public LibsnesApi.SNES_INPUT_PORT PortType => LibsnesApi.SNES_INPUT_PORT.Mouse; - private static readonly ControllerDefinition _definition = new ControllerDefinition - { - BoolButtons = new List - { - "0Mouse Left", - "0Mouse Right" - }, - AxisControls = - { - "0Mouse X", - "0Mouse Y" - }, - AxisRanges = ControllerDefinition.CreateAxisRangePair(-127, 0, 127, ControllerDefinition.AxisPairOrientation.RightAndDown) //TODO verify direction against hardware, R+D inferred from behaviour in Mario Paint - }; + private static readonly ControllerDefinition _definition + = new ControllerDefinition { BoolButtons = { "0Mouse Left", "0Mouse Right" } } + .AddXYPair("0Mouse {0}", AxisPairOrientation.RightAndDown, -127, 0, 127); //TODO verify direction against hardware, R+D inferred from behaviour in Mario Paint public ControllerDefinition Definition => _definition; @@ -332,22 +327,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES { public LibsnesApi.SNES_INPUT_PORT PortType => LibsnesApi.SNES_INPUT_PORT.SuperScope; - private static readonly ControllerDefinition _definition = new ControllerDefinition - { - BoolButtons = new List - { - "0Trigger", - "0Cursor", - "0Turbo", - "0Pause" - }, - AxisControls = - { - "0Scope X", - "0Scope Y" - }, - AxisRanges = LibsnesControllerDeck.LightGunRanges - }; + private static readonly ControllerDefinition _definition + = new ControllerDefinition { BoolButtons = { "0Trigger", "0Cursor", "0Turbo", "0Pause" } } + .AddLightGun("0Scope {0}"); public ControllerDefinition Definition => _definition; @@ -379,24 +361,10 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES { public LibsnesApi.SNES_INPUT_PORT PortType => LibsnesApi.SNES_INPUT_PORT.Justifier; - private static readonly ControllerDefinition _definition = new ControllerDefinition - { - BoolButtons = new List - { - "0Trigger", - "0Start", - "1Trigger", - "1Start" - }, - AxisControls = - { - "0Justifier X", - "0Justifier Y", - "1Justifier X", - "1Justifier Y", - }, - AxisRanges = LibsnesControllerDeck.LightGunRanges.Concat(LibsnesControllerDeck.LightGunRanges).ToList() - }; + private static readonly ControllerDefinition _definition + = new ControllerDefinition { BoolButtons = { "0Trigger", "0Start", "1Trigger", "1Start" } } + .AddLightGun("0Justifier {0}") + .AddLightGun("1Justifier {0}"); public ControllerDefinition Definition => _definition; diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES9X/Snes9x.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES9X/Snes9x.cs index 8770fdda1f..a6fe42c822 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES9X/Snes9x.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES9X/Snes9x.cs @@ -7,6 +7,10 @@ using BizHawk.Common; using System.Collections.Generic; using System.Linq; +using BizHawk.Emulation.Cores.Nintendo.SNES; + +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Cores.Nintendo.SNES9X { [Core(CoreNames.Snes9X, "", true, true, @@ -202,62 +206,27 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES9X private class Mouse : Analog { - private static readonly ControllerDefinition _definition = new ControllerDefinition - { - BoolButtons = new List - { - "0Mouse Left", - "0Mouse Right" - }, - AxisControls = - { - "0Mouse X", - "0Mouse Y" - }, - AxisRanges = ControllerDefinition.CreateAxisRangePair(-127, 0, 127, ControllerDefinition.AxisPairOrientation.RightAndUp) //TODO verify direction against hardware - }; + private static readonly ControllerDefinition _definition + = new ControllerDefinition { BoolButtons = { "0Mouse Left", "0Mouse Right" } } + .AddXYPair("0Mouse {0}", AxisPairOrientation.RightAndUp, -127, 0, 127); //TODO verify direction against hardware public override ControllerDefinition Definition => _definition; } private class SuperScope : Analog { - private static readonly ControllerDefinition _definition = new ControllerDefinition - { - BoolButtons = new List - { - "0Trigger", - "0Cursor", - "0Turbo", - "0Pause" - }, - AxisControls = - { - "0Scope X", - "0Scope Y" - }, - AxisRanges = SNES.LibsnesControllerDeck.LightGunRanges - }; + private static readonly ControllerDefinition _definition + = new ControllerDefinition { BoolButtons = { "0Trigger", "0Cursor", "0Turbo", "0Pause" } } + .AddLightGun("0Scope {0}"); public override ControllerDefinition Definition => _definition; } private class Justifier : Analog { - private static readonly ControllerDefinition _definition = new ControllerDefinition - { - BoolButtons = new List - { - "0Trigger", - "0Start", - }, - AxisControls = - { - "0Justifier X", - "0Justifier Y", - }, - AxisRanges = SNES.LibsnesControllerDeck.LightGunRanges - }; + private static readonly ControllerDefinition _definition + = new ControllerDefinition { BoolButtons = { "0Trigger", "0Start" } } + .AddLightGun("0Justifier {0}"); public override ControllerDefinition Definition => _definition; } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.Input.cs b/src/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.Input.cs index 138b52cc19..6e630c67dc 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.Input.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.Input.cs @@ -3,6 +3,8 @@ using System.Linq; using BizHawk.Emulation.Common; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Cores.Sega.MasterSystem { public partial class SMS @@ -44,8 +46,8 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem }, AxisRanges = { - new ControllerDefinition.AxisRange(0, 128, 255), - new ControllerDefinition.AxisRange(0, 128, 255) + new AxisRange(0, 128, 255), + new AxisRange(0, 128, 255) } }; @@ -55,21 +57,9 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem BoolButtons = { "Reset", "Pause", - "P1 Trigger", - }, - AxisControls = - { - "P1 X", "P1 Y", - }, - AxisRanges = - { - new ControllerDefinition.AxisRange(0, 64, 127), - new ControllerDefinition.AxisRange(0, 500, 1000) + "P1 Trigger" } - }; - - /// TODO verify direction against hardware - private static readonly List SportsPadTrackballRanges = ControllerDefinition.CreateAxisRangePair(-64, 0, 63, ControllerDefinition.AxisPairOrientation.RightAndUp); + }.AddXYPair("P1 {0}", AxisPairOrientation.RightAndUp, (0, 64, 127), (0, 500, 1000)); //TODO verify direction against hardware public static readonly ControllerDefinition SMSSportsPadController = new ControllerDefinition { @@ -79,14 +69,9 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem "Reset", "Pause", "P1 Left", "P1 Right", "P1 Up", "P1 Down", "P1 B1", "P1 B2", "P2 Left", "P2 Right", "P2 Up", "P2 Down", "P2 B1", "P2 B2" - }, - AxisControls = - { - "P1 X", "P1 Y", - "P2 X", "P2 Y" - }, - AxisRanges = SportsPadTrackballRanges.Concat(SportsPadTrackballRanges).ToList() - }; + } + }.AddXYPair("P1 {0}", AxisPairOrientation.RightAndUp, -64, 0, 63) //TODO verify direction against hardware + .AddXYPair("P2 {0}", AxisPairOrientation.RightAndUp, -64, 0, 63); //TODO ditto public static readonly ControllerDefinition SMSKeyboardController = new ControllerDefinition { diff --git a/src/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GPGXControlConverter.cs b/src/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GPGXControlConverter.cs index 454979d4e8..66e67682fa 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GPGXControlConverter.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Sega/gpgx64/GPGXControlConverter.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using BizHawk.Emulation.Common; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx { public class GPGXControlConverter @@ -93,13 +95,6 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx new CName("XE E2", LibGPGX.INPUT_KEYS.INPUT_XE_E2), }; - private static readonly ControllerDefinition.AxisRange MouseRange = new ControllerDefinition.AxisRange(-256, 0, 255); - - // lightgun needs to be transformed to match the current screen resolution - private static readonly ControllerDefinition.AxisRange LightgunRange = new ControllerDefinition.AxisRange(0, 5000, 10000); - - private static readonly ControllerDefinition.AxisRange Xea1PRange = new ControllerDefinition.AxisRange(-128, 0, 127); - private LibGPGX.InputData _target; private IController _source; @@ -126,12 +121,9 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx private void DoMouseAnalog(int idx, int player) { - string nx = $"P{player} Mouse X"; - string ny = $"P{player} Mouse Y"; - ControllerDef.AxisControls.Add(nx); - ControllerDef.AxisControls.Add(ny); - ControllerDef.AxisRanges.Add(MouseRange); - ControllerDef.AxisRanges.Add(MouseRange); + ControllerDef.AddXYPair($"P{player} Mouse {{0}}", AxisPairOrientation.RightAndUp, -256, 0, 255); //TODO verify direction against hardware + var nx = $"P{player} Mouse X"; + var ny = $"P{player} Mouse Y"; _converts.Add(delegate { _target.analog[(2 * idx) + 0] = (short)_source.AxisValue(nx); @@ -141,12 +133,10 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx private void DoLightgunAnalog(int idx, int player) { - string nx = $"P{player} Lightgun X"; - string ny = $"P{player} Lightgun Y"; - ControllerDef.AxisControls.Add(nx); - ControllerDef.AxisControls.Add(ny); - ControllerDef.AxisRanges.Add(LightgunRange); - ControllerDef.AxisRanges.Add(LightgunRange); + // lightgun needs to be transformed to match the current screen resolution + ControllerDef.AddXYPair($"P{player} Lightgun {{0}}", AxisPairOrientation.RightAndUp, 0, 5000, 10000); //TODO verify direction against hardware + var nx = $"P{player} Lightgun X"; + var ny = $"P{player} Lightgun Y"; _converts.Add(delegate { _target.analog[(2 * idx) + 0] = (short)(_source.AxisValue(nx) / 10000.0f * (ScreenWidth - 1)); @@ -156,15 +146,10 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx private void DoXea1PAnalog(int idx, int player) { - string nx = $"P{player} Stick X"; - string ny = $"P{player} Stick Y"; - string nz = $"P{player} Stick Z"; - ControllerDef.AxisControls.Add(nx); - ControllerDef.AxisControls.Add(ny); - ControllerDef.AxisControls.Add(nz); - ControllerDef.AxisRanges.Add(Xea1PRange); - ControllerDef.AxisRanges.Add(Xea1PRange); - ControllerDef.AxisRanges.Add(Xea1PRange); + ControllerDef.AddXYZTriple($"P{player} Stick {{0}}", -128, 0, 127); + var nx = $"P{player} Stick X"; + var ny = $"P{player} Stick Y"; + var nz = $"P{player} Stick Z"; _converts.Add(delegate { _target.analog[(2 * idx) + 0] = (short)_source.AxisValue(nx); diff --git a/src/BizHawk.Emulation.Cores/Consoles/Sony/PSX/Octoshock.cs b/src/BizHawk.Emulation.Cores/Consoles/Sony/PSX/Octoshock.cs index 61674e5e08..2fcea21ec9 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Sony/PSX/Octoshock.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Sony/PSX/Octoshock.cs @@ -24,6 +24,8 @@ using BizHawk.Emulation.Common; using BizHawk.Common; using BizHawk.Emulation.DiscSystem; +using static BizHawk.Emulation.Common.ControllerDefinition; + #pragma warning disable 649 //adelikat: Disable dumb warnings until this file is complete namespace BizHawk.Emulation.Cores.Sony.PSX @@ -220,8 +222,6 @@ namespace BizHawk.Emulation.Cores.Sony.PSX public string SystemId => "PSX"; - public static readonly IReadOnlyList DualShockStickRanges = ControllerDefinition.CreateAxisRangePair(0, 128, 255, ControllerDefinition.AxisPairOrientation.RightAndDown); - public static ControllerDefinition CreateControllerDefinition(SyncSettings syncSettings) { var definition = new ControllerDefinition { Name = "PSX Front Panel" }; @@ -255,7 +255,7 @@ namespace BizHawk.Emulation.Cores.Sony.PSX "P" + pnum + " L" }); - var axisRange = new ControllerDefinition.AxisRange(0, 128, 255); + var axisRange = new AxisRange(0, 128, 255); definition.AxisRanges.Add(axisRange); definition.AxisRanges.Add(axisRange); definition.AxisRanges.Add(axisRange); @@ -287,16 +287,8 @@ namespace BizHawk.Emulation.Cores.Sony.PSX definition.BoolButtons.Add("P" + pnum + " L3"); definition.BoolButtons.Add("P" + pnum + " R3"); definition.BoolButtons.Add("P" + pnum + " MODE"); - - definition.AxisControls.AddRange(new[] - { - "P" + pnum + " LStick X", - "P" + pnum + " LStick Y", - "P" + pnum + " RStick X", - "P" + pnum + " RStick Y" - }); - - definition.AxisRanges.AddRange(DualShockStickRanges.Concat(DualShockStickRanges).ToList()); + definition.AddXYPair($"P{pnum} LStick {{0}}", AxisPairOrientation.RightAndDown, 0, 128, 255); + definition.AddXYPair($"P{pnum} RStick {{0}}", AxisPairOrientation.RightAndDown, 0, 128, 255); } } } @@ -313,7 +305,7 @@ namespace BizHawk.Emulation.Cores.Sony.PSX definition.AxisRanges.Add( //new ControllerDefinition.AxisRange(-1, -1, -1) //this is carefully chosen so that we end up with a -1 disc by default (indicating that it's never been set) //hmm.. I don't see why this wouldn't work - new ControllerDefinition.AxisRange(0, 1, 1) + new AxisRange(0, 1, 1) ); return definition; diff --git a/src/BizHawk.Emulation.Cores/Libretro/LibretroCore.cs b/src/BizHawk.Emulation.Cores/Libretro/LibretroCore.cs index 45149a1970..8c0eb9f247 100644 --- a/src/BizHawk.Emulation.Cores/Libretro/LibretroCore.cs +++ b/src/BizHawk.Emulation.Cores/Libretro/LibretroCore.cs @@ -9,6 +9,8 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using BizHawk.Emulation.Common; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Emulation.Cores.Libretro { [Core("Libretro", "zeromus", isPorted: false, isReleased: false)] @@ -253,9 +255,7 @@ namespace BizHawk.Emulation.Cores.Libretro definition.BoolButtons.Add(string.Format(item,"RetroPad")); definition.BoolButtons.Add("Pointer Pressed"); //TODO: this isnt showing up in the binding panel. I don't want to find out why. - definition.AxisControls.Add("Pointer X"); - definition.AxisControls.Add("Pointer Y"); - definition.AxisRanges.AddRange(ControllerDefinition.CreateAxisRangePair(-32767, 0, 32767, ControllerDefinition.AxisPairOrientation.RightAndUp)); + definition.AddXYPair("Pointer {0}", AxisPairOrientation.RightAndUp, -32767, 0, 32767); foreach (var key in new[]{ "Key Backspace", "Key Tab", "Key Clear", "Key Return", "Key Pause", "Key Escape", diff --git a/src/BizHawk.Tests/Client.Common/Display/InputDisplayTests.cs b/src/BizHawk.Tests/Client.Common/Display/InputDisplayTests.cs index eef2287b8d..a29cbea031 100644 --- a/src/BizHawk.Tests/Client.Common/Display/InputDisplayTests.cs +++ b/src/BizHawk.Tests/Client.Common/Display/InputDisplayTests.cs @@ -3,6 +3,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using BizHawk.Client.Common; using BizHawk.Emulation.Common; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Tests.Client.Common.Display { [TestClass] @@ -22,15 +24,7 @@ namespace BizHawk.Tests.Client.Common.Display _axisController = new SimpleController { - Definition = new ControllerDefinition - { - AxisControls = { "StickX", "StickY" }, - AxisRanges = - { - new ControllerDefinition.AxisRange(0, MidValue, 200), - new ControllerDefinition.AxisRange(0, MidValue, 200) - } - } + Definition = new ControllerDefinition().AddXYPair("Stick{0}", AxisPairOrientation.RightAndUp, 0, MidValue, 200) }; } diff --git a/src/BizHawk.Tests/Client.Common/Movie/LogGeneratorTests.cs b/src/BizHawk.Tests/Client.Common/Movie/LogGeneratorTests.cs index 0423ac7d27..44ef93ef5f 100644 --- a/src/BizHawk.Tests/Client.Common/Movie/LogGeneratorTests.cs +++ b/src/BizHawk.Tests/Client.Common/Movie/LogGeneratorTests.cs @@ -3,6 +3,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using BizHawk.Client.Common; using BizHawk.Emulation.Common; +using static BizHawk.Emulation.Common.ControllerDefinition; + namespace BizHawk.Common.Tests.Client.Common.Movie { [TestClass] @@ -21,15 +23,7 @@ namespace BizHawk.Common.Tests.Client.Common.Movie _floatController = new SimpleController { - Definition = new ControllerDefinition - { - AxisControls = { "StickX", "StickY" }, - AxisRanges = - { - new ControllerDefinition.AxisRange(0, 100, 200), - new ControllerDefinition.AxisRange(0, 100, 200) - } - } + Definition = new ControllerDefinition().AddXYPair("Stick{0}", AxisPairOrientation.RightAndUp, 0, 100, 200) }; }