JKSV/source/appstates/TitleSelectState.cpp
2025-09-09 20:47:48 -04:00

125 lines
3.8 KiB
C++

#include "appstates/TitleSelectState.hpp"
#include "StateManager.hpp"
#include "appstates/BackupMenuState.hpp"
#include "appstates/MainMenuState.hpp"
#include "appstates/TitleOptionState.hpp"
#include "config/config.hpp"
#include "graphics/colors.hpp"
#include "input.hpp"
#include "logging/logger.hpp"
#include "sdl.hpp"
#include "strings/strings.hpp"
#include <string_view>
namespace
{
// All of these states share the same render target.
constexpr std::string_view SECONDARY_TARGET = "SecondaryTarget";
} // namespace
TitleSelectState::TitleSelectState(data::User *user)
: TitleSelectCommon()
, m_user(user)
, m_renderTarget(sdl::TextureManager::load(SECONDARY_TARGET, 1080, 555, SDL_TEXTUREACCESS_TARGET))
, m_titleView(ui::TitleView::create(m_user)) {};
void TitleSelectState::update()
{
if (!TitleSelectState::title_count_check()) { return; }
const bool hasFocus = BaseState::has_focus();
const bool aPressed = input::button_pressed(HidNpadButton_A);
const bool bPressed = input::button_pressed(HidNpadButton_B);
const bool xPressed = input::button_pressed(HidNpadButton_X);
const bool yPressed = input::button_pressed(HidNpadButton_Y);
if (aPressed) { TitleSelectState::create_backup_menu(); }
else if (xPressed) { TitleSelectState::create_title_option_menu(); }
else if (yPressed) { TitleSelectState::add_remove_favorite(); }
else if (bPressed)
{
TitleSelectState::deactivate_state();
return;
}
m_titleView->update(hasFocus);
sm_controlGuide->update(hasFocus);
}
void TitleSelectState::render()
{
const bool hasFocus = BaseState::has_focus();
m_renderTarget->clear(colors::TRANSPARENT);
m_titleView->render(m_renderTarget, hasFocus);
sm_controlGuide->render(sdl::Texture::Null, hasFocus);
m_renderTarget->render(sdl::Texture::Null, 201, 91);
}
void TitleSelectState::refresh() { m_titleView->refresh(); }
bool TitleSelectState::title_count_check()
{
const int titleCount = m_user->get_total_data_entries();
if (titleCount <= 0)
{
BaseState::deactivate();
return false;
}
return true;
}
void TitleSelectState::create_backup_menu()
{
const int selected = m_titleView->get_selected();
const uint64_t applicationID = m_user->get_application_id_at(selected);
data::TitleInfo *titleInfo = data::get_title_info_by_id(applicationID);
auto backupMenu = std::make_shared<BackupMenuState>(m_user, titleInfo);
StateManager::push_state(backupMenu);
}
void TitleSelectState::create_title_option_menu()
{
const int selected = m_titleView->get_selected();
const uint64_t applicationID = m_user->get_application_id_at(selected);
data::TitleInfo *titleInfo = data::get_title_info_by_id(applicationID);
auto titleOptions = std::make_shared<TitleOptionState>(m_user, titleInfo, this);
StateManager::push_state(titleOptions);
}
void TitleSelectState::deactivate_state()
{
m_titleView->reset();
BaseState::deactivate();
}
void TitleSelectState::add_remove_favorite()
{
const int selected = m_titleView->get_selected();
const uint64_t applicationID = m_user->get_application_id_at(selected);
config::add_remove_favorite(applicationID);
// This applies to all users.
data::UserList userList;
data::get_users(userList);
for (data::User *user : userList) { user->sort_data(); }
// This is dirty, but it's the only way to pull it off without rewriting tons of code.
const int count = m_user->get_total_data_entries();
int i{};
for (i = 0; i < count; i++)
{
const uint64_t appIDAt = m_user->get_application_id_at(i);
if (appIDAt == applicationID) { break; }
}
m_titleView->set_selected(i);
MainMenuState::refresh_view_states();
}