summaryrefslogtreecommitdiff
path: root/src/input_common/helpers/stick_from_buttons.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/helpers/stick_from_buttons.cpp')
-rw-r--r--src/input_common/helpers/stick_from_buttons.cpp304
1 files changed, 304 insertions, 0 deletions
diff --git a/src/input_common/helpers/stick_from_buttons.cpp b/src/input_common/helpers/stick_from_buttons.cpp
new file mode 100644
index 000000000..77fcd655e
--- /dev/null
+++ b/src/input_common/helpers/stick_from_buttons.cpp
@@ -0,0 +1,304 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <chrono>
6#include <cmath>
7#include "common/math_util.h"
8#include "common/settings.h"
9#include "input_common/helpers/stick_from_buttons.h"
10
11namespace InputCommon {
12
13class Stick final : public Common::Input::InputDevice {
14public:
15 using Button = std::unique_ptr<Common::Input::InputDevice>;
16
17 Stick(Button up_, Button down_, Button left_, Button right_, Button modifier_,
18 float modifier_scale_, float modifier_angle_)
19 : up(std::move(up_)), down(std::move(down_)), left(std::move(left_)),
20 right(std::move(right_)), modifier(std::move(modifier_)), modifier_scale(modifier_scale_),
21 modifier_angle(modifier_angle_) {
22 Common::Input::InputCallback button_up_callback{
23 [this](Common::Input::CallbackStatus callback_) { UpdateUpButtonStatus(callback_); }};
24 Common::Input::InputCallback button_down_callback{
25 [this](Common::Input::CallbackStatus callback_) { UpdateDownButtonStatus(callback_); }};
26 Common::Input::InputCallback button_left_callback{
27 [this](Common::Input::CallbackStatus callback_) { UpdateLeftButtonStatus(callback_); }};
28 Common::Input::InputCallback button_right_callback{
29 [this](Common::Input::CallbackStatus callback_) {
30 UpdateRightButtonStatus(callback_);
31 }};
32 Common::Input::InputCallback button_modifier_callback{
33 [this](Common::Input::CallbackStatus callback_) { UpdateModButtonStatus(callback_); }};
34 up->SetCallback(button_up_callback);
35 down->SetCallback(button_down_callback);
36 left->SetCallback(button_left_callback);
37 right->SetCallback(button_right_callback);
38 modifier->SetCallback(button_modifier_callback);
39 last_x_axis_value = 0.0f;
40 last_y_axis_value = 0.0f;
41 }
42
43 bool IsAngleGreater(float old_angle, float new_angle) const {
44 constexpr float TAU = Common::PI * 2.0f;
45 // Use wider angle to ease the transition.
46 constexpr float aperture = TAU * 0.15f;
47 const float top_limit = new_angle + aperture;
48 return (old_angle > new_angle && old_angle <= top_limit) ||
49 (old_angle + TAU > new_angle && old_angle + TAU <= top_limit);
50 }
51
52 bool IsAngleSmaller(float old_angle, float new_angle) const {
53 constexpr float TAU = Common::PI * 2.0f;
54 // Use wider angle to ease the transition.
55 constexpr float aperture = TAU * 0.15f;
56 const float bottom_limit = new_angle - aperture;
57 return (old_angle >= bottom_limit && old_angle < new_angle) ||
58 (old_angle - TAU >= bottom_limit && old_angle - TAU < new_angle);
59 }
60
61 float GetAngle(std::chrono::time_point<std::chrono::steady_clock> now) const {
62 constexpr float TAU = Common::PI * 2.0f;
63 float new_angle = angle;
64
65 auto time_difference = static_cast<float>(
66 std::chrono::duration_cast<std::chrono::microseconds>(now - last_update).count());
67 time_difference /= 1000.0f * 1000.0f;
68 if (time_difference > 0.5f) {
69 time_difference = 0.5f;
70 }
71
72 if (IsAngleGreater(new_angle, goal_angle)) {
73 new_angle -= modifier_angle * time_difference;
74 if (new_angle < 0) {
75 new_angle += TAU;
76 }
77 if (!IsAngleGreater(new_angle, goal_angle)) {
78 return goal_angle;
79 }
80 } else if (IsAngleSmaller(new_angle, goal_angle)) {
81 new_angle += modifier_angle * time_difference;
82 if (new_angle >= TAU) {
83 new_angle -= TAU;
84 }
85 if (!IsAngleSmaller(new_angle, goal_angle)) {
86 return goal_angle;
87 }
88 } else {
89 return goal_angle;
90 }
91 return new_angle;
92 }
93
94 void SetGoalAngle(bool r, bool l, bool u, bool d) {
95 // Move to the right
96 if (r && !u && !d) {
97 goal_angle = 0.0f;
98 }
99
100 // Move to the upper right
101 if (r && u && !d) {
102 goal_angle = Common::PI * 0.25f;
103 }
104
105 // Move up
106 if (u && !l && !r) {
107 goal_angle = Common::PI * 0.5f;
108 }
109
110 // Move to the upper left
111 if (l && u && !d) {
112 goal_angle = Common::PI * 0.75f;
113 }
114
115 // Move to the left
116 if (l && !u && !d) {
117 goal_angle = Common::PI;
118 }
119
120 // Move to the bottom left
121 if (l && !u && d) {
122 goal_angle = Common::PI * 1.25f;
123 }
124
125 // Move down
126 if (d && !l && !r) {
127 goal_angle = Common::PI * 1.5f;
128 }
129
130 // Move to the bottom right
131 if (r && !u && d) {
132 goal_angle = Common::PI * 1.75f;
133 }
134 }
135
136 void UpdateUpButtonStatus(Common::Input::CallbackStatus button_callback) {
137 up_status = button_callback.button_status.value;
138 UpdateStatus();
139 }
140
141 void UpdateDownButtonStatus(Common::Input::CallbackStatus button_callback) {
142 down_status = button_callback.button_status.value;
143 UpdateStatus();
144 }
145
146 void UpdateLeftButtonStatus(Common::Input::CallbackStatus button_callback) {
147 left_status = button_callback.button_status.value;
148 UpdateStatus();
149 }
150
151 void UpdateRightButtonStatus(Common::Input::CallbackStatus button_callback) {
152 right_status = button_callback.button_status.value;
153 UpdateStatus();
154 }
155
156 void UpdateModButtonStatus(Common::Input::CallbackStatus button_callback) {
157 modifier_status = button_callback.button_status.value;
158 UpdateStatus();
159 }
160
161 void UpdateStatus() {
162 const float coef = modifier_status ? modifier_scale : 1.0f;
163
164 bool r = right_status;
165 bool l = left_status;
166 bool u = up_status;
167 bool d = down_status;
168
169 // Eliminate contradictory movements
170 if (r && l) {
171 r = false;
172 l = false;
173 }
174 if (u && d) {
175 u = false;
176 d = false;
177 }
178
179 // Move if a key is pressed
180 if (r || l || u || d) {
181 amplitude = coef;
182 } else {
183 amplitude = 0;
184 }
185
186 const auto now = std::chrono::steady_clock::now();
187 const auto time_difference = static_cast<u64>(
188 std::chrono::duration_cast<std::chrono::milliseconds>(now - last_update).count());
189
190 if (time_difference < 10) {
191 // Disable analog mode if inputs are too fast
192 SetGoalAngle(r, l, u, d);
193 angle = goal_angle;
194 } else {
195 angle = GetAngle(now);
196 SetGoalAngle(r, l, u, d);
197 }
198
199 last_update = now;
200 Common::Input::CallbackStatus status{
201 .type = Common::Input::InputType::Stick,
202 .stick_status = GetStatus(),
203 };
204 last_x_axis_value = status.stick_status.x.raw_value;
205 last_y_axis_value = status.stick_status.y.raw_value;
206 TriggerOnChange(status);
207 }
208
209 void ForceUpdate() override {
210 up->ForceUpdate();
211 down->ForceUpdate();
212 left->ForceUpdate();
213 right->ForceUpdate();
214 modifier->ForceUpdate();
215 }
216
217 void SoftUpdate() override {
218 Common::Input::CallbackStatus status{
219 .type = Common::Input::InputType::Stick,
220 .stick_status = GetStatus(),
221 };
222 if (last_x_axis_value == status.stick_status.x.raw_value &&
223 last_y_axis_value == status.stick_status.y.raw_value) {
224 return;
225 }
226 last_x_axis_value = status.stick_status.x.raw_value;
227 last_y_axis_value = status.stick_status.y.raw_value;
228 TriggerOnChange(status);
229 }
230
231 Common::Input::StickStatus GetStatus() const {
232 Common::Input::StickStatus status{};
233 status.x.properties = properties;
234 status.y.properties = properties;
235 if (Settings::values.emulate_analog_keyboard) {
236 const auto now = std::chrono::steady_clock::now();
237 float angle_ = GetAngle(now);
238 status.x.raw_value = std::cos(angle_) * amplitude;
239 status.y.raw_value = std::sin(angle_) * amplitude;
240 return status;
241 }
242 constexpr float SQRT_HALF = 0.707106781f;
243 int x = 0, y = 0;
244 if (right_status) {
245 ++x;
246 }
247 if (left_status) {
248 --x;
249 }
250 if (up_status) {
251 ++y;
252 }
253 if (down_status) {
254 --y;
255 }
256 const float coef = modifier_status ? modifier_scale : 1.0f;
257 status.x.raw_value = static_cast<float>(x) * coef * (y == 0 ? 1.0f : SQRT_HALF);
258 status.y.raw_value = static_cast<float>(y) * coef * (x == 0 ? 1.0f : SQRT_HALF);
259 return status;
260 }
261
262private:
263 Button up;
264 Button down;
265 Button left;
266 Button right;
267 Button modifier;
268 float modifier_scale;
269 float modifier_angle;
270 float angle{};
271 float goal_angle{};
272 float amplitude{};
273 bool up_status;
274 bool down_status;
275 bool left_status;
276 bool right_status;
277 bool modifier_status;
278 float last_x_axis_value;
279 float last_y_axis_value;
280 const Common::Input::AnalogProperties properties{0.0f, 1.0f, 0.5f, 0.0f, false};
281 std::chrono::time_point<std::chrono::steady_clock> last_update;
282};
283
284std::unique_ptr<Common::Input::InputDevice> StickFromButton::Create(
285 const Common::ParamPackage& params) {
286 const std::string null_engine = Common::ParamPackage{{"engine", "null"}}.Serialize();
287 auto up = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
288 params.Get("up", null_engine));
289 auto down = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
290 params.Get("down", null_engine));
291 auto left = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
292 params.Get("left", null_engine));
293 auto right = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
294 params.Get("right", null_engine));
295 auto modifier = Common::Input::CreateDeviceFromString<Common::Input::InputDevice>(
296 params.Get("modifier", null_engine));
297 auto modifier_scale = params.Get("modifier_scale", 0.5f);
298 auto modifier_angle = params.Get("modifier_angle", 5.5f);
299 return std::make_unique<Stick>(std::move(up), std::move(down), std::move(left),
300 std::move(right), std::move(modifier), modifier_scale,
301 modifier_angle);
302}
303
304} // namespace InputCommon