pokeemerald/src/menu_helpers.c

460 lines
11 KiB
C
Raw Normal View History

2018-05-14 23:03:40 +02:00
#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"
2018-10-21 09:24:57 +02:00
#include "graphics.h"
2018-05-14 23:03:40 +02:00
#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"
#include "constants/maps.h"
2021-04-11 22:23:10 +02:00
#define TAG_SWAP_LINE 109
2018-05-15 20:22:19 +02:00
static void Task_ContinueTaskAfterMessagePrints(u8 taskId);
static void Task_CallYesOrNoCallback(u8 taskId);
2021-08-03 08:17:01 +02:00
EWRAM_DATA static struct YesNoFuncTable sYesNo = {0};
EWRAM_DATA static u8 sMessageWindowId = 0;
2018-05-15 20:22:19 +02:00
2021-08-03 08:17:01 +02:00
static TaskFunc sMessageNextTask;
2018-05-15 20:22:19 +02:00
2021-04-11 22:23:10 +02:00
static const struct OamData sOamData_SwapLine =
2018-05-15 20:22:19 +02:00
{
.y = 0,
.affineMode = ST_OAM_AFFINE_OFF,
.objMode = ST_OAM_OBJ_NORMAL,
2018-05-15 20:22:19 +02:00
.mosaic = 0,
.bpp = ST_OAM_4BPP,
.shape = SPRITE_SHAPE(16x16),
2018-05-15 20:22:19 +02:00
.x = 0,
.matrixNum = 0,
.size = SPRITE_SIZE(16x16),
2018-05-15 20:22:19 +02:00
.tileNum = 0,
.priority = 0,
.paletteNum = 0,
.affineParam = 0
};
2021-04-11 22:23:10 +02:00
static const union AnimCmd sAnim_SwapLine_RightArrow[] =
2018-05-15 20:22:19 +02:00
{
ANIMCMD_FRAME(0, 0),
ANIMCMD_END
};
2021-04-11 22:23:10 +02:00
static const union AnimCmd sAnim_SwapLine_Line[] =
2018-05-15 20:22:19 +02:00
{
ANIMCMD_FRAME(4, 0),
ANIMCMD_END
};
2021-04-11 22:23:10 +02:00
static const union AnimCmd sAnim_SwapLine_LeftArrow[] =
2018-05-15 20:22:19 +02:00
{
2021-04-11 22:23:10 +02:00
ANIMCMD_FRAME(0, 0, .hFlip = TRUE),
2018-05-15 20:22:19 +02:00
ANIMCMD_END
};
2021-04-11 22:23:10 +02:00
static const union AnimCmd *const sAnims_SwapLine[] =
2018-05-15 20:22:19 +02:00
{
2021-04-11 22:23:10 +02:00
sAnim_SwapLine_RightArrow,
sAnim_SwapLine_Line,
sAnim_SwapLine_LeftArrow
2018-05-15 20:22:19 +02:00
};
2021-04-11 22:23:10 +02:00
static const struct CompressedSpriteSheet sSpriteSheet_SwapLine =
2018-05-15 20:22:19 +02:00
{
2021-04-11 22:23:10 +02:00
gBagSwapLineGfx, 0x100, TAG_SWAP_LINE
2018-05-15 20:22:19 +02:00
};
2021-04-11 22:23:10 +02:00
static const struct CompressedSpritePalette sSpritePalette_SwapLine =
2018-05-15 20:22:19 +02:00
{
2021-04-11 22:23:10 +02:00
gBagSwapLinePal, TAG_SWAP_LINE
2018-05-15 20:22:19 +02:00
};
2021-04-11 22:23:10 +02:00
static const struct SpriteTemplate sSpriteTemplate_SwapLine =
2018-05-15 20:22:19 +02:00
{
2021-04-11 22:23:10 +02:00
.tileTag = TAG_SWAP_LINE,
.paletteTag = TAG_SWAP_LINE,
.oam = &sOamData_SwapLine,
.anims = sAnims_SwapLine,
2018-05-15 20:22:19 +02:00
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCallbackDummy,
};
2018-05-14 23:03:40 +02:00
// 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, 0);
ChangeBgY(0, 0, 0);
ChangeBgX(1, 0, 0);
ChangeBgY(1, 0, 0);
ChangeBgX(2, 0, 0);
ChangeBgY(2, 0, 0);
ChangeBgX(3, 0, 0);
ChangeBgY(3, 0, 0);
}
void SetVBlankHBlankCallbacksToNull(void)
{
SetVBlankCallback(NULL);
SetHBlankCallback(NULL);
}
2021-08-03 08:17:01 +02:00
void DisplayMessageAndContinueTask(u8 taskId, u8 windowId, u16 tileNum, u8 paletteNum, u8 fontId, u8 textSpeed, const u8 *string, void *taskFunc)
2018-05-14 23:03:40 +02:00
{
2021-08-03 08:17:01 +02:00
sMessageWindowId = windowId;
DrawDialogFrameWithCustomTileAndPalette(windowId, TRUE, tileNum, paletteNum);
2018-05-14 23:03:40 +02:00
if (string != gStringVar4)
StringExpandPlaceholders(gStringVar4, string);
2018-11-06 17:44:48 +01:00
gTextFlags.canABSpeedUpPrint = 1;
AddTextPrinterParameterized2(windowId, fontId, gStringVar4, textSpeed, NULL, 2, 1, 3);
2021-08-03 08:17:01 +02:00
sMessageNextTask = taskFunc;
2018-05-14 23:03:40 +02:00
gTasks[taskId].func = Task_ContinueTaskAfterMessagePrints;
}
bool16 RunTextPrintersRetIsActive(u8 textPrinterId)
{
RunTextPrinters();
return IsTextPrinterActive(textPrinterId);
}
2018-05-15 20:22:19 +02:00
static void Task_ContinueTaskAfterMessagePrints(u8 taskId)
2018-05-14 23:03:40 +02:00
{
2021-08-03 08:17:01 +02:00
if (!RunTextPrintersRetIsActive(sMessageWindowId))
sMessageNextTask(taskId);
2018-05-14 23:03:40 +02:00
}
2018-12-31 23:09:45 +01:00
void DoYesNoFuncWithChoice(u8 taskId, const struct YesNoFuncTable *data)
2018-05-14 23:03:40 +02:00
{
2021-08-03 08:17:01 +02:00
sYesNo = *data;
2018-05-14 23:03:40 +02:00
gTasks[taskId].func = Task_CallYesOrNoCallback;
}
void CreateYesNoMenuWithCallbacks(u8 taskId, const struct WindowTemplate *template, u8 arg2, u8 arg3, u8 arg4, u16 tileStart, u8 palette, const struct YesNoFuncTable *yesNo)
{
CreateYesNoMenu(template, tileStart, palette, 0);
2021-08-03 08:17:01 +02:00
sYesNo = *yesNo;
2018-05-14 23:03:40 +02:00
gTasks[taskId].func = Task_CallYesOrNoCallback;
}
2018-05-15 20:22:19 +02:00
static void Task_CallYesOrNoCallback(u8 taskId)
2018-05-14 23:03:40 +02:00
{
2018-11-05 21:45:54 +01:00
switch (Menu_ProcessInputNoWrapClearOnChoose())
2018-05-14 23:03:40 +02:00
{
case 0:
PlaySE(SE_SELECT);
2021-08-03 08:17:01 +02:00
sYesNo.yesFunc(taskId);
2018-05-14 23:03:40 +02:00
break;
case 1:
case MENU_B_PRESSED:
PlaySE(SE_SELECT);
2021-08-03 08:17:01 +02:00
sYesNo.noFunc(taskId);
2018-05-14 23:03:40 +02:00
break;
}
}
bool8 AdjustQuantityAccordingToDPadInput(s16 *arg0, u16 arg1)
{
s16 valBefore = (*arg0);
if ((JOY_REPEAT(DPAD_ANY)) == DPAD_UP)
2018-05-14 23:03:40 +02:00
{
(*arg0)++;
if ((*arg0) > arg1)
(*arg0) = 1;
if ((*arg0) == valBefore)
{
return FALSE;
}
else
{
PlaySE(SE_SELECT);
return TRUE;
}
}
else if ((JOY_REPEAT(DPAD_ANY)) == DPAD_DOWN)
2018-05-14 23:03:40 +02:00
{
(*arg0)--;
if ((*arg0) <= 0)
(*arg0) = arg1;
if ((*arg0) == valBefore)
{
return FALSE;
}
else
{
PlaySE(SE_SELECT);
return TRUE;
}
}
else if ((JOY_REPEAT(DPAD_ANY)) == DPAD_RIGHT)
2018-05-14 23:03:40 +02:00
{
(*arg0) += 10;
if ((*arg0) > arg1)
(*arg0) = arg1;
if ((*arg0) == valBefore)
{
return FALSE;
}
else
{
PlaySE(SE_SELECT);
return TRUE;
}
}
else if ((JOY_REPEAT(DPAD_ANY)) == DPAD_LEFT)
2018-05-14 23:03:40 +02:00
{
(*arg0) -= 10;
if ((*arg0) <= 0)
(*arg0) = 1;
if ((*arg0) == valBefore)
{
return FALSE;
}
else
{
PlaySE(SE_SELECT);
return TRUE;
}
}
return FALSE;
}
2019-10-18 01:22:03 +02:00
u8 GetLRKeysPressed(void)
2018-05-14 23:03:40 +02:00
{
if (gSaveBlock2Ptr->optionsButtonMode == OPTIONS_BUTTON_MODE_LR)
{
if (JOY_NEW(L_BUTTON))
2019-10-18 01:22:03 +02:00
return MENU_L_PRESSED;
if (JOY_NEW(R_BUTTON))
2019-10-18 01:22:03 +02:00
return MENU_R_PRESSED;
2018-05-14 23:03:40 +02:00
}
return 0;
}
2019-10-18 01:22:03 +02:00
u8 GetLRKeysPressedAndHeld(void)
2018-05-14 23:03:40 +02:00
{
if (gSaveBlock2Ptr->optionsButtonMode == OPTIONS_BUTTON_MODE_LR)
{
if (JOY_REPEAT(L_BUTTON))
2019-10-18 01:22:03 +02:00
return MENU_L_PRESSED;
if (JOY_REPEAT(R_BUTTON))
2019-10-18 01:22:03 +02:00
return MENU_R_PRESSED;
2018-05-14 23:03:40 +02:00
}
return 0;
}
2021-08-03 08:17:01 +02:00
bool8 IsHoldingItemAllowed(u16 itemId)
2018-05-14 23:03:40 +02:00
{
2021-08-03 08:17:01 +02:00
// Enigma Berry can't be held in link areas
2018-05-14 23:03:40 +02:00
if (itemId != ITEM_ENIGMA_BERRY)
return TRUE;
2021-08-03 08:17:01 +02:00
else if (gSaveBlock1Ptr->location.mapGroup == MAP_GROUP(TRADE_CENTER)
&& gSaveBlock1Ptr->location.mapNum == MAP_NUM(TRADE_CENTER))
2018-05-14 23:03:40 +02:00
return FALSE;
else if (InUnionRoom() != TRUE)
return TRUE;
else
return FALSE;
}
2021-04-23 00:49:54 +02:00
bool8 IsWritingMailAllowed(u16 itemId)
2018-05-14 23:03:40 +02:00
{
if (IsUpdateLinkStateCBActive() != TRUE && InUnionRoom() != TRUE)
2018-05-14 23:03:40 +02:00
return TRUE;
else if (ItemIsMail(itemId) != TRUE)
return TRUE;
else
return FALSE;
}
2020-05-14 10:37:09 +02:00
bool8 MenuHelpers_LinkSomething(void)
2018-05-14 23:03:40 +02:00
{
if (IsUpdateLinkStateCBActive() == TRUE || gReceivedRemoteLinkPlayers == 1)
2018-05-14 23:03:40 +02:00
return TRUE;
else
return FALSE;
}
static bool8 sub_81221D0(void)
{
2020-05-14 10:37:09 +02:00
if (!MenuHelpers_LinkSomething())
2018-05-14 23:03:40 +02:00
return FALSE;
else
2021-03-19 01:35:39 +01:00
return Overworld_LinkRecvQueueLengthMoreThan2();
2018-05-14 23:03:40 +02:00
}
2020-05-14 10:37:09 +02:00
bool8 MenuHelpers_CallLinkSomething(void)
2018-05-14 23:03:40 +02:00
{
if (sub_81221D0() == TRUE)
return TRUE;
2021-03-19 19:10:13 +01:00
else if (IsLinkRecvQueueLengthAtLeast3() != TRUE)
2018-05-14 23:03:40 +02:00
return FALSE;
else
return TRUE;
}
2021-04-25 18:07:08 +02:00
void SetItemListPerPageCount(struct ItemSlot *slots, u8 slotsCount, u8 *pageItems, u8 *totalItems, u8 maxPerPage)
2018-05-14 23:03:40 +02:00
{
u16 i;
struct ItemSlot *slots_ = slots;
2021-04-25 18:07:08 +02:00
// Count the number of non-empty item slots
*totalItems = 0;
for (i = 0; i < slotsCount; i++)
2018-05-14 23:03:40 +02:00
{
if (slots_[i].itemId != ITEM_NONE)
2021-04-25 18:07:08 +02:00
(*totalItems)++;
2018-05-14 23:03:40 +02:00
}
2021-04-25 18:07:08 +02:00
(*totalItems)++; // + 1 for 'Cancel'
2018-05-14 23:03:40 +02:00
2021-04-25 18:07:08 +02:00
// Set number of items per page
if (*totalItems > maxPerPage)
*pageItems = maxPerPage;
2018-05-14 23:03:40 +02:00
else
2021-04-25 18:07:08 +02:00
*pageItems = *totalItems;
2018-05-14 23:03:40 +02:00
}
2021-04-25 18:07:08 +02:00
void SetCursorWithinListBounds(u16 *scrollOffset, u16 *cursorPos, u8 maxShownItems, u8 totalItems)
2018-05-14 23:03:40 +02:00
{
2021-04-25 18:07:08 +02:00
if (*scrollOffset != 0 && *scrollOffset + maxShownItems > totalItems)
*scrollOffset = totalItems - maxShownItems;
2018-05-14 23:03:40 +02:00
2021-04-25 18:07:08 +02:00
if (*scrollOffset + *cursorPos >= totalItems)
2018-05-14 23:03:40 +02:00
{
2021-04-25 18:07:08 +02:00
if (totalItems == 0)
*cursorPos = 0;
2018-05-14 23:03:40 +02:00
else
2021-04-25 18:07:08 +02:00
*cursorPos = totalItems - 1;
2018-05-14 23:03:40 +02:00
}
}
2021-08-03 08:17:01 +02:00
void SetCursorScrollWithinListBounds(u16 *scrollOffset, u16 *cursorPos, u8 shownItems, u8 totalItems, u8 maxShownItems)
2018-05-14 23:03:40 +02:00
{
2018-05-15 20:22:19 +02:00
u8 i;
2018-05-14 23:03:40 +02:00
2021-08-03 08:17:01 +02:00
if (maxShownItems % 2 != 0)
2018-05-15 20:22:19 +02:00
{
2021-08-03 08:17:01 +02:00
// Is cursor at least halfway down visible list
if (*cursorPos >= maxShownItems / 2)
2018-05-15 20:22:19 +02:00
{
2021-08-03 08:17:01 +02:00
for (i = 0; i < *cursorPos - (maxShownItems / 2); i++)
2018-05-15 20:22:19 +02:00
{
2021-08-03 08:17:01 +02:00
// Stop if reached end of list
if (*scrollOffset + shownItems == totalItems)
2018-05-15 20:22:19 +02:00
break;
2021-08-03 08:17:01 +02:00
(*cursorPos)--;
(*scrollOffset)++;
2018-05-15 20:22:19 +02:00
}
}
2018-05-14 23:03:40 +02:00
}
else
{
2021-08-03 08:17:01 +02:00
// Is cursor at least halfway down visible list
if (*cursorPos >= (maxShownItems / 2) + 1)
2018-05-15 20:22:19 +02:00
{
2021-08-03 08:17:01 +02:00
for (i = 0; i <= *cursorPos - (maxShownItems / 2); i++)
2018-05-15 20:22:19 +02:00
{
2021-08-03 08:17:01 +02:00
// Stop if reached end of list
if (*scrollOffset + shownItems == totalItems)
2018-05-15 20:22:19 +02:00
break;
2021-08-03 08:17:01 +02:00
(*cursorPos)--;
(*scrollOffset)++;
2018-05-15 20:22:19 +02:00
}
}
2018-05-14 23:03:40 +02:00
}
}
2021-04-11 22:23:10 +02:00
void LoadListMenuSwapLineGfx(void)
2018-05-14 23:03:40 +02:00
{
2021-04-11 22:23:10 +02:00
LoadCompressedSpriteSheet(&sSpriteSheet_SwapLine);
LoadCompressedSpritePalette(&sSpritePalette_SwapLine);
2018-05-14 23:03:40 +02:00
}
2018-05-15 20:22:19 +02:00
2021-04-11 22:23:10 +02:00
void CreateSwapLineSprites(u8 *spriteIds, u8 count)
2018-05-15 20:22:19 +02:00
{
u8 i;
for (i = 0; i < count; i++)
{
2021-04-11 22:23:10 +02:00
spriteIds[i] = CreateSprite(&sSpriteTemplate_SwapLine, i * 16, 0, 0);
2018-05-15 20:22:19 +02:00
if (i != 0)
StartSpriteAnim(&gSprites[spriteIds[i]], 1);
gSprites[spriteIds[i]].invisible = TRUE;
2018-05-15 20:22:19 +02:00
}
}
2021-04-11 22:23:10 +02:00
void DestroySwapLineSprites(u8 *spriteIds, u8 count)
2018-05-15 20:22:19 +02:00
{
u8 i;
for (i = 0; i < count; i++)
{
if (i == count - 1)
DestroySpriteAndFreeResources(&gSprites[spriteIds[i]]);
else
DestroySprite(&gSprites[spriteIds[i]]);
}
}
2021-04-11 22:23:10 +02:00
void SetSwapLineSpritesInvisibility(u8 *spriteIds, u8 count, bool8 invisible)
2018-05-15 20:22:19 +02:00
{
u8 i;
for (i = 0; i < count; i++)
gSprites[spriteIds[i]].invisible = invisible;
}
2021-04-11 22:23:10 +02:00
void UpdateSwapLineSpritesPos(u8 *spriteIds, u8 count, s16 x, u16 y)
2018-05-15 20:22:19 +02:00
{
u8 i;
2021-08-21 17:04:28 +02:00
bool8 hasMargin = count & SWAP_LINE_HAS_MARGIN;
count &= ~SWAP_LINE_HAS_MARGIN;
2018-05-15 20:22:19 +02:00
for (i = 0; i < count; i++)
{
2021-08-21 17:04:28 +02:00
// 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)
2021-07-07 15:11:52 +02:00
gSprites[spriteIds[i]].x2 = x - 8;
2018-05-15 20:22:19 +02:00
else
2021-07-07 15:11:52 +02:00
gSprites[spriteIds[i]].x2 = x;
2018-05-15 20:22:19 +02:00
2021-07-07 15:11:52 +02:00
gSprites[spriteIds[i]].y = 1 + y;
2018-05-15 20:22:19 +02:00
}
}