diff --git a/include/appstates/MainMenuState.hpp b/include/appstates/MainMenuState.hpp index 5d5ddf4..e8d14a4 100644 --- a/include/appstates/MainMenuState.hpp +++ b/include/appstates/MainMenuState.hpp @@ -50,6 +50,9 @@ class MainMenuState final : public BaseState /// @brief X coordinate of the control guide in the bottom right corner. int m_controlGuideX{}; + /// @brief Records the size of the sm_users vector. + static inline int sm_userCount{}; + /// @brief This is the list of user pointers from data. static inline data::UserList sm_users{}; @@ -61,4 +64,16 @@ class MainMenuState final : public BaseState /// @brief This is the vector of title selection states. static inline std::vector> sm_states{}; + + /// @brief Creates the settings and extras. + void initialize_settings_extras(); + + /// @brief Pushes the icons to the main menu. + void initialize_menu(); + + /// @brief Pushes the target state to the vector. + void push_target_state(); + + /// @brief Creates the user option state. + void create_user_options(); }; diff --git a/source/appstates/MainMenuState.cpp b/source/appstates/MainMenuState.cpp index 3c47110..795840b 100644 --- a/source/appstates/MainMenuState.cpp +++ b/source/appstates/MainMenuState.cpp @@ -1,4 +1,5 @@ #include "appstates/MainMenuState.hpp" + #include "StateManager.hpp" #include "appstates/ExtrasMenuState.hpp" #include "appstates/SettingsState.hpp" @@ -17,105 +18,63 @@ MainMenuState::MainMenuState() : m_renderTarget(sdl::TextureManager::create_load_texture("mainMenuTarget", 200, 555, - SDL_TEXTUREACCESS_STATIC | SDL_TEXTUREACCESS_TARGET)), - m_background(sdl::TextureManager::create_load_texture("mainBackground", "romfs:/Textures/MenuBackground.png")), - m_settingsIcon(sdl::TextureManager::create_load_texture("settingsIcon", "romfs:/Textures/SettingsIcon.png")), - m_extrasIcon(sdl::TextureManager::create_load_texture("extrasIcon", "romfs:/Textures/ExtrasIcon.png")), - m_mainMenu(50, 15, 555), m_controlGuide(strings::get_by_name(strings::names::CONTROL_GUIDES, 0)), - m_controlGuideX(1220 - sdl::text::get_width(22, m_controlGuide)) + SDL_TEXTUREACCESS_STATIC | SDL_TEXTUREACCESS_TARGET)) + , m_background(sdl::TextureManager::create_load_texture("mainBackground", "romfs:/Textures/MenuBackground.png")) + , m_settingsIcon(sdl::TextureManager::create_load_texture("settingsIcon", "romfs:/Textures/SettingsIcon.png")) + , m_extrasIcon(sdl::TextureManager::create_load_texture("extrasIcon", "romfs:/Textures/ExtrasIcon.png")) + , m_mainMenu(50, 15, 555) + , m_controlGuide(strings::get_by_name(strings::names::CONTROL_GUIDES, 0)) + , m_controlGuideX(1220 - sdl::text::get_width(22, m_controlGuide)) { - if (!sm_settingsState || !sm_extrasState) - { - sm_settingsState = std::make_shared(); - sm_extrasState = std::make_shared(); - } - - // Grab the users and setup the main menu. Users shouldn't change. - data::get_users(sm_users); - - for (data::User *user : sm_users) - { - m_mainMenu.add_option(user->get_shared_icon()); - } - - // Add the last two. - m_mainMenu.add_option(m_settingsIcon); - m_mainMenu.add_option(m_extrasIcon); - - // Just call this. + MainMenuState::initialize_settings_extras(); + MainMenuState::initialize_menu(); MainMenuState::initialize_view_states(); } void MainMenuState::update() { + const int selected = m_mainMenu.get_selected(); const bool hasFocus = BaseState::has_focus(); + const bool aPressed = input::button_pressed(HidNpadButton_A); + const bool xPressed = input::button_pressed(HidNpadButton_X); + + const bool toUserOptions = xPressed && selected < sm_userCount; + + if (aPressed) { MainMenuState::push_target_state(); } + else if (toUserOptions) { MainMenuState::create_user_options(); }; - // Update the main menu. m_mainMenu.update(hasFocus); - - int selected = m_mainMenu.get_selected(); - - // To do: Simplify this logic. - if (input::button_pressed(HidNpadButton_A) && selected < static_cast(sm_users.size()) && - sm_users.at(selected)->get_total_data_entries() > 0) - { - sm_states.at(selected)->reactivate(); - StateManager::push_state(sm_states.at(selected)); - } - else if (input::button_pressed(HidNpadButton_A) && selected >= static_cast(sm_users.size())) - { - sm_states.at(selected)->reactivate(); - StateManager::push_state(sm_states.at(selected)); - } - else if (input::button_pressed(HidNpadButton_X) && selected < static_cast(sm_users.size())) - { - // Get pointers to data the user option state needs. - data::User *targetUser = sm_users.at(selected); - TitleSelectCommon *targetTitleSelect = static_cast(sm_states.at(selected).get()); - - StateManager::push_state(std::make_shared(targetUser, targetTitleSelect)); - } } void MainMenuState::render() { const bool hasFocus = BaseState::has_focus(); + const int selected = m_mainMenu.get_selected(); - // Clear render target by rendering background to it. m_background->render(m_renderTarget->get(), 0, 0); - // render menu. m_mainMenu.render(m_renderTarget->get(), hasFocus); - // render target to screen. m_renderTarget->render(NULL, 0, 91); - // render next state for current user and control guide if this state has focus. To do: Maybe this different? if (hasFocus) { - sm_states.at(m_mainMenu.get_selected())->render(); + BaseState *target = sm_states.at(selected).get(); + target->render(); + sdl::text::render(NULL, m_controlGuideX, 673, 22, sdl::text::NO_TEXT_WRAP, colors::WHITE, m_controlGuide); } } void MainMenuState::initialize_view_states() { - // Constructor should have taken care of the menu and user list. - // Start by clearing the vector. + const bool jksmMode = config::get_by_key(config::keys::JKSM_TEXT_MODE); + sm_states.clear(); - - // Grab this from config instead of calling it every loop. - bool textMode = config::get_by_key(config::keys::JKSM_TEXT_MODE); - - // Loop through users. for (data::User *user : sm_users) { - if (textMode) - { - sm_states.push_back(std::make_shared(user)); - } - else - { - sm_states.push_back(std::make_shared(user)); - } + std::shared_ptr state{}; + if (jksmMode) { state = std::make_shared(user); } + else { state = std::make_shared(user); } + sm_states.push_back(state); } sm_states.push_back(sm_settingsState); sm_states.push_back(sm_extrasState); @@ -123,10 +82,46 @@ void MainMenuState::initialize_view_states() void MainMenuState::refresh_view_states() { - // For this, we're only looping through the user states to be extra careful because the last two don't have the refresh() function. - int userCount = sm_users.size(); - for (int i = 0; i < userCount; i++) + for (int i = 0; i < sm_userCount; i++) { - static_cast(sm_states.at(i).get())->refresh(); + TitleSelectCommon *target = static_cast(sm_states.at(i).get()); + target->refresh(); } } + +void MainMenuState::initialize_settings_extras() +{ + if (!sm_settingsState || !sm_extrasState) + { + sm_settingsState = std::make_shared(); + sm_extrasState = std::make_shared(); + } +} + +void MainMenuState::initialize_menu() +{ + data::get_users(sm_users); + sm_userCount = sm_users.size(); + for (data::User *user : sm_users) { m_mainMenu.add_option(user->get_shared_icon()); } + m_mainMenu.add_option(m_settingsIcon); + m_mainMenu.add_option(m_extrasIcon); +} + +void MainMenuState::push_target_state() +{ + const int selected = m_mainMenu.get_selected(); + auto &target = sm_states.at(selected); + + target->reactivate(); + StateManager::push_state(target); +} + +void MainMenuState::create_user_options() +{ + const int selected = m_mainMenu.get_selected(); + data::User *user = sm_users.at(selected); + TitleSelectCommon *titleSelect = static_cast(sm_states.at(selected).get()); + + auto userOptions = std::make_shared(user, titleSelect); + StateManager::push_state(userOptions); +}