From 8cd88cdeca3612ed5a1e24f19bf38a223cbc26dc Mon Sep 17 00:00:00 2001 From: Hathor86 Date: Wed, 6 Apr 2016 22:10:36 +0200 Subject: [PATCH] ApiHawk - Add few other system inputs SNES, SMS, Genesis, N64, GB Following some tests, it works (so I'd say that this feature is beta) --- BizHawk.Client.ApiHawk/Classes/ClientApi.cs | 58 ++++++--- BizHawk.Client.ApiHawk/Classes/Joypad.cs | 34 ++++++ .../Classes/JoypadStringToEnumConverter.cs | 114 +++++++++++++++++- BizHawk.Client.Common/SystemInfo.cs | 2 +- 4 files changed, 187 insertions(+), 21 deletions(-) diff --git a/BizHawk.Client.ApiHawk/Classes/ClientApi.cs b/BizHawk.Client.ApiHawk/Classes/ClientApi.cs index 89be21a4eb..30b9424167 100644 --- a/BizHawk.Client.ApiHawk/Classes/ClientApi.cs +++ b/BizHawk.Client.ApiHawk/Classes/ClientApi.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; +using System.Windows.Forms; using BizHawk.Client.Common; using BizHawk.Emulation.Cores.Nintendo.Gameboy; @@ -22,6 +23,7 @@ namespace BizHawk.Client.ApiHawk private static readonly Assembly clientAssembly; private static readonly object clientMainFormInstance; private static readonly Type mainFormClass; + private static readonly Array joypadButtonsArray = Enum.GetValues(typeof(JoypadButton)); internal static readonly BizHawkSystemIdToEnumConverter SystemIdConverter = new BizHawkSystemIdToEnumConverter(); internal static readonly JoypadStringToEnumConverter JoypadConverter = new JoypadStringToEnumConverter(); @@ -120,16 +122,13 @@ namespace BizHawk.Client.ApiHawk /// Bottom padding public static void SetExtraPadding(int left, int top, int right, int bottom) { - Type emuLuaLib = clientAssembly.GetType("BizHawk.Client.EmuHawk.EmuHawkLuaLibrary"); - MethodInfo paddingMethod = emuLuaLib.GetMethod("SetClientExtraPadding"); - paddingMethod.Invoke(paddingMethod, new object[] { left, top, right, bottom }); + FieldInfo f = clientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin").GetField("DisplayManager"); + object displayManager = f.GetValue(null); + f = f.FieldType.GetField("ClientExtraPadding"); + f.SetValue(displayManager, new Padding(left, top, right, bottom)); - /*FieldInfo f = clientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin").GetField("DisplayManager").FieldType.GetField("ClientExtraPadding"); - f.SetValue(clientAssembly.GetType("BizHawk.Client.EmuHawk.GlobalWin").GetField("DisplayManager"), new System.Windows.Forms.Padding(left, top, right, bottom)); MethodInfo resize = mainFormClass.GetMethod("FrameBufferResized"); - resize.Invoke(clientMainFormInstance, null);*/ - /*GlobalWin.DisplayManager.ClientExtraPadding = new System.Windows.Forms.Padding(left, top, right, bottom); - GlobalWin.MainForm.FrameBufferResized();*/ + resize.Invoke(clientMainFormInstance, null); } /// @@ -169,6 +168,7 @@ namespace BizHawk.Client.ApiHawk /// Player (one based) whom inputs must be set /// with inputs /// Raised when you specify a player less than 1 or greater than maximum allows (see SystemInfo class to get this information) + /// Still have some strange behaviour with multiple inputs; so this feature is still in beta public static void SetInput(int player, Joypad joypad) { if (player < 1 || player > RunningSystem.MaxControllers) @@ -184,16 +184,33 @@ namespace BizHawk.Client.ApiHawk } else { - Parallel.ForEach((IEnumerable)Enum.GetValues(typeof(JoypadButton)), button => + foreach (JoypadButton button in joypadButtonsArray) { if (joypad.Inputs.HasFlag(button)) { AutoFireStickyXorAdapter joypadAdaptor = Global.AutofireStickyXORAdapter; - joypadAdaptor.SetSticky(string.Format("P{0} {1}", player, JoypadConverter.ConvertBack(button, RunningSystem)), true); + if (RunningSystem == SystemInfo.GB) + { + joypadAdaptor.SetSticky(string.Format("{0}", JoypadConverter.ConvertBack(button, RunningSystem)), true); + } + else + { + joypadAdaptor.SetSticky(string.Format("P{0} {1}", player, JoypadConverter.ConvertBack(button, RunningSystem)), true); + } } } - ); } + + //Using this break joypad usage (even in UI); have to figure out why + /*if ((RunningSystem.AvailableButtons & JoypadButton.AnalogStick) == JoypadButton.AnalogStick) + { + AutoFireStickyXorAdapter joypadAdaptor = Global.AutofireStickyXORAdapter; + for (int i = 1; i <= RunningSystem.MaxControllers; i++) + { + joypadAdaptor.SetFloat(string.Format("P{0} X Axis", i), allJoypads[i - 1].AnalogX); + joypadAdaptor.SetFloat(string.Format("P{0} Y Axis", i), allJoypads[i - 1].AnalogY); + } + }*/ } } @@ -202,7 +219,7 @@ namespace BizHawk.Client.ApiHawk /// Resume the emulation /// public static void UnpauseEmulation() - { + { MethodInfo method = mainFormClass.GetMethod("UnpauseEmulator"); method.Invoke(clientMainFormInstance, null); } @@ -228,15 +245,26 @@ namespace BizHawk.Client.ApiHawk Parallel.ForEach(pressedButtons, button => { int player; - if (int.TryParse(button.Substring(1, 2), out player)) + if (RunningSystem == SystemInfo.GB) { - allJoypads[player - 1].AddInput(JoypadConverter.Convert(button.Substring(3))); + allJoypads[0].AddInput(JoypadConverter.Convert(button)); + } + else + { + if (int.TryParse(button.Substring(1, 2), out player)) + { + allJoypads[player - 1].AddInput(JoypadConverter.Convert(button.Substring(3))); + } } }); if ((RunningSystem.AvailableButtons & JoypadButton.AnalogStick) == JoypadButton.AnalogStick) { - //joypadAdaptor.GetFloat(); + for (int i = 1; i <= RunningSystem.MaxControllers; i++) + { + allJoypads[i - 1].AnalogX = joypadAdaptor.GetFloat(string.Format("P{0} X Axis", i)); + allJoypads[i - 1].AnalogY = joypadAdaptor.GetFloat(string.Format("P{0} Y Axis", i)); + } } } diff --git a/BizHawk.Client.ApiHawk/Classes/Joypad.cs b/BizHawk.Client.ApiHawk/Classes/Joypad.cs index e4dd93dc95..d13273e64e 100644 --- a/BizHawk.Client.ApiHawk/Classes/Joypad.cs +++ b/BizHawk.Client.ApiHawk/Classes/Joypad.cs @@ -12,6 +12,8 @@ namespace BizHawk.Client.ApiHawk private SystemInfo _System; private JoypadButton _PressedButtons; + private float _AnalogX; + private float _AnalogY; private int _Player; #endregion @@ -72,6 +74,38 @@ namespace BizHawk.Client.ApiHawk #region Properties + /// + /// Gets or sets X value for Analog stick + /// + /// The value you get will aways be rounded to 0 decimal + public float AnalogX + { + get + { + return (float)Math.Round(_AnalogX, 0); + } + set + { + _AnalogX = value; + } + } + + /// + /// Gets or sets Y value for Analog stick + /// + /// The value you get will aways be rounded to 0 decimal + public float AnalogY + { + get + { + return (float)Math.Round(_AnalogY, 0); + } + set + { + _AnalogY = value; + } + } + /// /// Gets or sets inputs /// If you pass inputs unavailable for current system, they'll be removed diff --git a/BizHawk.Client.ApiHawk/Classes/JoypadStringToEnumConverter.cs b/BizHawk.Client.ApiHawk/Classes/JoypadStringToEnumConverter.cs index 2ca467804b..0e82da2ea2 100644 --- a/BizHawk.Client.ApiHawk/Classes/JoypadStringToEnumConverter.cs +++ b/BizHawk.Client.ApiHawk/Classes/JoypadStringToEnumConverter.cs @@ -30,7 +30,37 @@ namespace BizHawk.Client.ApiHawk return JoypadButton.A; case "B": - return JoypadButton.B; + return JoypadButton.B; + + case "B1": + return JoypadButton.B1; + + case "B2": + return JoypadButton.B2; + + case "C": + return JoypadButton.C; + + case "C UP": + return JoypadButton.CUp; + + case "C DOWN": + return JoypadButton.CDown; + + case "C LEFT": + return JoypadButton.CLeft; + + case "C RIGHT": + return JoypadButton.CRight; + + case "X": + return JoypadButton.X; + + case "Y": + return JoypadButton.Y; + + case "Z": + return JoypadButton.Z; case "START": return JoypadButton.Start; @@ -39,17 +69,27 @@ namespace BizHawk.Client.ApiHawk return JoypadButton.Select; case "UP": + case "DPAD U": return JoypadButton.Up; case "DOWN": + case "DPAD D": return JoypadButton.Down; case "LEFT": + case "DPAD L": return JoypadButton.Left; case "RIGHT": + case "DPAD R": return JoypadButton.Right; + case "L": + return JoypadButton.L; + + case "R": + return JoypadButton.R; + default: throw new IndexOutOfRangeException(string.Format("{0} is missing in convert list", value)); } @@ -87,6 +127,36 @@ namespace BizHawk.Client.ApiHawk case JoypadButton.B: return "B"; + case JoypadButton.B1: + return "B1"; + + case JoypadButton.B2: + return "B2"; + + case JoypadButton.C: + return "C"; + + case JoypadButton.CUp: + return "C Up"; + + case JoypadButton.CDown: + return "C Down"; + + case JoypadButton.CLeft: + return "C Left"; + + case JoypadButton.CRight: + return "C Right"; + + case JoypadButton.X: + return "X"; + + case JoypadButton.Y: + return "Y"; + + case JoypadButton.Z: + return "Z"; + case JoypadButton.Start: return "Start"; @@ -94,16 +164,50 @@ namespace BizHawk.Client.ApiHawk return "Select"; case JoypadButton.Up: - return "Up"; + if (((SystemInfo)parameter) == SystemInfo.N64) + { + return "Dpad U"; + } + else + { + return "Up"; + } case JoypadButton.Down: - return "Down"; + if (((SystemInfo)parameter) == SystemInfo.N64) + { + return "Dpad D"; + } + else + { + return "Down"; + } case JoypadButton.Left: - return "Left"; + if (((SystemInfo)parameter) == SystemInfo.N64) + { + return "Dpad L"; + } + else + { + return "Left"; + } case JoypadButton.Right: - return "Right"; + if (((SystemInfo)parameter) == SystemInfo.N64) + { + return "Dpad R"; + } + else + { + return "Right"; + } + + case JoypadButton.L: + return "L"; + + case JoypadButton.R: + return "R"; default: throw new IndexOutOfRangeException(string.Format("{0} is missing in convert list", value)); diff --git a/BizHawk.Client.Common/SystemInfo.cs b/BizHawk.Client.Common/SystemInfo.cs index af19433b63..6d8fe53fa6 100644 --- a/BizHawk.Client.Common/SystemInfo.cs +++ b/BizHawk.Client.Common/SystemInfo.cs @@ -52,7 +52,7 @@ namespace BizHawk.Client.Common allSystemInfos.Add(new SystemInfo("Commodore 64", CoreSystem.Commodore64, 1)); allSystemInfos.Add(new SystemInfo("ColecoVision", CoreSystem.ColecoVision, 1)); allSystemInfos.Add(new SystemInfo("Gameboy Advance", CoreSystem.GameBoyAdvance, 1, StandardButtons | JoypadButton.L | JoypadButton.R)); - allSystemInfos.Add(new SystemInfo("Nintendo 64", CoreSystem.Nintendo64, 4, StandardButtons ^ JoypadButton.Select | JoypadButton.Z | JoypadButton.CUp | JoypadButton.CDown | JoypadButton.CLeft | JoypadButton.CRight | JoypadButton.AnalogStick)); + allSystemInfos.Add(new SystemInfo("Nintendo 64", CoreSystem.Nintendo64, 4, StandardButtons ^ JoypadButton.Select | JoypadButton.Z | JoypadButton.CUp | JoypadButton.CDown | JoypadButton.CLeft | JoypadButton.CRight | JoypadButton.AnalogStick | JoypadButton.L | JoypadButton.R)); allSystemInfos.Add(new SystemInfo("Saturn", CoreSystem.Saturn, 2, UpDownLeftRight | JoypadButton.A | JoypadButton.B | JoypadButton.C | JoypadButton.X | JoypadButton.Y | JoypadButton.Z)); allSystemInfos.Add(new SystemInfo("Game Boy Link", CoreSystem.DualGameBoy, 2, StandardButtons)); allSystemInfos.Add(new SystemInfo("WonderSwan", CoreSystem.WonderSwan, 1));