#include "service_menu.h" #include "MenuOption.h" // <-- Incluimos la nueva cabecera #include "screen.h" #include #include #include "text.h" #include "resource.h" #include "section.h" #include "audio.h" #include // ... (métodos singleton y constructores no cambian mucho) ... ServiceMenu *ServiceMenu::instance_ = nullptr; void ServiceMenu::init() { ServiceMenu::instance_ = new ServiceMenu(); } void ServiceMenu::destroy() { delete ServiceMenu::instance_; } ServiceMenu *ServiceMenu::get() { return ServiceMenu::instance_; } ServiceMenu::ServiceMenu() : element_text_(Resource::get()->getText("04b_25_flat")), title_text_(Resource::get()->getText("04b_25_flat_2x")), current_settings_group_(SettingsGroup::MAIN), previous_settings_group_(current_settings_group_) { restart_message_ui_ = std::make_unique( element_text_, Lang::getText("[SERVICE_MENU] NEED_RESTART_MESSAGE"), title_color_); reset(); } void ServiceMenu::initializeOptions() { options_.clear(); // --- Video --- options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] FULLSCREEN"), SettingsGroup::VIDEO, &Options::video.fullscreen)); options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] WINDOW_SIZE"), SettingsGroup::VIDEO, &Options::window.size, 1, Options::window.max_size, 1)); options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] SHADERS"), SettingsGroup::VIDEO, &Options::video.shaders)); options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] VSYNC"), SettingsGroup::VIDEO, &Options::video.v_sync)); options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] INTEGER_SCALE"), SettingsGroup::VIDEO, &Options::video.integer_scale)); // --- Audio --- options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] AUDIO"), SettingsGroup::AUDIO, &Options::audio.enabled)); options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] MAIN_VOLUME"), SettingsGroup::AUDIO, &Options::audio.volume, 0, 100, 5)); options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] MUSIC_VOLUME"), SettingsGroup::AUDIO, &Options::audio.music.volume, 0, 100, 5)); options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] SFX_VOLUME"), SettingsGroup::AUDIO, &Options::audio.sound.volume, 0, 100, 5)); // --- Settings --- options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] AUTOFIRE"), SettingsGroup::SETTINGS, &Options::settings.autofire)); // Opción de Idioma options_.push_back(std::make_unique( Lang::getText("[SERVICE_MENU] LANGUAGE"), SettingsGroup::SETTINGS, std::vector{Lang::getText("[SERVICE_MENU] LANG_ES"), Lang::getText("[SERVICE_MENU] LANG_BA"), Lang::getText("[SERVICE_MENU] LANG_EN")}, // Getter: Devuelve el nombre del idioma actual []() { return Lang::getNameFromCode(Options::pending_changes.new_language); }, // Setter: Establece el nuevo idioma a partir de su nombre [](const std::string &val) { Options::pending_changes.new_language = Lang::getCodeFromName(val); Options::checkPendingChanges(); })); // Opción de Dificultad options_.push_back(std::make_unique( Lang::getText("[SERVICE_MENU] DIFFICULTY"), SettingsGroup::SETTINGS, std::vector{Lang::getText("[SERVICE_MENU] EASY"), Lang::getText("[SERVICE_MENU] NORMAL"), Lang::getText("[SERVICE_MENU] HARD")}, // Getter: Devuelve el nombre de la dificultad actual []() { return Options::getDifficultyNameFromCode(Options::pending_changes.new_difficulty); }, // Setter: Establece la nueva dificultad a partir de su nombre [](const std::string &val) { Options::pending_changes.new_difficulty = Options::getDifficultyCodeFromName(val); Options::checkPendingChanges(); })); options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] ENABLE_SHUTDOWN"), SettingsGroup::SETTINGS, &Options::settings.shutdown_enabled)); // --- System (con lambdas para las acciones) --- options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] RESET"), SettingsGroup::SYSTEM, [this]() { Section::name = Section::Name::RESET; toggle(); })); options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] QUIT"), SettingsGroup::SYSTEM, []() { Section::name = Section::Name::QUIT; Section::options = Section::Options::NONE; })); options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] SHUTDOWN"), SettingsGroup::SYSTEM, []() { Section::name = Section::Name::QUIT; Section::options = Section::Options::SHUTDOWN; }, !Options::settings.shutdown_enabled)); // Se puede inicializar oculto // --- Menu principal (Carpetas) --- options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] VIDEO"), SettingsGroup::MAIN, SettingsGroup::VIDEO)); options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] AUDIO"), SettingsGroup::MAIN, SettingsGroup::AUDIO)); options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] SETTINGS"), SettingsGroup::MAIN, SettingsGroup::SETTINGS)); options_.push_back(std::make_unique(Lang::getText("[SERVICE_MENU] SYSTEM"), SettingsGroup::MAIN, SettingsGroup::SYSTEM)); precalculateMenuWidths(); } void ServiceMenu::adjustOption(bool adjust_up) { if (display_options_.empty() || selected_ >= display_options_.size()) return; auto &selected_option = display_options_.at(selected_); if (selected_option->getBehavior() == MenuOption::Behavior::ADJUST) { selected_option->adjustValue(adjust_up); // <-- ¡Llamada polimórfica! option_pairs_ = getOptionPairs(current_settings_group_); applySettings(current_settings_group_); playMenuSound(); } } void ServiceMenu::selectOption() { if (display_options_.empty() || selected_ >= display_options_.size()) return; if (current_settings_group_ == SettingsGroup::MAIN) main_menu_selected_ = selected_; auto &selected_option = display_options_.at(selected_); // Si es una carpeta, cambiamos de grupo if (auto folder = dynamic_cast(selected_option)) { previous_settings_group_ = current_settings_group_; current_settings_group_ = folder->getTargetGroup(); title_ = settingsGroupToString(current_settings_group_); updateMenu(current_settings_group_); selected_ = 0; setOptionsPosition(); playMenuSound(); return; } // Si es una acción, la ejecutamos if (selected_option->getBehavior() == MenuOption::Behavior::SELECT) { selected_option->executeAction(); // <-- ¡Llamada polimórfica! } } // --- Métodos de utilidad modificados --- ServiceMenu::OptionPairs ServiceMenu::getOptionPairs(SettingsGroup group) const { OptionPairs option_pairs; for (const auto &option : options_) { if (option->getGroup() == group && !option->isHidden()) { option_pairs.emplace_back(option->getCaption(), option->getValueAsString()); } } return option_pairs; } std::vector ServiceMenu::getOptionsByGroup(SettingsGroup group) { std::vector filtered_options; for (auto &option : options_) { if (option->getGroup() == group && !option->isHidden()) { filtered_options.push_back(option.get()); // Obtenemos el puntero raw } } return filtered_options; } void ServiceMenu::applySettings(SettingsGroup group) { // La aplicación de settings de Video y Audio se puede llamar tras un cambio // para que sea instantáneo, aunque la lógica principal ya está en cada `adjustValue`. if (group == SettingsGroup::VIDEO) Screen::get()->applySettings(); if (group == SettingsGroup::AUDIO) Audio::get()->applySettings(); // La lógica de UI, como ocultar/mostrar opciones, sí va aquí. if (group == SettingsGroup::SETTINGS) { auto option = getOptionByCaption(Lang::getText("[SERVICE_MENU] SHUTDOWN")); if (option) { option->setHidden(!Options::settings.shutdown_enabled); // Si ocultamos/mostramos, necesitamos refrescar la lista de opciones visibles. display_options_ = getOptionsByGroup(group); } } } void ServiceMenu::AdjustListValues() { for (auto &option : options_) { // dynamic_cast es una forma segura de ver si un objeto es de un tipo derivado if (auto list_option = dynamic_cast(option.get())) { list_option->sync(); } } } MenuOption *ServiceMenu::getOptionByCaption(const std::string &caption) { for (auto &option : options_) { if (option->getCaption() == caption) { return option.get(); } } return nullptr; } // El resto de los métodos (render, update, moveBack, etc.) permanecen prácticamente igual, // ya que su lógica no dependía tanto de la estructura interna de OptionEntry. // Aquí se incluyen para que el archivo esté completo. void ServiceMenu::toggle() { enabled_ = !enabled_; if (!enabled_) { reset(); } } void ServiceMenu::render() { if (enabled_) { int y = rect_.y; if (aspect_ == Aspect::ASPECT1) { SDL_FRect shadowRect = {rect_.x + 5, rect_.y + 5, rect_.w, rect_.h}; SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 64); SDL_RenderFillRect(Screen::get()->getRenderer(), &shadowRect); } const Uint8 ALPHA = aspect_ == Aspect::ASPECT1 ? 255 : 255; SDL_SetRenderDrawColor(Screen::get()->getRenderer(), bg_color_.r, bg_color_.g, bg_color_.b, ALPHA); SDL_RenderFillRect(Screen::get()->getRenderer(), &rect_); SDL_SetRenderDrawColor(Screen::get()->getRenderer(), title_color_.r, title_color_.g, title_color_.b, 255); SDL_RenderRect(Screen::get()->getRenderer(), &rect_); restart_message_ui_->render(); y += title_padding_; title_text_->writeDX(TEXT_COLOR | TEXT_CENTER, param.game.game_area.center_x, y, title_, -4, title_color_); y = rect_.y + upper_height_; SDL_SetRenderDrawColor(Screen::get()->getRenderer(), title_color_.r, title_color_.g, title_color_.b, 255); SDL_RenderLine(Screen::get()->getRenderer(), rect_.x + 20, y, rect_.x + rect_.w - 20, y); y = options_y_; for (size_t i = 0; i < option_pairs_.size(); ++i) { if (getGroupAlignment(current_settings_group_) == GroupAlignment::LEFT) { element_text_->writeColored(rect_.x + 20, y, option_pairs_.at(i).first, i == selected_ ? selected_color_ : text_color_, -2); const int X = rect_.x + rect_.w - 20 - element_text_->lenght(std::string(option_pairs_.at(i).second), -2); element_text_->writeColored(X, y, std::string(option_pairs_.at(i).second), i == selected_ ? selected_color_ : text_color_, -2); } else { element_text_->writeDX(TEXT_CENTER | TEXT_COLOR, rect_.x + rect_.w / 2, y, option_pairs_.at(i).first, -2, i == selected_ ? selected_color_ : text_color_); } y += options_height_ + options_padding_; } } } void ServiceMenu::update() { if (resizing_) { updateResizeAnimation(); return; } if (enabled_) { updateCounter(); selected_color_ = getSelectedColor(); } bool now_pending = Options::pending_changes.has_pending_changes; if (now_pending != last_pending_changes_) { if (now_pending) { float msg_x = param.game.game_area.center_x; float msg_y = rect_.y + 39.0f; restart_message_ui_->show(msg_x, msg_y); } else { restart_message_ui_->hide(); } last_pending_changes_ = now_pending; } restart_message_ui_->update(); } void ServiceMenu::reset() { selected_ = 0; previous_settings_group_ = current_settings_group_ = SettingsGroup::MAIN; title_ = settingsGroupToString(current_settings_group_); initializeOptions(); updateMenu(current_settings_group_); setAnchors(); } void ServiceMenu::setSelectorUp() { if (display_options_.empty()) return; selected_ = (selected_ > 0) ? selected_ - 1 : display_options_.size() - 1; playMenuSound(); } void ServiceMenu::setSelectorDown() { if (display_options_.empty()) return; selected_ = (selected_ + 1) % display_options_.size(); playMenuSound(); } void ServiceMenu::moveBack() { if (current_settings_group_ == SettingsGroup::MAIN) { enabled_ = false; return; } else { if (previous_settings_group_ == SettingsGroup::MAIN) selected_ = main_menu_selected_; else selected_ = 0; current_settings_group_ = previous_settings_group_; updateMenu(current_settings_group_); setOptionsPosition(); playMenuSound(); } } void ServiceMenu::setAnchors() { const size_t MAX_ENTRIES = findLargestGroupSize(); options_height_ = element_text_->getCharacterSize(); options_padding_ = 5; title_height_ = title_text_->getCharacterSize(); title_padding_ = title_height_ / 2; upper_height_ = (title_padding_ * 2) + title_height_; lower_padding_ = (options_padding_ * 3); lower_height_ = ((MAX_ENTRIES - 1) * (options_height_ + options_padding_)) + options_height_ + (lower_padding_ * 2); width_ = 240; height_ = upper_height_ + lower_height_; rect_ = {(param.game.width - width_) / 2.0f, (param.game.height - height_) / 2.0f, static_cast(width_), static_cast(height_)}; setOptionsPosition(); } void ServiceMenu::setOptionsPosition() { resize(); SDL_FRect new_rect = {(param.game.width - width_) / 2.0f, (param.game.height - height_) / 2.0f, static_cast(width_), static_cast(height_)}; options_y_ = new_rect.y + upper_height_ + lower_padding_; } void ServiceMenu::resize() { int menu_width = getMenuWidthForGroup(current_settings_group_); width_ = menu_width; lower_height_ = ((display_options_.size() - 1) * (options_height_ + options_padding_)) + options_height_ + (lower_padding_ * 2); height_ = upper_height_ + lower_height_; SDL_FRect new_rect = {(param.game.width - width_) / 2.0f, (param.game.height - height_) / 2.0f, static_cast(width_), static_cast(height_)}; if (rect_.x != new_rect.x || rect_.y != new_rect.y || rect_.w != new_rect.w || rect_.h != new_rect.h) { rect_anim_from_ = rect_; rect_anim_to_ = new_rect; resize_anim_step_ = 0; resizing_ = true; } else { rect_ = new_rect; resizing_ = false; } } void ServiceMenu::updateResizeAnimation() { if (!resizing_) return; ++resize_anim_step_; float t = static_cast(resize_anim_step_) / resize_anim_steps_; if (t >= 1.0f) { rect_ = rect_anim_to_; resizing_ = false; return; } float ease = 1 - (1 - t) * (1 - t); rect_.x = rect_anim_from_.x + (rect_anim_to_.x - rect_anim_from_.x) * ease; rect_.y = rect_anim_from_.y + (rect_anim_to_.y - rect_anim_from_.y) * ease; rect_.w = rect_anim_from_.w + (rect_anim_to_.w - rect_anim_from_.w) * ease; rect_.h = rect_anim_from_.h + (rect_anim_to_.h - rect_anim_from_.h) * ease; } void ServiceMenu::updateCounter() { static Uint64 lastUpdate = SDL_GetTicks(); Uint64 currentTicks = SDL_GetTicks(); if (currentTicks - lastUpdate >= 50) { counter_++; lastUpdate = currentTicks; } } Color ServiceMenu::getSelectedColor() const { static std::array colors = {Color(0xFF, 0xFB, 0x8A), Color(0xFF, 0xE4, 0x5D), Color(0xFF, 0xD1, 0x3C), Color(0xFF, 0xBF, 0x23), Color(0xFF, 0xAA, 0x12), Color(0xE6, 0x9A, 0x08), Color(0xE6, 0x9A, 0x08), Color(0xFF, 0xAA, 0x12), Color(0xFF, 0xBF, 0x23), Color(0xFF, 0xD1, 0x3C), Color(0xFF, 0xE4, 0x5D), Color(0xFF, 0xFB, 0x8A)}; return colors.at(counter_ % colors.size()); } void ServiceMenu::playMenuSound() { Audio::get()->playSound("clock.wav"); } void ServiceMenu::updateMenu(SettingsGroup group) { title_ = settingsGroupToString(group); AdjustListValues(); option_pairs_ = getOptionPairs(group); display_options_ = getOptionsByGroup(group); resize(); } int ServiceMenu::findLargestGroupSize() const { std::unordered_map group_counts; for (const auto &option : options_) ++group_counts[option->getGroup()]; int max_size = 0; for (const auto &pair : group_counts) if (pair.second > max_size) max_size = pair.second; return max_size; } ServiceMenu::GroupAlignment ServiceMenu::getGroupAlignment(SettingsGroup group) const { switch (group) { case SettingsGroup::VIDEO: case SettingsGroup::AUDIO: case SettingsGroup::SETTINGS: return GroupAlignment::LEFT; default: return GroupAlignment::CENTERED; } } std::string ServiceMenu::settingsGroupToString(SettingsGroup group) const { switch (group) { case SettingsGroup::MAIN: return Lang::getText("[SERVICE_MENU] TITLE"); case SettingsGroup::VIDEO: return Lang::getText("[SERVICE_MENU] VIDEO"); case SettingsGroup::AUDIO: return Lang::getText("[SERVICE_MENU] AUDIO"); case SettingsGroup::SETTINGS: return Lang::getText("[SERVICE_MENU] SETTINGS"); case SettingsGroup::SYSTEM: return Lang::getText("[SERVICE_MENU] SYSTEM"); default: return Lang::getText("[SERVICE_MENU] TITLE"); } }