aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Input/IMouse.cs
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/IMouse.cs
parentb898bc84ced7fe5b77e2018ce2ccb7389933b7cf (diff)
Add TouchScreen Manager (#2333)
Diffstat (limited to 'Ryujinx.Input/IMouse.cs')
-rw-r--r--Ryujinx.Input/IMouse.cs100
1 files changed, 100 insertions, 0 deletions
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