GTK3MouseDriver.cs 2.5 KB

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