mouse.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included
  4. #include <stop_token>
  5. #include <thread>
  6. #include <fmt/format.h>
  7. #include "common/param_package.h"
  8. #include "common/settings.h"
  9. #include "common/thread.h"
  10. #include "input_common/drivers/mouse.h"
  11. namespace InputCommon {
  12. constexpr int mouse_axis_x = 0;
  13. constexpr int mouse_axis_y = 1;
  14. constexpr int wheel_axis_x = 2;
  15. constexpr int wheel_axis_y = 3;
  16. constexpr int touch_axis_x = 10;
  17. constexpr int touch_axis_y = 11;
  18. constexpr PadIdentifier identifier = {
  19. .guid = Common::UUID{Common::INVALID_UUID},
  20. .port = 0,
  21. .pad = 0,
  22. };
  23. Mouse::Mouse(std::string input_engine_) : InputEngine(std::move(input_engine_)) {
  24. PreSetController(identifier);
  25. PreSetAxis(identifier, mouse_axis_x);
  26. PreSetAxis(identifier, mouse_axis_y);
  27. PreSetAxis(identifier, wheel_axis_x);
  28. PreSetAxis(identifier, wheel_axis_y);
  29. PreSetAxis(identifier, touch_axis_x);
  30. PreSetAxis(identifier, touch_axis_x);
  31. update_thread = std::jthread([this](std::stop_token stop_token) { UpdateThread(stop_token); });
  32. }
  33. void Mouse::UpdateThread(std::stop_token stop_token) {
  34. Common::SetCurrentThreadName("yuzu:input:Mouse");
  35. constexpr int update_time = 10;
  36. while (!stop_token.stop_requested()) {
  37. if (Settings::values.mouse_panning && !Settings::values.mouse_enabled) {
  38. // Slow movement by 4%
  39. last_mouse_change *= 0.96f;
  40. const float sensitivity =
  41. Settings::values.mouse_panning_sensitivity.GetValue() * 0.022f;
  42. SetAxis(identifier, mouse_axis_x, last_mouse_change.x * sensitivity);
  43. SetAxis(identifier, mouse_axis_y, -last_mouse_change.y * sensitivity);
  44. }
  45. if (mouse_panning_timout++ > 20) {
  46. StopPanning();
  47. }
  48. std::this_thread::sleep_for(std::chrono::milliseconds(update_time));
  49. }
  50. }
  51. void Mouse::MouseMove(int x, int y, f32 touch_x, f32 touch_y, int center_x, int center_y) {
  52. // If native mouse is enabled just set the screen coordinates
  53. if (Settings::values.mouse_enabled) {
  54. SetAxis(identifier, mouse_axis_x, touch_x);
  55. SetAxis(identifier, mouse_axis_y, touch_y);
  56. return;
  57. }
  58. SetAxis(identifier, touch_axis_x, touch_x);
  59. SetAxis(identifier, touch_axis_y, touch_y);
  60. if (Settings::values.mouse_panning) {
  61. auto mouse_change =
  62. (Common::MakeVec(x, y) - Common::MakeVec(center_x, center_y)).Cast<float>();
  63. mouse_panning_timout = 0;
  64. const auto move_distance = mouse_change.Length();
  65. if (move_distance == 0) {
  66. return;
  67. }
  68. // Make slow movements at least 3 units on lenght
  69. if (move_distance < 3.0f) {
  70. // Normalize value
  71. mouse_change /= move_distance;
  72. mouse_change *= 3.0f;
  73. }
  74. // Average mouse movements
  75. last_mouse_change = (last_mouse_change * 0.91f) + (mouse_change * 0.09f);
  76. const auto last_move_distance = last_mouse_change.Length();
  77. // Make fast movements clamp to 8 units on lenght
  78. if (last_move_distance > 8.0f) {
  79. // Normalize value
  80. last_mouse_change /= last_move_distance;
  81. last_mouse_change *= 8.0f;
  82. }
  83. // Ignore average if it's less than 1 unit and use current movement value
  84. if (last_move_distance < 1.0f) {
  85. last_mouse_change = mouse_change / mouse_change.Length();
  86. }
  87. return;
  88. }
  89. if (button_pressed) {
  90. const auto mouse_move = Common::MakeVec<int>(x, y) - mouse_origin;
  91. const float sensitivity = Settings::values.mouse_panning_sensitivity.GetValue() * 0.0012f;
  92. SetAxis(identifier, mouse_axis_x, static_cast<float>(mouse_move.x) * sensitivity);
  93. SetAxis(identifier, mouse_axis_y, static_cast<float>(-mouse_move.y) * sensitivity);
  94. }
  95. }
  96. void Mouse::PressButton(int x, int y, f32 touch_x, f32 touch_y, MouseButton button) {
  97. SetAxis(identifier, touch_axis_x, touch_x);
  98. SetAxis(identifier, touch_axis_y, touch_y);
  99. SetButton(identifier, static_cast<int>(button), true);
  100. // Set initial analog parameters
  101. mouse_origin = {x, y};
  102. last_mouse_position = {x, y};
  103. button_pressed = true;
  104. }
  105. void Mouse::ReleaseButton(MouseButton button) {
  106. SetButton(identifier, static_cast<int>(button), false);
  107. if (!Settings::values.mouse_panning && !Settings::values.mouse_enabled) {
  108. SetAxis(identifier, mouse_axis_x, 0);
  109. SetAxis(identifier, mouse_axis_y, 0);
  110. }
  111. button_pressed = false;
  112. }
  113. void Mouse::MouseWheelChange(int x, int y) {
  114. wheel_position.x += x;
  115. wheel_position.y += y;
  116. SetAxis(identifier, wheel_axis_x, static_cast<f32>(wheel_position.x));
  117. SetAxis(identifier, wheel_axis_y, static_cast<f32>(wheel_position.y));
  118. }
  119. void Mouse::ReleaseAllButtons() {
  120. ResetButtonState();
  121. button_pressed = false;
  122. }
  123. void Mouse::StopPanning() {
  124. last_mouse_change = {};
  125. }
  126. std::vector<Common::ParamPackage> Mouse::GetInputDevices() const {
  127. std::vector<Common::ParamPackage> devices;
  128. devices.emplace_back(Common::ParamPackage{
  129. {"engine", GetEngineName()},
  130. {"display", "Keyboard/Mouse"},
  131. });
  132. return devices;
  133. }
  134. AnalogMapping Mouse::GetAnalogMappingForDevice(
  135. [[maybe_unused]] const Common::ParamPackage& params) {
  136. // Only overwrite different buttons from default
  137. AnalogMapping mapping = {};
  138. Common::ParamPackage right_analog_params;
  139. right_analog_params.Set("engine", GetEngineName());
  140. right_analog_params.Set("axis_x", 0);
  141. right_analog_params.Set("axis_y", 1);
  142. right_analog_params.Set("threshold", 0.5f);
  143. right_analog_params.Set("range", 1.0f);
  144. right_analog_params.Set("deadzone", 0.0f);
  145. mapping.insert_or_assign(Settings::NativeAnalog::RStick, std::move(right_analog_params));
  146. return mapping;
  147. }
  148. Common::Input::ButtonNames Mouse::GetUIName(const Common::ParamPackage& params) const {
  149. if (params.Has("button")) {
  150. return Common::Input::ButtonNames::Value;
  151. }
  152. if (params.Has("axis")) {
  153. return Common::Input::ButtonNames::Value;
  154. }
  155. return Common::Input::ButtonNames::Invalid;
  156. }
  157. } // namespace InputCommon