AvaloniaMouseDriver.cs 4.8 KB

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