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