lua - implement controller number parameter to joypad.getimmediate and movie.getinput, consolidate input getting lua code

This commit is contained in:
adelikat 2020-01-03 09:51:48 -06:00
parent 4a5ece2076
commit 038f35f45e
7 changed files with 47 additions and 44 deletions

View File

@ -14,38 +14,14 @@ namespace BizHawk.Client.Common
private readonly Action<string> LogCallback;
public Dictionary<string, dynamic> Get(int? controller = null)
public IDictionary<string, dynamic> Get(int? controller = null)
{
var adapter = Global.AutofireStickyXORAdapter;
var buttons = new Dictionary<string, dynamic>();
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<string, dynamic> GetImmediate()
public IDictionary<string, dynamic> GetImmediate(int? controller = null)
{
var adapter = Global.ActiveController;
var buttons = new Dictionary<string, dynamic>();
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)

View File

@ -19,7 +19,7 @@ namespace BizHawk.Client.Common
public bool StartsFromSaveram() => Global.MovieSession.Movie.IsActive() && Global.MovieSession.Movie.StartsFromSaveRam;
public Dictionary<string, dynamic> GetInput(int frame)
public IDictionary<string, dynamic> 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<string, dynamic>();
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)

View File

@ -6,7 +6,7 @@ namespace BizHawk.Client.Common
bool StartsFromSavestate();
bool StartsFromSaveram();
string Filename();
Dictionary<string, dynamic> GetInput(int frame);
IDictionary<string, dynamic> GetInput(int frame, int? controller = null);
string GetInputAsMnemonic(int frame);
bool GetReadOnly();
ulong GetRerecordCount();

View File

@ -4,15 +4,12 @@ namespace BizHawk.Client.Common
{
public interface IJoypad : IExternalApi
{
Dictionary<string, dynamic> Get(int? controller = null);
// TODO: what about float controls?
Dictionary<string, dynamic> GetImmediate();
IDictionary<string, dynamic> Get(int? controller = null);
IDictionary<string, dynamic> GetImmediate(int? controller = null);
void SetFromMnemonicStr(string inputLogEntry);
void Set(Dictionary<string, bool> buttons, int? controller = null);
void Set(string button, bool? state = null, int? controller = null);
void SetAnalog(Dictionary<string, float> controls, object controller = null);
void SetAnalog(string control, float? value = null, object controller = null);
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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<string, dynamic> ToDictionary(this IController controller, int? controllerNum = null)
{
var buttons = new Dictionary<string, dynamic>();
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;
}
}
}