-
Notifications
You must be signed in to change notification settings - Fork 72
/
PadClamp.c
121 lines (106 loc) · 2.52 KB
/
PadClamp.c
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
#include <platform.h>
#include <dolphin/pad/forward.h>
#include <dolphin/pad/pad.h>
#include <dolphin/pad/PadClamp.h>
typedef struct PADClampRegion {
u8 minTrigger;
u8 maxTrigger;
s8 minStick;
s8 maxStick;
s8 xyStick;
s8 minSubstick;
s8 maxSubstick;
s8 xySubstick;
} PADClampRegion;
PADClampRegion ClampRegion = {
// Triggers
30,
180,
// Left stick
15,
72,
40,
// Right stick
15,
59,
31,
};
void ClampStick(s8* px, s8* py, s8 max, s8 xy, s8 min)
{
int x = *px;
int y = *py;
int signX;
int signY;
int d;
if (0 <= x) {
signX = 1;
} else {
signX = -1;
x = -x;
}
if (0 <= y) {
signY = 1;
} else {
signY = -1;
y = -y;
}
if (x <= min) {
x = 0;
} else {
x -= min;
}
if (y <= min) {
y = 0;
} else {
y -= min;
}
if (x == 0 && y == 0) {
*px = *py = 0;
return;
}
if (xy * y <= xy * x) {
d = xy * x + (max - xy) * y;
if (xy * max < d) {
x = (s8) (xy * max * x / d);
y = (s8) (xy * max * y / d);
}
} else {
d = xy * y + (max - xy) * x;
if (xy * max < d) {
x = (s8) (xy * max * x / d);
y = (s8) (xy * max * y / d);
}
}
*px = (s8) (signX * x);
*py = (s8) (signY * y);
}
void PADClamp(PADStatus* status)
{
int i;
for (i = 0; i < PAD_CHANMAX; i++, status++) {
if (status->err != PAD_ERR_NONE) {
continue;
}
ClampStick(&status->stickX, &status->stickY, ClampRegion.maxStick,
ClampRegion.xyStick, ClampRegion.minStick);
ClampStick(&status->substickX, &status->substickY,
ClampRegion.maxSubstick, ClampRegion.xySubstick,
ClampRegion.minSubstick);
if (status->triggerLeft <= ClampRegion.minTrigger) {
status->triggerLeft = 0;
} else {
if (ClampRegion.maxTrigger < status->triggerLeft) {
status->triggerLeft = ClampRegion.maxTrigger;
}
status->triggerLeft -= ClampRegion.minTrigger;
}
if (status->triggerRight <= ClampRegion.minTrigger) {
status->triggerRight = 0;
} else {
if (ClampRegion.maxTrigger < status->triggerRight) {
status->triggerRight = ClampRegion.maxTrigger;
}
status->triggerRight -= ClampRegion.minTrigger;
}
}
}