/ src / Ryujinx.Gtk3 / Input / GTK3 / GTK3MouseDriver.cs
GTK3MouseDriver.cs
  1  using Gdk;
  2  using Gtk;
  3  using System;
  4  using System.Numerics;
  5  using Size = System.Drawing.Size;
  6  
  7  namespace Ryujinx.Input.GTK3
  8  {
  9      public class GTK3MouseDriver : IGamepadDriver
 10      {
 11          private Widget _widget;
 12          private bool _isDisposed;
 13  
 14          public bool[] PressedButtons { get; }
 15  
 16          public Vector2 CurrentPosition { get; private set; }
 17          public Vector2 Scroll { get; private set; }
 18  
 19          public GTK3MouseDriver(Widget parent)
 20          {
 21              _widget = parent;
 22  
 23              _widget.MotionNotifyEvent += Parent_MotionNotifyEvent;
 24              _widget.ButtonPressEvent += Parent_ButtonPressEvent;
 25              _widget.ButtonReleaseEvent += Parent_ButtonReleaseEvent;
 26              _widget.ScrollEvent += Parent_ScrollEvent;
 27  
 28              PressedButtons = new bool[(int)MouseButton.Count];
 29          }
 30  
 31  
 32          [GLib.ConnectBefore]
 33          private void Parent_ScrollEvent(object o, ScrollEventArgs args)
 34          {
 35              Scroll = new Vector2((float)args.Event.X, (float)args.Event.Y);
 36          }
 37  
 38          [GLib.ConnectBefore]
 39          private void Parent_ButtonReleaseEvent(object o, ButtonReleaseEventArgs args)
 40          {
 41              PressedButtons[args.Event.Button - 1] = false;
 42          }
 43  
 44          [GLib.ConnectBefore]
 45          private void Parent_ButtonPressEvent(object o, ButtonPressEventArgs args)
 46          {
 47              PressedButtons[args.Event.Button - 1] = true;
 48          }
 49  
 50          [GLib.ConnectBefore]
 51          private void Parent_MotionNotifyEvent(object o, MotionNotifyEventArgs args)
 52          {
 53              if (args.Event.Device.InputSource == InputSource.Mouse)
 54              {
 55                  CurrentPosition = new Vector2((float)args.Event.X, (float)args.Event.Y);
 56              }
 57          }
 58  
 59          public bool IsButtonPressed(MouseButton button)
 60          {
 61              return PressedButtons[(int)button];
 62          }
 63  
 64          public Size GetClientSize()
 65          {
 66              return new Size(_widget.AllocatedWidth, _widget.AllocatedHeight);
 67          }
 68  
 69          public string DriverName => "GTK3";
 70  
 71          public event Action<string> OnGamepadConnected
 72          {
 73              add { }
 74              remove { }
 75          }
 76  
 77          public event Action<string> OnGamepadDisconnected
 78          {
 79              add { }
 80              remove { }
 81          }
 82  
 83          public ReadOnlySpan<string> GamepadsIds => new[] { "0" };
 84  
 85          public IGamepad GetGamepad(string id)
 86          {
 87              return new GTK3Mouse(this);
 88          }
 89  
 90          public void Dispose()
 91          {
 92              if (_isDisposed)
 93              {
 94                  return;
 95              }
 96  
 97              GC.SuppressFinalize(this);
 98  
 99              _isDisposed = true;
100  
101              _widget.MotionNotifyEvent -= Parent_MotionNotifyEvent;
102              _widget.ButtonPressEvent -= Parent_ButtonPressEvent;
103              _widget.ButtonReleaseEvent -= Parent_ButtonReleaseEvent;
104  
105              _widget = null;
106          }
107      }
108  }