diff options
Diffstat (limited to 'src/input_common/helpers/touch_from_buttons.cpp')
| -rw-r--r-- | src/input_common/helpers/touch_from_buttons.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/input_common/helpers/touch_from_buttons.cpp b/src/input_common/helpers/touch_from_buttons.cpp new file mode 100644 index 000000000..2abfaf841 --- /dev/null +++ b/src/input_common/helpers/touch_from_buttons.cpp | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | // Copyright 2020 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include "common/settings.h" | ||
| 7 | #include "core/frontend/framebuffer_layout.h" | ||
| 8 | #include "input_common/helpers/touch_from_buttons.h" | ||
| 9 | |||
| 10 | namespace InputCommon { | ||
| 11 | |||
| 12 | class TouchFromButtonDevice final : public Input::InputDevice { | ||
| 13 | public: | ||
| 14 | using Button = std::unique_ptr<Input::InputDevice>; | ||
| 15 | TouchFromButtonDevice(Button button_, u32 touch_id_, float x_, float y_) | ||
| 16 | : button(std::move(button_)), touch_id(touch_id_), x(x_), y(y_) { | ||
| 17 | Input::InputCallback button_up_callback{ | ||
| 18 | [this](Input::CallbackStatus callback_) { UpdateButtonStatus(callback_); }}; | ||
| 19 | button->SetCallback(button_up_callback); | ||
| 20 | } | ||
| 21 | |||
| 22 | Input::TouchStatus GetStatus(bool pressed) const { | ||
| 23 | const Input::ButtonStatus button_status{ | ||
| 24 | .value = pressed, | ||
| 25 | }; | ||
| 26 | Input::TouchStatus status{ | ||
| 27 | .pressed = button_status, | ||
| 28 | .x = {}, | ||
| 29 | .y = {}, | ||
| 30 | .id = touch_id, | ||
| 31 | }; | ||
| 32 | status.x.properties = properties; | ||
| 33 | status.y.properties = properties; | ||
| 34 | |||
| 35 | if (!pressed) { | ||
| 36 | return status; | ||
| 37 | } | ||
| 38 | |||
| 39 | status.x.raw_value = x; | ||
| 40 | status.y.raw_value = y; | ||
| 41 | return status; | ||
| 42 | } | ||
| 43 | |||
| 44 | void UpdateButtonStatus(Input::CallbackStatus button_callback) { | ||
| 45 | const Input::CallbackStatus status{ | ||
| 46 | .type = Input::InputType::Touch, | ||
| 47 | .touch_status = GetStatus(button_callback.button_status.value), | ||
| 48 | }; | ||
| 49 | TriggerOnChange(status); | ||
| 50 | } | ||
| 51 | |||
| 52 | private: | ||
| 53 | Button button; | ||
| 54 | const u32 touch_id; | ||
| 55 | const float x; | ||
| 56 | const float y; | ||
| 57 | const Input::AnalogProperties properties{0.0f, 1.0f, 0.5f, 0.0f, false}; | ||
| 58 | }; | ||
| 59 | |||
| 60 | std::unique_ptr<Input::InputDevice> TouchFromButton::Create(const Common::ParamPackage& params) { | ||
| 61 | const std::string null_engine = Common::ParamPackage{{"engine", "null"}}.Serialize(); | ||
| 62 | auto button = | ||
| 63 | Input::CreateDeviceFromString<Input::InputDevice>(params.Get("button", null_engine)); | ||
| 64 | const auto touch_id = params.Get("touch_id", 0); | ||
| 65 | const float x = params.Get("x", 0.0f) / 1280.0f; | ||
| 66 | const float y = params.Get("y", 0.0f) / 720.0f; | ||
| 67 | return std::make_unique<TouchFromButtonDevice>(std::move(button), touch_id, x, y); | ||
| 68 | } | ||
| 69 | |||
| 70 | } // namespace InputCommon | ||