summaryrefslogtreecommitdiff
path: root/src/d/d_pad_nav.cpp
blob: 50c9e08142df5f747ab5c8eb8727560f459ef6f5 (plain)
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
#include "d/d_pad_nav.h"

#include "d/d_pad.h"
#include "m/m_vec.h"

namespace dPadNav {

bool sIsNavEnabled = false;
bool sIsMplsNavLeftGesture = false;
bool sIsMplsNavRightGesture = false;
static bool sDisableAutoReturnToPointerNav = false;

bool sIsPointerVisible = true;
bool sPrevIsPointerVisible = true;
bool sIsCursorStickVisible = true;

s32 sFSStickDirection;
s32 sFSStickNavDirection;

static s32 sFSStickNavActiveTimer = 0;
static s32 sFSStickDirectionStableTimer = 0;
static s32 sMPLSNavGestureTimer = 0;

void init() {
    sIsNavEnabled = false;
    sIsPointerVisible = true;
    sIsMplsNavLeftGesture = false;
    sIsMplsNavRightGesture = false;
    sDisableAutoReturnToPointerNav = false;
    sPrevIsPointerVisible = true;
    sFSStickDirection = FS_STICK_NONE;
    sFSStickNavDirection = FS_STICK_NONE;
    sFSStickNavActiveTimer = 0;
    sFSStickDirectionStableTimer = 0;
    sMPLSNavGestureTimer = 0;
    setCursorStickInvisible();
}

void calc() {
    sPrevIsPointerVisible = sIsPointerVisible;
    sFSStickNavDirection = FS_STICK_NONE;

    if (!sIsNavEnabled) {
        return;
    }

    mVec3_c vel = dPad::ex_c::getInstance()->getMPLSVelocity();
    if (sMPLSNavGestureTimer != 0) {
        sMPLSNavGestureTimer--;
        sIsMplsNavLeftGesture = false;
        sIsMplsNavRightGesture = false;
    } else {
        checkForNavLeftGesture();
        checkForNavRightGesture();
    }

    if (vel.squareMagXY() > 1.5f) {
        // Large MPLS movement - stop FS stick navigation
        stopFSStickNav();
        return;
    }

    if (sIsPointerVisible) {
        if (dPad::ex_c::getInstance()->getFSStickTrig()) {
            // starting FS stick navigation - immediate nav event, with delay of
            // 8 frames (267ms) for repeat nav event
            sFSStickDirection = getFSStickDirectionTrig();
            sFSStickNavDirection = sFSStickDirection;
            sFSStickNavActiveTimer = 120;
            sFSStickDirectionStableTimer = 8;
            sIsPointerVisible = false;
        }
    } else {
        s32 direction = getFSStickDirection();
        if (sFSStickDirection != FS_STICK_NONE && direction == sFSStickDirection) {
            // no change in FS stick direction
            sFSStickNavActiveTimer = 120;
            if (sFSStickDirectionStableTimer == 0) {
                // repeat event - now with a delay of 3 frames (100ms)
                sFSStickNavDirection = sFSStickDirection;
                sFSStickDirectionStableTimer = 3;
            } else {
                sFSStickDirectionStableTimer--;
            }
        } else {
            if (dPad::ex_c::getInstance()->getFSStickTrig()) {
                // change in FS stick direction - same as if started navigation
                direction = getFSStickDirectionTrig();
                sFSStickDirectionStableTimer = 8;
                sFSStickDirection = direction;
                sFSStickNavDirection = direction;
                sFSStickNavActiveTimer = 120;
            } else {
                // let go of FS stick
                sFSStickDirection = FS_STICK_NONE;
                sFSStickDirectionStableTimer = 0;
            }
        }

        if (!sDisableAutoReturnToPointerNav) {
            if (sFSStickNavActiveTimer == 0) {
                // FS stick nav timer expired (or was set to 0 via explicit call),
                // check if we need to return to pointer nav
                if (direction != FS_STICK_NONE && sFSStickDirection == direction) {
                    sFSStickNavActiveTimer = 120;
                } else {
                    if (direction == FS_STICK_NONE) {
                        // No direction, return to pointer nav
                        sFSStickDirection = FS_STICK_NONE;
                        sIsPointerVisible = true;
                    } else {
                        sFSStickDirectionStableTimer = 8;
                        // Note: Redundant check
                        if (!sDisableAutoReturnToPointerNav) {
                            sFSStickDirection = direction;
                        }
                        sFSStickNavDirection = sFSStickDirection;
                        sFSStickNavActiveTimer = 120;
                    }
                }
            } else {
                sFSStickNavActiveTimer--;
            }
        }
    }
}

void setNavEnabled(bool navEnabled, bool disableAutoReturnToPointerNav) {
    sIsNavEnabled = navEnabled;
    sIsPointerVisible = true;
    if (navEnabled) {
        setCursorStickVisible();
        sDisableAutoReturnToPointerNav = disableAutoReturnToPointerNav;
    } else {
        setCursorStickInvisible();
        sDisableAutoReturnToPointerNav = false;
    }
}

s32 getFSStickDirectionTrig() {
    if (dPad::ex_c::getInstance()->getFSStickTrig(0x1)) {
        return FS_STICK_UP;
    }

    if (dPad::ex_c::getInstance()->getFSStickTrig(0x80)) {
        return FS_STICK_UP_RIGHT;
    }

    if (dPad::ex_c::getInstance()->getFSStickTrig(0x40)) {
        return FS_STICK_RIGHT;
    }

    if (dPad::ex_c::getInstance()->getFSStickTrig(0x20)) {
        return FS_STICK_DOWN_RIGHT;
    }

    if (dPad::ex_c::getInstance()->getFSStickTrig(0x10)) {
        return FS_STICK_DOWN;
    }

    if (dPad::ex_c::getInstance()->getFSStickTrig(0x8)) {
        return FS_STICK_DOWN_LEFT;
    }

    if (dPad::ex_c::getInstance()->getFSStickTrig(0x4)) {
        return FS_STICK_LEFT;
    }

    if (dPad::ex_c::getInstance()->getFSStickTrig(0x2)) {
        return FS_STICK_UP_LEFT;
    }

    return FS_STICK_NONE;
}

s32 getFSStickDirection() {
    switch (dPad::ex_c::getInstance()->mFSStickMask) {
        case 0x1:  return FS_STICK_UP;
        case 0x80: return FS_STICK_UP_RIGHT;
        case 0x40: return FS_STICK_RIGHT;
        case 0x20: return FS_STICK_DOWN_RIGHT;
        case 0x10: return FS_STICK_DOWN;
        case 0x8:  return FS_STICK_DOWN_LEFT;
        case 0x4:  return FS_STICK_LEFT;
        case 0x2:  return FS_STICK_UP_LEFT;
    }

    return FS_STICK_NONE;
}

void stopFSStickNav() {
    sFSStickNavActiveTimer = 0;
    sIsPointerVisible = true;
}

void hidePointer() {
    sIsPointerVisible = false;
}

void setCursorStickInvisible() {
    sIsCursorStickVisible = false;
}

void setCursorStickVisible() {
    sIsCursorStickVisible = true;
}

void checkForNavLeftGesture() {
    mVec3_c vel = dPad::ex_c::getInstance()->getMPLSVelocity();
    if (vel.y <= 0.0f && vel.squareMagXY() > 1.5f) {
        if (!sIsPointerVisible) {
            sMPLSNavGestureTimer = 10;
            sIsMplsNavLeftGesture = false;
        } else {
            sIsMplsNavLeftGesture = true;
        }
    } else {
        sIsMplsNavLeftGesture = false;
    }
}

void checkForNavRightGesture() {
    mVec3_c vel = dPad::ex_c::getInstance()->getMPLSVelocity();
    if (vel.y >= 0.0f && vel.squareMagXY() > 1.5f) {
        if (!sIsPointerVisible) {
            sMPLSNavGestureTimer = 10;
            sIsMplsNavRightGesture = false;
        } else {
            sIsMplsNavRightGesture = true;
        }
    } else {
        sIsMplsNavRightGesture = false;
    }
}

void scrollRelated() {
    sFSStickDirectionStableTimer = 3;
    sFSStickNavActiveTimer = 120;
}

} // namespace dPadNav