ServiceMenu: text justificat o centrat segons el submenu

ServiceMenu: alt de la finestra en funció del submenu amb mes items
This commit is contained in:
2025-06-06 20:59:42 +02:00
parent a76740f0b7
commit 0010d64d62
2 changed files with 72 additions and 8 deletions

View File

@@ -28,8 +28,8 @@ ServiceMenu::ServiceMenu()
current_settings_group_(SettingsGroup::MAIN), current_settings_group_(SettingsGroup::MAIN),
previous_settings_group_(current_settings_group_) previous_settings_group_(current_settings_group_)
{ {
setAnchors();
initializeOptions(); initializeOptions();
setAnchors();
updateMenu(current_settings_group_); updateMenu(current_settings_group_);
} }
@@ -44,7 +44,6 @@ void ServiceMenu::render()
if (enabled_) if (enabled_)
{ {
int y = rect_.y; int y = rect_.y;
constexpr int H_PADDING = 20;
// SOMBRA // SOMBRA
if (aspect_ == Aspect::ASPECT1) if (aspect_ == Aspect::ASPECT1)
@@ -70,15 +69,33 @@ void ServiceMenu::render()
// LINEA // LINEA
y += line_height_ * 2; y += line_height_ * 2;
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), title_color_.r, title_color_.g, title_color_.b, 255); 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 // LIST
for (size_t i = 0; i < option_pairs_.size(); ++i) for (size_t i = 0; i < option_pairs_.size(); ++i)
{ {
y += line_height_; y += line_height_;
element_text_->writeColored(rect_.x + H_PADDING, y, option_pairs_.at(i).first, i == selected_ ? selected_color_ : text_color_, -2); if (getGroupAlignment(current_settings_group_) == GroupAlignment::LEFT)
element_text_->writeColored(rect_.x + H_PADDING + 100, y, "" + std::string(option_pairs_.at(i).second), i == selected_ ? selected_color_ : text_color_, -2); {
// 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() void ServiceMenu::setAnchors()
{ {
width_ = 220;
height_ = 200;
line_height_ = element_text_->getCharacterSize() + 5; line_height_ = element_text_->getCharacterSize() + 5;
width_ = 220;
height_ = calculateMenuHeight();
rect_ = { rect_ = {
(param.game.width - width_) / 2, (param.game.width - width_) / 2,
(param.game.height - height_) / 2, (param.game.height - height_) / 2,
@@ -302,4 +319,41 @@ void ServiceMenu::reset()
selected_ = 0; selected_ = 0;
previous_settings_group_ = current_settings_group_ = SettingsGroup::MAIN; previous_settings_group_ = current_settings_group_ = SettingsGroup::MAIN;
updateMenu(current_settings_group_); 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<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;
}
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;
}
} }

View File

@@ -35,7 +35,8 @@ public:
private: private:
using OptionPairs = std::vector<std::pair<std::string, std::string>>; using OptionPairs = std::vector<std::pair<std::string, std::string>>;
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 enum class Aspect
{ {
@@ -66,6 +67,12 @@ private:
NONE NONE
}; };
enum class GroupAlignment
{
CENTERED,
LEFT
};
struct OptionEntry struct OptionEntry
{ {
std::string caption; // Texto visible en el menú std::string caption; // Texto visible en el menú
@@ -164,6 +171,9 @@ private:
void applySettings(SettingsGroup group); void applySettings(SettingsGroup group);
void updateMenu(SettingsGroup group); void updateMenu(SettingsGroup group);
void reset(); void reset();
int calculateMenuHeight();
int findLargestGroupSize();
GroupAlignment getGroupAlignment(SettingsGroup group);
// --- Patrón Singleton --- // --- Patrón Singleton ---
ServiceMenu(); // Constructor privado ServiceMenu(); // Constructor privado