diff options
| author | 2021-09-20 16:57:55 -0500 | |
|---|---|---|
| committer | 2021-11-24 20:30:22 -0600 | |
| commit | 4c6f2c2547e1d97f12ebe708fac693a6183bbc45 (patch) | |
| tree | 0abcd35f56088bbf732a92838995a465bae0f0ee /src/input_common/analog_from_button.cpp | |
| parent | input_common: Create input poller and mapping (diff) | |
| download | yuzu-4c6f2c2547e1d97f12ebe708fac693a6183bbc45.tar.gz yuzu-4c6f2c2547e1d97f12ebe708fac693a6183bbc45.tar.xz yuzu-4c6f2c2547e1d97f12ebe708fac693a6183bbc45.zip | |
input_common: Move touch and analog from button. Move udp protocol
Diffstat (limited to '')
| -rw-r--r--[-rwxr-xr-x] | src/input_common/helpers/stick_from_buttons.cpp (renamed from src/input_common/analog_from_button.cpp) | 142 |
1 files changed, 87 insertions, 55 deletions
diff --git a/src/input_common/analog_from_button.cpp b/src/input_common/helpers/stick_from_buttons.cpp index 2fafd077f..38f150746 100755..100644 --- a/src/input_common/analog_from_button.cpp +++ b/src/input_common/helpers/stick_from_buttons.cpp | |||
| @@ -2,32 +2,38 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <atomic> | ||
| 6 | #include <chrono> | 5 | #include <chrono> |
| 7 | #include <cmath> | 6 | #include <cmath> |
| 8 | #include <thread> | ||
| 9 | #include "common/math_util.h" | 7 | #include "common/math_util.h" |
| 10 | #include "common/settings.h" | 8 | #include "common/settings.h" |
| 11 | #include "input_common/analog_from_button.h" | 9 | #include "input_common/helpers/stick_from_buttons.h" |
| 12 | 10 | ||
| 13 | namespace InputCommon { | 11 | namespace InputCommon { |
| 14 | 12 | ||
| 15 | class Analog final : public Input::AnalogDevice { | 13 | class Stick final : public Input::InputDevice { |
| 16 | public: | 14 | public: |
| 17 | using Button = std::unique_ptr<Input::ButtonDevice>; | 15 | using Button = std::unique_ptr<Input::InputDevice>; |
| 18 | 16 | ||
| 19 | Analog(Button up_, Button down_, Button left_, Button right_, Button modifier_, | 17 | Stick(Button up_, Button down_, Button left_, Button right_, Button modifier_, |
| 20 | float modifier_scale_, float modifier_angle_) | 18 | float modifier_scale_, float modifier_angle_) |
| 21 | : up(std::move(up_)), down(std::move(down_)), left(std::move(left_)), | 19 | : up(std::move(up_)), down(std::move(down_)), left(std::move(left_)), |
| 22 | right(std::move(right_)), modifier(std::move(modifier_)), modifier_scale(modifier_scale_), | 20 | right(std::move(right_)), modifier(std::move(modifier_)), modifier_scale(modifier_scale_), |
| 23 | modifier_angle(modifier_angle_) { | 21 | modifier_angle(modifier_angle_) { |
| 24 | Input::InputCallback<bool> callbacks{ | 22 | Input::InputCallback button_up_callback{ |
| 25 | [this]([[maybe_unused]] bool status) { UpdateStatus(); }}; | 23 | [this](Input::CallbackStatus callback_) { UpdateUpButtonStatus(callback_); }}; |
| 26 | up->SetCallback(callbacks); | 24 | Input::InputCallback button_down_callback{ |
| 27 | down->SetCallback(callbacks); | 25 | [this](Input::CallbackStatus callback_) { UpdateDownButtonStatus(callback_); }}; |
| 28 | left->SetCallback(callbacks); | 26 | Input::InputCallback button_left_callback{ |
| 29 | right->SetCallback(callbacks); | 27 | [this](Input::CallbackStatus callback_) { UpdateLeftButtonStatus(callback_); }}; |
| 30 | modifier->SetCallback(callbacks); | 28 | Input::InputCallback button_right_callback{ |
| 29 | [this](Input::CallbackStatus callback_) { UpdateRightButtonStatus(callback_); }}; | ||
| 30 | Input::InputCallback button_modifier_callback{ | ||
| 31 | [this](Input::CallbackStatus callback_) { UpdateModButtonStatus(callback_); }}; | ||
| 32 | up->SetCallback(button_up_callback); | ||
| 33 | down->SetCallback(button_down_callback); | ||
| 34 | left->SetCallback(button_left_callback); | ||
| 35 | right->SetCallback(button_right_callback); | ||
| 36 | modifier->SetCallback(button_modifier_callback); | ||
| 31 | } | 37 | } |
| 32 | 38 | ||
| 33 | bool IsAngleGreater(float old_angle, float new_angle) const { | 39 | bool IsAngleGreater(float old_angle, float new_angle) const { |
| @@ -123,13 +129,38 @@ public: | |||
| 123 | } | 129 | } |
| 124 | } | 130 | } |
| 125 | 131 | ||
| 132 | void UpdateUpButtonStatus(Input::CallbackStatus button_callback) { | ||
| 133 | up_status = button_callback.button_status.value; | ||
| 134 | UpdateStatus(); | ||
| 135 | } | ||
| 136 | |||
| 137 | void UpdateDownButtonStatus(Input::CallbackStatus button_callback) { | ||
| 138 | down_status = button_callback.button_status.value; | ||
| 139 | UpdateStatus(); | ||
| 140 | } | ||
| 141 | |||
| 142 | void UpdateLeftButtonStatus(Input::CallbackStatus button_callback) { | ||
| 143 | left_status = button_callback.button_status.value; | ||
| 144 | UpdateStatus(); | ||
| 145 | } | ||
| 146 | |||
| 147 | void UpdateRightButtonStatus(Input::CallbackStatus button_callback) { | ||
| 148 | right_status = button_callback.button_status.value; | ||
| 149 | UpdateStatus(); | ||
| 150 | } | ||
| 151 | |||
| 152 | void UpdateModButtonStatus(Input::CallbackStatus button_callback) { | ||
| 153 | modifier_status = button_callback.button_status.value; | ||
| 154 | UpdateStatus(); | ||
| 155 | } | ||
| 156 | |||
| 126 | void UpdateStatus() { | 157 | void UpdateStatus() { |
| 127 | const float coef = modifier->GetStatus() ? modifier_scale : 1.0f; | 158 | const float coef = modifier_status ? modifier_scale : 1.0f; |
| 128 | 159 | ||
| 129 | bool r = right->GetStatus(); | 160 | bool r = right_status; |
| 130 | bool l = left->GetStatus(); | 161 | bool l = left_status; |
| 131 | bool u = up->GetStatus(); | 162 | bool u = up_status; |
| 132 | bool d = down->GetStatus(); | 163 | bool d = down_status; |
| 133 | 164 | ||
| 134 | // Eliminate contradictory movements | 165 | // Eliminate contradictory movements |
| 135 | if (r && l) { | 166 | if (r && l) { |
| @@ -162,49 +193,42 @@ public: | |||
| 162 | } | 193 | } |
| 163 | 194 | ||
| 164 | last_update = now; | 195 | last_update = now; |
| 196 | Input::CallbackStatus status{ | ||
| 197 | .type = Input::InputType::Stick, | ||
| 198 | .stick_status = GetStatus(), | ||
| 199 | }; | ||
| 200 | TriggerOnChange(status); | ||
| 165 | } | 201 | } |
| 166 | 202 | ||
| 167 | std::tuple<float, float> GetStatus() const override { | 203 | Input::StickStatus GetStatus() const { |
| 204 | Input::StickStatus status{}; | ||
| 205 | status.x.properties = properties; | ||
| 206 | status.y.properties = properties; | ||
| 168 | if (Settings::values.emulate_analog_keyboard) { | 207 | if (Settings::values.emulate_analog_keyboard) { |
| 169 | const auto now = std::chrono::steady_clock::now(); | 208 | const auto now = std::chrono::steady_clock::now(); |
| 170 | float angle_ = GetAngle(now); | 209 | float angle_ = GetAngle(now); |
| 171 | return std::make_tuple(std::cos(angle_) * amplitude, std::sin(angle_) * amplitude); | 210 | status.x.raw_value = std::cos(angle_) * amplitude; |
| 211 | status.y.raw_value = std::sin(angle_) * amplitude; | ||
| 212 | return status; | ||
| 172 | } | 213 | } |
| 173 | constexpr float SQRT_HALF = 0.707106781f; | 214 | constexpr float SQRT_HALF = 0.707106781f; |
| 174 | int x = 0, y = 0; | 215 | int x = 0, y = 0; |
| 175 | if (right->GetStatus()) { | 216 | if (right_status) { |
| 176 | ++x; | 217 | ++x; |
| 177 | } | 218 | } |
| 178 | if (left->GetStatus()) { | 219 | if (left_status) { |
| 179 | --x; | 220 | --x; |
| 180 | } | 221 | } |
| 181 | if (up->GetStatus()) { | 222 | if (up_status) { |
| 182 | ++y; | 223 | ++y; |
| 183 | } | 224 | } |
| 184 | if (down->GetStatus()) { | 225 | if (down_status) { |
| 185 | --y; | 226 | --y; |
| 186 | } | 227 | } |
| 187 | const float coef = modifier->GetStatus() ? modifier_scale : 1.0f; | 228 | const float coef = modifier_status ? modifier_scale : 1.0f; |
| 188 | return std::make_tuple(static_cast<float>(x) * coef * (y == 0 ? 1.0f : SQRT_HALF), | 229 | status.x.raw_value = static_cast<float>(x) * coef * (y == 0 ? 1.0f : SQRT_HALF); |
| 189 | static_cast<float>(y) * coef * (x == 0 ? 1.0f : SQRT_HALF)); | 230 | status.y.raw_value = static_cast<float>(y) * coef * (x == 0 ? 1.0f : SQRT_HALF); |
| 190 | } | 231 | return status; |
| 191 | |||
| 192 | Input::AnalogProperties GetAnalogProperties() const override { | ||
| 193 | return {modifier_scale, 1.0f, 0.5f}; | ||
| 194 | } | ||
| 195 | |||
| 196 | bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { | ||
| 197 | switch (direction) { | ||
| 198 | case Input::AnalogDirection::RIGHT: | ||
| 199 | return right->GetStatus(); | ||
| 200 | case Input::AnalogDirection::LEFT: | ||
| 201 | return left->GetStatus(); | ||
| 202 | case Input::AnalogDirection::UP: | ||
| 203 | return up->GetStatus(); | ||
| 204 | case Input::AnalogDirection::DOWN: | ||
| 205 | return down->GetStatus(); | ||
| 206 | } | ||
| 207 | return false; | ||
| 208 | } | 232 | } |
| 209 | 233 | ||
| 210 | private: | 234 | private: |
| @@ -218,21 +242,29 @@ private: | |||
| 218 | float angle{}; | 242 | float angle{}; |
| 219 | float goal_angle{}; | 243 | float goal_angle{}; |
| 220 | float amplitude{}; | 244 | float amplitude{}; |
| 245 | bool up_status; | ||
| 246 | bool down_status; | ||
| 247 | bool left_status; | ||
| 248 | bool right_status; | ||
| 249 | bool modifier_status; | ||
| 250 | const Input::AnalogProperties properties{0.0f, 1.0f, 0.5f, 0.0f, false}; | ||
| 221 | std::chrono::time_point<std::chrono::steady_clock> last_update; | 251 | std::chrono::time_point<std::chrono::steady_clock> last_update; |
| 222 | }; | 252 | }; |
| 223 | 253 | ||
| 224 | std::unique_ptr<Input::AnalogDevice> AnalogFromButton::Create(const Common::ParamPackage& params) { | 254 | std::unique_ptr<Input::InputDevice> StickFromButton::Create(const Common::ParamPackage& params) { |
| 225 | const std::string null_engine = Common::ParamPackage{{"engine", "null"}}.Serialize(); | 255 | const std::string null_engine = Common::ParamPackage{{"engine", "null"}}.Serialize(); |
| 226 | auto up = Input::CreateDevice<Input::ButtonDevice>(params.Get("up", null_engine)); | 256 | auto up = Input::CreateDeviceFromString<Input::InputDevice>(params.Get("up", null_engine)); |
| 227 | auto down = Input::CreateDevice<Input::ButtonDevice>(params.Get("down", null_engine)); | 257 | auto down = Input::CreateDeviceFromString<Input::InputDevice>(params.Get("down", null_engine)); |
| 228 | auto left = Input::CreateDevice<Input::ButtonDevice>(params.Get("left", null_engine)); | 258 | auto left = Input::CreateDeviceFromString<Input::InputDevice>(params.Get("left", null_engine)); |
| 229 | auto right = Input::CreateDevice<Input::ButtonDevice>(params.Get("right", null_engine)); | 259 | auto right = |
| 230 | auto modifier = Input::CreateDevice<Input::ButtonDevice>(params.Get("modifier", null_engine)); | 260 | Input::CreateDeviceFromString<Input::InputDevice>(params.Get("right", null_engine)); |
| 261 | auto modifier = | ||
| 262 | Input::CreateDeviceFromString<Input::InputDevice>(params.Get("modifier", null_engine)); | ||
| 231 | auto modifier_scale = params.Get("modifier_scale", 0.5f); | 263 | auto modifier_scale = params.Get("modifier_scale", 0.5f); |
| 232 | auto modifier_angle = params.Get("modifier_angle", 5.5f); | 264 | auto modifier_angle = params.Get("modifier_angle", 5.5f); |
| 233 | return std::make_unique<Analog>(std::move(up), std::move(down), std::move(left), | 265 | return std::make_unique<Stick>(std::move(up), std::move(down), std::move(left), |
| 234 | std::move(right), std::move(modifier), modifier_scale, | 266 | std::move(right), std::move(modifier), modifier_scale, |
| 235 | modifier_angle); | 267 | modifier_angle); |
| 236 | } | 268 | } |
| 237 | 269 | ||
| 238 | } // namespace InputCommon | 270 | } // namespace InputCommon |