diff --git a/src/BizHawk.Client.Common/Api/Classes/InputApi.cs b/src/BizHawk.Client.Common/Api/Classes/InputApi.cs index 19951ff5ba..9b7d2676c4 100644 --- a/src/BizHawk.Client.Common/Api/Classes/InputApi.cs +++ b/src/BizHawk.Client.Common/Api/Classes/InputApi.cs @@ -24,7 +24,7 @@ namespace BizHawk.Client.Common return buttons; } - public Dictionary GetMouse() + public IReadOnlyDictionary GetMouse() { var (pos, scroll, lmb, mmb, rmb, x1mb, x2mb) = _inputManager.GetMainFormMouseInfo(); // TODO - need to specify whether in "emu" or "native" coordinate space. @@ -41,5 +41,13 @@ namespace BizHawk.Client.Common ["Wheel"] = scroll }; } + + public IReadOnlyDictionary GetPressedAxes() + => _inputManager.ControllerInputCoalescer.AxisValues().ToDictionary(static kvp => kvp.Key, static kvp => kvp.Value); + + public IReadOnlyList GetPressedButtons() + => _inputManager.ControllerInputCoalescer.BoolButtons().Where(static kvp => kvp.Value) + .Select(static kvp => kvp.Key).ToList(); } } + diff --git a/src/BizHawk.Client.Common/Api/Interfaces/IInputApi.cs b/src/BizHawk.Client.Common/Api/Interfaces/IInputApi.cs index e756540fe3..6a1ffe9c88 100644 --- a/src/BizHawk.Client.Common/Api/Interfaces/IInputApi.cs +++ b/src/BizHawk.Client.Common/Api/Interfaces/IInputApi.cs @@ -1,10 +1,58 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace BizHawk.Client.Common { + /// for querying host input + /// public interface IInputApi : IExternalApi { + /// + /// Map of key/button names (of host) to their pressed state.
+ /// Only pressed buttons will appear (with a value of ), unpressed buttons are omitted. + ///
+ /// + /// Includes gamepad axes (!axis.isNeutral, with sticks as 4 "buttons" suffixed "Up"/"Down"/"Left"/"Right").
+ /// Includes mouse buttons, but not axes (cursor position and wheel rotation). + /// Unlike , these have the names "WMouse L", "WMouse R", "WMouse C", "WMouse 1", and "WMouse 2" for LMB, RMB, MMB, Mouse4, and Mouse5, respectively.
+ /// See for keyboard key names, though some are overridden by (check the source). + ///
+ /// + [Obsolete] Dictionary Get(); - Dictionary GetMouse(); + + /// + /// Map of (host) mouse button/axis names to their state: + /// either for a button (button.isPressed), or for an axis. + /// + /// + /// Buttons are "Left", "Right", "Middle", "XButton1", and "XButton2" for LMB, RMB, MMB, Mouse4, and Mouse5, respectively. + /// Mouse position is axes "X" and "Y". Mouse wheel rotation is the "Wheel" axis. + /// + IReadOnlyDictionary GetMouse(); + + /// + /// Map of (host) axis names to their state.
+ /// Axes may not appear if they have never been seen with a value other than 0 + /// (for example, if the gamepad has been set down on a table since launch, or if it was recently reconnected). + ///
+ /// + /// Includes mouse cursor position axes, but not mouse wheel rotation. + /// Unlike , these have the names "WMouse X" and "WMouse Y". + /// + IReadOnlyDictionary GetPressedAxes(); + + /// + /// List of (host) key/button names which are pressed + /// (i.e. were pressed when EmuHawk last polled; this is distinct from virtual gamepad polling/latching). + /// Unpressed buttons are omitted. + /// + /// + /// Includes gamepad axes (!axis.isNeutral, with sticks as 4 "buttons" suffixed "Up"/"Down"/"Left"/"Right").
+ /// Includes mouse buttons, but not axes (cursor position and wheel rotation). + /// Unlike , these have the names "WMouse L", "WMouse R", "WMouse C", "WMouse 1", and "WMouse 2" for LMB, RMB, MMB, Mouse4, and Mouse5, respectively.
+ /// See for keyboard key names, though some are overridden by (check the source). + ///
+ IReadOnlyList GetPressedButtons(); } } diff --git a/src/BizHawk.Client.Common/Api/Interfaces/IJoypadApi.cs b/src/BizHawk.Client.Common/Api/Interfaces/IJoypadApi.cs index a337681554..105b0e4be9 100644 --- a/src/BizHawk.Client.Common/Api/Interfaces/IJoypadApi.cs +++ b/src/BizHawk.Client.Common/Api/Interfaces/IJoypadApi.cs @@ -2,6 +2,8 @@ namespace BizHawk.Client.Common { + /// for querying or modifying virtual input + /// public interface IJoypadApi : IExternalApi { IReadOnlyDictionary Get(int? controller = null); diff --git a/src/BizHawk.Client.Common/controllers/SimpleController.cs b/src/BizHawk.Client.Common/controllers/SimpleController.cs index 9eae0f18f9..5954279425 100644 --- a/src/BizHawk.Client.Common/controllers/SimpleController.cs +++ b/src/BizHawk.Client.Common/controllers/SimpleController.cs @@ -44,7 +44,9 @@ namespace BizHawk.Client.Common public void SetHapticChannelStrength(string name, int strength) => HapticFeedback[name] = strength; - public IDictionary BoolButtons() => Buttons; + public IReadOnlyDictionary AxisValues() => Axes; + + public IReadOnlyDictionary BoolButtons() => Buttons; public void AcceptNewAxis(string axisId, int value) { diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/InputLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/InputLuaLibrary.cs index 77a2c8aa76..c04d6d8daa 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/InputLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/InputLuaLibrary.cs @@ -11,10 +11,12 @@ namespace BizHawk.Client.Common public override string Name => "input"; +#pragma warning disable CS0612 [LuaMethodExample("local nlinpget = input.get( );")] [LuaMethod("get", "Returns a lua table of all the buttons the user is currently pressing on their keyboard and gamepads\nAll buttons that are pressed have their key values set to true; all others remain nil.")] public LuaTable Get() => _th.DictToTable(APIs.Input.Get()); +#pragma warning restore CS0612 [LuaMethodExample("local nlinpget = input.getmouse( );")] [LuaMethod("getmouse", "Returns a lua table of the mouse X/Y coordinates and button states. Table keys are X, Y, Left, Middle, Right, XButton1, XButton2, Wheel.")]