Add TouchScreen Manager (#2333)
This commit is contained in:
parent
b898bc84ce
commit
bfcc6a8ad6
8 changed files with 418 additions and 83 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
57
Ryujinx.Input/HLE/TouchScreenManager.cs
Normal file
57
Ryujinx.Input/HLE/TouchScreenManager.cs
Normal file
|
@ -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() { }
|
||||
}
|
||||
}
|
100
Ryujinx.Input/IMouse.cs
Normal file
100
Ryujinx.Input/IMouse.cs
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
16
Ryujinx.Input/MouseButton.cs
Normal file
16
Ryujinx.Input/MouseButton.cs
Normal file
|
@ -0,0 +1,16 @@
|
|||
namespace Ryujinx.Input
|
||||
{
|
||||
public enum MouseButton : byte
|
||||
{
|
||||
Button1,
|
||||
Button2,
|
||||
Button3,
|
||||
Button4,
|
||||
Button5,
|
||||
Button6,
|
||||
Button7,
|
||||
Button8,
|
||||
Button9,
|
||||
Count
|
||||
}
|
||||
}
|
34
Ryujinx.Input/MouseStateSnapshot.cs
Normal file
34
Ryujinx.Input/MouseStateSnapshot.cs
Normal file
|
@ -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];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue