diff options
| author | 2023-02-21 21:46:42 -0600 | |
|---|---|---|
| committer | 2023-02-21 21:55:22 -0600 | |
| commit | 673accd630be88fde4b6b748608a9c3bc42ea60f (patch) | |
| tree | 72c5d94eb0ee9b260e3a2e1c1514a515388e8f41 /src/input_common/drivers/mouse.cpp | |
| parent | Merge pull request #9847 from german77/timeout (diff) | |
| download | yuzu-673accd630be88fde4b6b748608a9c3bc42ea60f.tar.gz yuzu-673accd630be88fde4b6b748608a9c3bc42ea60f.tar.xz yuzu-673accd630be88fde4b6b748608a9c3bc42ea60f.zip | |
input_common: Implement dedicated motion from mouse
Diffstat (limited to 'src/input_common/drivers/mouse.cpp')
| -rw-r--r-- | src/input_common/drivers/mouse.cpp | 94 |
1 files changed, 78 insertions, 16 deletions
diff --git a/src/input_common/drivers/mouse.cpp b/src/input_common/drivers/mouse.cpp index da50e0a24..8b7f9aee9 100644 --- a/src/input_common/drivers/mouse.cpp +++ b/src/input_common/drivers/mouse.cpp | |||
| @@ -10,17 +10,25 @@ | |||
| 10 | #include "input_common/drivers/mouse.h" | 10 | #include "input_common/drivers/mouse.h" |
| 11 | 11 | ||
| 12 | namespace InputCommon { | 12 | namespace InputCommon { |
| 13 | constexpr int update_time = 10; | ||
| 14 | constexpr float default_stick_sensitivity = 0.022f; | ||
| 15 | constexpr float default_motion_sensitivity = 0.008f; | ||
| 13 | constexpr int mouse_axis_x = 0; | 16 | constexpr int mouse_axis_x = 0; |
| 14 | constexpr int mouse_axis_y = 1; | 17 | constexpr int mouse_axis_y = 1; |
| 15 | constexpr int wheel_axis_x = 2; | 18 | constexpr int wheel_axis_x = 2; |
| 16 | constexpr int wheel_axis_y = 3; | 19 | constexpr int wheel_axis_y = 3; |
| 17 | constexpr int motion_wheel_y = 4; | ||
| 18 | constexpr PadIdentifier identifier = { | 20 | constexpr PadIdentifier identifier = { |
| 19 | .guid = Common::UUID{}, | 21 | .guid = Common::UUID{}, |
| 20 | .port = 0, | 22 | .port = 0, |
| 21 | .pad = 0, | 23 | .pad = 0, |
| 22 | }; | 24 | }; |
| 23 | 25 | ||
| 26 | constexpr PadIdentifier motion_identifier = { | ||
| 27 | .guid = Common::UUID{}, | ||
| 28 | .port = 0, | ||
| 29 | .pad = 1, | ||
| 30 | }; | ||
| 31 | |||
| 24 | constexpr PadIdentifier real_mouse_identifier = { | 32 | constexpr PadIdentifier real_mouse_identifier = { |
| 25 | .guid = Common::UUID{}, | 33 | .guid = Common::UUID{}, |
| 26 | .port = 1, | 34 | .port = 1, |
| @@ -37,47 +45,87 @@ Mouse::Mouse(std::string input_engine_) : InputEngine(std::move(input_engine_)) | |||
| 37 | PreSetController(identifier); | 45 | PreSetController(identifier); |
| 38 | PreSetController(real_mouse_identifier); | 46 | PreSetController(real_mouse_identifier); |
| 39 | PreSetController(touch_identifier); | 47 | PreSetController(touch_identifier); |
| 48 | PreSetController(motion_identifier); | ||
| 40 | 49 | ||
| 41 | // Initialize all mouse axis | 50 | // Initialize all mouse axis |
| 42 | PreSetAxis(identifier, mouse_axis_x); | 51 | PreSetAxis(identifier, mouse_axis_x); |
| 43 | PreSetAxis(identifier, mouse_axis_y); | 52 | PreSetAxis(identifier, mouse_axis_y); |
| 44 | PreSetAxis(identifier, wheel_axis_x); | 53 | PreSetAxis(identifier, wheel_axis_x); |
| 45 | PreSetAxis(identifier, wheel_axis_y); | 54 | PreSetAxis(identifier, wheel_axis_y); |
| 46 | PreSetAxis(identifier, motion_wheel_y); | ||
| 47 | PreSetAxis(real_mouse_identifier, mouse_axis_x); | 55 | PreSetAxis(real_mouse_identifier, mouse_axis_x); |
| 48 | PreSetAxis(real_mouse_identifier, mouse_axis_y); | 56 | PreSetAxis(real_mouse_identifier, mouse_axis_y); |
| 49 | PreSetAxis(touch_identifier, mouse_axis_x); | 57 | PreSetAxis(touch_identifier, mouse_axis_x); |
| 50 | PreSetAxis(touch_identifier, mouse_axis_y); | 58 | PreSetAxis(touch_identifier, mouse_axis_y); |
| 59 | |||
| 60 | // Initialize variables | ||
| 61 | mouse_origin = {}; | ||
| 62 | last_mouse_position = {}; | ||
| 63 | wheel_position = {}; | ||
| 64 | last_mouse_change = {}; | ||
| 65 | last_motion_change = {}; | ||
| 66 | |||
| 51 | update_thread = std::jthread([this](std::stop_token stop_token) { UpdateThread(stop_token); }); | 67 | update_thread = std::jthread([this](std::stop_token stop_token) { UpdateThread(stop_token); }); |
| 52 | } | 68 | } |
| 53 | 69 | ||
| 54 | void Mouse::UpdateThread(std::stop_token stop_token) { | 70 | void Mouse::UpdateThread(std::stop_token stop_token) { |
| 55 | Common::SetCurrentThreadName("Mouse"); | 71 | Common::SetCurrentThreadName("Mouse"); |
| 56 | constexpr int update_time = 10; | ||
| 57 | while (!stop_token.stop_requested()) { | ||
| 58 | if (Settings::values.mouse_panning) { | ||
| 59 | // Slow movement by 4% | ||
| 60 | last_mouse_change *= 0.96f; | ||
| 61 | const float sensitivity = | ||
| 62 | Settings::values.mouse_panning_sensitivity.GetValue() * 0.022f; | ||
| 63 | SetAxis(identifier, mouse_axis_x, last_mouse_change.x * sensitivity); | ||
| 64 | SetAxis(identifier, mouse_axis_y, -last_mouse_change.y * sensitivity); | ||
| 65 | } | ||
| 66 | 72 | ||
| 67 | SetAxis(identifier, motion_wheel_y, 0.0f); | 73 | while (!stop_token.stop_requested()) { |
| 74 | UpdateStickInput(); | ||
| 75 | UpdateMotionInput(); | ||
| 68 | 76 | ||
| 69 | if (mouse_panning_timout++ > 20) { | 77 | if (mouse_panning_timeout++ > 20) { |
| 70 | StopPanning(); | 78 | StopPanning(); |
| 71 | } | 79 | } |
| 72 | std::this_thread::sleep_for(std::chrono::milliseconds(update_time)); | 80 | std::this_thread::sleep_for(std::chrono::milliseconds(update_time)); |
| 73 | } | 81 | } |
| 74 | } | 82 | } |
| 75 | 83 | ||
| 84 | void Mouse::UpdateStickInput() { | ||
| 85 | if (!Settings::values.mouse_panning) { | ||
| 86 | return; | ||
| 87 | } | ||
| 88 | |||
| 89 | const float sensitivity = | ||
| 90 | Settings::values.mouse_panning_sensitivity.GetValue() * default_stick_sensitivity; | ||
| 91 | |||
| 92 | // Slow movement by 4% | ||
| 93 | last_mouse_change *= 0.96f; | ||
| 94 | SetAxis(identifier, mouse_axis_x, last_mouse_change.x * sensitivity); | ||
| 95 | SetAxis(identifier, mouse_axis_y, -last_mouse_change.y * sensitivity); | ||
| 96 | } | ||
| 97 | |||
| 98 | void Mouse::UpdateMotionInput() { | ||
| 99 | const float sensitivity = | ||
| 100 | Settings::values.mouse_panning_sensitivity.GetValue() * default_motion_sensitivity; | ||
| 101 | |||
| 102 | // Slow movement by 7% | ||
| 103 | if (Settings::values.mouse_panning) { | ||
| 104 | last_motion_change *= 0.93f; | ||
| 105 | } else { | ||
| 106 | last_motion_change.z *= 0.93f; | ||
| 107 | } | ||
| 108 | |||
| 109 | const BasicMotion motion_data{ | ||
| 110 | .gyro_x = last_motion_change.x * sensitivity, | ||
| 111 | .gyro_y = last_motion_change.y * sensitivity, | ||
| 112 | .gyro_z = last_motion_change.z * sensitivity, | ||
| 113 | .accel_x = 0, | ||
| 114 | .accel_y = 0, | ||
| 115 | .accel_z = 0, | ||
| 116 | .delta_timestamp = update_time * 1000, | ||
| 117 | }; | ||
| 118 | |||
| 119 | SetMotion(motion_identifier, 0, motion_data); | ||
| 120 | } | ||
| 121 | |||
| 76 | void Mouse::Move(int x, int y, int center_x, int center_y) { | 122 | void Mouse::Move(int x, int y, int center_x, int center_y) { |
| 77 | if (Settings::values.mouse_panning) { | 123 | if (Settings::values.mouse_panning) { |
| 124 | mouse_panning_timeout = 0; | ||
| 125 | |||
| 78 | auto mouse_change = | 126 | auto mouse_change = |
| 79 | (Common::MakeVec(x, y) - Common::MakeVec(center_x, center_y)).Cast<float>(); | 127 | (Common::MakeVec(x, y) - Common::MakeVec(center_x, center_y)).Cast<float>(); |
| 80 | mouse_panning_timout = 0; | 128 | Common::Vec3<float> motion_change{-mouse_change.y, -mouse_change.x, last_motion_change.z}; |
| 81 | 129 | ||
| 82 | const auto move_distance = mouse_change.Length(); | 130 | const auto move_distance = mouse_change.Length(); |
| 83 | if (move_distance == 0) { | 131 | if (move_distance == 0) { |
| @@ -93,6 +141,7 @@ void Mouse::Move(int x, int y, int center_x, int center_y) { | |||
| 93 | 141 | ||
| 94 | // Average mouse movements | 142 | // Average mouse movements |
| 95 | last_mouse_change = (last_mouse_change * 0.91f) + (mouse_change * 0.09f); | 143 | last_mouse_change = (last_mouse_change * 0.91f) + (mouse_change * 0.09f); |
| 144 | last_motion_change = (last_motion_change * 0.69f) + (motion_change * 0.31f); | ||
| 96 | 145 | ||
| 97 | const auto last_move_distance = last_mouse_change.Length(); | 146 | const auto last_move_distance = last_mouse_change.Length(); |
| 98 | 147 | ||
| @@ -116,6 +165,12 @@ void Mouse::Move(int x, int y, int center_x, int center_y) { | |||
| 116 | const float sensitivity = Settings::values.mouse_panning_sensitivity.GetValue() * 0.0012f; | 165 | const float sensitivity = Settings::values.mouse_panning_sensitivity.GetValue() * 0.0012f; |
| 117 | SetAxis(identifier, mouse_axis_x, static_cast<float>(mouse_move.x) * sensitivity); | 166 | SetAxis(identifier, mouse_axis_x, static_cast<float>(mouse_move.x) * sensitivity); |
| 118 | SetAxis(identifier, mouse_axis_y, static_cast<float>(-mouse_move.y) * sensitivity); | 167 | SetAxis(identifier, mouse_axis_y, static_cast<float>(-mouse_move.y) * sensitivity); |
| 168 | |||
| 169 | last_motion_change = { | ||
| 170 | static_cast<float>(-mouse_move.y) / 50.0f, | ||
| 171 | static_cast<float>(-mouse_move.x) / 50.0f, | ||
| 172 | last_motion_change.z, | ||
| 173 | }; | ||
| 119 | } | 174 | } |
| 120 | } | 175 | } |
| 121 | 176 | ||
| @@ -157,15 +212,19 @@ void Mouse::ReleaseButton(MouseButton button) { | |||
| 157 | SetAxis(identifier, mouse_axis_x, 0); | 212 | SetAxis(identifier, mouse_axis_x, 0); |
| 158 | SetAxis(identifier, mouse_axis_y, 0); | 213 | SetAxis(identifier, mouse_axis_y, 0); |
| 159 | } | 214 | } |
| 215 | |||
| 216 | last_motion_change.x = 0; | ||
| 217 | last_motion_change.y = 0; | ||
| 218 | |||
| 160 | button_pressed = false; | 219 | button_pressed = false; |
| 161 | } | 220 | } |
| 162 | 221 | ||
| 163 | void Mouse::MouseWheelChange(int x, int y) { | 222 | void Mouse::MouseWheelChange(int x, int y) { |
| 164 | wheel_position.x += x; | 223 | wheel_position.x += x; |
| 165 | wheel_position.y += y; | 224 | wheel_position.y += y; |
| 225 | last_motion_change.z += static_cast<f32>(y) / 100.0f; | ||
| 166 | SetAxis(identifier, wheel_axis_x, static_cast<f32>(wheel_position.x)); | 226 | SetAxis(identifier, wheel_axis_x, static_cast<f32>(wheel_position.x)); |
| 167 | SetAxis(identifier, wheel_axis_y, static_cast<f32>(wheel_position.y)); | 227 | SetAxis(identifier, wheel_axis_y, static_cast<f32>(wheel_position.y)); |
| 168 | SetAxis(identifier, motion_wheel_y, static_cast<f32>(y) / 100.0f); | ||
| 169 | } | 228 | } |
| 170 | 229 | ||
| 171 | void Mouse::ReleaseAllButtons() { | 230 | void Mouse::ReleaseAllButtons() { |
| @@ -234,6 +293,9 @@ Common::Input::ButtonNames Mouse::GetUIName(const Common::ParamPackage& params) | |||
| 234 | if (params.Has("axis_x") && params.Has("axis_y") && params.Has("axis_z")) { | 293 | if (params.Has("axis_x") && params.Has("axis_y") && params.Has("axis_z")) { |
| 235 | return Common::Input::ButtonNames::Engine; | 294 | return Common::Input::ButtonNames::Engine; |
| 236 | } | 295 | } |
| 296 | if (params.Has("motion")) { | ||
| 297 | return Common::Input::ButtonNames::Engine; | ||
| 298 | } | ||
| 237 | 299 | ||
| 238 | return Common::Input::ButtonNames::Invalid; | 300 | return Common::Input::ButtonNames::Invalid; |
| 239 | } | 301 | } |