forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmenu_helpers.c
453 lines (396 loc) · 10.8 KB
/
menu_helpers.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
#include "global.h"
#include "task.h"
#include "window.h"
#include "menu.h"
#include "menu_helpers.h"
#include "gpu_regs.h"
#include "bg.h"
#include "main.h"
#include "text.h"
#include "graphics.h"
#include "link.h"
#include "string_util.h"
#include "sound.h"
#include "mail.h"
#include "overworld.h"
#include "decompress.h"
#include "constants/songs.h"
#include "constants/items.h"
#define TAG_SWAP_LINE 109
static void Task_ContinueTaskAfterMessagePrints(u8 taskId);
static void Task_CallYesOrNoCallback(u8 taskId);
EWRAM_DATA static struct YesNoFuncTable sYesNo = {0};
EWRAM_DATA static u8 sMessageWindowId = 0;
static TaskFunc sMessageNextTask;
static const struct OamData sOamData_SwapLine =
{
.y = 0,
.affineMode = ST_OAM_AFFINE_OFF,
.objMode = ST_OAM_OBJ_NORMAL,
.mosaic = FALSE,
.bpp = ST_OAM_4BPP,
.shape = SPRITE_SHAPE(16x16),
.x = 0,
.matrixNum = 0,
.size = SPRITE_SIZE(16x16),
.tileNum = 0,
.priority = 0,
.paletteNum = 0,
.affineParam = 0
};
static const union AnimCmd sAnim_SwapLine_RightArrow[] =
{
ANIMCMD_FRAME(0, 0),
ANIMCMD_END
};
static const union AnimCmd sAnim_SwapLine_Line[] =
{
ANIMCMD_FRAME(4, 0),
ANIMCMD_END
};
static const union AnimCmd sAnim_SwapLine_LeftArrow[] =
{
ANIMCMD_FRAME(0, 0, .hFlip = TRUE),
ANIMCMD_END
};
static const union AnimCmd *const sAnims_SwapLine[] =
{
sAnim_SwapLine_RightArrow,
sAnim_SwapLine_Line,
sAnim_SwapLine_LeftArrow
};
static const struct CompressedSpriteSheet sSpriteSheet_SwapLine =
{
gSwapLineGfx, 0x100, TAG_SWAP_LINE
};
static const struct CompressedSpritePalette sSpritePalette_SwapLine =
{
gSwapLinePal, TAG_SWAP_LINE
};
static const struct SpriteTemplate sSpriteTemplate_SwapLine =
{
.tileTag = TAG_SWAP_LINE,
.paletteTag = TAG_SWAP_LINE,
.oam = &sOamData_SwapLine,
.anims = sAnims_SwapLine,
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCallbackDummy,
};
// code
void ResetVramOamAndBgCntRegs(void)
{
SetGpuReg(REG_OFFSET_DISPCNT, 0);
SetGpuReg(REG_OFFSET_BG3CNT, 0);
SetGpuReg(REG_OFFSET_BG2CNT, 0);
SetGpuReg(REG_OFFSET_BG1CNT, 0);
SetGpuReg(REG_OFFSET_BG0CNT, 0);
CpuFill16(0, (void *) VRAM, VRAM_SIZE);
CpuFill32(0, (void *) OAM, OAM_SIZE);
CpuFill16(0, (void *) PLTT, PLTT_SIZE);
}
void ResetAllBgsCoordinates(void)
{
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);
}
void SetVBlankHBlankCallbacksToNull(void)
{
SetVBlankCallback(NULL);
SetHBlankCallback(NULL);
}
void DisplayMessageAndContinueTask(u8 taskId, u8 windowId, u16 tileNum, u8 paletteNum, u8 fontId, u8 textSpeed, const u8 *string, void *taskFunc)
{
sMessageWindowId = windowId;
DrawDialogFrameWithCustomTileAndPalette(windowId, TRUE, tileNum, paletteNum);
if (string != gStringVar4)
StringExpandPlaceholders(gStringVar4, string);
gTextFlags.canABSpeedUpPrint = 1;
AddTextPrinterParameterized2(windowId, fontId, gStringVar4, textSpeed, NULL, TEXT_COLOR_DARK_GRAY, TEXT_COLOR_WHITE, TEXT_COLOR_LIGHT_GRAY);
sMessageNextTask = taskFunc;
gTasks[taskId].func = Task_ContinueTaskAfterMessagePrints;
}
bool16 RunTextPrintersRetIsActive(u8 textPrinterId)
{
RunTextPrinters();
return IsTextPrinterActive(textPrinterId);
}
static void Task_ContinueTaskAfterMessagePrints(u8 taskId)
{
if (!RunTextPrintersRetIsActive(sMessageWindowId))
sMessageNextTask(taskId);
}
void DoYesNoFuncWithChoice(u8 taskId, const struct YesNoFuncTable *data)
{
sYesNo = *data;
gTasks[taskId].func = Task_CallYesOrNoCallback;
}
void CreateYesNoMenuWithCallbacks(u8 taskId, const struct WindowTemplate *template, u8 unused1, u8 unused2, u8 unused3, u16 tileStart, u8 palette, const struct YesNoFuncTable *yesNo)
{
CreateYesNoMenu(template, tileStart, palette, 0);
sYesNo = *yesNo;
gTasks[taskId].func = Task_CallYesOrNoCallback;
}
static void Task_CallYesOrNoCallback(u8 taskId)
{
switch (Menu_ProcessInputNoWrapClearOnChoose())
{
case 0:
PlaySE(SE_SELECT);
sYesNo.yesFunc(taskId);
break;
case 1:
case MENU_B_PRESSED:
PlaySE(SE_SELECT);
sYesNo.noFunc(taskId);
break;
}
}
// Returns TRUE if the quantity was changed, FALSE if it remained the same
bool8 AdjustQuantityAccordingToDPadInput(s16 *quantity, u16 max)
{
s16 valBefore = *quantity;
if (JOY_REPEAT(DPAD_ANY) == DPAD_UP)
{
(*quantity)++;
if (*quantity > max)
*quantity = 1;
if (*quantity == valBefore)
{
return FALSE;
}
else
{
PlaySE(SE_SELECT);
return TRUE;
}
}
else if (JOY_REPEAT(DPAD_ANY) == DPAD_DOWN)
{
(*quantity)--;
if (*quantity <= 0)
*quantity = max;
if (*quantity == valBefore)
{
return FALSE;
}
else
{
PlaySE(SE_SELECT);
return TRUE;
}
}
else if (JOY_REPEAT(DPAD_ANY) == DPAD_RIGHT)
{
*quantity += 10;
if (*quantity > max)
*quantity = max;
if (*quantity == valBefore)
{
return FALSE;
}
else
{
PlaySE(SE_SELECT);
return TRUE;
}
}
else if (JOY_REPEAT(DPAD_ANY) == DPAD_LEFT)
{
*quantity -= 10;
if (*quantity <= 0)
*quantity = 1;
if (*quantity == valBefore)
{
return FALSE;
}
else
{
PlaySE(SE_SELECT);
return TRUE;
}
}
return FALSE;
}
u8 GetLRKeysPressed(void)
{
if (gSaveBlock2Ptr->optionsButtonMode == OPTIONS_BUTTON_MODE_LR)
{
if (JOY_NEW(L_BUTTON))
return MENU_L_PRESSED;
if (JOY_NEW(R_BUTTON))
return MENU_R_PRESSED;
}
return 0;
}
u8 GetLRKeysPressedAndHeld(void)
{
if (gSaveBlock2Ptr->optionsButtonMode == OPTIONS_BUTTON_MODE_LR)
{
if (JOY_REPEAT(L_BUTTON))
return MENU_L_PRESSED;
if (JOY_REPEAT(R_BUTTON))
return MENU_R_PRESSED;
}
return 0;
}
bool8 IsHoldingItemAllowed(u16 itemId)
{
// Enigma Berry can't be held in link areas
if (itemId == ITEM_ENIGMA_BERRY
&& ((gSaveBlock1Ptr->location.mapGroup == MAP_GROUP(TRADE_CENTER)
&& gSaveBlock1Ptr->location.mapNum == MAP_NUM(TRADE_CENTER))
|| InUnionRoom() == TRUE))
return FALSE;
else
return TRUE;
}
bool8 IsWritingMailAllowed(u16 itemId)
{
if ((IsOverworldLinkActive() == TRUE || InUnionRoom() == TRUE) && ItemIsMail(itemId) == TRUE)
return FALSE;
else
return TRUE;
}
bool8 MenuHelpers_IsLinkActive(void)
{
if (IsOverworldLinkActive() == TRUE || gReceivedRemoteLinkPlayers == 1)
return TRUE;
else
return FALSE;
}
static bool8 IsActiveOverworldLinkBusy(void)
{
if (!MenuHelpers_IsLinkActive())
return FALSE;
else
return Overworld_IsRecvQueueAtMax();
}
bool8 MenuHelpers_ShouldWaitForLinkRecv(void)
{
if (IsActiveOverworldLinkBusy() == TRUE || IsLinkRecvQueueAtOverworldMax() == TRUE )
return TRUE;
else
return FALSE;
}
void SetItemListPerPageCount(struct ItemSlot *slots, u8 slotsCount, u8 *pageItems, u8 *totalItems, u8 maxPerPage)
{
u16 i;
struct ItemSlot *slots_ = slots;
// Count the number of non-empty item slots
*totalItems = 0;
for (i = 0; i < slotsCount; i++)
{
if (slots_[i].itemId != ITEM_NONE)
(*totalItems)++;
}
(*totalItems)++; // + 1 for 'Cancel'
// Set number of items per page
if (*totalItems > maxPerPage)
*pageItems = maxPerPage;
else
*pageItems = *totalItems;
}
void SetCursorWithinListBounds(u16 *scrollOffset, u16 *cursorPos, u8 maxShownItems, u8 totalItems)
{
if (*scrollOffset != 0 && *scrollOffset + maxShownItems > totalItems)
*scrollOffset = totalItems - maxShownItems;
if (*scrollOffset + *cursorPos >= totalItems)
{
if (totalItems == 0)
*cursorPos = 0;
else
*cursorPos = totalItems - 1;
}
}
void SetCursorScrollWithinListBounds(u16 *scrollOffset, u16 *cursorPos, u8 shownItems, u8 totalItems, u8 maxShownItems)
{
u8 i;
if (maxShownItems % 2 != 0)
{
// Is cursor at least halfway down visible list
if (*cursorPos >= maxShownItems / 2)
{
for (i = 0; i < *cursorPos - (maxShownItems / 2); i++)
{
// Stop if reached end of list
if (*scrollOffset + shownItems == totalItems)
break;
(*cursorPos)--;
(*scrollOffset)++;
}
}
}
else
{
// Is cursor at least halfway down visible list
if (*cursorPos >= (maxShownItems / 2) + 1)
{
for (i = 0; i <= *cursorPos - (maxShownItems / 2); i++)
{
// Stop if reached end of list
if (*scrollOffset + shownItems == totalItems)
break;
(*cursorPos)--;
(*scrollOffset)++;
}
}
}
}
void LoadListMenuSwapLineGfx(void)
{
LoadCompressedSpriteSheet(&sSpriteSheet_SwapLine);
LoadCompressedSpritePalette(&sSpritePalette_SwapLine);
}
void CreateSwapLineSprites(u8 *spriteIds, u8 count)
{
u8 i;
for (i = 0; i < count; i++)
{
spriteIds[i] = CreateSprite(&sSpriteTemplate_SwapLine, i * 16, 0, 0);
if (i != 0)
StartSpriteAnim(&gSprites[spriteIds[i]], 1);
gSprites[spriteIds[i]].invisible = TRUE;
}
}
void DestroySwapLineSprites(u8 *spriteIds, u8 count)
{
u8 i;
for (i = 0; i < count; i++)
{
if (i == count - 1)
DestroySpriteAndFreeResources(&gSprites[spriteIds[i]]);
else
DestroySprite(&gSprites[spriteIds[i]]);
}
}
void SetSwapLineSpritesInvisibility(u8 *spriteIds, u8 count, bool8 invisible)
{
u8 i;
for (i = 0; i < count; i++)
gSprites[spriteIds[i]].invisible = invisible;
}
void UpdateSwapLineSpritesPos(u8 *spriteIds, u8 count, s16 x, u16 y)
{
u8 i;
bool8 hasMargin = count & SWAP_LINE_HAS_MARGIN;
count &= ~SWAP_LINE_HAS_MARGIN;
for (i = 0; i < count; i++)
{
// If the list menu has a right margin, the swap line
// shouldn't extend all the way to the edge of the screen.
// If this is the last sprite in the line, move it a bit
// to the left to keep it out of the margin.
if (i == count - 1 && hasMargin)
gSprites[spriteIds[i]].x2 = x - 8;
else
gSprites[spriteIds[i]].x2 = x;
gSprites[spriteIds[i]].y = 1 + y;
}
}