Text: revisada la classe

window_message: correcions
This commit is contained in:
2025-08-07 12:40:24 +02:00
parent 49145905e3
commit 100b7265d5
24 changed files with 467 additions and 444 deletions

View File

@@ -19,7 +19,7 @@ auto ActionListOption::getValueAsString() const -> std::string {
auto ActionListOption::getMaxValueWidth(Text* text) const -> int {
int max_width = 0;
for (const auto& option : options_) {
int width = text->lenght(option, -2);
int width = text->length(option, -2);
if (width > max_width) {
max_width = width;
}

View File

@@ -17,9 +17,9 @@
class MenuOption {
public:
enum class Behavior {
ADJUST, // Solo puede ajustar valor (como IntOption, BoolOption, ListOption)
SELECT, // Solo puede ejecutar acción (como ActionOption, FolderOption)
BOTH // Puede tanto ajustar como ejecutar acción (como ActionListOption)
ADJUST, // Solo puede ajustar valor (como IntOption, BoolOption, ListOption)
SELECT, // Solo puede ejecutar acción (como ActionOption, FolderOption)
BOTH // Puede tanto ajustar como ejecutar acción (como ActionListOption)
};
MenuOption(std::string caption, ServiceMenu::SettingsGroup group, bool hidden = false)
@@ -63,8 +63,8 @@ class BoolOption : public MenuOption {
}
auto getMaxValueWidth(Text *text_renderer) const -> int override {
return std::max(
text_renderer->lenght(Lang::getText("[SERVICE_MENU] ON"), -2),
text_renderer->lenght(Lang::getText("[SERVICE_MENU] OFF"), -2));
text_renderer->length(Lang::getText("[SERVICE_MENU] ON"), -2),
text_renderer->length(Lang::getText("[SERVICE_MENU] OFF"), -2));
}
private:
@@ -87,7 +87,7 @@ class IntOption : public MenuOption {
// Iterar por todos los valores posibles en el rango
for (int value = min_value_; value <= max_value_; value += step_value_) {
int width = text_renderer->lenght(std::to_string(value), -2);
int width = text_renderer->length(std::to_string(value), -2);
max_width = std::max(max_width, width);
}
@@ -135,7 +135,7 @@ class ListOption : public MenuOption {
auto getMaxValueWidth(Text *text_renderer) const -> int override {
int max_w = 0;
for (const auto &val : value_list_) {
max_w = std::max(max_w, text_renderer->lenght(val, -2));
max_w = std::max(max_w, text_renderer->length(val, -2));
}
return max_w;
}
@@ -179,24 +179,20 @@ class ActionOption : public MenuOption {
class ActionListOption : public MenuOption {
public:
using ValueGetter = std::function<std::string()>;
using ValueSetter = std::function<void(const std::string&)>;
using ValueSetter = std::function<void(const std::string &)>;
using ActionExecutor = std::function<void()>;
ActionListOption(const std::string& caption, ServiceMenu::SettingsGroup group,
std::vector<std::string> options, ValueGetter getter, ValueSetter setter,
ActionExecutor action_executor, bool hidden = false)
: MenuOption(caption, group, hidden), options_(std::move(options)),
value_getter_(std::move(getter)), value_setter_(std::move(setter)),
action_executor_(std::move(action_executor)), current_index_(0) {
ActionListOption(const std::string &caption, ServiceMenu::SettingsGroup group, std::vector<std::string> options, ValueGetter getter, ValueSetter setter, ActionExecutor action_executor, bool hidden = false)
: MenuOption(caption, group, hidden), options_(std::move(options)), value_getter_(std::move(getter)), value_setter_(std::move(setter)), action_executor_(std::move(action_executor)), current_index_(0) {
updateCurrentIndex();
}
[[nodiscard]] auto getBehavior() const -> Behavior override { return Behavior::BOTH; }
[[nodiscard]] auto getValueAsString() const -> std::string override;
[[nodiscard]] auto getMaxValueWidth(Text* text) const -> int override;
[[nodiscard]] auto getMaxValueWidth(Text *text) const -> int override;
void adjustValue(bool up) override;
void executeAction() override;
void sync(); // Sincroniza con el valor actual
void sync(); // Sincroniza con el valor actual
private:
std::vector<std::string> options_;

View File

@@ -7,7 +7,7 @@
#include "menu_option.h" // Para MenuOption
#include "param.h" // Para Param, param, ParamServiceMenu, ParamGame
#include "screen.h" // Para Screen
#include "text.h" // Para Text, TEXT_CENTER, TEXT_COLOR
#include "text.h" // Para Text, Text::CENTER, Text::COLOR
#include "utils.h" // Para Zone, truncateWithEllipsis
MenuRenderer::MenuRenderer(const ServiceMenu *menu_state, std::shared_ptr<Text> element_text, std::shared_ptr<Text> title_text)
@@ -35,7 +35,7 @@ void MenuRenderer::render(const ServiceMenu *menu_state) {
// Dibuja el título
float y = rect_.y + title_padding_;
title_text_->writeDX(TEXT_COLOR | TEXT_CENTER, param.game.game_area.center_x, y, menu_state->getTitle(), -4, param.service_menu.title_color);
title_text_->writeDX(Text::COLOR | Text::CENTER, param.game.game_area.center_x, y, menu_state->getTitle(), -4, param.service_menu.title_color);
// Dibuja la línea separadora
y = rect_.y + upper_height_;
@@ -46,38 +46,38 @@ void MenuRenderer::render(const ServiceMenu *menu_state) {
y = options_y_;
const auto &option_pairs = menu_state->getOptionPairs();
const auto &display_options = menu_state->getDisplayOptions();
for (size_t i = 0; i < option_pairs.size(); ++i) {
const bool IS_SELECTED = (i == menu_state->getSelectedIndex());
const Color &current_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 int available_width = rect_.w - (ServiceMenu::OPTIONS_HORIZONTAL_PADDING * 2) -
element_text_->length(option_pairs.at(i).first, -2) -
ServiceMenu::MIN_GAP_OPTION_VALUE;
std::string truncated_value = getTruncatedValue(option_pairs.at(i).second, available_width);
// Si la opción tiene Behavior::BOTH, añadir indicador visual
if (i < display_options.size() && display_options[i]->getBehavior() == MenuOption::Behavior::BOTH) {
truncated_value = "" + truncated_value + "";
truncated_value = "-" + truncated_value + "-";
}
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(truncated_value, -2);
const int X = rect_.x + rect_.w - ServiceMenu::OPTIONS_HORIZONTAL_PADDING - element_text_->length(truncated_value, -2);
element_text_->writeColored(X, y, truncated_value, current_color, -2);
} else {
// Para opciones centradas, también truncamos si es necesario
const int available_width = rect_.w - (ServiceMenu::OPTIONS_HORIZONTAL_PADDING * 2);
std::string truncated_caption = getTruncatedValue(option_pairs.at(i).first, available_width);
// Si la opción tiene Behavior::BOTH, añadir indicador visual
if (i < display_options.size() && display_options[i]->getBehavior() == MenuOption::Behavior::BOTH) {
truncated_caption = "" + truncated_caption + "";
truncated_caption = "-" + truncated_caption + "-";
}
element_text_->writeDX(TEXT_CENTER | TEXT_COLOR, rect_.x + rect_.w / 2, y, truncated_caption, -2, current_color);
element_text_->writeDX(Text::CENTER | Text::COLOR, rect_.x + rect_.w / 2, y, truncated_caption, -2, current_color);
}
y += options_height_ + options_padding_;
}
@@ -196,7 +196,7 @@ void MenuRenderer::precalculateMenuWidths(const std::vector<std::unique_ptr<Menu
if (option->getGroup() != sg) {
continue;
}
max_option_width = std::max(max_option_width, element_text_->lenght(option->getCaption(), -2));
max_option_width = std::max(max_option_width, element_text_->length(option->getCaption(), -2));
if (menu_state->getCurrentGroupAlignment() == ServiceMenu::GroupAlignment::LEFT) {
// 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 -
@@ -240,14 +240,14 @@ auto MenuRenderer::setRect(SDL_FRect rect) -> SDL_FRect {
}
auto MenuRenderer::getTruncatedValueWidth(const std::string &value, int available_width) const -> int {
int value_width = element_text_->lenght(value, -2);
int value_width = element_text_->length(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 ellipsis_width = element_text_->length("...", -2);
int available_for_text = available_width - ellipsis_width;
if (available_for_text <= 0) {
@@ -260,17 +260,17 @@ auto MenuRenderer::getTruncatedValueWidth(const std::string &value, int availabl
// Verificamos el ancho real del texto truncado
std::string truncated = truncateWithEllipsis(value, max_chars);
return element_text_->lenght(truncated, -2);
return element_text_->length(truncated, -2);
}
auto MenuRenderer::getTruncatedValue(const std::string &value, int available_width) const -> std::string {
int value_width = element_text_->lenght(value, -2);
int value_width = element_text_->length(value, -2);
if (value_width <= available_width) {
return value;
}
// Calculamos cuántos caracteres podemos mostrar
int ellipsis_width = element_text_->lenght("...", -2);
int ellipsis_width = element_text_->length("...", -2);
int available_for_text = available_width - ellipsis_width;
if (available_for_text <= 0) {
@@ -283,7 +283,7 @@ auto MenuRenderer::getTruncatedValue(const std::string &value, int available_wid
// 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) {
while (element_text_->length(truncated, -2) > available_width && max_chars > 1) {
max_chars--;
truncated = truncateWithEllipsis(value, max_chars);
}

View File

@@ -183,7 +183,7 @@ void Notifier::show(std::vector<std::string> texts, int icon, const std::string&
const float PADDING_IN_H = text_->getCharacterSize();
const float PADDING_IN_V = text_->getCharacterSize() / 2;
const int ICON_SPACE = icon >= 0 ? ICON_SIZE + PADDING_IN_H : 0;
const float WIDTH = text_->lenght(longest) + (PADDING_IN_H * 2) + ICON_SPACE;
const float WIDTH = text_->length(longest) + (PADDING_IN_H * 2) + ICON_SPACE;
const float HEIGHT = (text_->getCharacterSize() * texts.size()) + (PADDING_IN_V * 2);
const auto SHAPE = NotificationShape::SQUARED;

View File

@@ -153,7 +153,7 @@ void ServiceMenu::selectOption() {
current_settings_group_ = folder->getTargetGroup();
selected_ = 0;
updateMenu();
} else if (selected_option->getBehavior() == MenuOption::Behavior::SELECT) {
} else if (selected_option->getBehavior() == MenuOption::Behavior::SELECT or selected_option->getBehavior() == MenuOption::Behavior::BOTH) {
selected_option->executeAction();
}
playSelectSound();
@@ -554,7 +554,7 @@ void ServiceMenu::handleEvent(const SDL_Event &event) {
}
// Procesar eventos normales del ServiceMenu
switch (event.type) {
/*switch (event.type) {
case SDL_EVENT_KEY_DOWN:
switch (event.key.key) {
case SDLK_ESCAPE:
@@ -611,5 +611,5 @@ void ServiceMenu::handleEvent(const SDL_Event &event) {
default:
break;
}
}*/
}

View File

@@ -3,7 +3,7 @@
#include <cmath> // Para pow
#include <utility>
#include "text.h" // Para TEXT_CENTER, TEXT_COLOR, Text
#include "text.h" // Para Text::CENTER, Text::COLOR, Text
// Constructor: inicializa el renderizador, el texto y el color del mensaje
UIMessage::UIMessage(std::shared_ptr<Text> text_renderer, std::string message_text, const Color &color)
@@ -70,7 +70,7 @@ void UIMessage::updateAnimation() {
void UIMessage::render() {
if (visible_) {
text_renderer_->writeDX(
TEXT_COLOR | TEXT_CENTER,
Text::COLOR | Text::CENTER,
base_x_,
base_y_ + y_offset_,
text_,

View File

@@ -13,13 +13,15 @@ WindowMessage::WindowMessage(
const Color& bg_color,
const Color& border_color,
const Color& title_color,
const Color& text_color
) : text_renderer_(std::move(text_renderer)),
title_(title),
bg_color_(bg_color),
border_color_(border_color),
title_color_(title_color),
text_color_(text_color) {
const Color& text_color)
: text_renderer_(std::move(text_renderer)),
title_(title),
bg_color_(bg_color),
border_color_(border_color),
title_color_(title_color),
text_color_(text_color),
title_style_(Text::CENTER | Text::COLOR, title_color, title_color, 0, -2),
text_style_(Text::CENTER | Text::COLOR, text_color, text_color, 0, -2) {
}
void WindowMessage::render() {
@@ -28,42 +30,42 @@ void WindowMessage::render() {
}
SDL_Renderer* renderer = Screen::get()->getRenderer();
// Dibujar fondo con transparencia
SDL_SetRenderDrawColor(renderer, bg_color_.r, bg_color_.g, bg_color_.b, bg_color_.a);
SDL_RenderFillRect(renderer, &rect_);
// Dibujar borde
SDL_SetRenderDrawColor(renderer, border_color_.r, border_color_.g, border_color_.b, border_color_.a);
SDL_RenderRect(renderer, &rect_);
float current_y = rect_.y + padding_;
// Dibujar título si existe
if (!title_.empty()) {
text_renderer_->writeCentered(
text_renderer_->writeStyle(
rect_.x + rect_.w / 2.0f,
current_y,
title_
//title_color_
);
title_,
title_style_);
current_y += text_renderer_->getCharacterSize() + line_spacing_ * 2;
// Línea separadora debajo del título
SDL_SetRenderDrawColor(renderer, border_color_.r, border_color_.g, border_color_.b, border_color_.a);
SDL_RenderLine(renderer,
rect_.x + padding_, current_y - line_spacing_,
rect_.x + rect_.w - padding_, current_y - line_spacing_);
SDL_RenderLine(renderer,
rect_.x + padding_,
current_y - line_spacing_,
rect_.x + rect_.w - padding_,
current_y - line_spacing_);
}
// Dibujar textos
for (const auto& text : texts_) {
text_renderer_->writeCentered(
text_renderer_->writeStyle(
rect_.x + rect_.w / 2.0f,
current_y,
text
//text_color_
);
text,
text_style_);
current_y += text_renderer_->getCharacterSize() + line_spacing_;
}
}
@@ -123,14 +125,14 @@ void WindowMessage::autoSize() {
void WindowMessage::calculateAutoSize() {
float content_width = calculateContentWidth();
float content_height = calculateContentHeight();
rect_.w = content_width + (padding_ * 2);
rect_.h = content_height + (padding_ * 2);
// Aplicar límites mínimos y máximos
rect_.w = std::max(rect_.w, 200.0f);
rect_.h = std::max(rect_.h, 100.0f);
// No exceder el 80% de la pantalla
rect_.w = std::min(rect_.w, param.game.width * 0.8f);
rect_.h = std::min(rect_.h, param.game.height * 0.8f);
@@ -138,35 +140,35 @@ void WindowMessage::calculateAutoSize() {
auto WindowMessage::calculateContentHeight() const -> float {
float height = 0;
// Altura del título
if (!title_.empty()) {
height += text_renderer_->getCharacterSize() + line_spacing_ * 2; // Espacio extra para separador
height += text_renderer_->getCharacterSize() + line_spacing_ * 2; // Espacio extra para separador
}
// Altura de los textos
if (!texts_.empty()) {
height += (texts_.size() * text_renderer_->getCharacterSize());
height += ((texts_.size() - 1) * line_spacing_); // Espaciado entre líneas
height += ((texts_.size() - 1) * line_spacing_); // Espaciado entre líneas
}
return height;
}
auto WindowMessage::calculateContentWidth() const -> float {
float max_width = 200.0f; // Ancho mínimo
float max_width = 200.0f; // Ancho mínimo
// Ancho del título
if (!title_.empty()) {
float title_width = text_renderer_->lenght(title_, -2);
float title_width = text_renderer_->length(title_, -2);
max_width = std::max(max_width, title_width);
}
// Ancho de los textos
for (const auto& text : texts_) {
float text_width = text_renderer_->lenght(text, -2);
float text_width = text_renderer_->length(text, -2);
max_width = std::max(max_width, text_width);
}
return max_width;
}

View File

@@ -7,8 +7,7 @@
#include <vector>
#include "color.h"
class Text;
#include "text.h"
class WindowMessage {
public:
@@ -76,6 +75,10 @@ class WindowMessage {
Color border_color_;
Color title_color_;
Color text_color_;
// Estilos
Text::Style title_style_;
Text::Style text_style_;
// Métodos privados
void calculateAutoSize();