mouse_input.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <algorithm>
  6. #include <functional>
  7. #include <mutex>
  8. #include <thread>
  9. #include <unordered_map>
  10. #include "common/common_types.h"
  11. #include "common/threadsafe_queue.h"
  12. #include "core/frontend/input.h"
  13. #include "input_common/main.h"
  14. #include "input_common/motion_input.h"
  15. namespace MouseInput {
  16. enum class MouseButton {
  17. Left,
  18. Wheel,
  19. Right,
  20. Foward,
  21. Backward,
  22. Undefined,
  23. };
  24. struct MouseStatus {
  25. MouseButton button{MouseButton::Undefined};
  26. };
  27. struct MouseData {
  28. bool pressed{};
  29. std::array<int, 2> axis{};
  30. Input::MotionStatus motion{};
  31. Input::TouchStatus touch{};
  32. };
  33. class Mouse {
  34. public:
  35. Mouse();
  36. ~Mouse();
  37. /// Used for polling
  38. void BeginConfiguration();
  39. void EndConfiguration();
  40. /**
  41. * Signals that a button is pressed.
  42. * @param x the x-coordinate of the cursor
  43. * @param y the y-coordinate of the cursor
  44. * @param button the button pressed
  45. */
  46. void PressButton(int x, int y, int button_);
  47. /**
  48. * Signals that mouse has moved.
  49. * @param x the x-coordinate of the cursor
  50. * @param y the y-coordinate of the cursor
  51. */
  52. void MouseMove(int x, int y);
  53. /**
  54. * Signals that a motion sensor tilt has ended.
  55. */
  56. void ReleaseButton(int button_);
  57. [[nodiscard]] Common::SPSCQueue<MouseStatus>& GetMouseQueue();
  58. [[nodiscard]] const Common::SPSCQueue<MouseStatus>& GetMouseQueue() const;
  59. [[nodiscard]] MouseData& GetMouseState(std::size_t button);
  60. [[nodiscard]] const MouseData& GetMouseState(std::size_t button) const;
  61. private:
  62. void UpdateThread();
  63. void UpdateYuzuSettings();
  64. struct MouseInfo {
  65. InputCommon::MotionInput motion{0.0f, 0.0f, 0.0f};
  66. Common::Vec2<int> mouse_origin;
  67. Common::Vec2<int> last_mouse_position;
  68. bool is_tilting = false;
  69. float sensitivity{0.120f};
  70. float tilt_speed = 0;
  71. Common::Vec2<float> tilt_direction;
  72. MouseData data;
  73. };
  74. u16 buttons{};
  75. std::thread update_thread;
  76. MouseButton last_button{MouseButton::Undefined};
  77. std::array<MouseInfo, 5> mouse_info;
  78. Common::SPSCQueue<MouseStatus> mouse_queue;
  79. bool configuring{false};
  80. bool update_thread_running{true};
  81. };
  82. } // namespace MouseInput