mouse_input.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <mutex>
  7. #include <stop_token>
  8. #include <thread>
  9. #include "common/common_types.h"
  10. #include "common/threadsafe_queue.h"
  11. #include "common/vector_math.h"
  12. #include "core/frontend/input.h"
  13. #include "input_common/motion_input.h"
  14. namespace MouseInput {
  15. enum class MouseButton {
  16. Left,
  17. Right,
  18. Wheel,
  19. Backward,
  20. Forward,
  21. Task,
  22. Extra,
  23. Undefined,
  24. };
  25. struct MouseStatus {
  26. MouseButton button{MouseButton::Undefined};
  27. };
  28. struct MouseData {
  29. bool pressed{};
  30. std::array<int, 2> axis{};
  31. Input::MotionStatus motion{};
  32. Input::TouchStatus touch{};
  33. };
  34. class Mouse {
  35. public:
  36. Mouse();
  37. ~Mouse();
  38. /// Used for polling
  39. void BeginConfiguration();
  40. void EndConfiguration();
  41. /**
  42. * Signals that a button is pressed.
  43. * @param x the x-coordinate of the cursor
  44. * @param y the y-coordinate of the cursor
  45. * @param button_ the button pressed
  46. */
  47. void PressButton(int x, int y, MouseButton button_);
  48. /**
  49. * Signals that mouse has moved.
  50. * @param x the x-coordinate of the cursor
  51. * @param y the y-coordinate of the cursor
  52. * @param center_x the x-coordinate of the middle of the screen
  53. * @param center_y the y-coordinate of the middle of the screen
  54. */
  55. void MouseMove(int x, int y, int center_x, int center_y);
  56. /**
  57. * Signals that a button is released.
  58. * @param button_ the button pressed
  59. */
  60. void ReleaseButton(MouseButton button_);
  61. /**
  62. * Signals that all buttons are released
  63. */
  64. void ReleaseAllButtons();
  65. [[nodiscard]] bool ToggleButton(std::size_t button_);
  66. [[nodiscard]] bool UnlockButton(std::size_t button_);
  67. [[nodiscard]] Common::SPSCQueue<MouseStatus>& GetMouseQueue();
  68. [[nodiscard]] const Common::SPSCQueue<MouseStatus>& GetMouseQueue() const;
  69. [[nodiscard]] MouseData& GetMouseState(std::size_t button);
  70. [[nodiscard]] const MouseData& GetMouseState(std::size_t button) const;
  71. private:
  72. void UpdateThread(std::stop_token stop_token);
  73. void UpdateYuzuSettings();
  74. void StopPanning();
  75. struct MouseInfo {
  76. InputCommon::MotionInput motion{0.0f, 0.0f, 0.0f};
  77. Common::Vec2<int> mouse_origin;
  78. Common::Vec2<int> last_mouse_position;
  79. Common::Vec2<float> last_mouse_change;
  80. bool is_tilting = false;
  81. float sensitivity{0.120f};
  82. float tilt_speed = 0;
  83. Common::Vec2<float> tilt_direction;
  84. MouseData data;
  85. };
  86. u16 buttons{};
  87. u16 toggle_buttons{};
  88. u16 lock_buttons{};
  89. std::jthread update_thread;
  90. MouseButton last_button{MouseButton::Undefined};
  91. std::array<MouseInfo, 7> mouse_info;
  92. Common::SPSCQueue<MouseStatus> mouse_queue;
  93. bool configuring{false};
  94. int mouse_panning_timout{};
  95. };
  96. } // namespace MouseInput