From 77aab9aca302bbe635d94750f57fb9a1ad910b74 Mon Sep 17 00:00:00 2001 From: emmauss Date: Thu, 24 Jun 2021 00:09:08 +0000 Subject: Add Direct Mouse Support (#2374) * and direct mouse support * and direct mouse support * hide cursor if mouse enabled * add config * update docs * sorted usings --- Ryujinx.Input/HLE/InputManager.cs | 2 +- Ryujinx.Input/HLE/NpadManager.cs | 67 +++++++++++++++++++++++++++------ Ryujinx.Input/HLE/TouchScreenManager.cs | 4 +- Ryujinx.Input/IMouse.cs | 12 ++++-- Ryujinx.Input/MouseStateSnapshot.cs | 15 +++++++- 5 files changed, 79 insertions(+), 21 deletions(-) (limited to 'Ryujinx.Input') diff --git a/Ryujinx.Input/HLE/InputManager.cs b/Ryujinx.Input/HLE/InputManager.cs index 699e521d..bc38cf5a 100644 --- a/Ryujinx.Input/HLE/InputManager.cs +++ b/Ryujinx.Input/HLE/InputManager.cs @@ -23,7 +23,7 @@ namespace Ryujinx.Input.HLE public NpadManager CreateNpadManager() { - return new NpadManager(KeyboardDriver, GamepadDriver); + return new NpadManager(KeyboardDriver, GamepadDriver, MouseDriver); } public TouchScreenManager CreateTouchScreenManager() diff --git a/Ryujinx.Input/HLE/NpadManager.cs b/Ryujinx.Input/HLE/NpadManager.cs index abb820b0..c46f80b0 100644 --- a/Ryujinx.Input/HLE/NpadManager.cs +++ b/Ryujinx.Input/HLE/NpadManager.cs @@ -1,4 +1,4 @@ -using Ryujinx.Common.Configuration.Hid; +using Ryujinx.Common.Configuration.Hid; using Ryujinx.Common.Configuration.Hid.Controller; using Ryujinx.Common.Configuration.Hid.Keyboard; using Ryujinx.HLE.HOS.Services.Hid; @@ -6,7 +6,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.CompilerServices; - using CemuHookClient = Ryujinx.Input.Motion.CemuHook.Client; using Switch = Ryujinx.HLE.Switch; @@ -26,22 +25,23 @@ namespace Ryujinx.Input.HLE private readonly IGamepadDriver _keyboardDriver; private readonly IGamepadDriver _gamepadDriver; - + private readonly IGamepadDriver _mouseDriver; private bool _isDisposed; private List _inputConfig; private bool _enableKeyboard; + private bool _enableMouse; private Switch _device; - public NpadManager(IGamepadDriver keyboardDriver, IGamepadDriver gamepadDriver) + public NpadManager(IGamepadDriver keyboardDriver, IGamepadDriver gamepadDriver, IGamepadDriver mouseDriver) { _controllers = new NpadController[MaxControllers]; _cemuHookClient = new CemuHookClient(this); _keyboardDriver = keyboardDriver; _gamepadDriver = gamepadDriver; + _mouseDriver = mouseDriver; _inputConfig = new List(); - _enableKeyboard = false; _gamepadDriver.OnGamepadConnected += HandleOnGamepadConnected; _gamepadDriver.OnGamepadDisconnected += HandleOnGamepadDisconnected; @@ -58,13 +58,13 @@ namespace Ryujinx.Input.HLE private void HandleOnGamepadDisconnected(string obj) { // Force input reload - ReloadConfiguration(_inputConfig, _enableKeyboard); + ReloadConfiguration(_inputConfig, _enableKeyboard, _enableMouse); } private void HandleOnGamepadConnected(string id) { // Force input reload - ReloadConfiguration(_inputConfig, _enableKeyboard); + ReloadConfiguration(_inputConfig, _enableKeyboard, _enableMouse); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -93,7 +93,7 @@ namespace Ryujinx.Input.HLE } } - public void ReloadConfiguration(List inputConfig, bool enableKeyboard) + public void ReloadConfiguration(List inputConfig, bool enableKeyboard, bool enableMouse) { lock (_lock) { @@ -119,8 +119,9 @@ namespace Ryujinx.Input.HLE } } - _inputConfig = inputConfig; + _inputConfig = inputConfig; _enableKeyboard = enableKeyboard; + _enableMouse = enableMouse; _device.Hid.RefreshInputConfig(inputConfig); } @@ -142,15 +143,15 @@ namespace Ryujinx.Input.HLE } } - public void Initialize(Switch device, List inputConfig, bool enableKeyboard) + public void Initialize(Switch device, List inputConfig, bool enableKeyboard, bool enableMouse) { _device = device; _device.Configuration.RefreshInputConfig = RefreshInputConfigForHLE; - ReloadConfiguration(inputConfig, enableKeyboard); + ReloadConfiguration(inputConfig, enableKeyboard, enableMouse); } - public void Update() + public void Update(float aspectRatio = 0) { lock (_lock) { @@ -206,6 +207,48 @@ namespace Ryujinx.Input.HLE _device.Hid.Keyboard.Update(hleKeyboardInput.Value); } + if (_enableMouse) + { + var mouse = _mouseDriver.GetGamepad("0") as IMouse; + + var mouseInput = IMouse.GetMouseStateSnapshot(mouse); + + uint buttons = 0; + + if (mouseInput.IsPressed(MouseButton.Button1)) + { + buttons |= 1 << 0; + } + + if (mouseInput.IsPressed(MouseButton.Button2)) + { + buttons |= 1 << 1; + } + + if (mouseInput.IsPressed(MouseButton.Button3)) + { + buttons |= 1 << 2; + } + + if (mouseInput.IsPressed(MouseButton.Button4)) + { + buttons |= 1 << 3; + } + + if (mouseInput.IsPressed(MouseButton.Button5)) + { + buttons |= 1 << 4; + } + + var position = IMouse.GetScreenPosition(mouseInput.Position, mouse.ClientSize, aspectRatio); + + _device.Hid.Mouse.Update((int)position.X, (int)position.Y, buttons, (int)mouseInput.Scroll.X, (int)mouseInput.Scroll.Y, true); + } + else + { + _device.Hid.Mouse.Update(0, 0); + } + _device.TamperMachine.UpdateInput(hleInputStates); } } diff --git a/Ryujinx.Input/HLE/TouchScreenManager.cs b/Ryujinx.Input/HLE/TouchScreenManager.cs index 579dcd74..e4b0f8fc 100644 --- a/Ryujinx.Input/HLE/TouchScreenManager.cs +++ b/Ryujinx.Input/HLE/TouchScreenManager.cs @@ -29,7 +29,7 @@ namespace Ryujinx.Input.HLE if (_wasClicking && !isClicking) { MouseStateSnapshot snapshot = IMouse.GetMouseStateSnapshot(_mouse); - var touchPosition = IMouse.GetTouchPosition(snapshot.Position, _mouse.ClientSize, aspectRatio); + var touchPosition = IMouse.GetScreenPosition(snapshot.Position, _mouse.ClientSize, aspectRatio); TouchPoint currentPoint = new TouchPoint { @@ -58,7 +58,7 @@ namespace Ryujinx.Input.HLE if (aspectRatio > 0) { MouseStateSnapshot snapshot = IMouse.GetMouseStateSnapshot(_mouse); - var touchPosition = IMouse.GetTouchPosition(snapshot.Position, _mouse.ClientSize, aspectRatio); + var touchPosition = IMouse.GetScreenPosition(snapshot.Position, _mouse.ClientSize, aspectRatio); TouchAttribute attribute = TouchAttribute.None; diff --git a/Ryujinx.Input/IMouse.cs b/Ryujinx.Input/IMouse.cs index 37de0229..fde150fc 100644 --- a/Ryujinx.Input/IMouse.cs +++ b/Ryujinx.Input/IMouse.cs @@ -23,6 +23,11 @@ namespace Ryujinx.Input /// Vector2 GetPosition(); + /// + /// Get the mouse scroll delta. + /// + Vector2 GetScroll(); + /// /// Get the client size. /// @@ -40,22 +45,21 @@ namespace Ryujinx.Input /// A snaphost of the state of the mouse. public static MouseStateSnapshot GetMouseStateSnapshot(IMouse mouse) { - var position = mouse.GetPosition(); bool[] buttons = new bool[(int)MouseButton.Count]; mouse.Buttons.CopyTo(buttons, 0); - return new MouseStateSnapshot(buttons, position); + return new MouseStateSnapshot(buttons, mouse.GetPosition(), mouse.GetScroll()); } /// - /// Get the touch position of a mouse position relative to the app's view + /// Get the position of a mouse on screen relative to the app's view /// /// The position of the mouse in the client /// The size of the client /// The aspect ratio of the view /// A snaphost of the state of the mouse. - public static Vector2 GetTouchPosition(Vector2 mousePosition, Size clientSize, float aspectRatio) + public static Vector2 GetScreenPosition(Vector2 mousePosition, Size clientSize, float aspectRatio) { float mouseX = mousePosition.X; float mouseY = mousePosition.Y; diff --git a/Ryujinx.Input/MouseStateSnapshot.cs b/Ryujinx.Input/MouseStateSnapshot.cs index 4fbfeebd..ddfdebc6 100644 --- a/Ryujinx.Input/MouseStateSnapshot.cs +++ b/Ryujinx.Input/MouseStateSnapshot.cs @@ -10,17 +10,28 @@ namespace Ryujinx.Input { private bool[] _buttonState; + /// + /// The position of the mouse cursor + /// public Vector2 Position { get; } + /// + /// The scroll delta of the mouse + /// + public Vector2 Scroll { get; } + /// /// Create a new . /// - /// The keys state - public MouseStateSnapshot(bool[] buttonState, Vector2 position) + /// The button state + /// The position of the cursor + /// The scroll delta + public MouseStateSnapshot(bool[] buttonState, Vector2 position, Vector2 scroll) { _buttonState = buttonState; Position = position; + Scroll = scroll; } /// -- cgit v1.2.3