forked from hrydgard/ppsspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTiltEventProcessor.cpp
378 lines (304 loc) · 10.3 KB
/
TiltEventProcessor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <mutex>
#include "Common/Math/math_util.h"
#include "Common/Math/lin/vec3.h"
#include "Common/Math/lin/matrix4x4.h"
#include "Common/Log.h"
#include "Common/System/Display.h"
#include "Core/Config.h"
#include "Core/ConfigValues.h"
#include "Core/HLE/sceCtrl.h"
#include "Core/TiltEventProcessor.h"
namespace TiltEventProcessor {
static u32 tiltButtonsDown = 0;
float rawTiltAnalogX;
float rawTiltAnalogY;
float g_currentYAngle = 0.0f;
float GetCurrentYAngle() {
return g_currentYAngle;
}
// These functions generate tilt events given the current Tilt amount,
// and the deadzone radius.
void GenerateAnalogStickEvent(float analogX, float analogY);
void GenerateDPadEvent(int digitalX, int digitalY);
void GenerateActionButtonEvent(int digitalX, int digitalY);
void GenerateTriggerButtonEvent(int digitalX, int digitalY);
}
// deadzone is normalized - 0 to 1
// sensitivity controls how fast the deadzone reaches max value
inline float ApplyDeadzoneAxis(float x, float deadzone) {
if (deadzone >= 0.99f) {
// Meaningless, and not reachable with normal controls.
return x;
}
const float factor = 1.0f / (1.0f - deadzone);
if (x > deadzone) {
return (x - deadzone) * factor + deadzone;
} else if (x < -deadzone) {
return (x + deadzone) * factor - deadzone;
} else {
return 0.0f;
}
}
inline void ApplyDeadzoneXY(float x, float y, float *adjustedX, float *adjustedY, float deadzone, bool circular) {
if (circular) {
if (x == 0.0f && y == 0.0f) {
*adjustedX = 0.0f;
*adjustedY = 0.0f;
return;
}
float magnitude = sqrtf(x * x + y * y);
if (magnitude <= deadzone + 0.00001f) {
*adjustedX = 0.0f;
*adjustedY = 0.0f;
return;
}
float factor = 1.0f / (1.0f - deadzone);
float newMagnitude = (magnitude - deadzone) * factor;
*adjustedX = (x / magnitude) * newMagnitude;
*adjustedY = (y / magnitude) * newMagnitude;
} else {
*adjustedX = ApplyDeadzoneAxis(x, deadzone);
*adjustedY = ApplyDeadzoneAxis(y, deadzone);
}
}
namespace TiltEventProcessor {
// Also clamps to -1.0..1.0.
// This applies a (circular if desired) inverse deadzone.
inline void ApplyInverseDeadzone(float x, float y, float *outX, float *outY, float inverseDeadzone, bool circular) {
if (inverseDeadzone == 0.0f) {
*outX = Clamp(x, -1.0f, 1.0f);
*outY = Clamp(y, -1.0f, 1.0f);
return;
}
if (circular) {
// If the regular deadzone centered it, let's leave it as-is.
if (x == 0.0f && y == 0.0f) {
*outX = x;
*outY = y;
return;
}
float magnitude = sqrtf(x * x + y * y);
if (magnitude > 0.00001f) {
magnitude = (magnitude + inverseDeadzone) / magnitude;
}
*outX = Clamp(x * magnitude, -1.0f, 1.0f);
*outY = Clamp(y * magnitude, -1.0f, 1.0f);
} else {
// If the regular deadzone centered it, let's leave it as-is.
*outX = x == 0.0f ? 0.0f : Clamp(x + copysignf(inverseDeadzone, x), -1.0f, 1.0f);
*outY = y == 0.0f ? 0.0f : Clamp(y + copysignf(inverseDeadzone, y), -1.0f, 1.0f);
}
}
void ProcessTilt(bool landscape, float calibrationAngle, float x, float y, float z, bool invertX, bool invertY, float xSensitivity, float ySensitivity) {
if (g_Config.iTiltInputType == TILT_NULL) {
// Turned off - nothing to do.
return;
}
if (landscape) {
std::swap(x, y);
} else {
x *= -1.0f;
}
Lin::Vec3 down = Lin::Vec3(x, y, z).normalized();
float angleAroundX = atan2(down.z, down.y);
g_currentYAngle = angleAroundX; // TODO: Should smooth this out over time a bit.
float yAngle = angleAroundX - calibrationAngle;
float xAngle = asinf(down.x);
_dbg_assert_(!my_isnanorinf(angleAroundX));
_dbg_assert_(!my_isnanorinf(yAngle));
_dbg_assert_(!my_isnanorinf(xAngle));
float tiltX = xAngle;
float tiltY = yAngle;
// invert x and y axes if requested. Can probably remove this.
if (invertX) {
tiltX = -tiltX;
}
if (invertY) {
tiltY = -tiltY;
}
// It's not obvious what the factor for converting from tilt angle to value should be,
// but there's nothing that says that 1 would make sense. The important thing is that
// the sensitivity sliders get a range of values that makes sense.
const float tiltFactor = 3.0f;
tiltX *= xSensitivity * tiltFactor;
tiltY *= ySensitivity * tiltFactor;
if (g_Config.iTiltInputType == TILT_ANALOG) {
// Only analog mappings use the deadzone.
float adjustedTiltX;
float adjustedTiltY;
ApplyDeadzoneXY(tiltX, tiltY, &adjustedTiltX, &adjustedTiltY, g_Config.fTiltAnalogDeadzoneRadius, g_Config.bTiltCircularDeadzone);
_dbg_assert_(!my_isnanorinf(adjustedTiltX));
_dbg_assert_(!my_isnanorinf(adjustedTiltY));
// Unlike regular deadzone, where per-axis is okay, inverse deadzone (to compensate for game deadzones) really needs to be
// applied on the two axes together.
// TODO: Share this code with the joystick code. For now though, we keep it separate.
ApplyInverseDeadzone(adjustedTiltX, adjustedTiltY, &adjustedTiltX, &adjustedTiltY, g_Config.fTiltInverseDeadzone, g_Config.bTiltCircularDeadzone);
_dbg_assert_(!my_isnanorinf(adjustedTiltX));
_dbg_assert_(!my_isnanorinf(adjustedTiltY));
rawTiltAnalogX = adjustedTiltX;
rawTiltAnalogY = adjustedTiltY;
GenerateAnalogStickEvent(adjustedTiltX, adjustedTiltY);
return;
}
// Remaining are digital now so do the digital check here.
// We use a fixed 0.3 threshold instead of a deadzone since you can simply use sensitivity to set it -
// these parameters were never independent. It should feel similar to analog that way.
int digitalX = 0;
int digitalY = 0;
const float threshold = 0.5f;
if (tiltX < -threshold) {
digitalX = -1;
} else if (tiltX > threshold) {
digitalX = 1;
}
if (tiltY < -threshold) {
digitalY = -1;
} else if (tiltY > threshold) {
digitalY = 1;
}
switch (g_Config.iTiltInputType) {
case TILT_DPAD:
GenerateDPadEvent(digitalX, digitalY);
break;
case TILT_ACTION_BUTTON:
GenerateActionButtonEvent(digitalX, digitalY);
break;
case TILT_TRIGGER_BUTTONS:
GenerateTriggerButtonEvent(digitalX, digitalY);
break;
default:
break;
}
}
inline float clamp(float f) {
if (f > 1.0f) return 1.0f;
if (f < -1.0f) return -1.0f;
return f;
}
// TODO: Instead of __Ctrl, route data into the ControlMapper.
void GenerateAnalogStickEvent(float tiltX, float tiltY) {
__CtrlSetAnalogXY(CTRL_STICK_LEFT, clamp(tiltX), clamp(tiltY));
}
void GenerateDPadEvent(int digitalX, int digitalY) {
static const int dir[4] = { CTRL_RIGHT, CTRL_DOWN, CTRL_LEFT, CTRL_UP };
if (digitalX == 0) {
__CtrlUpdateButtons(0, tiltButtonsDown & (CTRL_RIGHT | CTRL_LEFT));
tiltButtonsDown &= ~(CTRL_LEFT | CTRL_RIGHT);
}
if (digitalY == 0) {
__CtrlUpdateButtons(0, tiltButtonsDown & (CTRL_UP | CTRL_DOWN));
tiltButtonsDown &= ~(CTRL_UP | CTRL_DOWN);
}
if (digitalX == 0 && digitalY == 0) {
return;
}
int ctrlMask = 0;
if (digitalX == -1) ctrlMask |= CTRL_LEFT;
if (digitalX == 1) ctrlMask |= CTRL_RIGHT;
if (digitalY == -1) ctrlMask |= CTRL_DOWN;
if (digitalY == 1) ctrlMask |= CTRL_UP;
ctrlMask &= ~__CtrlPeekButtons();
__CtrlUpdateButtons(ctrlMask, 0);
tiltButtonsDown |= ctrlMask;
}
void GenerateActionButtonEvent(int digitalX, int digitalY) {
static const int buttons[4] = { CTRL_CIRCLE, CTRL_CROSS, CTRL_SQUARE, CTRL_TRIANGLE };
if (digitalX == 0) {
__CtrlUpdateButtons(0, tiltButtonsDown & (CTRL_SQUARE | CTRL_CIRCLE));
tiltButtonsDown &= ~(CTRL_SQUARE | CTRL_CIRCLE);
}
if (digitalY == 0) {
__CtrlUpdateButtons(0, tiltButtonsDown & (CTRL_TRIANGLE | CTRL_CROSS));
tiltButtonsDown &= ~(CTRL_TRIANGLE | CTRL_CROSS);
}
if (digitalX == 0 && digitalY == 0) {
return;
}
int ctrlMask = 0;
if (digitalX == -1) ctrlMask |= CTRL_SQUARE;
if (digitalX == 1) ctrlMask |= CTRL_CIRCLE;
if (digitalY == -1) ctrlMask |= CTRL_CROSS;
if (digitalY == 1) ctrlMask |= CTRL_TRIANGLE;
ctrlMask &= ~__CtrlPeekButtons();
__CtrlUpdateButtons(ctrlMask, 0);
tiltButtonsDown |= ctrlMask;
}
void GenerateTriggerButtonEvent(int digitalX, int digitalY) {
u32 upButtons = 0;
u32 downButtons = 0;
// Y axis up for both
if (digitalY == 1) {
downButtons = CTRL_LTRIGGER | CTRL_RTRIGGER;
} else if (digitalX == 0) {
upButtons = CTRL_LTRIGGER | CTRL_RTRIGGER;
} else if (digitalX == -1) {
downButtons = CTRL_LTRIGGER;
upButtons = CTRL_RTRIGGER;
} else if (digitalX == 1) {
downButtons = CTRL_RTRIGGER;
upButtons = CTRL_LTRIGGER;
}
downButtons &= ~__CtrlPeekButtons();
__CtrlUpdateButtons(downButtons, tiltButtonsDown & upButtons);
tiltButtonsDown = (tiltButtonsDown & ~upButtons) | downButtons;
}
void ResetTiltEvents() {
// Reset the buttons we have marked pressed.
__CtrlUpdateButtons(0, tiltButtonsDown);
tiltButtonsDown = 0;
__CtrlSetAnalogXY(CTRL_STICK_LEFT, 0.0f, 0.0f);
}
} // namespace TiltEventProcessor
namespace MouseEventProcessor {
// Technically, we may be OK without a mutex here.
// But, the cost isn't high.
std::mutex g_mouseMutex;
float g_mouseDeltaXAccum = 0;
float g_mouseDeltaYAccum = 0;
float g_mouseDeltaX;
float g_mouseDeltaY;
void DecayMouse(double now) {
g_mouseDeltaX = g_mouseDeltaXAccum;
g_mouseDeltaY = g_mouseDeltaYAccum;
const float decay = g_Config.fMouseSmoothing;
static double lastTime = 0.0f;
if (lastTime == 0.0) {
lastTime = now;
return;
}
double dt = now - lastTime;
lastTime = now;
// Decay the mouse deltas. We do an approximation of the old polling.
// Should be able to use a smooth exponential here, when I get around to doing
// the math.
static double accumDt = 0.0;
accumDt += dt;
const double oldPollInterval = 1.0 / 250.0; // See Windows "PollControllers".
while (accumDt > oldPollInterval) {
accumDt -= oldPollInterval;
g_mouseDeltaXAccum *= decay;
g_mouseDeltaYAccum *= decay;
}
}
void ProcessDelta(double now, float dx, float dy) {
std::unique_lock<std::mutex> lock(g_mouseMutex);
// Accumulate mouse deltas, for some kind of smoothing.
g_mouseDeltaXAccum += dx;
g_mouseDeltaYAccum += dy;
DecayMouse(now);
}
void MouseDeltaToAxes(double now, float *mx, float *my) {
std::unique_lock<std::mutex> lock(g_mouseMutex);
float scaleFactor_x = g_display.dpi_scale_x * 0.1 * g_Config.fMouseSensitivity;
float scaleFactor_y = g_display.dpi_scale_y * 0.1 * g_Config.fMouseSensitivity;
DecayMouse(now);
// TODO: Make configurable.
float mouseDeadZone = 0.1f;
float outX = clamp_value(g_mouseDeltaX * scaleFactor_x, -1.0f, 1.0f);
float outY = clamp_value(g_mouseDeltaY * scaleFactor_y, -1.0f, 1.0f);
ApplyDeadzoneXY(outX, outY, mx, my, mouseDeadZone, true);
}
} // namespace