ServiceMenu: optimitzacions
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include "options.h"
|
||||
#include "section.h" // Para Name, name, Options, options, AttractMode
|
||||
#include "audio.h"
|
||||
#include <unordered_map>
|
||||
|
||||
// Singleton
|
||||
ServiceMenu *ServiceMenu::instance_ = nullptr;
|
||||
@@ -15,13 +16,13 @@ ServiceMenu *ServiceMenu::instance_ = nullptr;
|
||||
// Inicializa la instancia única del singleton
|
||||
void ServiceMenu::init() { ServiceMenu::instance_ = new ServiceMenu(); }
|
||||
|
||||
// Libera la instancia
|
||||
// Libera la instancia única del singleton
|
||||
void ServiceMenu::destroy() { delete ServiceMenu::instance_; }
|
||||
|
||||
// Obtiene la instancia
|
||||
// Devuelve la instancia única del singleton
|
||||
ServiceMenu *ServiceMenu::get() { return ServiceMenu::instance_; }
|
||||
|
||||
// Constructor
|
||||
// Constructor de ServiceMenu
|
||||
ServiceMenu::ServiceMenu()
|
||||
: element_text_(Resource::get()->getText("04b_25_flat")),
|
||||
title_text_(Resource::get()->getText("04b_25_flat_2x")),
|
||||
@@ -33,12 +34,14 @@ ServiceMenu::ServiceMenu()
|
||||
updateMenu(current_settings_group_);
|
||||
}
|
||||
|
||||
// Alterna la visibilidad del menú de servicio
|
||||
void ServiceMenu::toggle()
|
||||
{
|
||||
enabled_ = !enabled_;
|
||||
reset();
|
||||
}
|
||||
|
||||
// Dibuja el menú de servicio en pantalla
|
||||
void ServiceMenu::render()
|
||||
{
|
||||
if (enabled_)
|
||||
@@ -62,7 +65,7 @@ void ServiceMenu::render()
|
||||
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), title_color_.r, title_color_.g, title_color_.b, 255);
|
||||
SDL_RenderRect(Screen::get()->getRenderer(), &rect_);
|
||||
|
||||
// SERVICE MENU
|
||||
// TITULO
|
||||
y += line_height_;
|
||||
title_text_->writeDX(TEXT_COLOR | TEXT_CENTER, param.game.game_area.center_x, y, "SERVICE MENU", -4, title_color_);
|
||||
|
||||
@@ -71,7 +74,7 @@ void ServiceMenu::render()
|
||||
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), title_color_.r, title_color_.g, title_color_.b, 255);
|
||||
SDL_RenderLine(Screen::get()->getRenderer(), rect_.x + OPTIONS_HORIZONTAL_PADDING_, y, rect_.x + rect_.w - OPTIONS_HORIZONTAL_PADDING_, y);
|
||||
|
||||
// LIST
|
||||
// OPCIONES
|
||||
for (size_t i = 0; i < option_pairs_.size(); ++i)
|
||||
{
|
||||
y += line_height_;
|
||||
@@ -99,6 +102,7 @@ void ServiceMenu::render()
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado del menú de servicio (colores, animaciones, etc.)
|
||||
void ServiceMenu::update()
|
||||
{
|
||||
if (enabled_)
|
||||
@@ -108,6 +112,7 @@ void ServiceMenu::update()
|
||||
}
|
||||
}
|
||||
|
||||
// Calcula y establece los anclajes y dimensiones del menú
|
||||
void ServiceMenu::setAnchors()
|
||||
{
|
||||
line_height_ = element_text_->getCharacterSize() + 5;
|
||||
@@ -120,6 +125,7 @@ void ServiceMenu::setAnchors()
|
||||
static_cast<float>(height_)};
|
||||
}
|
||||
|
||||
// Actualiza el contador interno para animaciones o efectos visuales
|
||||
void ServiceMenu::updateCounter()
|
||||
{
|
||||
static Uint64 lastUpdate = SDL_GetTicks();
|
||||
@@ -131,7 +137,8 @@ void ServiceMenu::updateCounter()
|
||||
}
|
||||
}
|
||||
|
||||
Color ServiceMenu::getSelectedColor()
|
||||
// Devuelve el color actual del elemento seleccionado (animado)
|
||||
Color ServiceMenu::getSelectedColor() const
|
||||
{
|
||||
static std::array<Color, 12> colors = {
|
||||
Color(0xFF, 0xFB, 0x8A), // Amarillo suave
|
||||
@@ -153,31 +160,51 @@ Color ServiceMenu::getSelectedColor()
|
||||
return colors.at(index);
|
||||
}
|
||||
|
||||
// Método privado para reproducir el sonido del menú
|
||||
void ServiceMenu::playMenuSound()
|
||||
{
|
||||
Audio::get()->playSound(MENU_SOUND_);
|
||||
}
|
||||
|
||||
// Mueve el selector hacia arriba en la lista de opciones
|
||||
void ServiceMenu::setSelectorUp()
|
||||
{
|
||||
if (display_options_.empty())
|
||||
return;
|
||||
selected_ = (selected_ > 0) ? --selected_ : display_options_.size() - 1;
|
||||
Audio::get()->playSound(MENU_SOUND_);
|
||||
playMenuSound();
|
||||
}
|
||||
|
||||
// Mueve el selector hacia abajo en la lista de opciones
|
||||
void ServiceMenu::setSelectorDown()
|
||||
{
|
||||
if (display_options_.empty())
|
||||
return;
|
||||
selected_ = (selected_ + 1) % display_options_.size();
|
||||
Audio::get()->playSound(MENU_SOUND_);
|
||||
playMenuSound();
|
||||
}
|
||||
|
||||
// Ajusta el valor de la opción seleccionada (si es ajustable)
|
||||
void ServiceMenu::adjustOption(bool adjust_up)
|
||||
{
|
||||
if (display_options_.empty() || selected_ >= display_options_.size())
|
||||
return;
|
||||
|
||||
if (display_options_.at(selected_).behavior == OptionBehavior::ADJUST)
|
||||
{
|
||||
display_options_.at(selected_).adjustValue(adjust_up);
|
||||
option_pairs_ = getOptionPairs(current_settings_group_);
|
||||
applySettings(current_settings_group_);
|
||||
Audio::get()->playSound(MENU_SOUND_);
|
||||
playMenuSound();
|
||||
}
|
||||
}
|
||||
|
||||
// Ejecuta la acción de la opción seleccionada o navega a un submenú
|
||||
void ServiceMenu::selectOption()
|
||||
{
|
||||
if (display_options_.empty() || selected_ >= display_options_.size())
|
||||
return;
|
||||
|
||||
// Carpeta
|
||||
if (display_options_.at(selected_).type == ValueType::FOLDER)
|
||||
{
|
||||
@@ -185,7 +212,7 @@ void ServiceMenu::selectOption()
|
||||
current_settings_group_ = display_options_.at(selected_).target_group;
|
||||
updateMenu(current_settings_group_);
|
||||
selected_ = 0;
|
||||
Audio::get()->playSound(MENU_SOUND_);
|
||||
playMenuSound();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -214,6 +241,7 @@ void ServiceMenu::selectOption()
|
||||
}
|
||||
}
|
||||
|
||||
// Vuelve al grupo de opciones anterior o cierra el menú si está en el principal
|
||||
void ServiceMenu::moveBack()
|
||||
{
|
||||
if (current_settings_group_ == SettingsGroup::MAIN)
|
||||
@@ -226,10 +254,11 @@ void ServiceMenu::moveBack()
|
||||
selected_ = 0;
|
||||
current_settings_group_ = previous_settings_group_;
|
||||
updateMenu(current_settings_group_);
|
||||
Audio::get()->playSound(MENU_SOUND_);
|
||||
playMenuSound();
|
||||
}
|
||||
}
|
||||
|
||||
// Inicializa todas las opciones del menú de servicio
|
||||
void ServiceMenu::initializeOptions()
|
||||
{
|
||||
// Video
|
||||
@@ -261,6 +290,7 @@ void ServiceMenu::initializeOptions()
|
||||
options_.emplace_back("SYSTEM", SettingsGroup::MAIN, OptionBehavior::SELECT, SettingsGroup::SYSTEM);
|
||||
}
|
||||
|
||||
// Devuelve las opciones del grupo como pares (nombre, valor)
|
||||
ServiceMenu::OptionPairs ServiceMenu::getOptionPairs(ServiceMenu::SettingsGroup group) const
|
||||
{
|
||||
OptionPairs optionPairs;
|
||||
@@ -276,6 +306,7 @@ ServiceMenu::OptionPairs ServiceMenu::getOptionPairs(ServiceMenu::SettingsGroup
|
||||
return optionPairs;
|
||||
}
|
||||
|
||||
// Devuelve las opciones del grupo como un vector de OptionEntry
|
||||
std::vector<ServiceMenu::OptionEntry> ServiceMenu::getOptionsByGroup(SettingsGroup group) const
|
||||
{
|
||||
std::vector<OptionEntry> filteredOptions;
|
||||
@@ -291,6 +322,7 @@ std::vector<ServiceMenu::OptionEntry> ServiceMenu::getOptionsByGroup(SettingsGro
|
||||
return filteredOptions;
|
||||
}
|
||||
|
||||
// Aplica la configuración correspondiente al grupo seleccionado
|
||||
void ServiceMenu::applySettings(ServiceMenu::SettingsGroup group)
|
||||
{
|
||||
switch (group)
|
||||
@@ -308,12 +340,14 @@ void ServiceMenu::applySettings(ServiceMenu::SettingsGroup group)
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las opciones mostradas según el grupo seleccionado
|
||||
void ServiceMenu::updateMenu(SettingsGroup group)
|
||||
{
|
||||
option_pairs_ = getOptionPairs(group);
|
||||
display_options_ = getOptionsByGroup(group);
|
||||
}
|
||||
|
||||
// Reinicia el menú al estado inicial (grupo principal y opción seleccionada)
|
||||
void ServiceMenu::reset()
|
||||
{
|
||||
selected_ = 0;
|
||||
@@ -321,30 +355,28 @@ void ServiceMenu::reset()
|
||||
updateMenu(current_settings_group_);
|
||||
}
|
||||
|
||||
int ServiceMenu::calculateMenuHeight()
|
||||
// Calcula la altura total del menú en píxeles
|
||||
int ServiceMenu::calculateMenuHeight() const
|
||||
{
|
||||
return ((4 + findLargestGroupSize() + 1) * line_height_) - 5;
|
||||
}
|
||||
|
||||
int ServiceMenu::findLargestGroupSize()
|
||||
// Devuelve el tamaño (número de opciones) del grupo más grande
|
||||
int ServiceMenu::findLargestGroupSize() const
|
||||
{
|
||||
std::unordered_map<SettingsGroup, int> group_counts;
|
||||
for (const auto &option : options_)
|
||||
++group_counts[option.group];
|
||||
|
||||
int max_size = 0;
|
||||
// Recorremos todos los posibles grupos
|
||||
for (int group = static_cast<int>(SettingsGroup::VIDEO); group <= static_cast<int>(SettingsGroup::MAIN); ++group)
|
||||
{
|
||||
int count = 0;
|
||||
for (const auto &option : options_)
|
||||
{
|
||||
if (static_cast<int>(option.group) == group)
|
||||
++count;
|
||||
}
|
||||
if (count > max_size)
|
||||
max_size = count;
|
||||
}
|
||||
for (const auto &pair : group_counts)
|
||||
if (pair.second > max_size)
|
||||
max_size = pair.second;
|
||||
return max_size;
|
||||
}
|
||||
|
||||
ServiceMenu::GroupAlignment ServiceMenu::getGroupAlignment(SettingsGroup group)
|
||||
// Devuelve la alineación de las opciones para el grupo dado
|
||||
ServiceMenu::GroupAlignment ServiceMenu::getGroupAlignment(SettingsGroup group) const
|
||||
{
|
||||
switch (group)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user