AvaloniaMouseDriver.cs 3.6 KB

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