SDL2MouseDriver.cs 2.7 KB

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