AvaloniaMouseDriver.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using FluentAvalonia.Core;
  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. private readonly TopLevel _window;
  18. public bool[] PressedButtons { get; }
  19. public Vector2 CurrentPosition { get; private set; }
  20. public Vector2 Scroll { get; private set; }
  21. public string DriverName => "AvaloniaMouseDriver";
  22. public ReadOnlySpan<string> GamepadsIds => new[] { "0" };
  23. public AvaloniaMouseDriver(TopLevel window, Control parent)
  24. {
  25. _widget = parent;
  26. _window = window;
  27. _widget.PointerMoved += Parent_PointerMovedEvent;
  28. _widget.PointerPressed += Parent_PointerPressEvent;
  29. _widget.PointerReleased += Parent_PointerReleaseEvent;
  30. _widget.PointerWheelChanged += Parent_ScrollEvent;
  31. _window.PointerMoved += Parent_PointerMovedEvent;
  32. _window.PointerPressed += Parent_PointerPressEvent;
  33. _window.PointerReleased += Parent_PointerReleaseEvent;
  34. _window.PointerWheelChanged += Parent_ScrollEvent;
  35. PressedButtons = new bool[(int)MouseButton.Count];
  36. _size = new Size((int)parent.Bounds.Width, (int)parent.Bounds.Height);
  37. parent.GetObservable(Visual.BoundsProperty).Subscribe(Resized);
  38. }
  39. public event Action<string> OnGamepadConnected
  40. {
  41. add { }
  42. remove { }
  43. }
  44. public event Action<string> OnGamepadDisconnected
  45. {
  46. add { }
  47. remove { }
  48. }
  49. private void Resized(Rect rect)
  50. {
  51. _size = new Size((int)rect.Width, (int)rect.Height);
  52. }
  53. private void Parent_ScrollEvent(object o, PointerWheelEventArgs args)
  54. {
  55. Scroll = new Vector2((float)args.Delta.X, (float)args.Delta.Y);
  56. }
  57. private void Parent_PointerReleaseEvent(object o, PointerReleasedEventArgs args)
  58. {
  59. int button = (int)args.InitialPressMouseButton - 1;
  60. if (PressedButtons.Count() >= button)
  61. {
  62. PressedButtons[button] = false;
  63. }
  64. }
  65. private void Parent_PointerPressEvent(object o, PointerPressedEventArgs args)
  66. {
  67. int button = (int)args.GetCurrentPoint(_widget).Properties.PointerUpdateKind;
  68. if (PressedButtons.Count() >= button)
  69. {
  70. PressedButtons[button] = true;
  71. }
  72. }
  73. private void Parent_PointerMovedEvent(object o, PointerEventArgs args)
  74. {
  75. Point position = args.GetPosition(_widget);
  76. CurrentPosition = new Vector2((float)position.X, (float)position.Y);
  77. }
  78. public void SetMousePressed(MouseButton button)
  79. {
  80. if (PressedButtons.Count() >= (int)button)
  81. {
  82. PressedButtons[(int)button] = true;
  83. }
  84. }
  85. public void SetMouseReleased(MouseButton button)
  86. {
  87. if (PressedButtons.Count() >= (int)button)
  88. {
  89. PressedButtons[(int)button] = false;
  90. }
  91. }
  92. public void SetPosition(double x, double y)
  93. {
  94. CurrentPosition = new Vector2((float)x, (float)y);
  95. }
  96. public bool IsButtonPressed(MouseButton button)
  97. {
  98. if (PressedButtons.Count() >= (int)button)
  99. {
  100. return PressedButtons[(int)button];
  101. }
  102. return false;
  103. }
  104. public Size GetClientSize()
  105. {
  106. return _size;
  107. }
  108. public IGamepad GetGamepad(string id)
  109. {
  110. return new AvaloniaMouse(this);
  111. }
  112. public void Dispose()
  113. {
  114. if (_isDisposed)
  115. {
  116. return;
  117. }
  118. _isDisposed = true;
  119. _widget.PointerMoved -= Parent_PointerMovedEvent;
  120. _widget.PointerPressed -= Parent_PointerPressEvent;
  121. _widget.PointerReleased -= Parent_PointerReleaseEvent;
  122. _widget.PointerWheelChanged -= Parent_ScrollEvent;
  123. _window.PointerMoved -= Parent_PointerMovedEvent;
  124. _window.PointerPressed -= Parent_PointerPressEvent;
  125. _window.PointerReleased -= Parent_PointerReleaseEvent;
  126. _window.PointerWheelChanged -= Parent_ScrollEvent;
  127. _widget = null;
  128. }
  129. }
  130. }