aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Input
diff options
context:
space:
mode:
authoremmauss <emmausssss@gmail.com>2021-06-14 06:42:55 +0000
committerGitHub <noreply@github.com>2021-06-14 08:42:55 +0200
commitbfcc6a8ad63a0eb0f06968145a310c484a96e8ca (patch)
treea3acfa8364d43cd960f5cb559c109f6be863ba2c /Ryujinx.Input
parentb898bc84ced7fe5b77e2018ce2ccb7389933b7cf (diff)
Add TouchScreen Manager (#2333)
Diffstat (limited to 'Ryujinx.Input')
-rw-r--r--Ryujinx.Input/HLE/InputManager.cs19
-rw-r--r--Ryujinx.Input/HLE/TouchScreenManager.cs57
-rw-r--r--Ryujinx.Input/IMouse.cs100
-rw-r--r--Ryujinx.Input/MouseButton.cs16
-rw-r--r--Ryujinx.Input/MouseStateSnapshot.cs34
5 files changed, 226 insertions, 0 deletions
diff --git a/Ryujinx.Input/HLE/InputManager.cs b/Ryujinx.Input/HLE/InputManager.cs
index 277e8ec2..699e521d 100644
--- a/Ryujinx.Input/HLE/InputManager.cs
+++ b/Ryujinx.Input/HLE/InputManager.cs
@@ -6,6 +6,7 @@ namespace Ryujinx.Input.HLE
{
public IGamepadDriver KeyboardDriver { get; private set; }
public IGamepadDriver GamepadDriver { get; private set; }
+ public IGamepadDriver MouseDriver { get; private set; }
public InputManager(IGamepadDriver keyboardDriver, IGamepadDriver gamepadDriver)
{
@@ -13,10 +14,27 @@ namespace Ryujinx.Input.HLE
GamepadDriver = gamepadDriver;
}
+ public void SetMouseDriver(IGamepadDriver mouseDriver)
+ {
+ MouseDriver?.Dispose();
+
+ MouseDriver = mouseDriver;
+ }
+
public NpadManager CreateNpadManager()
{
return new NpadManager(KeyboardDriver, GamepadDriver);
}
+
+ public TouchScreenManager CreateTouchScreenManager()
+ {
+ if (MouseDriver == null)
+ {
+ throw new InvalidOperationException("Mouse Driver has not been initialized.");
+ }
+
+ return new TouchScreenManager(MouseDriver.GetGamepad("0") as IMouse);
+ }
protected virtual void Dispose(bool disposing)
{
@@ -24,6 +42,7 @@ namespace Ryujinx.Input.HLE
{
KeyboardDriver?.Dispose();
GamepadDriver?.Dispose();
+ MouseDriver?.Dispose();
}
}
diff --git a/Ryujinx.Input/HLE/TouchScreenManager.cs b/Ryujinx.Input/HLE/TouchScreenManager.cs
new file mode 100644
index 00000000..ffa8eeac
--- /dev/null
+++ b/Ryujinx.Input/HLE/TouchScreenManager.cs
@@ -0,0 +1,57 @@
+using Ryujinx.HLE;
+using Ryujinx.HLE.HOS.Services.Hid;
+using System;
+
+namespace Ryujinx.Input.HLE
+{
+ public class TouchScreenManager : IDisposable
+ {
+ private readonly IMouse _mouse;
+ private Switch _device;
+
+ public TouchScreenManager(IMouse mouse)
+ {
+ _mouse = mouse;
+ }
+
+ public void Initialize(Switch device)
+ {
+ _device = device;
+ }
+
+ public bool Update(bool isFocused, float aspectRatio = 0)
+ {
+ if (!isFocused)
+ {
+ _device.Hid.Touchscreen.Update();
+
+ return false;
+ }
+
+ if (aspectRatio > 0)
+ {
+ var snapshot = IMouse.GetMouseStateSnapshot(_mouse);
+ var touchPosition = IMouse.GetTouchPosition(snapshot.Position, _mouse.ClientSize, aspectRatio);
+
+ TouchPoint currentPoint = new TouchPoint
+ {
+ X = (uint)touchPosition.X,
+ Y = (uint)touchPosition.Y,
+
+ // Placeholder values till more data is acquired
+ DiameterX = 10,
+ DiameterY = 10,
+ Angle = 90
+ };
+
+ _device.Hid.Touchscreen.Update(currentPoint);
+
+ return true;
+ }
+
+ return false;
+ }
+
+ public void Dispose() { }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.Input/IMouse.cs b/Ryujinx.Input/IMouse.cs
new file mode 100644
index 00000000..37de0229
--- /dev/null
+++ b/Ryujinx.Input/IMouse.cs
@@ -0,0 +1,100 @@
+using System.Drawing;
+using System.Numerics;
+
+namespace Ryujinx.Input
+{
+ /// <summary>
+ /// Represent an emulated mouse.
+ /// </summary>
+ public interface IMouse : IGamepad
+ {
+ private const int SwitchPanelWidth = 1280;
+ private const int SwitchPanelHeight = 720;
+
+ /// <summary>
+ /// Check if a given button is pressed on the mouse.
+ /// </summary>
+ /// <param name="button">The button</param>
+ /// <returns>True if the given button is pressed on the mouse</returns>
+ bool IsButtonPressed(MouseButton button);
+
+ /// <summary>
+ /// Get the position of the mouse in the client.
+ /// </summary>
+ Vector2 GetPosition();
+
+ /// <summary>
+ /// Get the client size.
+ /// </summary>
+ Size ClientSize { get; }
+
+ /// <summary>
+ /// Get the button states of the mouse.
+ /// </summary>
+ bool[] Buttons { get; }
+
+ /// <summary>
+ /// Get a snaphost of the state of a mouse.
+ /// </summary>
+ /// <param name="mouse">The mouse to do a snapshot of</param>
+ /// <returns>A snaphost of the state of the mouse.</returns>
+ 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);
+ }
+
+ /// <summary>
+ /// Get the touch position of a mouse position relative to the app's view
+ /// </summary>
+ /// <param name="mousePosition">The position of the mouse in the client</param>
+ /// <param name="clientSize">The size of the client</param>
+ /// <param name="aspectRatio">The aspect ratio of the view</param>
+ /// <returns>A snaphost of the state of the mouse.</returns>
+ public static Vector2 GetTouchPosition(Vector2 mousePosition, Size clientSize, float aspectRatio)
+ {
+ float mouseX = mousePosition.X;
+ float mouseY = mousePosition.Y;
+
+ float aspectWidth = SwitchPanelHeight * aspectRatio;
+
+ int screenWidth = clientSize.Width;
+ int screenHeight = clientSize.Height;
+
+ if (clientSize.Width > clientSize.Height * aspectWidth / SwitchPanelHeight)
+ {
+ screenWidth = (int)(clientSize.Height * aspectWidth) / SwitchPanelHeight;
+ }
+ else
+ {
+ screenHeight = (clientSize.Width * SwitchPanelHeight) / (int)aspectWidth;
+ }
+
+ int startX = (clientSize.Width - screenWidth) >> 1;
+ int startY = (clientSize.Height - screenHeight) >> 1;
+
+ int endX = startX + screenWidth;
+ int endY = startY + screenHeight;
+
+ if (mouseX >= startX &&
+ mouseY >= startY &&
+ mouseX < endX &&
+ mouseY < endY)
+ {
+ int screenMouseX = (int)mouseX - startX;
+ int screenMouseY = (int)mouseY - startY;
+
+ mouseX = (screenMouseX * (int)aspectWidth) / screenWidth;
+ mouseY = (screenMouseY * SwitchPanelHeight) / screenHeight;
+
+ return new Vector2(mouseX, mouseY);
+ }
+
+ return new Vector2();
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.Input/MouseButton.cs b/Ryujinx.Input/MouseButton.cs
new file mode 100644
index 00000000..ab764216
--- /dev/null
+++ b/Ryujinx.Input/MouseButton.cs
@@ -0,0 +1,16 @@
+namespace Ryujinx.Input
+{
+ public enum MouseButton : byte
+ {
+ Button1,
+ Button2,
+ Button3,
+ Button4,
+ Button5,
+ Button6,
+ Button7,
+ Button8,
+ Button9,
+ Count
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.Input/MouseStateSnapshot.cs b/Ryujinx.Input/MouseStateSnapshot.cs
new file mode 100644
index 00000000..4fbfeebd
--- /dev/null
+++ b/Ryujinx.Input/MouseStateSnapshot.cs
@@ -0,0 +1,34 @@
+using System.Numerics;
+using System.Runtime.CompilerServices;
+
+namespace Ryujinx.Input
+{
+ /// <summary>
+ /// A snapshot of a <see cref="IMouse"/>.
+ /// </summary>
+ public class MouseStateSnapshot
+ {
+ private bool[] _buttonState;
+
+ public Vector2 Position { get; }
+
+ /// <summary>
+ /// Create a new <see cref="MouseStateSnapshot"/>.
+ /// </summary>
+ /// <param name="buttonState">The keys state</param>
+ public MouseStateSnapshot(bool[] buttonState, Vector2 position)
+ {
+ _buttonState = buttonState;
+
+ Position = position;
+ }
+
+ /// <summary>
+ /// Check if a given button is pressed.
+ /// </summary>
+ /// <param name="button">The button</param>
+ /// <returns>True if the given button is pressed</returns>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public bool IsPressed(MouseButton button) => _buttonState[(int)button];
+ }
+} \ No newline at end of file