summaryrefslogtreecommitdiff
path: root/src/nw4r/lyt/lyt_layout.cpp
blob: dcacf110c2b4f4bd46f9d55a8a73cf369102f24f (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
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
#include "nw4r/lyt/lyt_layout.h"

#include "nw4r/lyt/lyt_animation.h"
#include "nw4r/lyt/lyt_bounding.h"
#include "nw4r/lyt/lyt_group.h"
#include "nw4r/lyt/lyt_picture.h"
#include "nw4r/lyt/lyt_textBox.h"
#include "nw4r/lyt/lyt_utils.h"
#include "nw4r/lyt/lyt_window.h"
#include "nw4r/ut/ut_RuntimeTypeInfo.h"
#include "nw4r/ut/ut_TagProcessorBase.h"


namespace nw4r {

namespace lyt {

// mspAllocator__Q34nw4r3lyt6Layout
MEMAllocator *Layout::mspAllocator;

namespace {

// SetTagProcessorImpl__Q34nw4r3lyt24@unnamed@lyt_layout_cpp@FPQ34nw4r3lyt4PanePQ34nw4r2ut19TagProcessorBase<w>
void SetTagProcessorImpl(Pane *pPane, ut::TagProcessorBase<wchar_t> *pTagProcessor) {
    TextBox *pTextBox = ut::DynamicCast<TextBox *>(pPane);
    if (pTextBox) {
        pTextBox->SetTagProcessor(pTagProcessor);
    }
    for (ut::LinkList<Pane, 4>::Iterator it = pPane->GetChildList()->GetBeginIter();
         it != pPane->GetChildList()->GetEndIter(); ++it) {
        SetTagProcessorImpl(&*it, pTagProcessor);
    }
}

// IsIncludeAnimationGroupRef__Q34nw4r3lyt24@unnamed@lyt_layout_cpp@FPQ34nw4r3lyt14GroupContainerPCQ34nw4r3lyt17AnimationGroupRefUsbPQ34nw4r3lyt4Pane
// Doesnt match DWARF Vars, but matches
bool IsIncludeAnimationGroupRef(
    GroupContainer *pGroupContainer, const AnimationGroupRef *groupRefs, u16 bindGroupNum, bool bDescendingBind,
    Pane *pTargetPane
) {
    for (u16 grpIdx = 0; grpIdx < bindGroupNum; grpIdx++) {
        Group *pGroup = pGroupContainer->FindGroupByName(groupRefs[grpIdx].GetName());
        for (ut::LinkList<detail::PaneLink, 0>::Iterator paneList = pGroup->GetPaneList()->GetBeginIter();
             paneList != pGroup->GetPaneList()->GetEndIter(); paneList++) {
            const Pane *t = paneList->mTarget;
            if (t == pTargetPane) {
                return true;
            }
            if (bDescendingBind) {
                for (const Pane *pParentPane = pTargetPane->GetParent(); pParentPane != nullptr;
                     pParentPane = pParentPane->GetParent()) {
                    if (t == pParentPane) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

} // namespace

// __ct__Q34nw4r3lyt6LayoutFv
Layout::Layout() : mAnimTransList(), mpRootPane(nullptr), mpGroupContainer(nullptr) {}

// __dt__Q34nw4r3lyt6LayoutFv
Layout::~Layout() {
    DeleteObj(mpGroupContainer);
    if (mpRootPane && !mpRootPane->IsUserAllocated()) {
        DeleteObj(mpRootPane);
    }
    ut::LinkList<AnimTransform, 4>::Iterator it = mAnimTransList.GetBeginIter();
    while (it != mAnimTransList.GetEndIter()) {
        ut::LinkList<AnimTransform, 4>::Iterator currIt = it++;
        mAnimTransList.Erase(currIt);
        DeleteObj(&*currIt);
    }
}

// Build__Q34nw4r3lyt6LayoutFPCvPQ34nw4r3lyt16ResourceAccessor
bool Layout::Build(const void *lytResBuf, ResourceAccessor *pResAcsr) {
    const res::BinaryFileHeader *pFileHead = (const res::BinaryFileHeader *)(lytResBuf);
    if (!detail::TestFileHeader(*pFileHead, 'RLYT')) {
        return false;
    }
    if (!detail::TestFileVersion(*pFileHead)) {
        return false;
    }

    ResBlockSet resBlockSet = {nullptr, nullptr, nullptr, pResAcsr};
    Pane *pParentPane = nullptr;
    Pane *pLastPane = nullptr;
    bool bReadRootGroup = false;
    int groupNestLevel = 0;

    const void *dataPtr = ((u8 *)lytResBuf + pFileHead->headerSize);
    for (int i = 0; i < pFileHead->dataBlocks; i++) {
        const res::DataBlockHeader *pDataBlockHead = (const res::DataBlockHeader *)dataPtr;
        switch (detail::GetSignatureInt(pDataBlockHead->kind)) {
            case 'lyt1': // Main Layout
            {
                const res::Layout *pResLyt = ((const res::Layout *)dataPtr);
                mLayoutSize = pResLyt->layoutSize;
            } break;
            case 'txl1': // Texture List
                resBlockSet.pTextureList = (const res::TextureList *)dataPtr;
                break;
            case 'fnl1': // Font List
                resBlockSet.pFontList = (const res::FontList *)dataPtr;
                break;
            case 'mat1': // Material
                resBlockSet.pMaterialList = (const res::MaterialList *)dataPtr;
                break;
            case 'wnd1': // Window
            case 'pan1': // Pane
            case 'pic1': // Picture
            case 'txt1': // Text Box
            case 'bnd1': // Boundary Pane
            {
                Pane *pPane = BuildPaneObj(detail::GetSignatureInt(pDataBlockHead->kind), dataPtr, resBlockSet);
                if (pPane) {
                    if (mpRootPane == nullptr) {
                        mpRootPane = pPane;
                    }
                    if (pParentPane) {
                        pParentPane->AppendChild(pPane);
                    }
                    pLastPane = pPane;
                }
            } break;
            case 'usd1': // User Data
                pLastPane->SetExtUserDataList((const res::ExtUserDataList *)dataPtr);
                break;
            case 'pas1': // PaneChildren Start
                pParentPane = pLastPane;
                break;
            case 'pae1': // PaneChildren End
                pLastPane = pParentPane;
                pParentPane = pLastPane->GetParent();
                break;
            case 'grp1': // Group
                if (!bReadRootGroup) {
                    bReadRootGroup = true;
                    mpGroupContainer = NewObj<GroupContainer>();
                } else {
                    if (mpGroupContainer && groupNestLevel == 1) {
                        Group *pGroup = NewObj<Group>((const res::Group *)dataPtr, mpRootPane);
                        if (pGroup) {
                            mpGroupContainer->AppendGroup(pGroup);
                        }
                    }
                }
                break;
            case 'grs1': // Group Children Start
                groupNestLevel++;
                break;
            case 'gre1': // Group Children End
                groupNestLevel--;
                break;
            default: break;
        }
        dataPtr = ((u8 *)dataPtr + pDataBlockHead->size);
    }
    return true;
}

// CreateAnimTransform__Q34nw4r3lyt6LayoutFv
AnimTransform *Layout::CreateAnimTransform() {
    AnimTransformBasic *pAnimTrans = NewObj<AnimTransformBasic>();
    if (pAnimTrans) {
        mAnimTransList.PushBack(pAnimTrans);
    }
    return pAnimTrans;
}

// CreateAnimTransform__Q34nw4r3lyt6LayoutFPCvPQ34nw4r3lyt16ResourceAccessor
AnimTransform *Layout::CreateAnimTransform(const void *animResBuf, ResourceAccessor *pResAcsr) {
    return CreateAnimTransform(AnimResource(animResBuf), pResAcsr);
}

// CreateAnimTransform__Q34nw4r3lyt6LayoutFRCQ34nw4r3lyt12AnimResourcePQ34nw4r3lyt16ResourceAccessor
AnimTransform *Layout::CreateAnimTransform(const AnimResource &animRes, ResourceAccessor *pResAcsr) {
    const res::AnimationBlock *pAnimBlock = animRes.GetResourceBlock();
    if (!pAnimBlock) {
        return nullptr;
    }

    AnimTransform *pAnimTrans = CreateAnimTransform();
    if (pAnimTrans) {
        pAnimTrans->SetResource(pAnimBlock, pResAcsr);
    }

    return pAnimTrans;
}

// BindAnimation__Q34nw4r3lyt6LayoutFPQ34nw4r3lyt13AnimTransform
void Layout::BindAnimation(AnimTransform *pAnimTrans) {
    if (!mpRootPane) {
        return;
    }
    mpRootPane->BindAnimation(pAnimTrans, true, false);
}

// UnbindAnimation__Q34nw4r3lyt6LayoutFPQ34nw4r3lyt13AnimTransform
void Layout::UnbindAnimation(AnimTransform *pAnimTrans) {
    if (!mpRootPane) {
        return;
    }
    mpRootPane->UnbindAnimation(pAnimTrans, true);
}

// UnbindAllAnimation__Q34nw4r3lyt6LayoutFv
void Layout::UnbindAllAnimation() {
    UnbindAnimation(nullptr);
}

// BindAnimationAuto__Q34nw4r3lyt6LayoutFRCQ34nw4r3lyt12AnimResourcePQ34nw4r3lyt16ResourceAccessor
bool Layout::BindAnimationAuto(const AnimResource &animRes, ResourceAccessor *pResAcsr) {
    // Ensure Root pane and Resource Block Exists
    if (!mpRootPane) {
        return false;
    }
    if (!animRes.GetResourceBlock()) {
        return false;
    }

    AnimTransform *pAnimTrans = CreateAnimTransform();
    u16 bindGroupNum = animRes.GetGroupNum();
    u16 animNum = 0;
    if (bindGroupNum == 0) {
        // No Groups to bind with, only bind to root pane
        pAnimTrans->SetResource(animRes.GetResourceBlock(), pResAcsr, animRes.GetResourceBlock()->animContNum);
        mpRootPane->BindAnimation(pAnimTrans, true, true);
    } else {
        // Bind to all Groups
        const AnimationGroupRef *groupRefs = animRes.GetGroupArray();
        for (int grpIdx = 0; grpIdx < bindGroupNum; grpIdx++) {
            Group *pGroup = mpGroupContainer->FindGroupByName(groupRefs[grpIdx].GetName());
            animNum += animRes.CalcAnimationNum(pGroup, animRes.IsDescendingBind());
        }
        pAnimTrans->SetResource(animRes.GetResourceBlock(), pResAcsr, animNum);
        for (int grpIdx = 0; grpIdx < bindGroupNum; grpIdx++) {
            Group *pGroup = mpGroupContainer->FindGroupByName(groupRefs[grpIdx].GetName());
            lyt::BindAnimation(pGroup, pAnimTrans, animRes.IsDescendingBind(), true);
        }
    }
    u16 animSharInfoNum = animRes.GetAnimationShareInfoNum();
    if (animSharInfoNum != 0) {
        const AnimationShareInfo *animSharInfoAry = animRes.GetAnimationShareInfoArray();
        for (int i = 0; i < animSharInfoNum; i++) {
            Pane *pSrcPane = mpRootPane->FindPaneByName(animSharInfoAry[i].GetSrcPaneName(), true);
            detail::AnimPaneTree animPaneTree = detail::AnimPaneTree(pSrcPane, animRes);
            if (animPaneTree.GetLinkNum()) {
                Group *pGroup = mpGroupContainer->FindGroupByName(animSharInfoAry[i].GetTargetGroupName());
                ut::LinkList<detail::PaneLink, 0> *paneList = pGroup->GetPaneList();
                for (ut::LinkList<detail::PaneLink, 0>::Iterator it = paneList->GetBeginIter();
                     it != paneList->GetEndIter(); it++) {
                    if (it->mTarget != pSrcPane) {
                        if (bindGroupNum != 0) {
                            bool bInclude = IsIncludeAnimationGroupRef(
                                mpGroupContainer, animRes.GetGroupArray(), bindGroupNum, animRes.IsDescendingBind(),
                                it->mTarget
                            );
                            if (!bInclude) {
                                continue;
                            }
                        }
                        animPaneTree.Bind(this, it->mTarget, pResAcsr);
                    }
                }
            }
        }
    }
    return true;
}

// SetAnimationEnable__Q34nw4r3lyt6LayoutFPQ34nw4r3lyt13AnimTransformb
void Layout::SetAnimationEnable(AnimTransform *pAnimTrans, bool bEnable) {
    if (!mpRootPane) {
        return;
    }
    mpRootPane->SetAnimationEnable(pAnimTrans, bEnable, true);
}

// CalculateMtx__Q34nw4r3lyt6LayoutFRCQ34nw4r3lyt8DrawInfo
void Layout::CalculateMtx(const DrawInfo &drawInfo) {
    if (!mpRootPane) {
        return;
    }
    mpRootPane->CalculateMtx(drawInfo);
}

// Draw__Q34nw4r3lyt6LayoutFRCQ34nw4r3lyt8DrawInfo
void Layout::Draw(const DrawInfo &drawInfo) {
    if (!mpRootPane) {
        return;
    }
    mpRootPane->Draw(drawInfo);
}

// Animate__Q34nw4r3lyt6LayoutFUl
void Layout::Animate(u32 option) {
    if (!mpRootPane) {
        return;
    }
    mpRootPane->Animate(option);
}

// GetLayoutRect__Q34nw4r3lyt6LayoutCFv
ut::Rect Layout::GetLayoutRect() const {
    return ut::Rect(-mLayoutSize.width / 2, mLayoutSize.height / 2, mLayoutSize.width / 2, -mLayoutSize.height / 2);
}

// SetTagProcessor__Q34nw4r3lyt6LayoutFPQ34nw4r2ut19TagProcessorBase<w>
void Layout::SetTagProcessor(ut::TagProcessorBase<wchar_t> *pTagProcessor) {
    SetTagProcessorImpl(this->mpRootPane, pTagProcessor);
}

// BuildPaneObj__Q34nw4r3lyt6LayoutFlPCvRCQ34nw4r3lyt11ResBlockSet
Pane *Layout::BuildPaneObj(s32 kind, const void *dataPtr, const ResBlockSet &resBlockSet) {
    // Oddly enough the breaks are required here to not inline the function???
    // Had them as left over from editing and somehow when removing them it just broke Build.
    // Probably some analysis depth as 3 break statements do it
    switch (kind) {
        case 'pan1': {
            const res::Pane *pResPane = (const res::Pane *)dataPtr;
            return NewObj<Pane>(pResPane);
        } break;
        case 'pic1': {
            const res::Picture *pResPic = (const res::Picture *)dataPtr;
            return NewObj<Picture>(pResPic, resBlockSet);
        } break;
        case 'txt1': {
            const res::TextBox *pBlock = (const res::TextBox *)dataPtr;
            return NewObj<TextBox>(pBlock, resBlockSet);
        } break;
        case 'wnd1': {
            const res::Window *pBlock = (const res::Window *)dataPtr;
            return NewObj<Window>(pBlock, resBlockSet);
        } break;
        case 'bnd1': {
            const res::Bounding *pResBounding = (const res::Bounding *)dataPtr;
            return NewObj<Bounding>(pResBounding, resBlockSet);
        } break;
        default: return nullptr;
    }
}

} // namespace lyt

} // namespace nw4r