forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnaming_screen.c
2629 lines (2310 loc) · 68.5 KB
/
naming_screen.c
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "global.h"
#include "naming_screen.h"
#include "malloc.h"
#include "palette.h"
#include "task.h"
#include "sprite.h"
#include "string_util.h"
#include "window.h"
#include "bg.h"
#include "gpu_regs.h"
#include "pokemon.h"
#include "field_specials.h"
#include "field_player_avatar.h"
#include "event_object_movement.h"
#include "event_data.h"
#include "constants/songs.h"
#include "pokemon_storage_system.h"
#include "graphics.h"
#include "sound.h"
#include "trig.h"
#include "field_effect.h"
#include "pokemon_icon.h"
#include "data.h"
#include "strings.h"
#include "menu.h"
#include "text_window.h"
#include "overworld.h"
#include "walda_phrase.h"
#include "main.h"
#include "constants/event_objects.h"
#include "constants/rgb.h"
enum {
INPUT_NONE,
INPUT_DPAD_UP,
INPUT_DPAD_DOWN,
INPUT_DPAD_LEFT,
INPUT_DPAD_RIGHT,
INPUT_A_BUTTON,
INPUT_B_BUTTON,
INPUT_LR_BUTTON,
INPUT_SELECT,
INPUT_START,
};
#define KBROW_COUNT 4
#if FRENCH
#define KBCOL_COUNT 9
#else //ENGLISH || ITALIAN
#define KBCOL_COUNT 8
#endif
enum {
GFXTAG_BACK_BUTTON,
GFXTAG_OK_BUTTON,
GFXTAG_PAGE_SWAP_FRAME,
GFXTAG_PAGE_SWAP_BUTTON,
GFXTAG_PAGE_SWAP_UPPER,
GFXTAG_PAGE_SWAP_LOWER,
GFXTAG_PAGE_SWAP_OTHERS,
GFXTAG_CURSOR,
GFXTAG_CURSOR_SQUISHED,
GFXTAG_CURSOR_FILLED,
GFXTAG_INPUT_ARROW,
GFXTAG_UNDERSCORE,
};
enum {
PALTAG_MENU, // Also the PC icon
PALTAG_PAGE_SWAP_UPPER,
PALTAG_PAGE_SWAP_LOWER,
PALTAG_PAGE_SWAP_OTHERS, // Also the input arrow/underscore
PALTAG_PAGE_SWAP,
PALTAG_CURSOR,
PALTAG_BACK_BUTTON,
PALTAG_OK_BUTTON,
};
enum {
WIN_KB_PAGE_1, // Which of these two windows is in front is cycled as the player swaps
WIN_KB_PAGE_2, // Initially WIN_KB_PAGE_1 is in front, with WIN_KB_PAGE_2 on deck
WIN_TEXT_ENTRY,
WIN_TEXT_ENTRY_BOX,
WIN_BANNER,
WIN_COUNT,
};
// The constants for the pages are needlessly complicated because GF didn't keep the indexing order consistent
// This set is used for sNamingScreen->currentPage. It uses the order that the pages are cycled in
enum {
KBPAGE_SYMBOLS,
KBPAGE_LETTERS_UPPER,
KBPAGE_LETTERS_LOWER,
KBPAGE_COUNT,
};
// This set is used for initializing a page's keyboard text and getting its number of columns
enum {
KEYBOARD_LETTERS_LOWER,
KEYBOARD_LETTERS_UPPER,
KEYBOARD_SYMBOLS,
};
// This set is used for getting the gfx/pal tags of the page's swap button
enum {
PAGE_SWAP_UPPER,
PAGE_SWAP_OTHERS,
PAGE_SWAP_LOWER,
};
enum {
KEY_ROLE_CHAR,
KEY_ROLE_PAGE,
KEY_ROLE_BACKSPACE,
KEY_ROLE_OK,
};
enum {
BUTTON_PAGE,
BUTTON_BACK,
BUTTON_OK,
BUTTON_COUNT,
};
// states for Task_NamingScreen
enum {
STATE_FADE_IN,
STATE_WAIT_FADE_IN,
STATE_HANDLE_INPUT,
STATE_MOVE_TO_OK_BUTTON,
STATE_START_PAGE_SWAP,
STATE_WAIT_PAGE_SWAP,
STATE_PRESSED_OK,
STATE_WAIT_SENT_TO_PC_MESSAGE,
STATE_FADE_OUT,
STATE_EXIT,
};
// sates for Task_HandleInput
enum
{
INPUT_STATE_DISABLED,
INPUT_STATE_ENABLED,
INPUT_STATE_OVERRIDE,
};
struct NamingScreenTemplate
{
u8 copyExistingString;
u8 maxChars;
u8 iconFunction;
u8 addGenderIcon;
u8 initialPage;
u8 unused;
const u8 *title;
};
struct NamingScreenData
{
u8 tilemapBuffer1[0x800];
u8 tilemapBuffer2[0x800];
u8 tilemapBuffer3[0x800];
u8 textBuffer[16];
u8 tileBuffer[0x600];
u8 state;
u8 windows[WIN_COUNT];
u16 inputCharBaseXPos;
u16 bg1vOffset;
u16 bg2vOffset;
u16 bg1Priority;
u16 bg2Priority;
u8 bgToReveal;
u8 bgToHide;
u8 currentPage;
u8 cursorSpriteId;
u8 swapBtnFrameSpriteId;
u8 keyRepeatStartDelayCopy;
const struct NamingScreenTemplate *template;
u8 templateNum;
u8 *destBuffer;
u16 monSpecies;
u16 monGender;
u32 monPersonality;
MainCallback returnCallback;
};
EWRAM_DATA static struct NamingScreenData *sNamingScreen = NULL;
static const u8 sPCIconOff_Gfx[] = INCBIN_U8("graphics/naming_screen/pc_icon_off.4bpp");
static const u8 sPCIconOn_Gfx[] = INCBIN_U8("graphics/naming_screen/pc_icon_on.4bpp");
static const u16 sKeyboard_Pal[] = INCBIN_U16("graphics/naming_screen/keyboard.gbapal");
static const u16 sRival_Pal[] = INCBIN_U16("graphics/naming_screen/rival.gbapal"); // Unused, leftover from FRLG rival
static const u8 *const sTransferredToPCMessages[] =
{
gText_PkmnTransferredSomeonesPC,
gText_PkmnTransferredLanettesPC,
gText_PkmnTransferredSomeonesPCBoxFull,
gText_PkmnTransferredLanettesPCBoxFull
};
static const u8 sText_AlphabetUpperLower[] = _("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!");
static const struct BgTemplate sBgTemplates[] =
{
{
.bg = 0,
.charBaseIndex = 0,
.mapBaseIndex = 30,
.priority = 0
},
{
.bg = 1,
.charBaseIndex = 2,
.mapBaseIndex = 29,
.priority = 1
},
{
.bg = 2,
.charBaseIndex = 2,
.mapBaseIndex = 28,
.priority = 2
},
{
.bg = 3,
.charBaseIndex = 3,
.mapBaseIndex = 31,
.priority = 3
}
};
static const struct WindowTemplate sWindowTemplates[WIN_COUNT + 1] =
{
[WIN_KB_PAGE_1] = {
.bg = 1,
.tilemapLeft = 3,
.tilemapTop = 10,
.width = 19,
.height = 8,
.paletteNum = 10,
.baseBlock = 0x030
},
[WIN_KB_PAGE_2] = {
.bg = 2,
.tilemapLeft = 3,
.tilemapTop = 10,
.width = 19,
.height = 8,
.paletteNum = 10,
.baseBlock = 0x0C8
},
[WIN_TEXT_ENTRY] = {
.bg = 3,
.tilemapLeft = 8,
.tilemapTop = 6,
.width = 17,
.height = 2,
.paletteNum = 10,
.baseBlock = 0x030
},
[WIN_TEXT_ENTRY_BOX] = {
.bg = 3,
.tilemapLeft = 8,
.tilemapTop = 4,
.width = 17,
.height = 2,
.paletteNum = 10,
.baseBlock = 0x052
},
[WIN_BANNER] = {
.bg = 0,
.tilemapLeft = 0,
.tilemapTop = 0,
.width = DISPLAY_TILE_WIDTH,
.height = 2,
.paletteNum = 11,
.baseBlock = 0x074
},
DUMMY_WIN_TEMPLATE
};
// This handles what characters get inserted when a key is pressed
// The keys shown on the keyboard are handled separately by sNamingScreenKeyboardText
static const u8 sKeyboardChars[KBPAGE_COUNT][KBROW_COUNT][KBCOL_COUNT] = {
#if ENGLISH || ITALIAN || SPANISH
[KEYBOARD_LETTERS_LOWER] = {
__("abcdef ."),
__("ghijkl ,"),
__("mnopqrs "),
__("tuvwxyz "),
},
[KEYBOARD_LETTERS_UPPER] = {
__("ABCDEF ."),
__("GHIJKL ,"),
__("MNOPQRS "),
__("TUVWXYZ "),
},
[KEYBOARD_SYMBOLS] = {
__("01234 "),
__("56789 "),
__("!?♂♀/- "),
__("…“”‘' "),
}
#elif FRENCH
[KEYBOARD_LETTERS_LOWER] = {
__("abcdefgh."),
__("ijklmnop,"),
__("qrstuvwx "),
__("yz - "),
},
[KEYBOARD_LETTERS_UPPER] = {
__("ABCDEFGH."),
__("IJKLMNOP,"),
__("QRSTUVWX "),
__("YZ - "),
},
[KEYBOARD_SYMBOLS] = {
__("01234 "),
__("56789 "),
__("!?♂♀/ "),
__("…“”‘’ "),
}
#endif
};
static const u8 sPageColumnCounts[KBPAGE_COUNT] = {
[KEYBOARD_LETTERS_LOWER] = KBCOL_COUNT,
[KEYBOARD_LETTERS_UPPER] = KBCOL_COUNT,
[KEYBOARD_SYMBOLS] = 6
};
static const u8 sPageColumnXPos[KBPAGE_COUNT][KBCOL_COUNT] = {
#if ENGLISH || ITALIAN || SPANISH
[KEYBOARD_LETTERS_LOWER] = {0, 12, 24, 56, 68, 80, 92, 123},
[KEYBOARD_LETTERS_UPPER] = {0, 12, 24, 56, 68, 80, 92, 123},
[KEYBOARD_SYMBOLS] = {0, 22, 44, 66, 88, 110}
#elif FRENCH
[KEYBOARD_LETTERS_LOWER] = {0, 12, 24, 36, 62, 74, 86, 98, 123},
[KEYBOARD_LETTERS_UPPER] = {0, 12, 24, 36, 62, 74, 86, 98, 123},
[KEYBOARD_SYMBOLS] = {0, 22, 44, 66, 88, 110}
#endif
};
static const struct NamingScreenTemplate *const sNamingScreenTemplates[];
static const struct SubspriteTable sSubspriteTable_PageSwapFrame[];
static const struct SubspriteTable sSubspriteTable_PageSwapText[];
static const struct SubspriteTable sSubspriteTable_Button[];
static const struct SubspriteTable sSubspriteTable_PCIcon[];
static const struct SpriteTemplate sSpriteTemplate_PageSwapFrame;
static const struct SpriteTemplate sSpriteTemplate_PageSwapButton;
static const struct SpriteTemplate sSpriteTemplate_PageSwapText;
static const struct SpriteTemplate sSpriteTemplate_BackButton;
static const struct SpriteTemplate sSpriteTemplate_OkButton;
static const struct SpriteTemplate sSpriteTemplate_Cursor;
static const struct SpriteTemplate sSpriteTemplate_InputArrow;
static const struct SpriteTemplate sSpriteTemplate_Underscore;
static const struct SpriteTemplate sSpriteTemplate_PCIcon;
static const u8 *const sNamingScreenKeyboardText[KBPAGE_COUNT][KBROW_COUNT];
static const struct SpriteSheet sSpriteSheets[];
static const struct SpritePalette sSpritePalettes[];
static void CB2_LoadNamingScreen(void);
static void NamingScreen_Init(void);
static void NamingScreen_InitBGs(void);
static void CreateNamingScreenTask(void);
static void Task_NamingScreen(u8 taskId);
static bool8 MainState_FadeIn(void);
static bool8 MainState_WaitFadeIn(void);
static bool8 MainState_HandleInput(void);
static bool8 MainState_MoveToOKButton(void);
static bool8 MainState_PressedOKButton(void);
static bool8 MainState_FadeOut(void);
static bool8 MainState_Exit(void);
static void DisplaySentToPCMessage(void);
static bool8 MainState_WaitSentToPCMessage(void);
static bool8 MainState_StartPageSwap(void);
static bool8 MainState_WaitPageSwap(void);
static void StartPageSwapAnim(void);
static void Task_HandlePageSwapAnim(u8);
static bool8 IsPageSwapAnimNotInProgress(void);
static void TryStartButtonFlash(u8, bool8, bool8);
static void Task_UpdateButtonFlash(u8);
static u16 GetButtonPalOffset(u8);
static void RestoreButtonColor(u8);
static void StartButtonFlash(struct Task *, u8, bool8);
static void CreateSprites(void);
static void CreateCursorSprite(void);
static void SetCursorPos(s16, s16);
static void GetCursorPos(s16 *x, s16 *y);
static void MoveCursorToOKButton(void);
static void SetCursorInvisibility(u8);
static void SetCursorFlashing(bool8);
static u8 IsCursorAnimFinished(void);
static u8 GetCurrentPageColumnCount(void);
static void CreatePageSwapButtonSprites(void);
static void StartPageSwapButtonAnim(void);
static void SetPageSwapButtonGfx(u8, struct Sprite *, struct Sprite *);
static void CreateBackOkSprites(void);
static void CreateTextEntrySprites(void);
static void CreateInputTargetIcon(void);
static u8 HandleKeyboardEvent(void);
static u8 SwapKeyboardPage(void);
static u8 GetInputEvent(void);
static void SetInputState(u8);
static void DrawTextEntryBox(void);
static u8 GetTextEntryPosition(void);
static void DeleteTextCharacter(void);
static bool8 AddTextCharacter(void);
static void BufferCharacter(u8);
static void SaveInputText(void);
static void LoadGfx(void);
static void CreateHelperTasks(void);
static void LoadPalettes(void);
static void DrawBgTilemap(u8, const void *);
static void NamingScreen_Dummy(u8, u8);
static void DrawTextEntry(void);
static void PrintKeyboardKeys(u8, u8);
static void DrawKeyboardPageOnDeck(void);
static void PrintControls(void);
static void CB2_NamingScreen(void);
static void ResetVHBlank(void);
static void SetVBlank(void);
static void VBlankCB_NamingScreen(void);
static void NamingScreen_ShowBgs(void);
static bool8 IsWideLetter(u8);
void DoNamingScreen(u8 templateNum, u8 *destBuffer, u16 monSpecies, u16 monGender, u32 monPersonality, MainCallback returnCallback)
{
sNamingScreen = Alloc(sizeof(struct NamingScreenData));
if (!sNamingScreen)
{
SetMainCallback2(returnCallback);
}
else
{
sNamingScreen->templateNum = templateNum;
sNamingScreen->monSpecies = monSpecies;
sNamingScreen->monGender = monGender;
sNamingScreen->monPersonality = monPersonality;
sNamingScreen->destBuffer = destBuffer;
sNamingScreen->returnCallback = returnCallback;
if (templateNum == NAMING_SCREEN_PLAYER)
StartTimer1();
SetMainCallback2(CB2_LoadNamingScreen);
}
}
static void CB2_LoadNamingScreen(void)
{
switch (gMain.state)
{
case 0:
ResetVHBlank();
NamingScreen_Init();
gMain.state++;
break;
case 1:
NamingScreen_InitBGs();
gMain.state++;
break;
case 2:
ResetPaletteFade();
gMain.state++;
break;
case 3:
ResetSpriteData();
FreeAllSpritePalettes();
gMain.state++;
break;
case 4:
ResetTasks();
gMain.state++;
break;
case 5:
LoadPalettes();
gMain.state++;
break;
case 6:
LoadGfx();
gMain.state++;
break;
case 7:
CreateSprites();
UpdatePaletteFade();
NamingScreen_ShowBgs();
gMain.state++;
break;
default:
CreateHelperTasks();
CreateNamingScreenTask();
break;
}
}
static void NamingScreen_Init(void)
{
sNamingScreen->state = STATE_FADE_IN;
sNamingScreen->bg1vOffset = 0;
sNamingScreen->bg2vOffset = 0;
sNamingScreen->bg1Priority = BGCNT_PRIORITY(1);
sNamingScreen->bg2Priority = BGCNT_PRIORITY(2);
sNamingScreen->bgToReveal = 0;
sNamingScreen->bgToHide = 1;
sNamingScreen->template = sNamingScreenTemplates[sNamingScreen->templateNum];
sNamingScreen->currentPage = sNamingScreen->template->initialPage;
sNamingScreen->inputCharBaseXPos = (DISPLAY_WIDTH - sNamingScreen->template->maxChars * 8) / 2 + 6;
if (sNamingScreen->templateNum == NAMING_SCREEN_WALDA)
sNamingScreen->inputCharBaseXPos += 11;
sNamingScreen->keyRepeatStartDelayCopy = gKeyRepeatStartDelay;
memset(sNamingScreen->textBuffer, EOS, sizeof(sNamingScreen->textBuffer));
if (sNamingScreen->template->copyExistingString)
StringCopy(sNamingScreen->textBuffer, sNamingScreen->destBuffer);
gKeyRepeatStartDelay = 16;
}
static void SetSpritesVisible(void)
{
u8 i;
for (i = 0; i < MAX_SPRITES; i++)
{
if (gSprites[i].inUse)
gSprites[i].invisible = FALSE;
}
SetCursorInvisibility(FALSE);
}
static void NamingScreen_InitBGs(void)
{
u8 i;
DmaClearLarge16(3, (void *)VRAM, VRAM_SIZE, 0x1000);
DmaClear32(3, (void *)OAM, OAM_SIZE);
DmaClear16(3, (void *)PLTT, PLTT_SIZE);
SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_MODE_0);
ResetBgsAndClearDma3BusyFlags(0);
InitBgsFromTemplates(0, sBgTemplates, ARRAY_COUNT(sBgTemplates));
ChangeBgX(0, 0, BG_COORD_SET);
ChangeBgY(0, 0, BG_COORD_SET);
ChangeBgX(1, 0, BG_COORD_SET);
ChangeBgY(1, 0, BG_COORD_SET);
ChangeBgX(2, 0, BG_COORD_SET);
ChangeBgY(2, 0, BG_COORD_SET);
ChangeBgX(3, 0, BG_COORD_SET);
ChangeBgY(3, 0, BG_COORD_SET);
InitStandardTextBoxWindows();
InitTextBoxGfxAndPrinters();
for (i = 0; i < WIN_COUNT; i++)
sNamingScreen->windows[i] = AddWindow(&sWindowTemplates[i]);
SetGpuReg(REG_OFFSET_DISPCNT, DISPCNT_OBJ_1D_MAP | DISPCNT_OBJ_ON);
SetGpuReg(REG_OFFSET_BLDCNT, BLDCNT_EFFECT_BLEND | BLDCNT_TGT2_BG1 | BLDCNT_TGT2_BG2);
SetGpuReg(REG_OFFSET_BLDALPHA, BLDALPHA_BLEND(12, 8));
SetBgTilemapBuffer(1, sNamingScreen->tilemapBuffer1);
SetBgTilemapBuffer(2, sNamingScreen->tilemapBuffer2);
SetBgTilemapBuffer(3, sNamingScreen->tilemapBuffer3);
FillBgTilemapBufferRect_Palette0(1, 0, 0, 0, 0x20, 0x20);
FillBgTilemapBufferRect_Palette0(2, 0, 0, 0, 0x20, 0x20);
FillBgTilemapBufferRect_Palette0(3, 0, 0, 0, 0x20, 0x20);
}
static void CreateNamingScreenTask(void)
{
CreateTask(Task_NamingScreen, 2);
SetMainCallback2(CB2_NamingScreen);
}
static void Task_NamingScreen(u8 taskId)
{
switch (sNamingScreen->state)
{
case STATE_FADE_IN:
MainState_FadeIn();
SetSpritesVisible();
SetVBlank();
break;
case STATE_WAIT_FADE_IN:
MainState_WaitFadeIn();
break;
case STATE_HANDLE_INPUT:
MainState_HandleInput();
break;
case STATE_MOVE_TO_OK_BUTTON:
MainState_MoveToOKButton();
MainState_HandleInput();
break;
case STATE_START_PAGE_SWAP:
MainState_StartPageSwap();
break;
case STATE_WAIT_PAGE_SWAP:
MainState_WaitPageSwap();
break;
case STATE_PRESSED_OK:
MainState_PressedOKButton();
break;
case STATE_WAIT_SENT_TO_PC_MESSAGE:
MainState_WaitSentToPCMessage();
break;
case STATE_FADE_OUT:
MainState_FadeOut();
break;
case STATE_EXIT:
MainState_Exit();
break;
}
}
// Which gfx/pal to load for the swap page button
static const u8 sPageToNextGfxId[KBPAGE_COUNT] =
{
[KBPAGE_SYMBOLS] = PAGE_SWAP_UPPER,
[KBPAGE_LETTERS_UPPER] = PAGE_SWAP_LOWER,
[KBPAGE_LETTERS_LOWER] = PAGE_SWAP_OTHERS
};
static const u8 sPageToNextKeyboardId[KBPAGE_COUNT] =
{
[KBPAGE_SYMBOLS] = KEYBOARD_LETTERS_UPPER,
[KBPAGE_LETTERS_UPPER] = KEYBOARD_LETTERS_LOWER,
[KBPAGE_LETTERS_LOWER] = KEYBOARD_SYMBOLS
};
static const u8 sPageToKeyboardId[KBPAGE_COUNT] =
{
[KBPAGE_SYMBOLS] = KEYBOARD_SYMBOLS,
[KBPAGE_LETTERS_UPPER] = KEYBOARD_LETTERS_UPPER,
[KBPAGE_LETTERS_LOWER] = KEYBOARD_LETTERS_LOWER
};
static u8 PageToNextGfxId(u8 page)
{
return sPageToNextGfxId[page];
}
static u8 CurrentPageToNextKeyboardId(void)
{
return sPageToNextKeyboardId[sNamingScreen->currentPage];
}
static u8 CurrentPageToKeyboardId(void)
{
return sPageToKeyboardId[sNamingScreen->currentPage];
}
static bool8 MainState_FadeIn(void)
{
DrawBgTilemap(3, gNamingScreenBackground_Tilemap);
sNamingScreen->currentPage = KBPAGE_LETTERS_UPPER;
DrawBgTilemap(2, gNamingScreenKeyboardLower_Tilemap);
DrawBgTilemap(1, gNamingScreenKeyboardUpper_Tilemap);
PrintKeyboardKeys(sNamingScreen->windows[WIN_KB_PAGE_2], KEYBOARD_LETTERS_LOWER);
PrintKeyboardKeys(sNamingScreen->windows[WIN_KB_PAGE_1], KEYBOARD_LETTERS_UPPER);
NamingScreen_Dummy(2, KEYBOARD_LETTERS_LOWER);
NamingScreen_Dummy(1, KEYBOARD_LETTERS_UPPER);
DrawTextEntry();
DrawTextEntryBox();
PrintControls();
CopyBgTilemapBufferToVram(1);
CopyBgTilemapBufferToVram(2);
CopyBgTilemapBufferToVram(3);
BlendPalettes(PALETTES_ALL, 16, 0);
BeginNormalPaletteFade(PALETTES_ALL, 0, 16, 0, RGB_BLACK);
sNamingScreen->state++;
return FALSE;
}
static bool8 MainState_WaitFadeIn(void)
{
if (!gPaletteFade.active)
{
SetInputState(INPUT_STATE_ENABLED);
SetCursorFlashing(TRUE);
sNamingScreen->state++;
}
return FALSE;
}
static bool8 MainState_HandleInput(void)
{
return HandleKeyboardEvent();
}
static bool8 MainState_MoveToOKButton(void)
{
if (IsCursorAnimFinished())
{
SetInputState(INPUT_STATE_ENABLED);
MoveCursorToOKButton();
sNamingScreen->state = STATE_HANDLE_INPUT;
}
return FALSE;
}
static bool8 MainState_PressedOKButton(void)
{
SaveInputText();
SetInputState(INPUT_STATE_DISABLED);
SetCursorFlashing(FALSE);
TryStartButtonFlash(BUTTON_COUNT, FALSE, TRUE);
if (sNamingScreen->templateNum == NAMING_SCREEN_CAUGHT_MON
&& CalculatePlayerPartyCount() >= PARTY_SIZE)
{
DisplaySentToPCMessage();
sNamingScreen->state = STATE_WAIT_SENT_TO_PC_MESSAGE;
return FALSE;
}
else
{
sNamingScreen->state = STATE_FADE_OUT;
return TRUE;
}
}
static bool8 MainState_FadeOut(void)
{
BeginNormalPaletteFade(PALETTES_ALL, 0, 0, 16, RGB_BLACK);
sNamingScreen->state++;
return FALSE;
}
static bool8 MainState_Exit(void)
{
if (!gPaletteFade.active)
{
if (sNamingScreen->templateNum == NAMING_SCREEN_PLAYER)
SeedRngAndSetTrainerId();
SetMainCallback2(sNamingScreen->returnCallback);
DestroyTask(FindTaskIdByFunc(Task_NamingScreen));
FreeAllWindowBuffers();
FREE_AND_SET_NULL(sNamingScreen);
}
return FALSE;
}
static void DisplaySentToPCMessage(void)
{
u8 stringToDisplay = 0;
if (!IsDestinationBoxFull())
{
StringCopy(gStringVar1, GetBoxNamePtr(VarGet(VAR_PC_BOX_TO_SEND_MON)));
StringCopy(gStringVar2, sNamingScreen->destBuffer);
}
else
{
StringCopy(gStringVar1, GetBoxNamePtr(VarGet(VAR_PC_BOX_TO_SEND_MON)));
StringCopy(gStringVar2, sNamingScreen->destBuffer);
StringCopy(gStringVar3, GetBoxNamePtr(GetPCBoxToSendMon()));
stringToDisplay = 2;
}
if (FlagGet(FLAG_SYS_PC_LANETTE))
stringToDisplay++;
StringExpandPlaceholders(gStringVar4, sTransferredToPCMessages[stringToDisplay]);
DrawDialogueFrame(0, FALSE);
gTextFlags.canABSpeedUpPrint = TRUE;
AddTextPrinterParameterized2(0, FONT_NORMAL, gStringVar4, GetPlayerTextSpeedDelay(), 0, TEXT_COLOR_DARK_GRAY, TEXT_COLOR_WHITE, TEXT_COLOR_LIGHT_GRAY);
CopyWindowToVram(0, COPYWIN_FULL);
}
static bool8 MainState_WaitSentToPCMessage(void)
{
RunTextPrinters();
if (!IsTextPrinterActive(0) && JOY_NEW(A_BUTTON))
sNamingScreen->state = STATE_FADE_OUT;
return FALSE;
}
static bool8 MainState_StartPageSwap(void)
{
SetInputState(INPUT_STATE_DISABLED);
StartPageSwapButtonAnim();
StartPageSwapAnim();
SetCursorInvisibility(TRUE);
TryStartButtonFlash(BUTTON_PAGE, FALSE, TRUE);
PlaySE(SE_WIN_OPEN);
sNamingScreen->state = STATE_WAIT_PAGE_SWAP;
return FALSE;
}
static bool8 MainState_WaitPageSwap(void)
{
s16 cursorX;
s16 cursorY;
bool32 onLastColumn;
if (IsPageSwapAnimNotInProgress())
{
GetCursorPos(&cursorX, &cursorY);
onLastColumn = (cursorX == GetCurrentPageColumnCount());
sNamingScreen->state = STATE_HANDLE_INPUT;
sNamingScreen->currentPage++;
sNamingScreen->currentPage %= KBPAGE_COUNT;
if (onLastColumn)
{
cursorX = GetCurrentPageColumnCount();
}
else
{
if (cursorX >= GetCurrentPageColumnCount())
cursorX = GetCurrentPageColumnCount() - 1;
}
SetCursorPos(cursorX, cursorY);
DrawKeyboardPageOnDeck();
SetInputState(INPUT_STATE_ENABLED);
SetCursorInvisibility(FALSE);
}
return FALSE;
}
//--------------------------------------------------
// Page Swap
//--------------------------------------------------
#define tState data[0]
#define tFrameCount data[1]
static bool8 PageSwapAnimState_Init(struct Task *);
static bool8 PageSwapAnimState_1(struct Task *);
static bool8 PageSwapAnimState_2(struct Task *);
static bool8 PageSwapAnimState_Done(struct Task *);
static bool8 (*const sPageSwapAnimStateFuncs[])(struct Task *) =
{
PageSwapAnimState_Init,
PageSwapAnimState_1,
PageSwapAnimState_2,
PageSwapAnimState_Done,
};
static void StartPageSwapAnim(void)
{
u8 taskId;
taskId = CreateTask(Task_HandlePageSwapAnim, 0);
Task_HandlePageSwapAnim(taskId);
}
static void Task_HandlePageSwapAnim(u8 taskId)
{
while (sPageSwapAnimStateFuncs[gTasks[taskId].tState](&gTasks[taskId]) != 0);
}
static bool8 IsPageSwapAnimNotInProgress(void)
{
if (FindTaskIdByFunc(Task_HandlePageSwapAnim) == TASK_NONE)
return TRUE;
else
return FALSE;
}
static bool8 PageSwapAnimState_Init(struct Task *task)
{
sNamingScreen->bg1vOffset = 0;
sNamingScreen->bg2vOffset = 0;
task->tState++;
return 0;
}
static bool8 PageSwapAnimState_1(struct Task *task)
{
u16 *const vOffsets[] =
{
&sNamingScreen->bg2vOffset,
&sNamingScreen->bg1vOffset
};
task->tFrameCount += 4;
*vOffsets[sNamingScreen->bgToReveal] = Sin(task->tFrameCount, 40);
*vOffsets[sNamingScreen->bgToHide] = Sin((task->tFrameCount + 128) & 0xFF, 40);
if (task->tFrameCount >= 64)
{
u8 temp = sNamingScreen->bg1Priority; //Why u8 and not u16?
sNamingScreen->bg1Priority = sNamingScreen->bg2Priority;
sNamingScreen->bg2Priority = temp;
task->tState++;
}
return 0;
}
static bool8 PageSwapAnimState_2(struct Task *task)
{
u16 *const vOffsets[] =
{
&sNamingScreen->bg2vOffset,
&sNamingScreen->bg1vOffset
};
task->tFrameCount += 4;
*vOffsets[sNamingScreen->bgToReveal] = Sin(task->tFrameCount, 40);
*vOffsets[sNamingScreen->bgToHide] = Sin((task->tFrameCount + 128) & 0xFF, 40);
if (task->tFrameCount >= 128)
{
u8 temp = sNamingScreen->bgToReveal;
sNamingScreen->bgToReveal = sNamingScreen->bgToHide;
sNamingScreen->bgToHide = temp;
task->tState++;
}
return 0;
}
static bool8 PageSwapAnimState_Done(struct Task *task)
{
DestroyTask(FindTaskIdByFunc(Task_HandlePageSwapAnim));
return 0;
}
#undef tState
#undef tFrameCount
//--------------------------------------------------
//
//--------------------------------------------------
#define tButtonId data[0]
#define tKeepFlashing data[1]
#define tAllowFlash data[2]
#define tColor data[3]
#define tColorIncr data[4]
#define tColorDelay data[5]
#define tColorDelta data[6]
static void CreateButtonFlashTask(void)
{
u8 taskId;
taskId = CreateTask(Task_UpdateButtonFlash, 3);
gTasks[taskId].tButtonId = BUTTON_COUNT;
}
static void TryStartButtonFlash(u8 button, bool8 keepFlashing, bool8 interruptCurFlash)
{
struct Task *task = &gTasks[FindTaskIdByFunc(Task_UpdateButtonFlash)];
if (button == task->tButtonId && !interruptCurFlash)
{
task->tKeepFlashing = keepFlashing;
task->tAllowFlash = TRUE;
return;
}
if (button == BUTTON_COUNT && !task->tKeepFlashing && !interruptCurFlash)
return;
if (task->tButtonId != BUTTON_COUNT)
RestoreButtonColor(task->tButtonId);
StartButtonFlash(task, button, keepFlashing);
}
static void Task_UpdateButtonFlash(u8 taskId)
{
struct Task *task = &gTasks[taskId];
if (task->tButtonId == BUTTON_COUNT || !task->tAllowFlash)
return;
MultiplyInvertedPaletteRGBComponents(GetButtonPalOffset(task->tButtonId), task->tColor, task->tColor, task->tColor);
if (task->tColorDelay && --task->tColorDelay)
return;
task->tColorDelay = 2;
if (task->tColorIncr >= 0)
{
if (task->tColor < 14)
{
task->tColor += task->tColorIncr;
task->tColorDelta += task->tColorIncr;
}
else
{
task->tColor = 16;
task->tColorDelta++;
}
}
else
{
task->tColor += task->tColorIncr;
task->tColorDelta += task->tColorIncr;
}
if (task->tColor == 16 && task->tColorDelta == 22)
{
task->tColorIncr = -4;