From 0010d64d62f8f15e0c848a632a1c40259af67f09 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 6 Jun 2025 20:59:42 +0200 Subject: [PATCH] =?UTF-8?q?ServiceMenu:=20text=20justificat=20o=20centrat?= =?UTF-8?q?=20segons=20el=20submenu=20ServiceMenu:=20alt=20de=20la=20fines?= =?UTF-8?q?tra=20en=20funci=C3=B3=20del=20submenu=20amb=20mes=20items?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/service_menu.cpp | 68 ++++++++++++++++++++++++++++++++++++----- source/service_menu.h | 12 +++++++- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/source/service_menu.cpp b/source/service_menu.cpp index cb736ac..3f7b616 100644 --- a/source/service_menu.cpp +++ b/source/service_menu.cpp @@ -28,8 +28,8 @@ ServiceMenu::ServiceMenu() current_settings_group_(SettingsGroup::MAIN), previous_settings_group_(current_settings_group_) { - setAnchors(); initializeOptions(); + setAnchors(); updateMenu(current_settings_group_); } @@ -44,7 +44,6 @@ void ServiceMenu::render() if (enabled_) { int y = rect_.y; - constexpr int H_PADDING = 20; // SOMBRA if (aspect_ == Aspect::ASPECT1) @@ -70,15 +69,33 @@ void ServiceMenu::render() // LINEA y += line_height_ * 2; SDL_SetRenderDrawColor(Screen::get()->getRenderer(), title_color_.r, title_color_.g, title_color_.b, 255); - SDL_RenderLine(Screen::get()->getRenderer(), rect_.x + H_PADDING, y, rect_.x + rect_.w - H_PADDING, y); + SDL_RenderLine(Screen::get()->getRenderer(), rect_.x + OPTIONS_HORIZONTAL_PADDING_, y, rect_.x + rect_.w - OPTIONS_HORIZONTAL_PADDING_, y); // LIST for (size_t i = 0; i < option_pairs_.size(); ++i) { y += line_height_; - element_text_->writeColored(rect_.x + H_PADDING, y, option_pairs_.at(i).first, i == selected_ ? selected_color_ : text_color_, -2); - element_text_->writeColored(rect_.x + H_PADDING + 100, y, "" + std::string(option_pairs_.at(i).second), i == selected_ ? selected_color_ : text_color_, -2); + if (getGroupAlignment(current_settings_group_) == GroupAlignment::LEFT) + { + // Nombre de la opción + element_text_->writeColored(rect_.x + OPTIONS_HORIZONTAL_PADDING_, y, option_pairs_.at(i).first, i == selected_ ? selected_color_ : text_color_, -2); + // Valor de la opción + const int X = rect_.x + rect_.w - OPTIONS_HORIZONTAL_PADDING_ - 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 + { + // Nombre de la opción + 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_); + } } + /* + SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 255, 0, 255); + for (int i = rect_.y; i < rect_.y + rect_.h; i += line_height_) + { + SDL_RenderLine(Screen::get()->getRenderer(), rect_.x, i, rect_.x + rect_.w, i); + } + */ } } @@ -93,9 +110,9 @@ void ServiceMenu::update() void ServiceMenu::setAnchors() { - width_ = 220; - height_ = 200; line_height_ = element_text_->getCharacterSize() + 5; + width_ = 220; + height_ = calculateMenuHeight(); rect_ = { (param.game.width - width_) / 2, (param.game.height - height_) / 2, @@ -302,4 +319,41 @@ void ServiceMenu::reset() selected_ = 0; previous_settings_group_ = current_settings_group_ = SettingsGroup::MAIN; updateMenu(current_settings_group_); +} + +int ServiceMenu::calculateMenuHeight() +{ + return ((4 + findLargestGroupSize() + 1) * line_height_) - 5; +} + +int ServiceMenu::findLargestGroupSize() +{ + int max_size = 0; + // Recorremos todos los posibles grupos + for (int group = static_cast(SettingsGroup::VIDEO); group <= static_cast(SettingsGroup::MAIN); ++group) + { + int count = 0; + for (const auto &option : options_) + { + if (static_cast(option.group) == group) + ++count; + } + if (count > max_size) + max_size = count; + } + return max_size; +} + +ServiceMenu::GroupAlignment ServiceMenu::getGroupAlignment(SettingsGroup group) +{ + switch (group) + { + case SettingsGroup::VIDEO: + case SettingsGroup::AUDIO: + case SettingsGroup::GAME: + return GroupAlignment::LEFT; + + default: + return GroupAlignment::CENTERED; + } } \ No newline at end of file diff --git a/source/service_menu.h b/source/service_menu.h index 3122639..774e979 100644 --- a/source/service_menu.h +++ b/source/service_menu.h @@ -35,7 +35,8 @@ public: private: using OptionPairs = std::vector>; - static constexpr const char* MENU_SOUND_ = "clock.wav"; + static constexpr const char *MENU_SOUND_ = "clock.wav"; + static constexpr int OPTIONS_HORIZONTAL_PADDING_ = 20; enum class Aspect { @@ -66,6 +67,12 @@ private: NONE }; + enum class GroupAlignment + { + CENTERED, + LEFT + }; + struct OptionEntry { std::string caption; // Texto visible en el menú @@ -164,6 +171,9 @@ private: void applySettings(SettingsGroup group); void updateMenu(SettingsGroup group); void reset(); + int calculateMenuHeight(); + int findLargestGroupSize(); + GroupAlignment getGroupAlignment(SettingsGroup group); // --- Patrón Singleton --- ServiceMenu(); // Constructor privado