summaryrefslogtreecommitdiff
path: root/src/engine/actors/Text.cpp
blob: d2468d2a1000590796bf21e717062f2f18831b79 (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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#include <libultraship.h>
#include <libultra/gbi.h>
#include <vector>

#include "Text.h"

#include "port/interpolation/FrameInterpolation.h"
#include "engine/Matrix.h"
#include "engine/editor/EditorMath.h"

extern "C" {
#include "defines.h"
#include "main.h"
#include "menu_items.h"
#include "assets/models/data_segment2.h"
#include "render_player.h"
#include "math_util.h"
#include "assets/textures/texture_data_2.h"
#include "render_objects.h"
#include "common_structs.h"
#include "code_80005FD0.h"
}

size_t AText::_count;

AText::AText(const SpawnParams& params) : AActor(params) {
    Name = "Text";
    ResourceName = "hm:text";
    _idx = _count;
    
    SpawnPos = params.Location.value_or(FVector(0.0f, 100.0f, 0.0f));
    Pos[0] = SpawnPos.x;
    Pos[1] = SpawnPos.y;
    Pos[2] = SpawnPos.z;

    SpawnRot = params.Rotation.value_or(IRotator(0, 0, 0));
    Rot[0] = SpawnRot.pitch;
    Rot[1] = SpawnRot.yaw;
    Rot[2] = SpawnRot.roll;

    SpawnScale = params.Scale.value_or(FVector(1.0f, 1.0f, 1.0f));
    Scale = SpawnScale;

    ScaleX = params.FVec2.value_or(FVector{1.0f, 0.0f, 0.0f}).x;

    Mode = static_cast<TextMode>(params.Type.value_or(0)); // STATIONARY

    Animate = params.Bool.value_or(false);
    FaceCamera = params.Bool2.value_or(true);

    WidthOffset = params.Speed.value_or(0.0f);
    HeightOffset = params.SpeedB.value_or(8.0f);

    FVector options = params.Velocity.value_or(FVector{1.0f, 14000.0f, 0.0f});
    LetterSpacing = options.x;
    Far = options.y;
    Close = options.z;

    TextColour[0] = params.Colour.value_or(RGBA8{255, 255, 255, 255});
    TextColour[1] = params.Colour2.value_or(RGBA8{255, 255, 255, 255});
    TextColour[2] = params.Colour3.value_or(RGBA8{255, 255, 255, 255});
    TextColour[3] = params.Colour4.value_or(RGBA8{255, 255, 255, 255});

    PlayerIndex = static_cast<uint32_t>(params.Behaviour.value_or(0));
    if (PlayerIndex < 0 || PlayerIndex >= NUM_PLAYERS) {
        PlayerIndex = 0;
    }

    Text = ValidateString(params.Skin.value_or("Harbour Masters"));
    AText::Print3D((char*)Text.c_str(), 0, CENTER_TEXT_MODE_2);

    _count += 1;
}

/**
 * Filters out bad characters (allows a-z, A-Z, 0-9, space)
 * Returns "Blank Text" for blank input
 * Returns "Invalid" if no valid input found
 * Limits str to 20 characters
 * 
 * The font does support some symbols and other language characters
 * But these need to be checked thoroughly before white-listing.
 */
std::string AText::ValidateString(const std::string_view& s) {
    if (CVarGetInteger("gIsEditorEnabled", false) == true) {
        if (s.empty()) { return "Blank Text"; }
    } else {
        if (s.empty()) { return ""; }
    }

    Text.clear();

    for (char c : s) {
        if (std::isalpha(static_cast<unsigned char>(c)) || 
            std::isdigit(static_cast<unsigned char>(c)) || 
            c == ' ') 
        {
            Text += c;
            if (Text.size() >= 20) {
                break;
            };
        }
    }

    // No valid characters found
    if (Text.empty()) {
        return "Invalid";
    }
    return Text;
}

void AText::SetText(std::string text) {
    AText::ValidateString(text);
    Refresh();
}

/*
 * Most changes during runtime require a refresh because the text is generated statically
 * with the intention of this code being somewhat performant
 */
void AText::Refresh() {
    AText::TextureList.clear();
    AText::Print3D((char*)Text.c_str(), 0, CENTER_TEXT_MODE_2);
}

bool AText::IsMod() { return true; }

void AText::SetSpawnParams(SpawnParams& params) {
    AActor::SetSpawnParams(params);
    params.Name = ResourceName;
    params.Type = static_cast<int16_t>(Mode);
    params.Behaviour = PlayerIndex;
    params.Skin = Text;

    params.Colour = TextColour[0];
    params.Colour2 = TextColour[1];
    params.Colour3 = TextColour[2];
    params.Colour4 = TextColour[3];

    params.Speed = WidthOffset;
    params.SpeedB = HeightOffset;

    params.Velocity = {LetterSpacing, Far, Close};
    params.FVec2 = {ScaleX, 0.0f, 0.0f};

    params.Bool = Animate;
    params.Bool2 = FaceCamera;
}

void AText::Tick() {
    switch(Mode) {
        case STATIONARY:
            break; // Do nothing
        case FOLLOW_PLAYER:
            AText::FollowPlayer();
            break;
    }
}

void AText::FollowPlayer() {
    Pos[0] = gPlayers[PlayerIndex].pos[0] + WidthOffset;
    Pos[1] = gPlayers[PlayerIndex].pos[1] + HeightOffset;
    Pos[2] = gPlayers[PlayerIndex].pos[2];

    // Animate text if player is in first place
    // if (gGPCurrentRaceRankByPlayerId[PlayerIndex] == 0) {
    //     Animate = true;
    // } else {
    //     Animate = false;
    // }
}

void AText::Draw(Camera* camera) {
    switch(Mode) {
        case STATIONARY:
            break; // Do nothing
        case FOLLOW_PLAYER:
            if (PlayerIndex == camera->playerId) {
                return; // Do not draw the local players own name
            }
            if ((gPlayers[PlayerIndex].effects & BOO_EFFECT) == BOO_EFFECT) {
                FadeState = FADE_OUT;
                AText::DrawText3D(camera);
                return; // Skip expensive calculations below
            }
            break;
    }

    f32 distance = is_within_render_distance(camera->pos, (float*)&Pos[0], camera->rot[1], Close,
                                             camera->fieldOfView, Far);

    if (distance == -1.0f) {
        Dist = DistanceProps::TOO_FAR;
    } else if (distance < Close) {
        Dist = DistanceProps::TOO_CLOSE;
    } else {
        Dist = DistanceProps::ACTIVE;
    }

    if (Dist != PrevState) {
        PrevState = Dist;
        if ((distance == -1.0f) || (distance < Close)) {
            FadeState = FADE_OUT;
        } else {
            FadeState = FADE_IN;
        }
    }

    AText::DrawText3D(camera);
}

/**
 * These have been refactored for efficiency purposes.
 * The new method uses 1 matrix to display the whole string
 * And then setting vertex data is done during the setup/constructor phase,
 * instead of during rendering
 * This requires a refresh if the data ever changes
 * 
 * This method is more efficient because the original version does all this work
 * during the render phase. Now it's done during the actor spawn phase with the exception
 * if any changes are made at runtime.
 */
void AText::Print3D(char* text, s32 tracking, s32 mode) {
    char* temp_string = text;
    s32 stringWidth = 0;
    s32 glyphIndex;
    s32 sp60;

    s32 column = 0;
    s32 row = 0;

    if (text == NULL) {
        // @port if invalid text is loaded it will skip rendering it.
        return;
    }

    while (*temp_string != '\0') {
        glyphIndex = char_to_glyph_index(temp_string);
        if (glyphIndex >= 0) {
            stringWidth += ((gGlyphDisplayWidth[glyphIndex] + tracking) * ScaleX);
        } else if ((glyphIndex != -2) && (glyphIndex == -1)) {
            stringWidth += ((tracking + 7) * ScaleX);
        } else {
            return;
        }
        if (glyphIndex >= 0x30) {
            temp_string += 2;
        } else {
            temp_string += 1;
        }
    }

    switch (mode) {
        case LEFT_TEXT:
            //! FAKE:
            do {
            } while (0);
        case RIGHT_TEXT:
            column -= stringWidth;
            break;
        case CENTER_TEXT_MODE_1:
        case CENTER_TEXT_MODE_2:
            column -= stringWidth / 2;
            break;
        default:
            break;
    }

    if (mode < 3) {
        sp60 = 1;
    } else {
        sp60 = 2;
    }

    while (*text != '\0') {
        glyphIndex = char_to_glyph_index(text);
        if (glyphIndex >= 0) {
            AText::PrintLetter3D(gGlyphTextureLUT[glyphIndex],
                                            column, row, sp60);
            column = column + (s32) ((gGlyphDisplayWidth[glyphIndex] + tracking) * ScaleX);
        } else if ((glyphIndex != -2) && (glyphIndex == -1)) {
            column = column + (s32) ((tracking + 7) * ScaleX);
        } else {
            return;
        }
        if (glyphIndex >= 0x30) {
            text += 2;
        } else {
            text += 1;
        }
    }
    SetupVtx(); // position each letter
}

void AText::PrintLetter3D(MenuTexture* glyphTexture, f32 column, f32 row, s32 mode) {
    s32 var_v0;
    u8* temp_v0_2;
    f32 thing0;
    f32 thing1;
    MenuTexture* texture;

    texture = glyphTexture;
    while (texture->textureData != NULL) {
        if (texture->textureData != 0) {
            f32 col = texture->dX + column;

            TextureList.emplace_back(CharacterList{
                (const char*) texture->textureData,
                col,
                texture->dY + row,
                texture->width,
                texture->height,
                mode,
            });
        }
        texture++;
    }
}

void AText::SetupVtx() {
    for (CharacterList& character : TextureList) {

        Vtx* vtxPtr;
        switch (character.width) {
            default:
                vtxPtr = (Vtx*)&AText::myVtx[4];
                break;
            case 16:
                vtxPtr = (Vtx*)&AText::myVtx[4];
                break;
            case 26:
                vtxPtr = (Vtx*)&AText::myVtx[0];
                break;
            case 30:
                vtxPtr = (Vtx*)&AText::myVtx[8];
                break;
        }
        // printf("col %f width %d span %d\n", character.column, character.width, character.span);
        // memcpy the vtx data into the unique vtx data for this letter
        Vtx* vtxSrc = (Vtx*)vtxPtr;
        memcpy(&character.vtx, vtxSrc, sizeof(Vtx) * 4);

        for (size_t i = 0; i < 4; i++) {
            // Set the location for this letter (beside the previous letter)     center the text over the anchor point
            float span = (character.column * LetterSpacing);
            character.vtx[i].v.ob[0] += (s16)span;

            // Set the colour for this letter
            character.vtx[i].v.cn[0] = TextColour[i].r;
            character.vtx[i].v.cn[1] = TextColour[i].g;
            character.vtx[i].v.cn[2] = TextColour[i].b;
            character.vtx[i].v.cn[3] = TextColour[i].a;
        }
    }
}

void AText::DrawText3D(Camera* camera) { // Based on func_80095BD0
    Mat4 mtx;

    FrameInterpolation_RecordOpenChild("actor_text", (_idx << 4) | camera->cameraId);
    if (FaceCamera) {
        ApplySphericalBillBoard(mtx, *(FVector*)Pos, Scale, camera->cameraId);
    } else {
        ApplyMatrixTransformations(mtx, *(FVector*)Pos, *(IRotator*)Rot, Scale);
    }

    AddObjectMatrix(mtx, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_MODELVIEW);

    gSPDisplayList(gDisplayListHead++, (Gfx*)D_020077A8);

    for (CharacterList& tex : TextureList) {
        switch (tex.mode) {
            case 1:
                gSPDisplayList(gDisplayListHead++, (Gfx*)D_020077F8);
                break;
            case 2:
                gSPDisplayList(gDisplayListHead++, (Gfx*)D_02007818);
                break;
        }

        //printf("tex texture %p width %d height %d mode %d col %f\n", tex.Texture, tex.width, tex.height, tex.mode, tex.column);
        gDPLoadTextureTile_4b(gDisplayListHead++, (Gfx*)tex.Texture, G_IM_FMT_I, tex.width, 0, 0, 0, tex.width, tex.height + 2, 0,
                            G_TX_NOMIRROR | G_TX_CLAMP, G_TX_NOMIRROR | G_TX_CLAMP, G_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD,
                            G_TX_NOLOD);

       // gSPClearGeometryMode(gDisplayListHead++, G_ZBUFFER);

        if (Animate) {
            AnimateColour(tex.vtx);
        }

        switch(FadeState) {
            case FADE_IN:
                AText::FadeIn(tex.vtx);
                break;
            case FADE_OUT:
                AText::FadeOut(tex.vtx);
                break;
        }

        gSPVertex(gDisplayListHead++, (uintptr_t)tex.vtx, 4, 0);
        gSP2Triangles(gDisplayListHead++, 0, 2, 1, 0, 0, 3, 2, 0);

        // gSPSetGeometryMode(gDisplayListHead++, G_ZBUFFER);
    }

    gSPDisplayList(gDisplayListHead++, (Gfx*)D_020077D8);
    FrameInterpolation_RecordCloseChild();
}

void AText::AnimateColour(Vtx* vtx) {
    u8 r = (sin(gGlobalTimer * 0.1f) * 0.5f + 0.5f) * 200;
    u8 g = (sin(gGlobalTimer * 0.1f + 2.0f) * 0.5f + 0.5f) * 200;
    u8 b = (sin(gGlobalTimer * 0.1f + 4.0f) * 0.5f + 0.5f) * 200;

    for (size_t i = 0; i < 4; i++) {
        vtx[i].v.cn[0] = r;
        vtx[i].v.cn[1] = g;
        vtx[i].v.cn[2] = b;
    }
}

#define fadeSpeed 16
void AText::FadeIn(Vtx* vtx) {
    uint8_t alpha = vtx[0].v.cn[3];
    if (alpha + fadeSpeed > 255) {
        alpha = 255;
        FadeState = NO_FADE;
    } else {
        alpha += fadeSpeed;
    }

    // Apply alpha to all 4 vertices
    for (size_t i = 0; i < 4; i++) {
        vtx[i].v.cn[3] = alpha;
    }
}

void AText::FadeOut(Vtx* vtx) {
    uint8_t alpha = vtx[0].v.cn[3];
    if (alpha < fadeSpeed) {
        alpha = 0;
        FadeState = NO_FADE;
    } else {
        alpha -= fadeSpeed;
    }

    // Apply alpha to all 4 vertices
    for (size_t i = 0; i < 4; i++) {
        vtx[i].v.cn[3] = alpha;
    }
}
#undef fadeSpeed

void AText::DrawEditorProperties() {
    bool updated = false;
    ImGui::Text("Text");
    ImGui::SameLine();

    char text[21] = "";
    strncpy(text, Text.c_str(), sizeof(text));
    ImGui::InputText("Enter text", text, IM_ARRAYSIZE(text));

    Text = std::string(text);

    ImGui::Text("Mode");
    ImGui::SameLine();

    int32_t mode = static_cast<int32_t>(Mode);
    const char* items[] = { "STATIONARY", "FOLLOW PLAYER" };

    if (ImGui::Combo("##Type", &mode, items, IM_ARRAYSIZE(items))) {
        Mode = static_cast<TextMode>(mode);
        updated = true;
    }

    ImGui::DragFloat("Far Render Dist", &Far);
    ImGui::DragFloat("Close Render Dist", &Close);

    ImGui::Checkbox("Face Camera", &FaceCamera);
    if (!FaceCamera) {
        ImGui::Text("Rotation");
        ImGui::SameLine();

        IRotator objRot = GetRotation();

        // Convert to temporary int values (to prevent writing 32bit values to 16bit variables)
        int rot[3] = {
            objRot.pitch,
            objRot.yaw,
            objRot.roll
        };

        if (ImGui::DragInt3("##Rotation", rot, 5.0f)) {
            for (size_t i = 0; i < 3; i++) {
                // Wrap around 0–65535
                rot[i] = (rot[i] % 65536 + 65536) % 65536;
            }
            IRotator newRot;
            newRot.Set(
                static_cast<uint16_t>(rot[0]),
                static_cast<uint16_t>(rot[1]),
                static_cast<uint16_t>(rot[2])
            );
            Rotate(newRot);
        }
    }

    switch(mode) {
        case STATIONARY: {
            ImGui::Text("Location");
            ImGui::SameLine();
            FVector location = GetLocation();
            if (ImGui::DragFloat3("##Location", (float*)&location)) {
                Translate(location);
                gEditor.eObjectPicker.eGizmo.Pos = location;
            }
            break;
        }
        case FOLLOW_PLAYER:
            // Allow setting PlayerIndex
            int32_t playerIdx = PlayerIndex + 1;

            // Draw the input box (ImGui input limited between 1 and 8)
            ImGui::SetNextItemWidth(100);
            if (ImGui::InputInt("Follow Player", &playerIdx)) {
                // Clamp display value to [1, 8]
                if (playerIdx < 1) playerIdx = 1;
                if (playerIdx > 8) playerIdx = 8;

                // Update the internal value (0–7)
                PlayerIndex = playerIdx - 1;
            }

            ImGui::DragFloat("Width Offset", &WidthOffset);
            ImGui::DragFloat("Height Offset", &HeightOffset);
            break;
    }

    DrawColourEditor(&updated);

    ImGui::Text("Transform Settings");
    ImGui::Separator();

    ImGui::SetNextItemWidth(100);
    ImGui::DragFloat("Scale X", &ScaleX, 0.1f, -5.0f, 5.0f, "%.2f");

    ImGui::SetNextItemWidth(100);
    if (ImGui::DragFloat("Letter Spacing", &LetterSpacing, 0.1f, 0.0f, 5.0f, "%.2f")) {
        updated = true;
    }

    if (updated) {
        Refresh();
    }
}

void AText::DrawColourEditor(bool* updated) {
    ImGui::Checkbox("Use single colour", &SingleColour);

    if (SingleColour)
    {
        // Convert 8bit colours to float
        ImVec4 colour(
            TextColour[0].r / 255.0f,
            TextColour[0].g / 255.0f,
            TextColour[0].b / 255.0f,
            TextColour[0].a / 255.0f
        );

        // Single color input
        ImGui::ColorEdit4("Colour", (float*)&colour);
            // Apply same color to all vertices
            for (int i = 0; i < 4; i++) {
                TextColour[i].r = FloatToU8(colour.x);
                TextColour[i].g = FloatToU8(colour.y);
                TextColour[i].b = FloatToU8(colour.z);
                TextColour[i].a = FloatToU8(colour.w);
            }
            *updated = true;
        
    } else {
        // Separate color pickers for each vertex
        for (int i = 0; i < 4; i++)
        {
            ImVec4 colour2(
                TextColour[i].r / 255.0f,
                TextColour[i].g / 255.0f,
                TextColour[i].b / 255.0f,
                TextColour[i].a / 255.0f
            );
            char label[32];
            snprintf(label, sizeof(label), "Vtx %d Colour", i);
            if (ImGui::ColorEdit4(label, (float*)&colour2)) {
                TextColour[i].r = FloatToU8(colour2.x);
                TextColour[i].g = FloatToU8(colour2.y);
                TextColour[i].b = FloatToU8(colour2.z);
                TextColour[i].a = FloatToU8(colour2.w);
                *updated = true;
            }
        }
    }
}