summaryrefslogtreecommitdiff
path: root/src/help_system_util.c
blob: f23bbbcfff9379d100272b4ee38a934de6d44c33 (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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
#include "global.h"
#include "gflib.h"
#include "decompress.h"
#include "m4a.h"
#include "event_data.h"
#include "help_system.h"
#include "list_menu.h"
#include "strings.h"
#include "constants/songs.h"

#define ZERO 0

bool8 gHelpSystemEnabled;

struct HelpSystemVideoState
{
    /*0x00*/ MainCallback savedVblankCb;
    /*0x04*/ MainCallback savedHblankCb;
    /*0x08*/ u16 savedDispCnt;
    /*0x0a*/ u16 savedBg0Cnt;
    /*0x0c*/ u16 savedBg0Hofs;
    /*0x0e*/ u16 savedBg0Vofs;
    /*0x10*/ u16 savedBldCnt;
    /*0x12*/ u8 savedTextColor[3];
    /*0x15*/ u8 state;
};

static EWRAM_DATA u8 sMapTilesBackup[BG_CHAR_SIZE] = {0};
EWRAM_DATA u8 gDisableHelpSystemVolumeReduce = 0;
EWRAM_DATA bool8 gHelpSystemToggleWithRButtonDisabled = FALSE;
static EWRAM_DATA u8 sDelayTimer = 0;
static EWRAM_DATA u8 sInHelpSystem = 0;
static EWRAM_DATA struct HelpSystemVideoState sVideoState = {0};
EWRAM_DATA struct HelpSystemListMenu gHelpSystemListMenu = {0};
EWRAM_DATA struct ListMenuItem gHelpSystemListMenuItems[52] = {0};

static const u16 sTiles[] = INCBIN_U16("graphics/help_system/bg_tiles.4bpp");
static const u16 sPals[] = INCBIN_U16("graphics/help_system/bg_tiles.gbapal");

u8 RunHelpSystemCallback(void)
{
    s32 i;

    switch (sVideoState.state)
    {
    case 0:
        sInHelpSystem = 0;
        if (gSaveBlock2Ptr->optionsButtonMode != OPTIONS_BUTTON_MODE_HELP)
            return 0;
        if (JOY_NEW(R_BUTTON) && gHelpSystemToggleWithRButtonDisabled == TRUE)
            return 0;
        if (JOY_NEW(L_BUTTON | R_BUTTON))
        {
            if (!HelpSystem_IsSinglePlayer() || !gHelpSystemEnabled)
            {
                PlaySE(SE_HELP_ERROR);
                return 0;
            }
            m4aMPlayStop(&gMPlayInfo_SE1);
            m4aMPlayStop(&gMPlayInfo_SE2);
            PlaySE(SE_HELP_OPEN);
            if (!gDisableHelpSystemVolumeReduce)
                m4aMPlayVolumeControl(&gMPlayInfo_BGM, 0xFFFF, 0x80);
            SaveCallbacks();
            sInHelpSystem = 1;
            sVideoState.state = 1;
        }
        break;
    case 1:
        SaveMapTiles();
        SaveMapGPURegs();
        SaveMapTextColors();
        (*(vu16 *)PLTT) = sPals[15];
        SetGpuReg(REG_OFFSET_DISPCNT, 0);
        sVideoState.state = 2;
        break;
    case 2:
        RequestDma3Fill(0, (void *)BG_CHAR_ADDR(3), BG_CHAR_SIZE, DMA3_16BIT);
        RequestDma3Copy(sPals, (void *)PLTT, sizeof(sPals), DMA3_16BIT);
        RequestDma3Copy(sTiles, gDecompressionBuffer + 0x3EE0, sizeof(sTiles), DMA3_16BIT);
        sVideoState.state = 3;
        break;
    case 3:
        HS_BufferFillMapWithTile1FF();
        HelpSystem_FillPanel3();
        HelpSystem_FillPanel2();
        HelpSystem_PrintTextInTopLeftCorner(gString_Help);
        HS_ShowOrHideWordHELPinTopLeft(1);
        if (HelpSystem_UpdateHasntSeenIntro() == TRUE)
            HelpSystemSubroutine_PrintWelcomeMessage(&gHelpSystemListMenu, gHelpSystemListMenuItems);
        else
            HelpSystemSubroutine_WelcomeEndGotoMenu(&gHelpSystemListMenu, gHelpSystemListMenuItems);
        HS_ShowOrHideHeaderAndFooterLines_Lighter(1);
        HS_ShowOrHideVerticalBlackBarsAlongSides(1);
        CommitTilemap();
        sVideoState.state = 4;
        break;
    case 4:
        SetGpuReg(REG_OFFSET_BLDCNT, 0);
        SetGpuReg(REG_OFFSET_BG0HOFS, 0);
        SetGpuReg(REG_OFFSET_BG0VOFS, 0);
        SetGpuReg(REG_OFFSET_BG0CNT, BGCNT_PRIORITY(0) | BGCNT_CHARBASE(3) | BGCNT_16COLOR | BGCNT_SCREENBASE(31));
        SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_BG0_ON);
        sVideoState.state = 5;
        break;
    case 5:
        if (!RunHelpMenuSubroutine(&gHelpSystemListMenu, gHelpSystemListMenuItems))
        {
            PlaySE(SE_HELP_CLOSE);
            sVideoState.state = 6;
        }
        break;
    case 6:
        SetGpuReg(REG_OFFSET_DISPCNT, 0);
        RestoreMapTiles();
        for (i = 0; i < 0x200; i += 2)
        {
            *((vu16 *)(PLTT + 0x000 + i)) = sPals[15];
            *((vu16 *)(PLTT + 0x200 + i)) = sPals[15];
        }
        sVideoState.state = 7;
        break;
    case 7:
        if (!gDisableHelpSystemVolumeReduce)
            m4aMPlayVolumeControl(&gMPlayInfo_BGM, 0xFFFF, 0x100);
        RestoreMapTextColors();
        RestoreGPURegs();
        sVideoState.state = 8;
        break;
    case 8:
        RestoreCallbacks();
        sInHelpSystem = 0;
        sVideoState.state = 0;
        break;
    }
    return sVideoState.state;
}

void SaveCallbacks(void)
{
    vu16 * dma;
    sVideoState.savedVblankCb = gMain.vblankCallback;
    sVideoState.savedHblankCb = gMain.hblankCallback;
    gMain.vblankCallback = NULL;
    gMain.hblankCallback = NULL;

    dma = (void *)REG_ADDR_DMA0;
    dma[5] &= ~(DMA_START_MASK | DMA_DREQ_ON | DMA_REPEAT);
    dma[5] &= ~DMA_ENABLE;
    dma[5];
}

void SaveMapGPURegs(void)
{
    sVideoState.savedDispCnt = GetGpuReg(REG_OFFSET_DISPCNT);
    sVideoState.savedBg0Cnt = GetGpuReg(REG_OFFSET_BG0CNT);
    sVideoState.savedBg0Hofs = GetGpuReg(REG_OFFSET_BG0HOFS);
    sVideoState.savedBg0Vofs = GetGpuReg(REG_OFFSET_BG0VOFS);
    sVideoState.savedBldCnt = GetGpuReg(REG_OFFSET_BLDCNT);
}

void SaveMapTiles(void)
{
    RequestDma3Copy((void *)BG_CHAR_ADDR(3), sMapTilesBackup, BG_CHAR_SIZE, DMA3_16BIT);
}

void SaveMapTextColors(void)
{
    SaveTextColors(
        &sVideoState.savedTextColor[0],
        &sVideoState.savedTextColor[1],
        &sVideoState.savedTextColor[2]
    );
}

void RestoreCallbacks(void)
{
    gMain.vblankCallback = sVideoState.savedVblankCb;
    gMain.hblankCallback = sVideoState.savedHblankCb;
}

void RestoreGPURegs(void)
{
    SetGpuReg(REG_OFFSET_BLDCNT, sVideoState.savedBldCnt);
    SetGpuReg(REG_OFFSET_BG0HOFS, sVideoState.savedBg0Hofs);
    SetGpuReg(REG_OFFSET_BG0VOFS, sVideoState.savedBg0Vofs);
    SetGpuReg(REG_OFFSET_BG0CNT, sVideoState.savedBg0Cnt);
    SetGpuReg(REG_OFFSET_DISPCNT, sVideoState.savedDispCnt);
}

void RestoreMapTiles(void)
{
    RequestDma3Copy(sMapTilesBackup, (void *)BG_CHAR_ADDR(3), BG_CHAR_SIZE, DMA3_16BIT);
}

void RestoreMapTextColors(void)
{
    RestoreTextColors(
        &sVideoState.savedTextColor[0],
        &sVideoState.savedTextColor[1],
        &sVideoState.savedTextColor[2]
    );
}

void CommitTilemap(void)
{
    RequestDma3Copy(gDecompressionBuffer, (void *)BG_CHAR_ADDR(3), BG_CHAR_SIZE, DMA3_16BIT);
}

void HS_DrawBgTilemapRect(u16 baseTile, u8 left, u8 top, u8 width, u8 height, u16 increment)
{
    u16 i, j;

    for (i = top; i < top + height; i++)
    {
        for (j = left; j < left + width; j++)
        {
            *((u16 *)(gDecompressionBuffer + 0x3800 + 64 * i + 2 * j)) = baseTile;
            baseTile += increment;
        }
    }

    CommitTilemap();
}

void HS_BufferFillMapWithTile1FF(void)
{
    HS_DrawBgTilemapRect(0x1FF, 0, 0, 30, 20, 0);
}

void HS_ShowOrHideWordHELPinTopLeft(u8 mode)
{
    switch (mode)
    {
    case 0:
        HS_DrawBgTilemapRect(0x1FF, 1, 0, 7, 2, 0);
        break;
    case 1:
        HS_DrawBgTilemapRect(0x1E8, 1, 0, 7, 2, 1);
        break;
    }
}

void HS_ShowOrHideControlsGuideInTopRight(u8 mode)
{
    switch (mode)
    {
    case 0:
        HS_DrawBgTilemapRect(0x1FF, 13, 0, 16, 2, 0);
        break;
    case 1:
        HS_DrawBgTilemapRect(0x1A0, 13, 0, 16, 2, 1);
        break;
    }
}

void HS_ShowOrHideMainWindowText(u8 mode)
{
    switch (mode)
    {
    case 0:
        HS_DrawBgTilemapRect(0x1FF, 2, 3, 26, 16, 0);
        break;
    case 1:
        HS_DrawBgTilemapRect(0x000, 2, 3, 26, 16, 1);
        break;
    }
}

void HS_SetMainWindowBgBrightness(u8 mode)
{
    switch (mode)
    {
    case 0:
        // Brighter
        HS_DrawBgTilemapRect(0x1FF, 1, 3, 28, 16, 0);
        break;
    case 1:
        // Darker
        HS_DrawBgTilemapRect(0x1FA, 1, 3, 28, 17, 0);
        break;
    }
}

void HS_ShowOrHideToplevelTooltipWindow(u8 mode)
{
    switch (mode)
    {
    case 0:
        HS_DrawBgTilemapRect(0x1FF, 2, 14, 26, 5, 0);
        break;
    case 1:
        HS_DrawBgTilemapRect(0x11E, 2, 14, 26, 5, 1);
        break;
    }
}

void HS_ShowOrHideHeaderAndFooterLines_Lighter(u8 mode)
{
    switch (mode)
    {
    case 0:
        HS_DrawBgTilemapRect(0x1FF, 1,  2, 28, 1, 0);
        HS_DrawBgTilemapRect(0x1FF, 1, 19, 28, 1, 0);
        break;
    case 1:
        HS_DrawBgTilemapRect(0x1F7, 1,  2, 28, 1, 0);
        HS_DrawBgTilemapRect(0x1F8, 1, 19, 28, 1, 0);
        break;
    }
}

void HS_ShowOrHideHeaderAndFooterLines_Darker(u8 mode)
{
    switch (mode)
    {
    case 0:
        HS_DrawBgTilemapRect(0x1FF, 1,  2, 28, 1, 0);
        HS_DrawBgTilemapRect(0x1FF, 1, 19, 28, 1, 0);
        break;
    case 1:
        HS_DrawBgTilemapRect(0x1FB, 1,  2, 28, 1, 0);
        HS_DrawBgTilemapRect(0x1FC, 1, 19, 28, 1, 0);
        break;
    }
}

void HS_ShowOrHideVerticalBlackBarsAlongSides(u8 mode)
{
    switch (mode)
    {
    case 0:
        HS_DrawBgTilemapRect(0x1FF,  0, 0, 1, 20, 0);
        HS_DrawBgTilemapRect(0x1FF, 29, 0, 1, 20, 0);
        break;
    case 1:
        HS_DrawBgTilemapRect(0x1F9,  0, 0, 1, 20, 0);
        HS_DrawBgTilemapRect(0x1F9, 29, 0, 1, 20, 0);
        break;
    }
}

void HS_ShowOrHideHeaderLine_Darker_FooterStyle(u8 mode)
{
    switch (mode)
    {
    case 0:
        HS_DrawBgTilemapRect(0x1FF, 1, 5, 28, 1, 0);
        break;
    case 1:
        HS_DrawBgTilemapRect(0x1FC, 1, 5, 28, 1, 0);
        break;
    }
}

void HS_ShowOrHideScrollArrows(u8 which, u8 mode)
{
    switch (mode)
    {
    case 0:
        HS_DrawBgTilemapRect(0x1FF, 28,  3, 1, 1, 0);
        HS_DrawBgTilemapRect(0x1FF, 28, 18, 1, 1, 0);
        break;
    case 1:
        if (which == 0) // top
            HS_DrawBgTilemapRect(0x1FE, 28,  3, 1, 1, 0);
        else // bottom
            HS_DrawBgTilemapRect(0x1FD, 28, 18, 1, 1, 0);
        break;
    }
}

void HelpSystemRenderText(u8 font, u8 * dest, const u8 * src, u8 x, u8 y, u8 width, u8 height)
{
    // font -> sp+24
    // dest -> sp+28
    // src -> r9
    // x -> sp+34
    // y -> r10
    // width -> sp+2C
    // height -> sp+30
    struct Bitmap srcBlit;
    struct Bitmap destBlit;
    u8 orig_x = x;
    u8 i = 0;
    s32 clearPixels = 0;

    while (1)
    {
        u16 curChar = *src;
        src++;
        switch (curChar)
        {
        case EOS:
            return;
        case CHAR_NEWLINE:
            x = orig_x;
            y += gGlyphInfo.height + 1;
            break;
        case PLACEHOLDER_BEGIN:
            curChar = *src;
            src++;
            if (curChar == PLACEHOLDER_ID_PLAYER)
            {
                for (i = 0; i < 10; i++)
                {
                    if (gSaveBlock2Ptr->playerName[i] == EOS)
                    {
                        break;
                    }
                    DecompressAndRenderGlyph(font, gSaveBlock2Ptr->playerName[i], &srcBlit, &destBlit, dest, x, y, width, height);
                    // This is required to match a dummy [sp+#0x24] read here
                    if (font == 0)
                    {
                        x += gGlyphInfo.width;
                    }
                    else
                    {
                        x += gGlyphInfo.width + ZERO;
                    }
                }
            }
            else if (curChar == PLACEHOLDER_ID_STRING_VAR_1)
            {
                for (i = 0; ; i++)
                {
                    if (FlagGet(FLAG_SYS_NOT_SOMEONES_PC) == TRUE)
                    {
                        if (gString_Bill[i] == EOS)
                        {
                            break;
                        }
                        DecompressAndRenderGlyph(font, gString_Bill[i], &srcBlit, &destBlit, dest, x, y, width, height);
                    }
                    else
                    {
                        if (gString_Someone[i] == EOS)
                        {
                            break;
                        }
                        DecompressAndRenderGlyph(font, gString_Someone[i], &srcBlit, &destBlit, dest, x, y, width, height);
                    }
                    if (font == 0)
                    {
                        x += gGlyphInfo.width;
                    }
                    else
                    {
                        x += gGlyphInfo.width + ZERO;
                    }
                }
            }
            break;
        case CHAR_PROMPT_SCROLL:
        case CHAR_PROMPT_CLEAR:
            x = orig_x;
            y += gGlyphInfo.height + 1;
            break;
        case EXT_CTRL_CODE_BEGIN:
            curChar = *src;
            src++;
            switch (curChar)
            {
            case EXT_CTRL_CODE_COLOR_HIGHLIGHT_SHADOW:
                src++;
                //fallthrough
            case EXT_CTRL_CODE_PLAY_BGM:
            case EXT_CTRL_CODE_PLAY_SE:
                src++;
                //fallthrough
            case EXT_CTRL_CODE_COLOR:
            case EXT_CTRL_CODE_HIGHLIGHT:
            case EXT_CTRL_CODE_SHADOW:
            case EXT_CTRL_CODE_PALETTE:
            case EXT_CTRL_CODE_FONT:
            case EXT_CTRL_CODE_PAUSE:
            case EXT_CTRL_CODE_ESCAPE:
            case EXT_CTRL_CODE_SHIFT_RIGHT:
            case EXT_CTRL_CODE_SHIFT_DOWN:
                src++;
            case EXT_CTRL_CODE_RESET_FONT:
            case EXT_CTRL_CODE_WAIT_BUTTON:
            case EXT_CTRL_CODE_WAIT_SE:
            case EXT_CTRL_CODE_FILL_WINDOW:
                break;
            case EXT_CTRL_CODE_CLEAR:
            case EXT_CTRL_CODE_SKIP:
                src++;
                break;
            case EXT_CTRL_CODE_CLEAR_TO:
            {
                clearPixels = *src + orig_x - x;

                if (clearPixels > 0)
                {
                    destBlit.pixels = dest;
                    destBlit.width = width * 8;
                    destBlit.height = height * 8;
                    FillBitmapRect4Bit(&destBlit, x, y, clearPixels, GetFontAttribute(font, FONTATTR_MAX_LETTER_HEIGHT), 0);
                    x += clearPixels;
                }
                src++;
                break;
            }
            case EXT_CTRL_CODE_MIN_LETTER_SPACING:
                src++;
                break;
            case EXT_CTRL_CODE_JPN:
            case EXT_CTRL_CODE_ENG:
                break;
            }
            break;
        case CHAR_KEYPAD_ICON:
            curChar = *src;
            src++;
            srcBlit.pixels = (u8 *)&gKeypadIconTiles[0x20 * GetKeypadIconTileOffset(curChar)];
            srcBlit.width = 0x80;
            srcBlit.height = 0x80;
            destBlit.pixels = dest;
            destBlit.width = width * 8;
            destBlit.height = height * 8;
            BlitBitmapRect4Bit(&srcBlit, &destBlit, 0, 0, x, y, GetKeypadIconWidth(curChar), GetKeypadIconHeight(curChar), 0);
            x += GetKeypadIconWidth(curChar);
            break;
        case CHAR_EXTRA_EMOJI:
            curChar = *src + 0x100;
            src++;
            //fallthrough
        default:
            if (curChar == CHAR_SPACE)
            {
                if (font == 0)
                {
                    x += 5;
                }
                else
                {
                    x += 4;
                }
            }
            else
            {
                DecompressAndRenderGlyph(font, curChar, &srcBlit, &destBlit, dest, x, y, width, height);
                if (font == 0)
                {
                    x += gGlyphInfo.width;
                }
                else
                {
                    x += gGlyphInfo.width + ZERO;
                }
            }
            break;
        }
    }
}

void DecompressAndRenderGlyph(u8 font, u16 glyph, struct Bitmap *srcBlit, struct Bitmap *destBlit, u8 *destBuffer, u8 x, u8 y, u8 width, u8 height)
{
    if (font == 0)
        DecompressGlyphFont0(glyph, FALSE);
    else if (font == 5)
        DecompressGlyphFont5(glyph, FALSE);
    else
        DecompressGlyphFont2(glyph, FALSE);
    srcBlit->pixels = gGlyphInfo.pixels;
    srcBlit->width = 16;
    srcBlit->height = 16;
    destBlit->pixels = destBuffer;
    destBlit->width = width * 8;
    destBlit->height = height * 8;
    BlitBitmapRect4Bit(srcBlit, destBlit, 0, 0, x, y, gGlyphInfo.width, gGlyphInfo.height, 0);
}

void HelpSystem_PrintTextInTopLeftCorner(const u8 * str)
{
    GenerateFontHalfRowLookupTable(1, 15, 2);
    HelpSystemRenderText(5, gDecompressionBuffer + 0x3D00, str, 6, 2, 7, 2);
}

void HelpSystem_PrintTextRightAlign_Row52(const u8 * str)
{
    s32 left = 0x7C - GetStringWidth(0, str, 0);
    GenerateFontHalfRowLookupTable(1, 15, 2);
    HelpSystemRenderText(0, gDecompressionBuffer + 0x3400, str, left, 2, 16, 2);
}

void HelpSystem_PrintTextAt(const u8 * str, u8 x, u8 y)
{
    GenerateFontHalfRowLookupTable(1, 15, 2);
    HelpSystemRenderText(2, gDecompressionBuffer + 0x0000, str, x, y, 26, 16);
}

void HelpSystem_PrintQuestionAndAnswerPair(const u8 * question, const u8 * answer)
{
    CpuFill16(0xEEEE, gDecompressionBuffer + 0x0000, 0x3400);
    GenerateFontHalfRowLookupTable(1, 14, 2);
    HelpSystemRenderText(2, gDecompressionBuffer + 0x0000, question, 0, 0, 26, 16);
    HelpSystemRenderText(2, gDecompressionBuffer + 0x09C0, answer, 0, 0, 26, 13);
}

void HelpSystem_PrintTopicMouseoverDescription(const u8 * str)
{
    CpuFill16(0x1111, gDecompressionBuffer + 0x23C0, 0x1040);
    GenerateFontHalfRowLookupTable(2, 1, 3);
    HelpSystemRenderText(2, gDecompressionBuffer + 0x23C0, str, 2, 6, 26, 5);
}

void HelpSystem_FillPanel3(void)
{
    CpuFill16(0xFFFF, gDecompressionBuffer + 0x3D00, 0x1C0);
}

void HelpSystem_FillPanel2(void)
{
    CpuFill16(0xFFFF, gDecompressionBuffer + 0x3400, 0x400);
}

void HelpSystem_FillPanel1(void)
{
    CpuFill16(0xFFFF, gDecompressionBuffer + 0x0000, 0x3400);
}

void HelpSystem_InitListMenuController(struct HelpSystemListMenu * a0, u8 a1, u8 a2)
{
    gHelpSystemListMenu.sub = a0->sub;
    gHelpSystemListMenu.itemsAbove = a1;
    gHelpSystemListMenu.cursorPos = a2;
    gHelpSystemListMenu.state = 0;
    if (gHelpSystemListMenu.sub.totalItems < gHelpSystemListMenu.sub.maxShowed)
        gHelpSystemListMenu.sub.maxShowed = gHelpSystemListMenu.sub.totalItems;
    HS_ShowOrHideMainWindowText(0);
    HelpSystem_FillPanel1();
    PrintListMenuItems();
    PlaceListMenuCursor();
}

void HelpSystem_SetInputDelay(u8 a0)
{
    sDelayTimer = a0;
}

s32 HelpSystem_GetMenuInput(void)
{
    if (sDelayTimer != 0)
    {
        sDelayTimer--;
        return -1;
    }
    else if (JOY_NEW(A_BUTTON))
    {
        PlaySE(SE_SELECT);
        return gHelpSystemListMenu.sub.items[gHelpSystemListMenu.itemsAbove + gHelpSystemListMenu.cursorPos].index;
    }
    else if (JOY_NEW(B_BUTTON))
    {
        PlaySE(SE_SELECT);
        return -2;
    }
    else if (JOY_NEW(L_BUTTON | R_BUTTON))
    {
        return -6;
    }
    else if (JOY_REPT(DPAD_UP))
    {
        if (!MoveCursor(1, 0))
            PlaySE(SE_SELECT);
        return -4;
    }
    else if (JOY_REPT(DPAD_DOWN))
    {
        if (!MoveCursor(1, 1))
            PlaySE(SE_SELECT);
        return -5;
    }
    else if (JOY_REPT(DPAD_LEFT))
    {
        if (!MoveCursor(7, 0))
            PlaySE(SE_SELECT);
        return -4;
    }
    else if (JOY_REPT(DPAD_RIGHT))
    {
        if (!MoveCursor(7, 1))
            PlaySE(SE_SELECT);
        return -5;
    }
    else
        return -1;
}

void HS_UpdateMenuScrollArrows(void)
{
    u8 topItemIdx = gHelpSystemListMenu.sub.totalItems - 7;
    if (gHelpSystemListMenu.sub.totalItems > 7)
    {
        s32 cursorPos = gHelpSystemListMenu.itemsAbove + gHelpSystemListMenu.cursorPos;
        HS_ShowOrHideScrollArrows(0, 0); // Hide both
        if (cursorPos == 0)
            HS_ShowOrHideScrollArrows(1, 1); // Show bottom
        else if (gHelpSystemListMenu.itemsAbove == 0 && gHelpSystemListMenu.cursorPos != 0)
            HS_ShowOrHideScrollArrows(1, 1); // Show bottom
        else if (gHelpSystemListMenu.itemsAbove == topItemIdx)
            HS_ShowOrHideScrollArrows(0, 1); // Show top
        else if (gHelpSystemListMenu.itemsAbove != 0)
        {
            // Show both
            HS_ShowOrHideScrollArrows(0, 1);
            HS_ShowOrHideScrollArrows(1, 1);
        }
    }
}

void PrintListMenuItems(void)
{
    u8 glyphHeight = GetFontAttribute(2, FONTATTR_MAX_LETTER_HEIGHT) + 1;
    s32 i;
    s32 r5 = gHelpSystemListMenu.itemsAbove;

    for (i = 0; i < gHelpSystemListMenu.sub.maxShowed; i++)
    {
        u8 x = gHelpSystemListMenu.sub.left + 8;
        u8 y = gHelpSystemListMenu.sub.top + glyphHeight * i;
        HelpSystem_PrintTextAt(gHelpSystemListMenu.sub.items[r5].label, x, y);
        r5++;
    }
}

void PlaceListMenuCursor(void)
{
    u8 glyphHeight = GetFontAttribute(2, FONTATTR_MAX_LETTER_HEIGHT) + 1;
    u8 x = gHelpSystemListMenu.sub.left;
    u8 y = gHelpSystemListMenu.sub.top + glyphHeight * gHelpSystemListMenu.cursorPos;
    HelpSystem_PrintTextAt(gText_SelectorArrow2, x, y);
}

void HS_RemoveSelectionCursorAt(u8 i)
{
    u8 glyphHeight = GetFontAttribute(2, FONTATTR_MAX_LETTER_HEIGHT) + 1;
    u8 x = gHelpSystemListMenu.sub.left;
    u8 y = gHelpSystemListMenu.sub.top + i * glyphHeight;
    HelpSystem_PrintTextAt(gString_HelpSystem_ClearTo8, x, y);
}

u8 TryMoveCursor1(u8 dirn)
{
    u16 midPoint;
    if (dirn == 0)
    {
        if (gHelpSystemListMenu.sub.maxShowed == 1)
            midPoint = 0;
        else
            midPoint = gHelpSystemListMenu.sub.maxShowed - (gHelpSystemListMenu.sub.maxShowed / 2 + (gHelpSystemListMenu.sub.maxShowed & 1)) - 1;
        if (gHelpSystemListMenu.itemsAbove == 0)
        {
            if (gHelpSystemListMenu.cursorPos != 0)
            {
                gHelpSystemListMenu.cursorPos--;
                return 1;
            }
            else
                return 0;
        }
        if (gHelpSystemListMenu.cursorPos > midPoint)
        {
            gHelpSystemListMenu.cursorPos--;
            return 1;
        }
        else
        {
            gHelpSystemListMenu.itemsAbove--;
            return 2;
        }
    }
    else
    {
        if (gHelpSystemListMenu.sub.maxShowed == 1)
            midPoint = 0;
        else
            midPoint = gHelpSystemListMenu.sub.maxShowed / 2 + (gHelpSystemListMenu.sub.maxShowed & 1);
        if (gHelpSystemListMenu.itemsAbove == gHelpSystemListMenu.sub.totalItems - gHelpSystemListMenu.sub.maxShowed)
        {
            if (gHelpSystemListMenu.cursorPos < gHelpSystemListMenu.sub.maxShowed - 1)
            {
                gHelpSystemListMenu.cursorPos++;
                return 1;
            }
            else
                return 0;
        }
        else if (gHelpSystemListMenu.cursorPos < midPoint)
        {
            gHelpSystemListMenu.cursorPos++;
            return 1;
        }
        else
        {
            gHelpSystemListMenu.itemsAbove++;
            return 2;
        }
    }
}

bool8 MoveCursor(u8 by, u8 dirn)
{
    u8 r7 = gHelpSystemListMenu.cursorPos;
    u8 flags = 0;
    s32 i;
    for (i = 0; i < by; i++)
        flags |= TryMoveCursor1(dirn);

    switch (flags)
    {
    case 0:
    default:
        // neither changed
        return TRUE;
    case 1:
        // changed cursorPos only
        HS_RemoveSelectionCursorAt(r7);
        PlaceListMenuCursor();
        CommitTilemap();
        break;
    case 2:
    case 3:
        // changed itemsAbove
        if (GetHelpSystemMenuLevel() == 1)
        {
            HelpSystem_SetInputDelay(2);
            HelpSystem_FillPanel1();
            PrintListMenuItems();
            PlaceListMenuCursor();
            HelpSystem_PrintTopicLabel();
            HS_UpdateMenuScrollArrows();
        }
        else
        {
            HS_ShowOrHideMainWindowText(0);
            HelpSystem_FillPanel1();
            PrintListMenuItems();
            PlaceListMenuCursor();
            HS_ShowOrHideMainWindowText(1);
        }
        CommitTilemap();
        break;
    }
    return FALSE;
}