Refactor ExtrasMenu code (So far)

This commit is contained in:
J-D-K 2025-07-18 10:29:02 -04:00
parent 6d3b1b72b5
commit 2564dcbd6d
4 changed files with 56 additions and 32 deletions

View File

@ -36,24 +36,20 @@ class ConfirmState final : public BaseState
using TaskFunction = void (*)(TaskType *, std::shared_ptr<StructType>);
/// @brief Constructor for new ConfirmState.
/// @param queryString The string displayed.
/// @param query The string displayed.
/// @param holdRequired Whether or not confirmation requires holding A for three seconds.
/// @param function Function executed on confirmation.
/// @param dataStruct shared_ptr<StructType> that is passed to function. I tried templating this and it was a nightmare.
ConfirmState(std::string_view queryString,
bool holdRequired,
TaskFunction function,
std::shared_ptr<StructType> dataStruct)
ConfirmState(std::string_view query, bool holdRequired, TaskFunction function, std::shared_ptr<StructType> dataStruct)
: BaseState(false)
, m_queryString(queryString)
, m_queryString(query)
, m_yesText(strings::get_by_name(strings::names::YES_NO, 0))
, m_noText(strings::get_by_name(strings::names::YES_NO, 1))
, m_holdRequired(holdRequired)
, m_function(function)
, m_dataStruct(dataStruct)
{
const int yesWidth = sdl::text::get_width(22, m_yesText);
const int noWidth = sdl::text::get_width(22, m_noText);
const int noWidth = sdl::text::get_width(22, m_noText);
// This stays the same from here on out.
m_noX = COORD_NO_X - (noWidth / 2);
@ -66,6 +62,15 @@ class ConfirmState final : public BaseState
/// @brief Required even if it does nothing.
~ConfirmState() {};
/// @brief Creation function to help clean up code elsewhere.
std::shared_ptr<ConfirmState> create(std::string_view query,
bool holdRequired,
TaskFunction function,
std::shared_ptr<StructType> dataStruct)
{
return std::make_shared<ConfirmState>(query, holdRequired, function, dataStruct);
}
/// @brief Just updates the ConfirmState.
void update() override
{

View File

@ -25,4 +25,10 @@ class ExtrasMenuState final : public BaseState
/// @brief Render target for menu.
sdl::SharedTexture m_renderTarget{};
/// @brief Creates and loads the menu strings.
void initialize_menu();
/// @brief This function is called when Reinitialize data is selected.
void reinitialize_data();
};

View File

@ -69,7 +69,8 @@
"Terminate Process"
],
"ExtrasPopMessages": [
"Data reinitialized!"
"Data reinitialized!",
"Data reinitialization failed"
],
"YesNo": [
"Yes [A]",

View File

@ -1,4 +1,5 @@
#include "appstates/ExtrasMenuState.hpp"
#include "appstates/MainMenuState.hpp"
#include "colors.hpp"
#include "data/data.hpp"
@ -6,6 +7,7 @@
#include "keyboard.hpp"
#include "strings.hpp"
#include "ui/PopMessageManager.hpp"
#include <string_view>
namespace
@ -27,23 +29,20 @@ namespace
} // namespace
ExtrasMenuState::ExtrasMenuState()
: m_extrasMenu(32, 8, 1000, 24, 555),
m_renderTarget(sdl::TextureManager::create_load_texture(SECONDARY_TARGET,
: m_extrasMenu(32, 8, 1000, 24, 555)
, m_renderTarget(sdl::TextureManager::create_load_texture(SECONDARY_TARGET,
1080,
555,
SDL_TEXTUREACCESS_STATIC | SDL_TEXTUREACCESS_TARGET))
{
const char *extrasString = nullptr;
int currentString = 0;
while ((extrasString = strings::get_by_name(strings::names::EXTRAS_MENU, currentString++)) != nullptr)
{
m_extrasMenu.add_option(extrasString);
}
ExtrasMenuState::initialize_menu();
}
void ExtrasMenuState::update()
{
const bool hasFocus = BaseState::has_focus();
const bool aPressed = input::button_pressed(HidNpadButton_A);
const bool bPressed = input::button_pressed(HidNpadButton_B);
m_extrasMenu.update(hasFocus);
@ -51,23 +50,10 @@ void ExtrasMenuState::update()
{
switch (m_extrasMenu.get_selected())
{
case REINIT_DATA:
{
// Reinitialize the data with the cache cleared.
data::initialize(true);
ui::PopMessageManager::push_message(ui::PopMessageManager::DEFAULT_MESSAGE_TICKS,
strings::get_by_name(strings::names::EXTRAS_POP_MESSAGES, 0));
// This should be adequate for this...
MainMenuState::refresh_view_states();
}
break;
case REINIT_DATA: ExtrasMenuState::reinitialize_data(); break;
}
}
else if (input::button_pressed(HidNpadButton_B))
{
BaseState::deactivate();
}
else if (input::button_pressed(HidNpadButton_B)) { BaseState::deactivate(); }
}
void ExtrasMenuState::render()
@ -78,3 +64,29 @@ void ExtrasMenuState::render()
m_extrasMenu.render(m_renderTarget->get(), hasFocus);
m_renderTarget->render(NULL, 201, 91);
}
void ExtrasMenuState::initialize_menu()
{
for (int i = 0; const char *option = strings::get_by_name(strings::names::EXTRAS_MENU, i); i++)
{
m_extrasMenu.add_option(option);
}
}
void ExtrasMenuState::reinitialize_data()
{
const int popTicks = ui::PopMessageManager::DEFAULT_MESSAGE_TICKS;
const char *popSuccess = strings::get_by_name(strings::names::EXTRAS_POP_MESSAGES, 0);
const char *popFailure = strings::get_by_name(strings::names::EXTRAS_POP_MESSAGES, 1);
// Call data and make in reinit and delete the cache first.
const bool initSuccess = data::initialize(true);
if (!initSuccess)
{
ui::PopMessageManager::push_message(popTicks, popFailure);
return;
}
MainMenuState::refresh_view_states();
ui::PopMessageManager::push_message(popTicks, popSuccess);
}