GTK3MouseDriver.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 Vector2 Scroll{ get; private set; }
  15. public GTK3MouseDriver(Widget parent)
  16. {
  17. _widget = parent;
  18. _widget.MotionNotifyEvent += Parent_MotionNotifyEvent;
  19. _widget.ButtonPressEvent += Parent_ButtonPressEvent;
  20. _widget.ButtonReleaseEvent += Parent_ButtonReleaseEvent;
  21. _widget.ScrollEvent += Parent_ScrollEvent;
  22. PressedButtons = new bool[(int)MouseButton.Count];
  23. }
  24. [GLib.ConnectBefore]
  25. private void Parent_ScrollEvent(object o, ScrollEventArgs args)
  26. {
  27. Scroll = new Vector2((float)args.Event.X, (float)args.Event.Y);
  28. }
  29. [GLib.ConnectBefore]
  30. private void Parent_ButtonReleaseEvent(object o, ButtonReleaseEventArgs args)
  31. {
  32. PressedButtons[args.Event.Button - 1] = false;
  33. }
  34. [GLib.ConnectBefore]
  35. private void Parent_ButtonPressEvent(object o, ButtonPressEventArgs args)
  36. {
  37. PressedButtons[args.Event.Button - 1] = true;
  38. }
  39. [GLib.ConnectBefore]
  40. private void Parent_MotionNotifyEvent(object o, MotionNotifyEventArgs args)
  41. {
  42. if (args.Event.Device.InputSource == InputSource.Mouse)
  43. {
  44. CurrentPosition = new Vector2((float)args.Event.X, (float)args.Event.Y);
  45. }
  46. }
  47. public bool IsButtonPressed(MouseButton button)
  48. {
  49. return PressedButtons[(int) button];
  50. }
  51. public Size GetClientSize()
  52. {
  53. return new Size(_widget.AllocatedWidth, _widget.AllocatedHeight);
  54. }
  55. public string DriverName => "GTK3";
  56. public event Action<string> OnGamepadConnected
  57. {
  58. add { }
  59. remove { }
  60. }
  61. public event Action<string> OnGamepadDisconnected
  62. {
  63. add { }
  64. remove { }
  65. }
  66. public ReadOnlySpan<string> GamepadsIds => new[] {"0"};
  67. public IGamepad GetGamepad(string id)
  68. {
  69. return new GTK3Mouse(this);
  70. }
  71. public void Dispose()
  72. {
  73. if (_isDisposed)
  74. {
  75. return;
  76. }
  77. _isDisposed = true;
  78. _widget.MotionNotifyEvent -= Parent_MotionNotifyEvent;
  79. _widget.ButtonPressEvent -= Parent_ButtonPressEvent;
  80. _widget.ButtonReleaseEvent -= Parent_ButtonReleaseEvent;
  81. _widget = null;
  82. }
  83. }
  84. }