Migrate JoypadLuaLibrary to ApiHawk delegation
`joypad.getimmediate()` now returns float controls as well as bools
This commit is contained in:
parent
f4a229888f
commit
1042f746f4
|
@ -7,6 +7,15 @@ namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
public sealed class JoypadApi : IJoypad
|
public sealed class JoypadApi : IJoypad
|
||||||
{
|
{
|
||||||
|
public JoypadApi(Action<string> logCallback)
|
||||||
|
{
|
||||||
|
LogCallback = logCallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JoypadApi() : this(Console.WriteLine) {}
|
||||||
|
|
||||||
|
private readonly Action<string> LogCallback;
|
||||||
|
|
||||||
public Dictionary<string,dynamic> Get(int? controller = null)
|
public Dictionary<string,dynamic> Get(int? controller = null)
|
||||||
{
|
{
|
||||||
var buttons = new Dictionary<string, dynamic>();
|
var buttons = new Dictionary<string, dynamic>();
|
||||||
|
@ -42,7 +51,6 @@ namespace BizHawk.Client.Common
|
||||||
return buttons;
|
return buttons;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: what about float controls?
|
|
||||||
public Dictionary<string, dynamic> GetImmediate()
|
public Dictionary<string, dynamic> GetImmediate()
|
||||||
{
|
{
|
||||||
var buttons = new Dictionary<string, dynamic>();
|
var buttons = new Dictionary<string, dynamic>();
|
||||||
|
@ -79,7 +87,7 @@ namespace BizHawk.Client.Common
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"invalid mnemonic string: {inputLogEntry}");
|
LogCallback($"invalid mnemonic string: {inputLogEntry}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
using NLua;
|
using NLua;
|
||||||
|
|
||||||
// ReSharper disable UnusedMember.Global
|
// ReSharper disable UnusedMember.Global
|
||||||
namespace BizHawk.Client.Common
|
namespace BizHawk.Client.Common
|
||||||
{
|
{
|
||||||
public sealed class JoypadLuaLibrary : LuaLibraryBase
|
public sealed class JoypadLuaLibrary : DelegatingLuaLibrary
|
||||||
{
|
{
|
||||||
public JoypadLuaLibrary(Lua lua)
|
public JoypadLuaLibrary(Lua lua)
|
||||||
: base(lua) { }
|
: base(lua) { }
|
||||||
|
@ -18,169 +20,42 @@ namespace BizHawk.Client.Common
|
||||||
[LuaMethod("get", "returns a lua table of the controller buttons pressed. If supplied, it will only return a table of buttons for the given controller")]
|
[LuaMethod("get", "returns a lua table of the controller buttons pressed. If supplied, it will only return a table of buttons for the given controller")]
|
||||||
public LuaTable Get(int? controller = null)
|
public LuaTable Get(int? controller = null)
|
||||||
{
|
{
|
||||||
var buttons = Lua.NewTable();
|
var result = APIs.Joypad.Get(controller);
|
||||||
var adapter = Global.AutofireStickyXORAdapter;
|
var table = Lua.NewTable();
|
||||||
foreach (var button in adapter.Source.Definition.BoolButtons)
|
foreach (var kvp in result) table[kvp.Key] = kvp.Value;
|
||||||
{
|
return table;
|
||||||
if (!controller.HasValue)
|
|
||||||
{
|
|
||||||
buttons[button] = adapter.IsPressed(button);
|
|
||||||
}
|
|
||||||
else if (button.Length >= 3 && button.Substring(0, 2) == $"P{controller}")
|
|
||||||
{
|
|
||||||
buttons[button.Substring(3)] = adapter.IsPressed($"P{controller} {button.Substring(3)}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var button in adapter.Source.Definition.FloatControls)
|
|
||||||
{
|
|
||||||
if (controller == null)
|
|
||||||
{
|
|
||||||
buttons[button] = adapter.GetFloat(button);
|
|
||||||
}
|
|
||||||
else if (button.Length >= 3 && button.Substring(0, 2) == $"P{controller}")
|
|
||||||
{
|
|
||||||
buttons[button.Substring(3)] = adapter.GetFloat($"P{controller} {button.Substring(3)}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
buttons["clear"] = null;
|
|
||||||
buttons["getluafunctionslist"] = null;
|
|
||||||
buttons["output"] = null;
|
|
||||||
|
|
||||||
return buttons;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: what about float controls?
|
|
||||||
[LuaMethodExample("local nljoyget = joypad.getimmediate( );")]
|
[LuaMethodExample("local nljoyget = joypad.getimmediate( );")]
|
||||||
[LuaMethod("getimmediate", "returns a lua table of any controller buttons currently pressed by the user")]
|
[LuaMethod("getimmediate", "returns a lua table of any controller buttons currently pressed by the user")]
|
||||||
public LuaTable GetImmediate()
|
public LuaTable GetImmediate()
|
||||||
{
|
{
|
||||||
var buttons = Lua.NewTable();
|
var result = APIs.Joypad.GetImmediate();
|
||||||
foreach (var button in Global.ActiveController.Definition.BoolButtons)
|
var table = Lua.NewTable();
|
||||||
{
|
foreach (var kvp in result) table[kvp.Key] = kvp.Value;
|
||||||
buttons[button] = Global.ActiveController.IsPressed(button);
|
return table;
|
||||||
}
|
|
||||||
|
|
||||||
return buttons;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[LuaMethodExample("joypad.setfrommnemonicstr( \"| 0, 0, 0, 100,...R..B....|\" );")]
|
[LuaMethodExample("joypad.setfrommnemonicstr( \"| 0, 0, 0, 100,...R..B....|\" );")]
|
||||||
[LuaMethod("setfrommnemonicstr", "sets the given buttons to their provided values for the current frame, string will be interpretted the same way an entry from a movie input log would be")]
|
[LuaMethod("setfrommnemonicstr", "sets the given buttons to their provided values for the current frame, string will be interpretted the same way an entry from a movie input log would be")]
|
||||||
public void SetFromMnemonicStr(string inputLogEntry)
|
public void SetFromMnemonicStr(string inputLogEntry) => APIs.Joypad.SetFromMnemonicStr(inputLogEntry);
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var lg = Global.MovieSession.MovieControllerInstance();
|
|
||||||
lg.SetControllersAsMnemonic(inputLogEntry);
|
|
||||||
|
|
||||||
foreach (var button in lg.Definition.BoolButtons)
|
|
||||||
{
|
|
||||||
Global.LuaAndAdaptor.SetButton(button, lg.IsPressed(button));
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var floatButton in lg.Definition.FloatControls)
|
|
||||||
{
|
|
||||||
Global.LuaAndAdaptor.SetFloat(floatButton, lg.GetFloat(floatButton));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
Log($"invalid mnemonic string: {inputLogEntry}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[LuaMethodExample("joypad.set( { [\"Left\"] = true, [ \"A\" ] = true, [ \"B\" ] = true } );")]
|
[LuaMethodExample("joypad.set( { [\"Left\"] = true, [ \"A\" ] = true, [ \"B\" ] = true } );")]
|
||||||
[LuaMethod("set", "sets the given buttons to their provided values for the current frame")]
|
[LuaMethod("set", "sets the given buttons to their provided values for the current frame")]
|
||||||
public void Set(LuaTable buttons, int? controller = null)
|
public void Set(LuaTable buttons, int? controller = null)
|
||||||
{
|
{
|
||||||
try
|
var dict = new Dictionary<string, bool>();
|
||||||
{
|
foreach (var k in buttons.Keys) dict[k.ToString()] = (bool) buttons[k];
|
||||||
foreach (var button in buttons.Keys)
|
APIs.Joypad.Set(dict, controller);
|
||||||
{
|
|
||||||
var invert = false;
|
|
||||||
bool? theValue;
|
|
||||||
var theValueStr = buttons[button].ToString();
|
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(theValueStr))
|
|
||||||
{
|
|
||||||
if (theValueStr.ToLower() == "false")
|
|
||||||
{
|
|
||||||
theValue = false;
|
|
||||||
}
|
|
||||||
else if (theValueStr.ToLower() == "true")
|
|
||||||
{
|
|
||||||
theValue = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
invert = true;
|
|
||||||
theValue = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
theValue = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var toPress = button.ToString();
|
|
||||||
if (controller.HasValue)
|
|
||||||
{
|
|
||||||
toPress = $"P{controller} {button}";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!invert)
|
|
||||||
{
|
|
||||||
if (theValue.HasValue) // Force
|
|
||||||
{
|
|
||||||
Global.LuaAndAdaptor.SetButton(toPress, theValue.Value);
|
|
||||||
Global.ActiveController.Overrides(Global.LuaAndAdaptor);
|
|
||||||
}
|
|
||||||
else // Unset
|
|
||||||
{
|
|
||||||
Global.LuaAndAdaptor.UnSet(toPress);
|
|
||||||
Global.ActiveController.Overrides(Global.LuaAndAdaptor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else // Inverse
|
|
||||||
{
|
|
||||||
Global.LuaAndAdaptor.SetInverse(toPress);
|
|
||||||
Global.ActiveController.Overrides(Global.LuaAndAdaptor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
/*Eat it*/
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[LuaMethodExample("joypad.setanalog( { [ \"Tilt X\" ] = true, [ \"Tilt Y\" ] = false } );")]
|
[LuaMethodExample("joypad.setanalog( { [ \"Tilt X\" ] = true, [ \"Tilt Y\" ] = false } );")]
|
||||||
[LuaMethod("setanalog", "sets the given analog controls to their provided values for the current frame. Note that unlike set() there is only the logic of overriding with the given value.")]
|
[LuaMethod("setanalog", "sets the given analog controls to their provided values for the current frame. Note that unlike set() there is only the logic of overriding with the given value.")]
|
||||||
public void SetAnalog(LuaTable controls, object controller = null)
|
public void SetAnalog(LuaTable controls, object controller = null)
|
||||||
{
|
{
|
||||||
try
|
var dict = new Dictionary<string, float>();
|
||||||
{
|
foreach (var k in controls.Keys) dict[k.ToString()] = (float) controls[k];
|
||||||
foreach (var name in controls.Keys)
|
APIs.Joypad.SetAnalog(dict, controller);
|
||||||
{
|
|
||||||
var theValueStr = controls[name].ToString();
|
|
||||||
float? theValue = null;
|
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(theValueStr))
|
|
||||||
{
|
|
||||||
if (float.TryParse(theValueStr, out var f))
|
|
||||||
{
|
|
||||||
theValue = f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Global.StickyXORAdapter.SetFloat(controller == null ? name.ToString() : $"P{controller} {name}", theValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
/*Eat it*/
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue