From 98a6fec7acfbc5ea2c6432f1f1c4c65a1f0a4247 Mon Sep 17 00:00:00 2001 From: somewhatlurker <52014015+somewhatlurker@users.noreply.github.com> Date: Wed, 23 Oct 2019 05:36:27 +1100 Subject: [PATCH] pause: menu code cleanup --- .../source/plugins/TLAC/Components/Pause.cpp | 65 ++++++---------- .../source/plugins/TLAC/Components/Pause.h | 78 +++++++++++++++---- 2 files changed, 87 insertions(+), 56 deletions(-) diff --git a/source-code/source/plugins/TLAC/Components/Pause.cpp b/source-code/source/plugins/TLAC/Components/Pause.cpp index 5ef1689..550b552 100644 --- a/source-code/source/plugins/TLAC/Components/Pause.cpp +++ b/source-code/source/plugins/TLAC/Components/Pause.cpp @@ -14,10 +14,10 @@ namespace TLAC::Components bool Pause::isPaused = false; bool Pause::giveUp = false; bool Pause::showUI = true; - unsigned int Pause::menuPos = 0; - unsigned int Pause::mainMenuPos = 0; - unsigned int Pause::menuSet = MENUSET_MAIN; - std::chrono::time_point Pause::menuItemSelectTime; + int Pause::curMenuPos = 0; + int Pause::mainMenuPos = 0; + Pause::menusets Pause::curMenuSet = MENUSET_MAIN; + std::chrono::time_point Pause::menuItemMoveTime; std::vector Pause::origAetMovOp; uint8_t* Pause::aetMovPatchAddress = (uint8_t*)0x1401703b3; std::vector Pause::origFramespeedOp; @@ -98,10 +98,8 @@ namespace TLAC::Components // block all buttons from being passed to game filteredButtons = allButtons; - menuPos = 0; - menuSet = MENUSET_MAIN; + setMenuPos(MENUSET_MAIN, 0); showUI = true; - menuItemSelectTime = std::chrono::high_resolution_clock::now(); isPaused = true; } @@ -129,47 +127,32 @@ namespace TLAC::Components if (showUI) { if (inputState->Tapped.Buttons & JVS_L) - { - menuPos -= 1; - menuItemSelectTime = std::chrono::high_resolution_clock::now(); - } + setMenuPos(curMenuSet, curMenuPos - 1); if (inputState->Tapped.Buttons & JVS_R) - { - menuPos += 1; - menuItemSelectTime = std::chrono::high_resolution_clock::now(); - } + setMenuPos(curMenuSet, curMenuPos + 1); if (inputState->Tapped.Buttons & JVS_TRIANGLE) { - if (menuSet != MENUSET_MAIN) + if (curMenuSet != MENUSET_MAIN) { - menuSet = MENUSET_MAIN; - menuPos = mainMenuPos; - menuItemSelectTime = std::chrono::high_resolution_clock::now(); + setMenuPos(MENUSET_MAIN, mainMenuPos); } } - - menuPos %= menuItems[menuSet].size(); - - if (menuSet == MENUSET_MAIN) - mainMenuPos = menuPos; - - // use released when unpausing from X so it doesn't trigger input - if (inputState->Released.Buttons & JVS_CROSS) + + if (inputState->Tapped.Buttons & JVS_CROSS) pause = false; - // use released because this can trigger an unpause too - if (inputState->Released.Buttons & JVS_CIRCLE) - menuItems[menuSet][menuPos].second(); + if (inputState->Tapped.Buttons & JVS_CIRCLE) + menu[curMenuSet].items[curMenuPos].callback(); - if (menuSet == MENUSET_SEVOL) + if (curMenuSet == MENUSET_SEVOL) { const char volformat[] = "%d"; size_t size = snprintf(nullptr, 0, volformat, playerData->act_vol) + 1; char* buf = new char[size]; snprintf(buf, size, volformat, playerData->act_vol); - menuItems[MENUSET_SEVOL][1].first = buf; + menu[MENUSET_SEVOL].items[1].name = buf; delete[] buf; } } @@ -269,8 +252,8 @@ namespace TLAC::Components dtParams.layer = contentLayer; // selection cursor - int selectBoxOrigin = menuY - menuItemHeight * (menuItems[menuSet].size() / 2.0) - (menuItemHeight - menuTextSize) / 2; - int selectBoxPos = selectBoxOrigin + menuItemHeight * menuPos; + int selectBoxOrigin = menuY - menuItemHeight * (menu[curMenuSet].items.size() / 2.0) - (menuItemHeight - menuTextSize) / 2; + int selectBoxPos = selectBoxOrigin + menuItemHeight * curMenuPos; const float selectBoxWidth = 200; const float selectBoxHeight = menuItemHeight; const float selectBoxThickness = 2; @@ -287,16 +270,16 @@ namespace TLAC::Components // menu fontInfo.setSize(menuTextSize, menuTextSize); - dtParams.originLoc = { menuX, menuY - menuItemHeight * (menuItems[menuSet].size() / 2.0f) }; + dtParams.originLoc = { menuX, menuY - menuItemHeight * (menu[curMenuSet].items.size() / 2.0f) }; - for (int i = 0; i < menuItems[menuSet].size(); i++) + for (int i = 0; i < menu[curMenuSet].items.size(); i++) { - std::pair &item = menuItems[menuSet][i]; + menuItem &item = menu[curMenuSet].items[i]; - if (i == menuPos) + if (i == curMenuPos) { const int duration = 1500000000; // 1.5s - std::chrono::nanoseconds cyclePos = (std::chrono::high_resolution_clock::now() - menuItemSelectTime) % std::chrono::nanoseconds(duration); + std::chrono::nanoseconds cyclePos = (std::chrono::high_resolution_clock::now() - menuItemMoveTime) % std::chrono::nanoseconds(duration); uint8_t alpha = (cosf(cyclePos.count() * 6.283185f / duration) * 0.15 + 0.85) * 255; dtParams.colour = 0x00ffff00 | (alpha << 24); } @@ -306,7 +289,7 @@ namespace TLAC::Components } dtParams.currentLoc = dtParams.originLoc; - drawText(&dtParams, (drawTextFlags)(DRAWTEXT_ALIGN_CENTRE), item.first); + drawText(&dtParams, (drawTextFlags)(DRAWTEXT_ALIGN_CENTRE), item.name); dtParams.originLoc.y += menuItemHeight; } @@ -328,7 +311,7 @@ namespace TLAC::Components dtParams.colour = 0xffffffff; drawTextW(&dtParams, (drawTextFlags)(DRAWTEXT_ENABLE_LAYOUT), L"L/R:Move "); - if (menuSet != MENUSET_MAIN) + if (curMenuSet != MENUSET_MAIN) { spriteLoc.x = dtParams.originLoc.x + halfSpriteSize; triangleAet = createAetLayer(3, 0x19, CREATEAET_20000, "button_sankaku", &spriteLoc, 0, nullptr, nullptr, 0, 0, &spriteScale, 0); diff --git a/source-code/source/plugins/TLAC/Components/Pause.h b/source-code/source/plugins/TLAC/Components/Pause.h index 4c4253b..30170f1 100644 --- a/source-code/source/plugins/TLAC/Components/Pause.h +++ b/source-code/source/plugins/TLAC/Components/Pause.h @@ -59,16 +59,13 @@ namespace TLAC::Components static const uint32_t contentLayer = 0x19; // same as startup screen static bool showUI; - static unsigned int menuPos; - static unsigned int mainMenuPos; // syncs to menuPos when menuSet == menuset_main (used for restoring on back) - static unsigned int menuSet; int triangleAet; int squareAet; int crossAet; int circleAet; - static std::chrono::time_point menuItemSelectTime; + static std::chrono::time_point menuItemMoveTime; // used for animation enum menusets { @@ -76,29 +73,80 @@ namespace TLAC::Components MENUSET_SEVOL = 1, }; - static void mainmenu() { menuSet = MENUSET_MAIN; menuPos = mainMenuPos; menuItemSelectTime = std::chrono::high_resolution_clock::now(); }; + struct menuItem + { + std::string name; + void(*callback)(); + bool keyRepeat; // unimplemented + + menuItem(std::string _name, void(*_callback)(), bool _keyRepeat) + { + name = _name; + callback = _callback; + keyRepeat = _keyRepeat; + } + }; + + struct menuSet + { + std::string name; + std::vector items; + }; + + static int curMenuPos; + static int mainMenuPos; // syncs to menuPos when menuSet == menuset_main (used for restoring on back) + static menusets curMenuSet; + + static void mainmenu() { curMenuSet = MENUSET_MAIN; curMenuPos = mainMenuPos; menuItemMoveTime = std::chrono::high_resolution_clock::now(); }; static void unpause() { pause = false; }; //static void restart() { *(uint8_t*)0x140d0b538 = 1; unpause(); } static void giveup() { giveUp = true; }; - static void sevolmenu() { menuSet = MENUSET_SEVOL; menuPos = 1; menuItemSelectTime = std::chrono::high_resolution_clock::now(); }; + static void sevolmenu() { curMenuSet = MENUSET_SEVOL; curMenuPos = 1; menuItemMoveTime = std::chrono::high_resolution_clock::now(); }; static void sevolplus() { setSEVolume(10); }; static void sevolminus() { setSEVolume(-10); }; // defined as an array now so submenus could be added - std::vector> menuItems[2] = { + // not sure how to make this static for consistency so whatever + menuSet menu[2] = { { - std::pair(std::string("RESUME"), &unpause), - //std::pair(std::string("RESTART"), &restart), - std::pair(std::string("SE VOLUME"), &sevolmenu), - std::pair(std::string("GIVE UP"), &giveup), + "PAUSED", + { + { "RESUME", unpause, false }, + //{ "RESTART", restart, false }, + { "SE VOLUME", sevolmenu, false }, + { "GIVE UP", giveup, false }, + } }, { - std::pair(std::string("+"), &sevolplus), - std::pair(std::string("XX"), &mainmenu), - std::pair(std::string("-"), &sevolminus), - } + "SE VOLUME", + { + { "+", sevolplus, true }, + { "XX", mainmenu, false }, + { "-", sevolminus, true }, + } + }, }; + + void setMenuPos(menusets set, int pos) + { + if (set >= 0 && set < _countof(menu)) + curMenuSet = set; + else + curMenuSet = MENUSET_MAIN; + + if (pos < 0) + pos = menu[curMenuSet].items.size() - 1; + else if (pos >= menu[curMenuSet].items.size()) + pos = 0; + + curMenuPos = pos; + + if (curMenuSet == MENUSET_MAIN) + mainMenuPos = curMenuPos; + + menuItemMoveTime = std::chrono::high_resolution_clock::now(); + } }; }