aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Headless.SDL2/SDL2MouseDriver.cs
diff options
context:
space:
mode:
authorMary <me@thog.eu>2021-07-06 22:08:44 +0200
committerGitHub <noreply@github.com>2021-07-06 22:08:44 +0200
commit31cbd09a75a9d5f4814c3907a060e0961eb2bb15 (patch)
tree094863555b29fcb254e023e9bf9a46f929b04fc2 /Ryujinx.Headless.SDL2/SDL2MouseDriver.cs
parentd125fce3e8c780c042040ac8064155cd6751d353 (diff)
frontend: Add a SDL2 headless window (#2310)
Diffstat (limited to 'Ryujinx.Headless.SDL2/SDL2MouseDriver.cs')
-rw-r--r--Ryujinx.Headless.SDL2/SDL2MouseDriver.cs104
1 files changed, 104 insertions, 0 deletions
diff --git a/Ryujinx.Headless.SDL2/SDL2MouseDriver.cs b/Ryujinx.Headless.SDL2/SDL2MouseDriver.cs
new file mode 100644
index 00000000..24f8e864
--- /dev/null
+++ b/Ryujinx.Headless.SDL2/SDL2MouseDriver.cs
@@ -0,0 +1,104 @@
+using Ryujinx.Common.Logging;
+using Ryujinx.Input;
+using System;
+using System.Diagnostics;
+using System.Drawing;
+using System.Numerics;
+using System.Runtime.CompilerServices;
+using static SDL2.SDL;
+
+namespace Ryujinx.Headless.SDL2
+{
+ class SDL2MouseDriver : IGamepadDriver
+ {
+ private bool _isDisposed;
+
+ public bool[] PressedButtons { get; }
+
+ public Vector2 CurrentPosition { get; private set; }
+ public Vector2 Scroll { get; private set; }
+ public Size _clientSize;
+
+ public SDL2MouseDriver()
+ {
+ PressedButtons = new bool[(int)MouseButton.Count];
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private static MouseButton DriverButtonToMouseButton(uint rawButton)
+ {
+ Debug.Assert(rawButton > 0 && rawButton <= (int)MouseButton.Count);
+
+ return (MouseButton)(rawButton - 1);
+ }
+
+ public void Update(SDL_Event evnt)
+ {
+ if (evnt.type == SDL_EventType.SDL_MOUSEBUTTONDOWN || evnt.type == SDL_EventType.SDL_MOUSEBUTTONUP)
+ {
+ uint rawButton = evnt.button.button;
+
+ if (rawButton > 0 && rawButton <= (int)MouseButton.Count)
+ {
+ PressedButtons[(int)DriverButtonToMouseButton(rawButton)] = evnt.type == SDL_EventType.SDL_MOUSEBUTTONDOWN;
+
+ CurrentPosition = new Vector2(evnt.button.x, evnt.button.y);
+ }
+ }
+ else if (evnt.type == SDL_EventType.SDL_MOUSEMOTION)
+ {
+ CurrentPosition = new Vector2(evnt.motion.x, evnt.motion.y);
+ }
+ else if (evnt.type == SDL_EventType.SDL_MOUSEWHEEL)
+ {
+ Scroll = new Vector2(evnt.wheel.x, evnt.wheel.y);
+ }
+ }
+
+ public void SetClientSize(int width, int height)
+ {
+ _clientSize = new Size(width, height);
+ }
+
+ public bool IsButtonPressed(MouseButton button)
+ {
+ return PressedButtons[(int)button];
+ }
+
+ public Size GetClientSize()
+ {
+ return _clientSize;
+ }
+
+ public string DriverName => "SDL2";
+
+ public event Action<string> OnGamepadConnected
+ {
+ add { }
+ remove { }
+ }
+
+ public event Action<string> OnGamepadDisconnected
+ {
+ add { }
+ remove { }
+ }
+
+ public ReadOnlySpan<string> GamepadsIds => new[] { "0" };
+
+ public IGamepad GetGamepad(string id)
+ {
+ return new SDL2Mouse(this);
+ }
+
+ public void Dispose()
+ {
+ if (_isDisposed)
+ {
+ return;
+ }
+
+ _isDisposed = true;
+ }
+ }
+}