Fixes joypad.set() method in Lua Console (#1898)

* Fixed lua joypad set method to accept button inputs with "P{controller}"
designations. Also allowed joypad set method to accept 0 or 1 as per
documentation

* Added comment
This commit is contained in:
campbell000 2020-03-24 13:40:18 -04:00 committed by GitHub
parent d0cf220391
commit 373a05dcb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 2 deletions

View File

@ -44,7 +44,9 @@ namespace BizHawk.Client.Common
public void Set(Dictionary<string, bool> buttons, int? controller = null)
{
foreach (var button in Global.InputManager.ActiveController.Definition.BoolButtons)
// If a controller is specified, we need to iterate over unique button names. If not, we iterate over
// ALL button names with P{controller} prefixes
foreach (var button in Global.InputManager.ActiveController.ToBoolButtonNameList(controller))
{
Set(button, buttons.TryGetValue(button, out var state) ? state : (bool?) null, controller);
}

View File

@ -45,7 +45,7 @@ namespace BizHawk.Client.Common
public void Set(LuaTable buttons, int? controller = null)
{
var dict = new Dictionary<string, bool>();
foreach (var k in buttons.Keys) dict[k.ToString()] = (bool) buttons[k];
foreach (var k in buttons.Keys) dict[k.ToString()] = Convert.ToBoolean(buttons[k]); // Accepts 1/0 or true/false
APIs.Joypad.Set(dict, controller);
}

View File

@ -314,6 +314,47 @@ namespace BizHawk.Emulation.Common
return !info.GetCustomAttributes(false).Any(a => a is FeatureNotImplementedAttribute);
}
/// <summary>
/// Gets a list of boolean button names. If a controller number is specified, only returns button names
/// (without the "P" prefix) that match that controller number. If a controller number is NOT specified,
/// then all button names are returned.
///
/// For example, consider example "P1 A", "P1 B", "P2 A", "P2 B". See below for sample outputs:
/// - ToBoolButtonNameList(controller, 1) -> [A, B]
/// - ToBoolButtonNameList(controller, 2) -> [A, B]
/// - ToBoolButtonNameList(controller, null) -> [P1 A, P1 B, P2 A, P2 B]
/// </summary>
public static List<string> ToBoolButtonNameList(this IController controller, int? controllerNum = null)
{
return ToControlNameList(controller.Definition.BoolButtons, controllerNum);
}
/// <summary>
/// See ToBoolButtonNameList(). Works the same except with float controls
/// </summary>
public static List<string> ToFloatControlNameList(this IController controller, int? controllerNum = null)
{
return ToControlNameList(controller.Definition.FloatControls, controllerNum);
}
private static List<string> ToControlNameList(List<string> buttonList, int? controllerNum = null)
{
var buttons = new List<string>();
foreach (var button in buttonList)
{
if (controllerNum != null && button.Length > 2 && button.Substring(0, 2) == $"P{controllerNum}")
{
var sub = button.Substring(3);
buttons.Add(sub);
}
else if (controllerNum == null)
{
buttons.Add(button);
}
}
return buttons;
}
public static IDictionary<string, dynamic> ToDictionary(this IController controller, int? controllerNum = null)
{
var buttons = new Dictionary<string, dynamic>();