AvaloniaMouseDriver.cs 3.7 KB

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