From 038f35f45e41f75fb9b60436b885f86850bc3593 Mon Sep 17 00:00:00 2001 From: adelikat Date: Fri, 3 Jan 2020 09:51:48 -0600 Subject: [PATCH] lua - implement controller number parameter to joypad.getimmediate and movie.getinput, consolidate input getting lua code --- .../Api/Classes/JoypadApi.cs | 32 +++-------------- BizHawk.Client.Common/Api/Classes/MovieApi.cs | 8 ++--- .../Api/Interfaces/IInputMovie.cs | 2 +- .../Api/Interfaces/IJoypad.cs | 7 ++-- .../lua/EmuLuaLibrary.Joypad.cs | 4 +-- .../lua/EmuLuaLibrary.Movie.cs | 4 +-- BizHawk.Client.Common/lua/LuaHelper.cs | 34 ++++++++++++++++++- 7 files changed, 47 insertions(+), 44 deletions(-) diff --git a/BizHawk.Client.Common/Api/Classes/JoypadApi.cs b/BizHawk.Client.Common/Api/Classes/JoypadApi.cs index 446f8956c5..968a4b5b0e 100644 --- a/BizHawk.Client.Common/Api/Classes/JoypadApi.cs +++ b/BizHawk.Client.Common/Api/Classes/JoypadApi.cs @@ -14,38 +14,14 @@ namespace BizHawk.Client.Common private readonly Action LogCallback; - public Dictionary Get(int? controller = null) + public IDictionary Get(int? controller = null) { - var adapter = Global.AutofireStickyXORAdapter; - var buttons = new Dictionary(); - foreach (var button in adapter.Source.Definition.BoolButtons) - { - if (controller == null) buttons[button] = adapter.IsPressed(button); - else if (button.Length > 2 && button.Substring(0, 2) == $"P{controller}") - { - var sub = button.Substring(3); - buttons[sub] = adapter.IsPressed($"P{controller} {sub}"); - } - } - foreach (var button in adapter.Source.Definition.FloatControls) - { - if (controller == null) buttons[button] = adapter.GetFloat(button); - else if (button.Length > 2 && button.Substring(0, 2) == $"P{controller}") - { - var sub = button.Substring(3); - buttons[sub] = adapter.GetFloat($"P{controller} {sub}"); - } - } - return buttons; + return Global.AutofireStickyXORAdapter.ToDictionary(controller); } - public Dictionary GetImmediate() + public IDictionary GetImmediate(int? controller = null) { - var adapter = Global.ActiveController; - var buttons = new Dictionary(); - foreach (var button in adapter.Definition.BoolButtons) buttons[button] = adapter.IsPressed(button); - foreach (var button in adapter.Definition.FloatControls) buttons[button] = adapter.GetFloat(button); - return buttons; + return Global.ActiveController.ToDictionary(controller); } public void SetFromMnemonicStr(string inputLogEntry) diff --git a/BizHawk.Client.Common/Api/Classes/MovieApi.cs b/BizHawk.Client.Common/Api/Classes/MovieApi.cs index 63473f9e13..b45634bbfe 100644 --- a/BizHawk.Client.Common/Api/Classes/MovieApi.cs +++ b/BizHawk.Client.Common/Api/Classes/MovieApi.cs @@ -19,7 +19,7 @@ namespace BizHawk.Client.Common public bool StartsFromSaveram() => Global.MovieSession.Movie.IsActive() && Global.MovieSession.Movie.StartsFromSaveRam; - public Dictionary GetInput(int frame) + public IDictionary GetInput(int frame, int? controller = null) { if (Global.MovieSession.Movie.NotActive()) { @@ -32,10 +32,8 @@ namespace BizHawk.Client.Common LogCallback("Can't get input of the last frame of the movie. Use the previous frame"); return null; } - var input = new Dictionary(); - foreach (var button in adapter.Definition.BoolButtons) input[button] = adapter.IsPressed(button); - foreach (var button in adapter.Definition.FloatControls) input[button] = adapter.GetFloat(button); - return input; + + return adapter.ToDictionary(controller); } public string GetInputAsMnemonic(int frame) diff --git a/BizHawk.Client.Common/Api/Interfaces/IInputMovie.cs b/BizHawk.Client.Common/Api/Interfaces/IInputMovie.cs index 9ff1b17776..446a21040f 100644 --- a/BizHawk.Client.Common/Api/Interfaces/IInputMovie.cs +++ b/BizHawk.Client.Common/Api/Interfaces/IInputMovie.cs @@ -6,7 +6,7 @@ namespace BizHawk.Client.Common bool StartsFromSavestate(); bool StartsFromSaveram(); string Filename(); - Dictionary GetInput(int frame); + IDictionary GetInput(int frame, int? controller = null); string GetInputAsMnemonic(int frame); bool GetReadOnly(); ulong GetRerecordCount(); diff --git a/BizHawk.Client.Common/Api/Interfaces/IJoypad.cs b/BizHawk.Client.Common/Api/Interfaces/IJoypad.cs index eacd3b6618..182645c467 100644 --- a/BizHawk.Client.Common/Api/Interfaces/IJoypad.cs +++ b/BizHawk.Client.Common/Api/Interfaces/IJoypad.cs @@ -4,15 +4,12 @@ namespace BizHawk.Client.Common { public interface IJoypad : IExternalApi { - Dictionary Get(int? controller = null); - - // TODO: what about float controls? - Dictionary GetImmediate(); + IDictionary Get(int? controller = null); + IDictionary GetImmediate(int? controller = null); void SetFromMnemonicStr(string inputLogEntry); void Set(Dictionary buttons, int? controller = null); void Set(string button, bool? state = null, int? controller = null); void SetAnalog(Dictionary controls, object controller = null); void SetAnalog(string control, float? value = null, object controller = null); - } } diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs index b84768c54d..5a3a229439 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs @@ -29,10 +29,10 @@ namespace BizHawk.Client.Common [LuaMethodExample("local nljoyget = joypad.getimmediate( );")] [LuaMethod("getimmediate", "returns a lua table of any controller buttons currently pressed by the user")] - public LuaTable GetImmediate() + public LuaTable GetImmediate(int? controller = null) { return APIs.Joypad - .GetImmediate() + .GetImmediate(controller) .ToLuaTable(Lua); } diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs index 11e6c12491..1cc3fc0197 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs @@ -29,10 +29,10 @@ namespace BizHawk.Client.Common [LuaMethodExample("local nlmovget = movie.getinput( 500 );")] [LuaMethod("getinput", "Returns a table of buttons pressed on a given frame of the loaded movie")] - public LuaTable GetInput(int frame) + public LuaTable GetInput(int frame, int? controller = null) { return APIs.Movie - .GetInput(frame) + .GetInput(frame, controller) .ToLuaTable(Lua); } diff --git a/BizHawk.Client.Common/lua/LuaHelper.cs b/BizHawk.Client.Common/lua/LuaHelper.cs index 4f8283c9d7..7d1938018c 100644 --- a/BizHawk.Client.Common/lua/LuaHelper.cs +++ b/BizHawk.Client.Common/lua/LuaHelper.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; - +using BizHawk.Emulation.Common; using NLua; namespace BizHawk.Client.Common @@ -59,5 +59,37 @@ namespace BizHawk.Client.Common } return table; } + + public static IDictionary ToDictionary(this IController controller, int? controllerNum = null) + { + var buttons = new Dictionary(); + + foreach (var button in controller.Definition.BoolButtons) + { + if (controllerNum == null) + { + buttons[button] = controller.IsPressed(button); + } + else if (button.Length > 2 && button.Substring(0, 2) == $"P{controllerNum}") + { + var sub = button.Substring(3); + buttons[sub] = controller.IsPressed($"P{controllerNum} {sub}"); + } + } + foreach (var button in controller.Definition.FloatControls) + { + if (controllerNum == null) + { + buttons[button] = controller.GetFloat(button); + } + else if (button.Length > 2 && button.Substring(0, 2) == $"P{controllerNum}") + { + var sub = button.Substring(3); + buttons[sub] = controller.GetFloat($"P{controllerNum} {sub}"); + } + } + + return buttons; + } } }