Afegides traduccions per a elements nous de service menu
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "menu_renderer.h"
|
||||
|
||||
#include <algorithm> // Para max
|
||||
#include <algorithm> // Para max, min
|
||||
#include <utility> // Para pair, move
|
||||
|
||||
#include "color.h" // Para Color, generateMirroredCycle, ColorCycleStyle
|
||||
@@ -8,10 +8,12 @@
|
||||
#include "param.h" // Para Param, param, ParamServiceMenu, ParamGame
|
||||
#include "screen.h" // Para Screen
|
||||
#include "text.h" // Para Text, TEXT_CENTER, TEXT_COLOR
|
||||
#include "utils.h" // Para Zone
|
||||
#include "utils.h" // Para Zone, truncateWithEllipsis
|
||||
|
||||
MenuRenderer::MenuRenderer(const ServiceMenu *menu_state, std::shared_ptr<Text> element_text, std::shared_ptr<Text> title_text)
|
||||
: element_text_(std::move(element_text)), title_text_(std::move(title_text)) {}
|
||||
: element_text_(std::move(element_text)), title_text_(std::move(title_text)) {
|
||||
initializeMaxSizes();
|
||||
}
|
||||
|
||||
void MenuRenderer::render(const ServiceMenu *menu_state) {
|
||||
// Dibuja la sombra
|
||||
@@ -48,11 +50,22 @@ void MenuRenderer::render(const ServiceMenu *menu_state) {
|
||||
const Color ¤t_color = IS_SELECTED ? param.service_menu.selected_color : param.service_menu.text_color;
|
||||
|
||||
if (menu_state->getCurrentGroupAlignment() == ServiceMenu::GroupAlignment::LEFT) {
|
||||
// Para opciones alineadas a la izquierda, truncamos el valor si es necesario
|
||||
const int available_width = rect_.w - (ServiceMenu::OPTIONS_HORIZONTAL_PADDING * 2) -
|
||||
element_text_->lenght(option_pairs.at(i).first, -2) -
|
||||
ServiceMenu::MIN_GAP_OPTION_VALUE;
|
||||
|
||||
const std::string truncated_value = getTruncatedValue(option_pairs.at(i).second, available_width);
|
||||
|
||||
element_text_->writeColored(rect_.x + ServiceMenu::OPTIONS_HORIZONTAL_PADDING, y, option_pairs.at(i).first, current_color, -2);
|
||||
const int X = rect_.x + rect_.w - ServiceMenu::OPTIONS_HORIZONTAL_PADDING - element_text_->lenght(option_pairs.at(i).second, -2);
|
||||
element_text_->writeColored(X, y, option_pairs.at(i).second, current_color, -2);
|
||||
const int X = rect_.x + rect_.w - ServiceMenu::OPTIONS_HORIZONTAL_PADDING - element_text_->lenght(truncated_value, -2);
|
||||
element_text_->writeColored(X, y, truncated_value, current_color, -2);
|
||||
} else {
|
||||
element_text_->writeDX(TEXT_CENTER | TEXT_COLOR, rect_.x + rect_.w / 2, y, option_pairs.at(i).first, -2, current_color);
|
||||
// Para opciones centradas, también truncamos si es necesario
|
||||
const int available_width = rect_.w - (ServiceMenu::OPTIONS_HORIZONTAL_PADDING * 2);
|
||||
const std::string truncated_caption = getTruncatedValue(option_pairs.at(i).first, available_width);
|
||||
|
||||
element_text_->writeDX(TEXT_CENTER | TEXT_COLOR, rect_.x + rect_.w / 2, y, truncated_caption, -2, current_color);
|
||||
}
|
||||
y += options_height_ + options_padding_;
|
||||
}
|
||||
@@ -80,6 +93,13 @@ void MenuRenderer::setLayout(const ServiceMenu *menu_state) {
|
||||
setSize(menu_state);
|
||||
}
|
||||
|
||||
void MenuRenderer::initializeMaxSizes() {
|
||||
// Establecemos los límites máximos basados en el tamaño de la pantalla
|
||||
// Dejamos un margen del 10% en cada lado para que el menú no ocupe toda la pantalla
|
||||
max_menu_width_ = static_cast<size_t>(param.game.game_area.rect.w * 0.9f);
|
||||
max_menu_height_ = static_cast<size_t>(param.game.game_area.rect.h * 0.9f);
|
||||
}
|
||||
|
||||
void MenuRenderer::setAnchors(const ServiceMenu *menu_state) {
|
||||
size_t max_entries = 0;
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
@@ -102,10 +122,11 @@ void MenuRenderer::setAnchors(const ServiceMenu *menu_state) {
|
||||
}
|
||||
|
||||
auto MenuRenderer::calculateNewRect(const ServiceMenu *menu_state) -> SDL_FRect {
|
||||
width_ = getMenuWidthForGroup(menu_state->getCurrentGroup());
|
||||
width_ = std::min(static_cast<size_t>(getMenuWidthForGroup(menu_state->getCurrentGroup())), max_menu_width_);
|
||||
|
||||
const auto &display_options = menu_state->getDisplayOptions();
|
||||
lower_height_ = ((!display_options.empty() ? display_options.size() - 1 : 0) * (options_height_ + options_padding_)) + options_height_ + (lower_padding_ * 2);
|
||||
height_ = upper_height_ + lower_height_;
|
||||
height_ = std::min(upper_height_ + lower_height_, max_menu_height_);
|
||||
|
||||
return {(param.game.width - width_) / 2.0F, (param.game.height - height_) / 2.0F, (float)width_, (float)height_};
|
||||
}
|
||||
@@ -165,14 +186,21 @@ void MenuRenderer::precalculateMenuWidths(const std::vector<std::unique_ptr<Menu
|
||||
}
|
||||
max_option_width = std::max(max_option_width, element_text_->lenght(option->getCaption(), -2));
|
||||
if (menu_state->getCurrentGroupAlignment() == ServiceMenu::GroupAlignment::LEFT) {
|
||||
max_value_width = std::max(max_value_width, option->getMaxValueWidth(element_text_.get()));
|
||||
// Para calcular el ancho máximo, necesitamos considerar la truncación
|
||||
int max_available_value_width = static_cast<int>(max_menu_width_) - max_option_width -
|
||||
(ServiceMenu::OPTIONS_HORIZONTAL_PADDING * 2) -
|
||||
ServiceMenu::MIN_GAP_OPTION_VALUE;
|
||||
|
||||
int actual_value_width = getTruncatedValueWidth(option->getValueAsString(), max_available_value_width);
|
||||
max_value_width = std::max(max_value_width, actual_value_width);
|
||||
}
|
||||
}
|
||||
size_t total_width = max_option_width + (ServiceMenu::OPTIONS_HORIZONTAL_PADDING * 2);
|
||||
if (menu_state->getCurrentGroupAlignment() == ServiceMenu::GroupAlignment::LEFT) {
|
||||
total_width += ServiceMenu::MIN_GAP_OPTION_VALUE + max_value_width;
|
||||
}
|
||||
group_menu_widths_[group] = std::max((int)ServiceMenu::MIN_WIDTH, (int)total_width);
|
||||
group_menu_widths_[group] = std::min(std::max((int)ServiceMenu::MIN_WIDTH, (int)total_width),
|
||||
static_cast<int>(max_menu_width_));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,4 +225,56 @@ auto MenuRenderer::getAnimatedSelectedColor() const -> Color {
|
||||
auto MenuRenderer::setRect(SDL_FRect rect) -> SDL_FRect {
|
||||
border_rect_ = {rect.x - 1, rect.y + 1, rect.w + 2, rect.h - 2};
|
||||
return rect;
|
||||
}
|
||||
|
||||
auto MenuRenderer::getTruncatedValueWidth(const std::string &value, int available_width) const -> int {
|
||||
int value_width = element_text_->lenght(value, -2);
|
||||
if (value_width <= available_width) {
|
||||
return value_width;
|
||||
}
|
||||
|
||||
// Calculamos cuántos caracteres podemos mostrar más los puntos suspensivos
|
||||
// Estimamos el ancho de los puntos suspensivos como 3 caracteres promedio
|
||||
int ellipsis_width = element_text_->lenght("...", -2);
|
||||
int available_for_text = available_width - ellipsis_width;
|
||||
|
||||
if (available_for_text <= 0) {
|
||||
return ellipsis_width; // Solo mostramos los puntos suspensivos
|
||||
}
|
||||
|
||||
// Calculamos aproximadamente cuántos caracteres caben
|
||||
float char_width = static_cast<float>(value_width) / value.length();
|
||||
size_t max_chars = static_cast<size_t>(available_for_text / char_width);
|
||||
|
||||
// Verificamos el ancho real del texto truncado
|
||||
std::string truncated = truncateWithEllipsis(value, max_chars);
|
||||
return element_text_->lenght(truncated, -2);
|
||||
}
|
||||
|
||||
auto MenuRenderer::getTruncatedValue(const std::string &value, int available_width) const -> std::string {
|
||||
int value_width = element_text_->lenght(value, -2);
|
||||
if (value_width <= available_width) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// Calculamos cuántos caracteres podemos mostrar
|
||||
int ellipsis_width = element_text_->lenght("...", -2);
|
||||
int available_for_text = available_width - ellipsis_width;
|
||||
|
||||
if (available_for_text <= 0) {
|
||||
return "..."; // Solo mostramos los puntos suspensivos
|
||||
}
|
||||
|
||||
// Calculamos aproximadamente cuántos caracteres caben
|
||||
float char_width = static_cast<float>(value_width) / value.length();
|
||||
size_t max_chars = static_cast<size_t>(available_for_text / char_width);
|
||||
|
||||
// Ajustamos iterativamente hasta que el texto quepa
|
||||
std::string truncated = truncateWithEllipsis(value, max_chars);
|
||||
while (element_text_->lenght(truncated, -2) > available_width && max_chars > 1) {
|
||||
max_chars--;
|
||||
truncated = truncateWithEllipsis(value, max_chars);
|
||||
}
|
||||
|
||||
return truncated;
|
||||
}
|
||||
Reference in New Issue
Block a user