summaryrefslogtreecommitdiff
path: root/src/input_common/helpers/touch_from_buttons.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/helpers/touch_from_buttons.cpp')
-rw-r--r--src/input_common/helpers/touch_from_buttons.cpp84
1 files changed, 84 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..ece1e3b32
--- /dev/null
+++ b/src/input_common/helpers/touch_from_buttons.cpp
@@ -0,0 +1,84 @@
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
10namespace InputCommon {
11
12class TouchFromButtonDevice final : public Common::Input::InputDevice {
13public:
14 using Button = std::unique_ptr<Common::Input::InputDevice>;
15 TouchFromButtonDevice(Button button_, int touch_id_, float x_, float y_)
16 : button(std::move(button_)), touch_id(touch_id_), x(x_), y(y_) {
17 last_button_value = false;
18 button->SetCallback({
19 .on_change =
20 [this](const Common::Input::CallbackStatus& callback_) {
21 UpdateButtonStatus(callback_);
22 },
23 });
24 button->ForceUpdate();
25 }
26
27 void ForceUpdate() override {
28 button->ForceUpdate();
29 }
30
31 Common::Input::TouchStatus GetStatus(bool pressed) const {
32 const Common::Input::ButtonStatus button_status{
33 .value = pressed,
34 };
35 Common::Input::TouchStatus status{
36 .pressed = button_status,
37 .x = {},
38 .y = {},
39 .id = touch_id,
40 };
41 status.x.properties = properties;
42 status.y.properties = properties;
43
44 if (!pressed) {
45 return status;
46 }
47
48 status.x.raw_value = x;
49 status.y.raw_value = y;
50 return status;
51 }
52
53 void UpdateButtonStatus(const Common::Input::CallbackStatus& button_callback) {
54 const Common::Input::CallbackStatus status{
55 .type = Common::Input::InputType::Touch,
56 .touch_status = GetStatus(button_callback.button_status.value),
57 };
58 if (last_button_value != button_callback.button_status.value) {
59 last_button_value = button_callback.button_status.value;
60 TriggerOnChange(status);
61 }
62 }
63
64private:
65 Button button;
66 bool last_button_value;
67 const int touch_id;
68 const float x;
69 const float y;
70 const Common::Input::AnalogProperties properties{0.0f, 1.0f, 0.5f, 0.0f, false};
71};
72
73std::unique_ptr<Common::Input::InputDevice> TouchFromButton::Create(
74 const Common::ParamPackage& params) {
75 const std::string null_engine = Common::ParamPackage{{"engine", "null"}}.Serialize();
76 auto button = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
77 params.Get("button", null_engine));
78 const auto touch_id = params.Get("touch_id", 0);
79 const float x = params.Get("x", 0.0f) / 1280.0f;
80 const float y = params.Get("y", 0.0f) / 720.0f;
81 return std::make_unique<TouchFromButtonDevice>(std::move(button), touch_id, x, y);
82}
83
84} // namespace InputCommon