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;
}
}