SDL2MouseDriver.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Ryujinx.Input;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Numerics;
  6. using System.Runtime.CompilerServices;
  7. using static SDL2.SDL;
  8. namespace Ryujinx.Headless.SDL2
  9. {
  10. class SDL2MouseDriver : IGamepadDriver
  11. {
  12. private bool _isDisposed;
  13. public bool[] PressedButtons { get; }
  14. public Vector2 CurrentPosition { get; private set; }
  15. public Vector2 Scroll { get; private set; }
  16. public Size _clientSize;
  17. public SDL2MouseDriver()
  18. {
  19. PressedButtons = new bool[(int)MouseButton.Count];
  20. }
  21. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  22. private static MouseButton DriverButtonToMouseButton(uint rawButton)
  23. {
  24. Debug.Assert(rawButton > 0 && rawButton <= (int)MouseButton.Count);
  25. return (MouseButton)(rawButton - 1);
  26. }
  27. public void Update(SDL_Event evnt)
  28. {
  29. if (evnt.type == SDL_EventType.SDL_MOUSEBUTTONDOWN || evnt.type == SDL_EventType.SDL_MOUSEBUTTONUP)
  30. {
  31. uint rawButton = evnt.button.button;
  32. if (rawButton > 0 && rawButton <= (int)MouseButton.Count)
  33. {
  34. PressedButtons[(int)DriverButtonToMouseButton(rawButton)] = evnt.type == SDL_EventType.SDL_MOUSEBUTTONDOWN;
  35. CurrentPosition = new Vector2(evnt.button.x, evnt.button.y);
  36. }
  37. }
  38. else if (evnt.type == SDL_EventType.SDL_MOUSEMOTION)
  39. {
  40. CurrentPosition = new Vector2(evnt.motion.x, evnt.motion.y);
  41. }
  42. else if (evnt.type == SDL_EventType.SDL_MOUSEWHEEL)
  43. {
  44. Scroll = new Vector2(evnt.wheel.x, evnt.wheel.y);
  45. }
  46. }
  47. public void SetClientSize(int width, int height)
  48. {
  49. _clientSize = new Size(width, height);
  50. }
  51. public bool IsButtonPressed(MouseButton button)
  52. {
  53. return PressedButtons[(int)button];
  54. }
  55. public Size GetClientSize()
  56. {
  57. return _clientSize;
  58. }
  59. public string DriverName => "SDL2";
  60. public event Action<string> OnGamepadConnected
  61. {
  62. add { }
  63. remove { }
  64. }
  65. public event Action<string> OnGamepadDisconnected
  66. {
  67. add { }
  68. remove { }
  69. }
  70. public ReadOnlySpan<string> GamepadsIds => new[] { "0" };
  71. public IGamepad GetGamepad(string id)
  72. {
  73. return new SDL2Mouse(this);
  74. }
  75. public void Dispose()
  76. {
  77. if (_isDisposed)
  78. {
  79. return;
  80. }
  81. _isDisposed = true;
  82. }
  83. }
  84. }