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),
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<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:
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
{
@@ -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