summaryrefslogtreecommitdiff
path: root/src/d/d_cursor_hit_check.cpp
blob: 9446db1803b15a20f3e98b506e7ada17258c5df7 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "d/d_cursor_hit_check.h"

#include "common.h"
#include "d/col/bg/d_bg_s.h"
#include "d/col/bg/d_bg_s_lin_chk.h"
#include "d/col/cc/d_cc_s.h"
#include "d/d_stage_mgr.h"
#include "m/m_mtx.h"
#include "m/m_vec.h"
#include "nw4r/g3d/g3d_camera.h"
#include "nw4r/lyt/lyt_bounding.h"
#include "nw4r/lyt/lyt_pane.h"
#include "nw4r/ut/ut_RuntimeTypeInfo.h"
#include "rvl/MTX/mtx.h"
#include "toBeSorted/d_d3d.h"
#include "toBeSorted/lyt_related_floats.h"

dCsMgr_c::dCsMgr_c() {
    // NONMATCHING - TList ctor issues
    field_0x18 = true;
    sInstance = this;
}

extern "C" bool lbl_80572D10;
void dCsMgr_c::execute() {
    // NONMATCHING - TList
    if (field_0x18 == 0 && lbl_80572D10 == 0) {
        for (CursorList::Iterator it = mList1.GetBeginIter(); it != mList1.GetEndIter(); ++it) {
            it->mpHit = nullptr;
        }
        return;
    }

    for (CursorList::Iterator it = mList1.GetBeginIter(); it != mList1.GetEndIter(); ++it) {
        mVec2_c &pos = it->getCursorPos();
        s16 x = pos.x;
        s16 y = pos.y;
        it->mpHit = 0;
        u32 mask = it->mCursorMask;

        if (mask) {
            for (HitCheckList::Iterator chkIt = mList2.GetBeginIter(); chkIt != mList2.GetEndIter(); ++it) {
                if ((chkIt->mMask & mask) != 0) {
                    bool hit = chkIt->checkHit(x, y);
                    if (chkIt->checkOverrideCallback(hit)) {
                        it->mpHit = &*chkIt;
                        break;
                    }
                }
            }
        }
    }
}

void dCsMgr_c::registCursor(dCursorInterface_c *cursor) {
    mList1.append(cursor);
}

void dCsMgr_c::unregistCursor(dCursorInterface_c *cursor) {
    // NONMATCHING - TList
    mList1.remove(cursor);
}

void dCsMgr_c::registCursorTarget(dCursorHitCheck_c *target) {
    // NONMATCHING - TList
    HitCheckList::Iterator it = mList2.GetBeginIter();
    HitCheckList::Iterator end = mList2.GetEndIter();
    while (it != end) {
        if (target->mPriority < it->mPriority) {
            if (it == end) {
                mList2.prepend(target);
                return;
            }

            mList2.insertBefore(&*it, target);
            return;
        }
        ++it;
    }
    mList2.append(target);
}

void dCsMgr_c::unregistCursorTarget(dCursorHitCheck_c *target) {
    // NONMATCHING - TList
    for (CursorList::Iterator it = mList1.GetBeginIter(); it != mList1.GetEndIter(); ++it) {
        if (target == it->mpHit) {
            it->mpHit = 0;
        }
    }
    mList2.remove(target);
}

bool dCsMgr_c::isRegist(dCursorHitCheck_c *target) {
    return mList2.GetPosition(target) != mList2.GetEndIter();
}

dCsMgr_c *dCsMgr_c::create(EGG::Heap *heap) {
    return new (heap) dCsMgr_c();
}

bool dCursorHitCheck_c::init(u8 prio, u8 a, u16 mask) {
    if (dCsMgr_c::GetInstance()->isRegist(this)) {
        return false;
    }
    mPriority = prio;
    field_0x017 = a;
    mMask = mask;
    return true;
}

bool dCursorHitCheck_c::checkOverrideCallback(bool b) {
    if (mOverrideHitCallback == nullptr) {
        return b;
    }
    return mOverrideHitCallback(b, this, mCallbackData);
}

bool dCursorHitCheckCC_c::checkHit(s32 x, s32 y) {
    // NONMATCHING - TODO
    mVec3_c pos(x, y, 10000.0f);
    pos.x = (pos.x - get_8057515C()) / get_80575148() * -2.0f - 1.0f;
    pos.y = (pos.y - get_8057519C()) / get_80575190() * 2.0f - 1.0f;
    mVec3_c v;
    d3d::fn_80016B60(v, pos);
    nw4r::g3d::Camera cam = dStageMgr_c::GetInstance()->getCamera(0);
    mMtx_c mtx;
    cam.GetCameraMtx(mtx);
    PSMTXInverse(mtx, mtx);
    mVec3_c a;
    a.x = mtx.m[0][3];
    a.y = mtx.m[1][3];
    a.z = mtx.m[2][3];
    dBgS_LinChk ck;
    ck.Set(&a, &v, nullptr);
    dBgS::GetInstance()->LineCross(&ck);
    cCcD_ShapeAttr *attr = nullptr;
    if (dCcS::GetInstance()->fn_80357c90(a, v, 0, &attr, 15.0f)) {
        // TODO - this branches into unsplit code in col/
        return true;
    } else {
        return false;
    }
}

bool dCursorHitCheckCC_c::init(u16 mask, u8 prio, u8 a) {
    return dCursorHitCheck_c::init(prio, a, mask);
}

dCursorHitCheckLyt_c::~dCursorHitCheckLyt_c() {
    delete[] mpBoundingData;
    mpBoundingData = nullptr;
}

bool dCursorHitCheckLyt_c::checkHit(s32 x, s32 y) {
    dCsCheckLyt_BoundingData *p = &mpBoundingData[0];
    for (u16 i = 0; i < mNumBoundings; i++) {
        if (p->mMinX <= x && x <= p->mMaxX && p->mMaxY >= y && y >= p->mMinY) {
            mpHitPane = p->mpPane;
            if (mpHitPane->GetAlpha() != 0 && mpHitPane->IsVisible()) {
                return true;
            }
        }
        p++;
    }

    mpHitPane = nullptr;
    return false;
}

bool dCursorHitCheckLyt_c::init(nw4r::lyt::Pane *pPane, u16 mask, u8 prio, u8 a) {
    if (!dCursorHitCheck_c::init(prio, a, mask)) {
        return false;
    }

    mpRootPane = pPane;
    mNumBoundings = 0;

    countBoundings(pPane);

    dCsCheckLyt_BoundingData *pData = new dCsCheckLyt_BoundingData[mNumBoundings]();
    mpBoundingData = pData;
    dCsCheckLyt_BoundingData *pEnd = pData + mNumBoundings;
    gatherBoundings(&pEnd, mpRootPane);

    return true;
}

void dCursorHitCheckLyt_c::execute() {
    if (!mHasCalculatedBoundingData) {
        dCsCheckLyt_BoundingData *dat = &mpBoundingData[0];
        for (u16 i = 0; i < mNumBoundings; i++) {
            nw4r::lyt::Pane *pane = dat->mpPane;
            f32 xx = pane->GetGlobalMtx()._00;
            switch (pane->GetPositionH()) {
                case 0:
                    dat->mMinX = (s16)pane->GetGlobalMtx()._03;
                    dat->mMaxX = (s16)pane->GetGlobalMtx()._03 + xx * pane->GetSize().width;
                    break;
                case 1:
                    dat->mMinX = (s16)pane->GetGlobalMtx()._03 - xx * pane->GetSize().width * 0.5f;
                    dat->mMaxX = (s16)pane->GetGlobalMtx()._03 + xx * pane->GetSize().width * 0.5f;
                    break;
                case 2:
                    dat->mMinX = (s16)pane->GetGlobalMtx()._03 - xx * pane->GetSize().width;
                    dat->mMaxX = (s16)pane->GetGlobalMtx()._03;
                    break;
            }

            switch (pane->GetPositionV()) {
                case 0:
                    dat->mMaxY = (s16)pane->GetGlobalMtx()._13;
                    dat->mMinY = (s16)pane->GetGlobalMtx()._13 - pane->GetSize().height;
                    break;
                case 1:
                    dat->mMaxY = (s16)pane->GetGlobalMtx()._13 + (pane->GetSize().height * 0.5f);
                    dat->mMinY = (s16)pane->GetGlobalMtx()._13 - (pane->GetSize().height * 0.5f);
                    break;
                case 2:
                    dat->mMaxY = (s16)pane->GetGlobalMtx()._13 + pane->GetSize().height;
                    dat->mMinY = (s16)pane->GetGlobalMtx()._13;
                    break;
            }

            dat++;
        }

        mHasCalculatedBoundingData = true;
    }
}

void dCursorHitCheckLyt_c::countBoundings(nw4r::lyt::Pane *pane) {
    nw4r::lyt::Bounding *b = nw4r::ut::DynamicCast<nw4r::lyt::Bounding *>(pane);
    if (b != nullptr) {
        mNumBoundings++;
    }

    for (nw4r::lyt::Pane::ChildList::RevIterator it = pane->GetChildList()->GetEndReverseIter();
         it != pane->GetChildList()->GetBeginIter(); ++it) {
        countBoundings(&*it);
    }
}

void dCursorHitCheckLyt_c::gatherBoundings(dCsCheckLyt_BoundingData **pEnd, nw4r::lyt::Pane *pane) {
    nw4r::lyt::Bounding *b = nw4r::ut::DynamicCast<nw4r::lyt::Bounding *>(pane);
    if (b != nullptr) {
        (*pEnd)--;
        (*pEnd)->mpPane = pane;
    }

    for (nw4r::lyt::Pane::ChildList::RevIterator it = pane->GetChildList()->GetEndReverseIter();
         it != pane->GetChildList()->GetBeginIter(); ++it) {
        gatherBoundings(pEnd, &*it);
    }
}

mVec2_c &dCursorInterface_c::getCursorPos() {
    // NONMATCHING - Controller stuff
    return *(mVec2_c *)nullptr;
}