window_message: provant el redimensionat i les animacions
This commit is contained in:
@@ -11,13 +11,53 @@
|
||||
|
||||
class WindowMessage {
|
||||
public:
|
||||
enum class PositionMode {
|
||||
CENTERED, // La ventana se centra en el punto especificado
|
||||
FIXED // La esquina superior izquierda coincide con el punto
|
||||
};
|
||||
|
||||
struct Config {
|
||||
// Colores
|
||||
Color bg_color;
|
||||
Color border_color;
|
||||
Color title_color;
|
||||
Color text_color;
|
||||
|
||||
// Espaciado y dimensiones
|
||||
float padding;
|
||||
float line_spacing;
|
||||
float title_separator_spacing; // Espacio extra para separador del título
|
||||
|
||||
// Límites de tamaño
|
||||
float min_width;
|
||||
float min_height;
|
||||
float max_width_ratio; // % máximo de ancho de pantalla
|
||||
float max_height_ratio; // % máximo de alto de pantalla
|
||||
|
||||
// Margen de seguridad para texto
|
||||
float text_safety_margin; // Margen extra para evitar texto cortado
|
||||
|
||||
// Constructor con valores por defecto
|
||||
Config()
|
||||
: bg_color{40, 40, 60, 220}
|
||||
, border_color{100, 100, 120, 255}
|
||||
, title_color{255, 255, 255, 255}
|
||||
, text_color{200, 200, 200, 255}
|
||||
, padding{15.0f}
|
||||
, line_spacing{5.0f}
|
||||
, title_separator_spacing{10.0f}
|
||||
, min_width{200.0f}
|
||||
, min_height{100.0f}
|
||||
, max_width_ratio{0.8f}
|
||||
, max_height_ratio{0.8f}
|
||||
, text_safety_margin{20.0f}
|
||||
{}
|
||||
};
|
||||
|
||||
WindowMessage(
|
||||
std::shared_ptr<Text> text_renderer,
|
||||
const std::string& title = "",
|
||||
const Color& bg_color = Color{40, 40, 60, 220},
|
||||
const Color& border_color = Color{100, 100, 120, 255},
|
||||
const Color& title_color = Color{255, 255, 255, 255},
|
||||
const Color& text_color = Color{200, 200, 200, 255}
|
||||
const Config& config = Config{}
|
||||
);
|
||||
|
||||
// Métodos principales
|
||||
@@ -35,31 +75,43 @@ class WindowMessage {
|
||||
void setTexts(const std::vector<std::string>& texts);
|
||||
void addText(const std::string& text);
|
||||
void clearTexts();
|
||||
|
||||
// Control de redimensionado automático
|
||||
void enableAutoResize(bool enabled) { auto_resize_enabled_ = enabled; }
|
||||
[[nodiscard]] auto isAutoResizeEnabled() const -> bool { return auto_resize_enabled_; }
|
||||
|
||||
// Configuración de posición y tamaño
|
||||
void setPosition(float x, float y);
|
||||
void setPosition(float x, float y, PositionMode mode = PositionMode::CENTERED);
|
||||
void setSize(float width, float height);
|
||||
void centerOnScreen();
|
||||
void autoSize(); // Ajusta automáticamente al contenido
|
||||
void autoSize(); // Ajusta automáticamente al contenido y reposiciona si es necesario
|
||||
|
||||
// Configuración de colores
|
||||
void setBackgroundColor(const Color& color) { bg_color_ = color; }
|
||||
void setBorderColor(const Color& color) { border_color_ = color; }
|
||||
void setTitleColor(const Color& color) { title_color_ = color; }
|
||||
void setTextColor(const Color& color) { text_color_ = color; }
|
||||
void setBackgroundColor(const Color& color) { config_.bg_color = color; }
|
||||
void setBorderColor(const Color& color) { config_.border_color = color; }
|
||||
void setTitleColor(const Color& color) { config_.title_color = color; updateStyles(); }
|
||||
void setTextColor(const Color& color) { config_.text_color = color; updateStyles(); }
|
||||
|
||||
// Configuración de espaciado
|
||||
void setPadding(float padding) { padding_ = padding; }
|
||||
void setLineSpacing(float spacing) { line_spacing_ = spacing; }
|
||||
void setPadding(float padding) { config_.padding = padding; }
|
||||
void setLineSpacing(float spacing) { config_.line_spacing = spacing; }
|
||||
|
||||
// Configuración avanzada
|
||||
void setConfig(const Config& config) { config_ = config; updateStyles(); }
|
||||
[[nodiscard]] auto getConfig() const -> const Config& { return config_; }
|
||||
|
||||
// Getters
|
||||
[[nodiscard]] auto getRect() const -> const SDL_FRect& { return rect_; }
|
||||
[[nodiscard]] auto getPositionMode() const -> PositionMode { return position_mode_; }
|
||||
[[nodiscard]] auto getAnchorPoint() const -> std::pair<float, float> { return {anchor_x_, anchor_y_}; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<Text> text_renderer_;
|
||||
Config config_;
|
||||
|
||||
// Estado de visibilidad
|
||||
// Estado de visibilidad y redimensionado
|
||||
bool visible_ = false;
|
||||
bool auto_resize_enabled_ = true; // Por defecto habilitado
|
||||
|
||||
// Contenido
|
||||
std::string title_;
|
||||
@@ -67,14 +119,40 @@ class WindowMessage {
|
||||
|
||||
// Posición y tamaño
|
||||
SDL_FRect rect_{0, 0, 300, 200};
|
||||
float padding_ = 15.0f;
|
||||
float line_spacing_ = 5.0f;
|
||||
|
||||
// Colores
|
||||
Color bg_color_;
|
||||
Color border_color_;
|
||||
Color title_color_;
|
||||
Color text_color_;
|
||||
PositionMode position_mode_ = PositionMode::CENTERED;
|
||||
float anchor_x_ = 0.0f;
|
||||
float anchor_y_ = 0.0f;
|
||||
|
||||
// Animación de redimensionado
|
||||
struct ResizeAnimation {
|
||||
bool active = false;
|
||||
float start_width, start_height;
|
||||
float target_width, target_height;
|
||||
float duration = 0.3f; // segundos
|
||||
float elapsed = 0.0f;
|
||||
|
||||
void start(float from_w, float from_h, float to_w, float to_h) {
|
||||
start_width = from_w;
|
||||
start_height = from_h;
|
||||
target_width = to_w;
|
||||
target_height = to_h;
|
||||
elapsed = 0.0f;
|
||||
active = true;
|
||||
}
|
||||
|
||||
void stop() {
|
||||
active = false;
|
||||
elapsed = 0.0f;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto isFinished() const -> bool {
|
||||
return elapsed >= duration;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto getProgress() const -> float {
|
||||
return std::min(elapsed / duration, 1.0f);
|
||||
}
|
||||
} resize_animation_;
|
||||
|
||||
// Estilos
|
||||
Text::Style title_style_;
|
||||
@@ -82,6 +160,17 @@ class WindowMessage {
|
||||
|
||||
// Métodos privados
|
||||
void calculateAutoSize();
|
||||
void updatePosition(); // Actualiza la posición según el modo y punto de anclaje
|
||||
void updateStyles(); // Actualiza los estilos de texto cuando cambian los colores
|
||||
void ensureTextFits(); // Verifica y ajusta para que todo el texto sea visible
|
||||
void triggerAutoResize(); // Inicia redimensionado automático si está habilitado
|
||||
void updateAnimation(float delta_time); // Actualiza la animación de redimensionado
|
||||
|
||||
// Función de suavizado (ease-out)
|
||||
[[nodiscard]] auto easeOut(float t) const -> float;
|
||||
|
||||
[[nodiscard]] auto calculateContentHeight() const -> float;
|
||||
[[nodiscard]] auto calculateContentWidth() const -> float;
|
||||
[[nodiscard]] auto getScreenWidth() const -> float;
|
||||
[[nodiscard]] auto getScreenHeight() const -> float;
|
||||
};
|
||||
Reference in New Issue
Block a user