summaryrefslogtreecommitdiff
path: root/libs/JSystem/src/J2DGraph/J2DTextBox.cpp
blob: 8f8a1681049826a6fd6d9dd4cfb69ea80275e9d6 (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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include "JSystem/JSystem.h" // IWYU pragma: keep

#include "JSystem/J2DGraph/J2DTextBox.h"
#include "JSystem/J2DGraph/J2DPrint.h"
#include "JSystem/JSupport/JSURandomInputStream.h"
#include "JSystem/JUtility/JUTResFont.h"
#include "JSystem/JUtility/JUTResource.h"
#include <cstring>

J2DTextBox::J2DTextBox()
    : mFont(NULL), mCharColor(), mGradientColor(), mStringPtr(NULL), mWhiteColor(), mBlackColor() {
    initiate(NULL, NULL, 0, HBIND_LEFT, VBIND_TOP);
}

J2DTextBox::J2DTextBox(J2DPane* p_pane, JSURandomInputStream* p_stream, JKRArchive* p_archive)
    : mFont(NULL), mCharColor(), mGradientColor(), mStringPtr(NULL), mWhiteColor(), mBlackColor() {
    private_readStream(p_pane, p_stream, p_archive);
}

J2DTextBox::J2DTextBox(J2DPane* p_pane, JSURandomInputStream* p_stream, u32 param_2,
                       J2DMaterial* p_material)
    : mFont(NULL), mCharColor(), mGradientColor(), mStringPtr(NULL), mWhiteColor(), mBlackColor() {
    J2DTextBoxInfo info;

    int startPos = p_stream->getPosition();

    J2DTbxBlockHeader header;
    p_stream->read(&header, 8);
    mKind = 'TBX1';

    int panHeaderPos = p_stream->getPosition();
    J2DTbxBlockHeader panHeader;
    p_stream->peek(&panHeader, 8);
    makePaneExStream(p_pane, p_stream);
    p_stream->seek(panHeaderPos + panHeader.mSize, JSUStreamSeekFrom_SET);

    p_stream->read(&info, 0x20);
    J2DMaterial* mat = NULL;

    if (info.mMaterialNum != 0xFFFF) {
        mat = &p_material[info.mMaterialNum];

        if (mat != NULL) {
            if (mat->getTevBlock() != NULL) {
                JUTFont* font = mat->getTevBlock()->getFont();

                if (font != NULL) {
                    mFont = font;
                    mat->getTevBlock()->setFontUndeleteFlag();
                }
            }
            mAlpha = mat->getColorBlock()->getMatColor(0)->a;
        }
    }

    mCharSpacing = info.mCharSpace;
    mLineSpacing = info.mLineSpace;
    mFontSizeX = info.mFontSizeX;
    mFontSizeY = info.mFontSizeY;
    mFlags = (info.mHBind << 2) | info.mVBind;
    mCharColor = JUtility::TColor(info.mCharColor);
    mGradientColor = JUtility::TColor(info.mGradColor);
    setConnectParent(info.mConnected);

    u16 strLength = 0;
    if (!(param_2 & 0x2000000)) {
        strLength = info.field_0x1c;
        if ((s16)info.field_0x1c == -1) {
            strLength = info.field_0x1e + 1;
        }
    }

    mStringLength = 0;
    mStringPtr = NULL;

    if (strLength != 0) {
        mStringPtr = new char[strLength];
    }

    if (mStringPtr != NULL) {
        mStringLength = strLength;

        u16 var_r26_2 = strLength - 1 < info.field_0x1e ? u16(strLength - 1) : info.field_0x1e;

        p_stream->peek(mStringPtr, var_r26_2);
        mStringPtr[var_r26_2] = 0;
    }

    p_stream->skip(info.field_0x1e);
    p_stream->seek(startPos + header.mSize, JSUStreamSeekFrom_SET);
    mBlackColor = JUtility::TColor(0);
    mWhiteColor = JUtility::TColor(0xFFFFFFFF);

    if (mat != NULL && mat->getTevBlock() != NULL) {
        u8 tevStageNum = u32(mat->getTevBlock()->getTevStageNum());
        if (tevStageNum != 1) {
            J2DGXColorS10 color0(*mat->getTevBlock()->getTevColor(0));

            J2DGXColorS10 color1(*mat->getTevBlock()->getTevColor(1));

            mBlackColor = JUtility::TColor(((u8)color0.r << 0x18) | ((u8)color0.g << 0x10) |
                                           ((u8)color0.b << 8) | (u8)color0.a);
            mWhiteColor = JUtility::TColor(((u8)color1.r << 0x18) | ((u8)color1.g << 0x10) |
                                           ((u8)color1.b << 8) | (u8)color1.a);
        }
    }

    field_0x10c = 0.0f;
    field_0x110 = 0.0f;
    mTextFontOwned = true;
}

J2DTextBox::J2DTextBox(u64 tag, JGeometry::TBox2<f32> const& bounds, ResFONT const* p_font,
                       char const* string, s16 strLength, J2DTextBoxHBinding hBind,
                       J2DTextBoxVBinding vBind)
    : J2DPane(tag, bounds), mFont(NULL), mCharColor(), mGradientColor(), mStringPtr(NULL),
      mWhiteColor(), mBlackColor() {
    initiate(p_font, string, strLength, hBind, vBind);
}

void J2DTextBox::initiate(ResFONT const* p_font, char const* string, s16 length,
                          J2DTextBoxHBinding hBind, J2DTextBoxVBinding vBind) {
    if (p_font != NULL) {
        mFont = new JUTResFont(p_font, NULL);
    }

    mCharColor.set(0xFFFFFFFF);
    mGradientColor.set(0xFFFFFFFF);
    mBlackColor = JUtility::TColor(0);
    mWhiteColor = JUtility::TColor(0xFFFFFFFF);

    mFlags = (hBind << 2) | vBind;
    mStringLength = 0;
    mStringPtr = NULL;

    if (string != NULL && length != 0) {
        u32 len = strlen(string);
        u16 stringLen = length;

        if (length == -1) {
            if (len >= 0xFFFF) {
                len = 0xFFFF - 1;
            }
            stringLen = len + 1;
        }

        mStringPtr = new char[stringLen];

        if (stringLen != 0 && mStringPtr != NULL) {
            strncpy(mStringPtr, string, stringLen - 1);
            mStringPtr[stringLen - 1] = 0;
            mStringLength = stringLen;
        }
    }

    field_0x10c = 0.0f;
    field_0x110 = 0.0f;
    mCharSpacing = 0.0f;

    if (mFont == NULL) {
        mLineSpacing = 0.0f;
        mFontSizeX = 0.0f;
        mFontSizeY = 0.0f;
    } else {
        mLineSpacing = mFont->getLeading();
        mFontSizeX = mFont->getWidth();
        mFontSizeY = mFont->getHeight();
    }

    mKind = 'TBX1';
    mTextFontOwned = true;
}

void J2DTextBox::private_readStream(J2DPane* p_pane, JSURandomInputStream* p_stream,
                                    JKRArchive* p_archive) {
    int position = p_stream->getPosition();

    J2DTbxBlockHeader header;
    p_stream->read(&header, 8);
    mKind = header.mTag;

    makePaneStream(p_pane, p_stream);
    {JUTResReference ref;}
    u8 spA = p_stream->readU8();

    ResFONT* fontPtr = (ResFONT*)getPointer(p_stream, 'FONT', p_archive);
    if (fontPtr != NULL) {
        mFont = new JUTResFont(fontPtr, NULL);
    }

    mCharColor.set(p_stream->read32b());
    mGradientColor.set(p_stream->read32b());
    u8 tmp = p_stream->read8b();
    mFlags = tmp;
    mCharSpacing = p_stream->readS16();
    mLineSpacing = p_stream->readS16();
    mFontSizeX = p_stream->read16b();
    mFontSizeY = p_stream->read16b();

    s16 stringLen = p_stream->read16b();
    mStringPtr = new char[stringLen + 1];

    if (mStringPtr != NULL) {
        p_stream->read(mStringPtr, stringLen);
        mStringPtr[stringLen] = 0;
        mStringLength = stringLen + 1;
    } else {
        p_stream->skip(stringLen);
        mStringLength = 0;
    }

    spA -= 10;
    if (spA != 0) {
        tmp = p_stream->read8b();
        if (tmp != 0) {
            setConnectParent(true);
        }

        spA--;
    }

    mBlackColor = JUtility::TColor(0);
    mWhiteColor = JUtility::TColor(0xFFFFFFFF);

    if (spA != 0) {
        mBlackColor.set(p_stream->read32b());
        spA--;
    }

    if (spA != 0) {
        mWhiteColor.set(p_stream->read32b());
        spA--;
    }

    field_0x10c = 0.0f;
    field_0x110 = 0.0f;
    p_stream->seek(position + header.mSize, JSUStreamSeekFrom_SET);
    mTextFontOwned = true;
}

J2DTextBox::~J2DTextBox() {
    if (mTextFontOwned) {
        delete mFont;
    }

    delete[] mStringPtr;
}

void J2DTextBox::setFont(JUTFont* p_font) {
    if (p_font) {
        if (mTextFontOwned) {
            delete mFont;
        }
        mFont = p_font;
        mTextFontOwned = false;
    }
}

void J2DTextBox::draw(f32 posX, f32 posY) {
    Mtx m;

    if (isVisible()) {
        J2DPrint print(mFont, mCharSpacing, mLineSpacing, mCharColor, mGradientColor, mBlackColor,
                       mWhiteColor);
        print.setFontSize(mFontSizeX, mFontSizeY);
        makeMatrix(posX, posY, 0.0f, 0.0f);

        GXLoadPosMtxImm(mPositionMtx, GX_PNMTX0);
        GXSetCurrentMtx(0);
        GXSetNumIndStages(0);
        for (int i = 0; i < 16; i++) {
            GXSetTevDirect((GXTevStageID)i);
        }
        GXSetNumTexGens(1);
        GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, 60);

        if (mStringPtr != NULL) {
            print.print(0.0f, 0.0f, mAlpha, "%s", mStringPtr);
        }
        MTXIdentity(m);
        GXLoadPosMtxImm(m, 0);
    }
}

void J2DTextBox::draw(f32 posX, f32 posY, f32 param_2, J2DTextBoxHBinding hBind) {
    Mtx m;

    if (isVisible()) {
        J2DPrint print(mFont, mCharSpacing, mLineSpacing, mCharColor, mGradientColor, mBlackColor,
                       mWhiteColor);
        print.setFontSize(mFontSizeX, mFontSizeY);
        makeMatrix(posX, posY, 0.0f, 0.0f);

        GXLoadPosMtxImm(mPositionMtx, GX_PNMTX0);
        GXSetCurrentMtx(0);
        GXSetNumIndStages(0);
        for (int i = 0; i < 16; i++) {
            GXSetTevDirect((GXTevStageID)i);
        }
        GXSetNumTexGens(1);
        GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, 60);

        if (mStringPtr != NULL) {
            print.printReturn(mStringPtr, param_2, 0.0f, hBind, VBIND_TOP, 0.0f, -mFontSizeY,
                              mAlpha);
        }
        MTXIdentity(m);
        GXLoadPosMtxImm(m, 0);
    }
}

char* J2DTextBox::getStringPtr() const {
    return mStringPtr;
}

s32 J2DTextBox::setString(char const* string, ...) {
    va_list args;
    va_start(args, string);

    delete[] mStringPtr;

    u32 len = strlen(string);

    if (len >= 0xFFFF) {
        len = 0xFFFF - 1;
    }

    mStringLength = 0;
    mStringPtr = new char[len + 1];

    if (mStringPtr) {
        mStringLength = len + 1;
        strcpy(mStringPtr, string);
    }

    va_end(args);
    return len;
}

s32 J2DTextBox::setString(s16 length, char const* string, ...) {
    va_list args;
    va_start(args, string);

    delete[] mStringPtr;
    mStringPtr = NULL;

    u32 len = strlen(string);
    u16 stringLen = length;

    if (length == -1) {
        if (len >= 0xFFFF) {
            len = 0xFFFF - 1;
        }
        stringLen = len + 1;
    }

    mStringLength = 0;

    if (stringLen != 0) {
        mStringPtr = new char[stringLen];
    }

    if (mStringPtr != NULL) {
        strncpy(mStringPtr, string, stringLen - 1);
        mStringPtr[stringLen - 1] = 0;
        mStringLength = stringLen;
    }

    va_end(args);
    return len;
}

bool J2DTextBox::setConnectParent(bool connected) {
    if (getPaneTree()->getParent() == NULL) {
        return false;
    }

    if (getPaneTree()->getParent()->getObject()->getTypeID() != 0x11) {
        return false;
    }

    mConnected = connected;
    return connected;
}

void J2DTextBox::drawSelf(f32 param_0, f32 param_1) {
    Mtx identity;
    MTXIdentity(identity);

    drawSelf(param_0, param_1, &identity);
}

void J2DTextBox::drawSelf(f32 param_0, f32 param_1, Mtx* p_mtx) {
    Mtx m;

    J2DPrint print(mFont, mCharSpacing, mLineSpacing, mCharColor, mGradientColor, mBlackColor,
                   mWhiteColor);
    print.setFontSize(mFontSizeX, mFontSizeY);
    MTXConcat(*p_mtx, mGlobalMtx, m);

    GXLoadPosMtxImm(m, GX_PNMTX0);
    GXSetNumIndStages(0);
    for (int i = 0; i < 16; i++) {
        GXSetTevDirect((GXTevStageID)i);
    }
    GXSetNumTexGens(1);
    GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, 60);

    print.locate(param_0 + mBounds.i.x, param_1 + mBounds.i.y);
    if (mStringPtr != NULL) {
        print.printReturn(mStringPtr, mBounds.getWidth() + 0.0001f, mBounds.getHeight(),
                          getHBinding(), getVBinding(), field_0x10c, field_0x110, mColorAlpha);
    }
}

void J2DTextBox::resize(f32 x, f32 y) {
    if (mConnected && getPaneTree() != NULL && getPaneTree()->getParent() != NULL) {
        J2DPane* obj = getPaneTree()->getParent()->getObject();

        if (obj->getTypeID() == 0x11) {
            f32 obj_x = obj->getWidth() + (x - getWidth());
            f32 obj_y = obj->getHeight() + (y - getHeight());
            obj->resize(obj_x, obj_y);
            return;
        }
    }

    J2DPane::resize(x, y);
}

bool J2DTextBox::isUsed(ResFONT const* p_font) {
    if (mFont != NULL && mFont->getResFont() == p_font) {
        return true;
    }

    return J2DPane::isUsed(p_font);
}

u16 J2DTextBox::getTypeID() const {
    return 19;
}

bool J2DTextBox::isUsed(ResTIMG const* p_timg) {
    return J2DPane::isUsed(p_timg);
}

void J2DTextBox::rewriteAlpha() {
    /* empty function */
}