AvaloniaMouseDriver.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Avalonia.Controls;
  2. using Avalonia.Input;
  3. using Avalonia.Threading;
  4. using Ryujinx.Input;
  5. using System;
  6. using System.Numerics;
  7. using MouseButton = Ryujinx.Input.MouseButton;
  8. using Size = System.Drawing.Size;
  9. namespace Ryujinx.Ava.Input
  10. {
  11. public class AvaloniaMouseDriver : IGamepadDriver
  12. {
  13. private Control _widget;
  14. private bool _isDisposed;
  15. public bool[] PressedButtons { get; }
  16. public Vector2 CurrentPosition { get; private set; }
  17. public Vector2 Scroll { get; private set; }
  18. public AvaloniaMouseDriver(Control parent)
  19. {
  20. _widget = parent;
  21. _widget.PointerMoved += Parent_PointerMovedEvent;
  22. _widget.PointerPressed += Parent_PointerPressEvent;
  23. _widget.PointerReleased += Parent_PointerReleaseEvent;
  24. _widget.PointerWheelChanged += Parent_ScrollEvent;
  25. PressedButtons = new bool[(int)MouseButton.Count];
  26. }
  27. private void Parent_ScrollEvent(object o, PointerWheelEventArgs args)
  28. {
  29. Scroll = new Vector2((float)args.Delta.X, (float)args.Delta.Y);
  30. }
  31. private void Parent_PointerReleaseEvent(object o, PointerReleasedEventArgs args)
  32. {
  33. var pointerProperties = args.GetCurrentPoint(_widget).Properties;
  34. PressedButtons[(int)args.InitialPressMouseButton - 1] = false;
  35. }
  36. private void Parent_PointerPressEvent(object o, PointerPressedEventArgs args)
  37. {
  38. var pointerProperties = args.GetCurrentPoint(_widget).Properties;
  39. PressedButtons[(int)pointerProperties.PointerUpdateKind] = true;
  40. }
  41. private void Parent_PointerMovedEvent(object o, PointerEventArgs args)
  42. {
  43. var position = args.GetPosition(_widget);
  44. CurrentPosition = new Vector2((float)position.X, (float)position.Y);
  45. }
  46. public void SetMousePressed(MouseButton button)
  47. {
  48. PressedButtons[(int)button] = true;
  49. }
  50. public void SetMouseReleased(MouseButton button)
  51. {
  52. PressedButtons[(int)button] = false;
  53. }
  54. public void SetPosition(double x, double y)
  55. {
  56. CurrentPosition = new Vector2((float)x, (float)y);
  57. }
  58. public bool IsButtonPressed(MouseButton button)
  59. {
  60. return PressedButtons[(int)button];
  61. }
  62. public Size GetClientSize()
  63. {
  64. Size size = new();
  65. Dispatcher.UIThread.InvokeAsync(() =>
  66. {
  67. size = new Size((int)_widget.Bounds.Width, (int)_widget.Bounds.Height);
  68. }).Wait();
  69. return size;
  70. }
  71. public string DriverName => "Avalonia";
  72. public event Action<string> OnGamepadConnected
  73. {
  74. add { }
  75. remove { }
  76. }
  77. public event Action<string> OnGamepadDisconnected
  78. {
  79. add { }
  80. remove { }
  81. }
  82. public ReadOnlySpan<string> GamepadsIds => new[] { "0" };
  83. public IGamepad GetGamepad(string id)
  84. {
  85. return new AvaloniaMouse(this);
  86. }
  87. public void Dispose()
  88. {
  89. if (_isDisposed)
  90. {
  91. return;
  92. }
  93. _isDisposed = true;
  94. _widget.PointerMoved -= Parent_PointerMovedEvent;
  95. _widget.PointerPressed -= Parent_PointerPressEvent;
  96. _widget.PointerReleased -= Parent_PointerReleaseEvent;
  97. _widget.PointerWheelChanged -= Parent_ScrollEvent;
  98. _widget = null;
  99. }
  100. }
  101. }