Files
coffee_crisis_arcade_edition/source/UIMessage.cpp
2025-06-15 19:20:01 +02:00

81 lines
1.8 KiB
C++

#include "UIMessage.h"
#include <cmath> // Para pow
#include "screen.h" // Para param y TEXT_CENTER
// Constructor
UIMessage::UIMessage(std::shared_ptr<Text> text_renderer, const std::string& message_text, const Color& color)
: text_renderer_(text_renderer), text_(message_text), color_(color)
{
}
void UIMessage::show(float base_x, float base_y)
{
if (visible_ && target_y_ == 0.0f) return; // Ya está visible y quieto
base_x_ = base_x;
base_y_ = base_y;
start_y_ = -8.0f; // Empieza 8 píxeles arriba
target_y_ = 0.0f; // La posición final es el base_y
y_offset_ = start_y_;
anim_step_ = 0;
animating_ = true;
visible_ = true;
}
void UIMessage::hide()
{
if (!visible_) return;
start_y_ = y_offset_;
target_y_ = -8.0f; // Sube 8 píxeles para desaparecer
anim_step_ = 0;
animating_ = true;
}
void UIMessage::update()
{
if (animating_)
{
updateAnimation();
}
}
void UIMessage::updateAnimation()
{
anim_step_++;
float t = static_cast<float>(anim_step_) / ANIMATION_STEPS;
// Ease Out Cubic para una animación más suave
t = 1 - pow(1 - t, 3);
y_offset_ = start_y_ + (target_y_ - start_y_) * t;
if (anim_step_ >= ANIMATION_STEPS)
{
y_offset_ = target_y_;
animating_ = false;
// Si el objetivo era ocultarlo (target_y negativo), lo marcamos como no visible
if (target_y_ < 0.0f) {
visible_ = false;
}
}
}
void UIMessage::render()
{
if (visible_)
{
text_renderer_->writeDX(
TEXT_COLOR | TEXT_CENTER,
base_x_,
base_y_ + y_offset_,
text_,
-2,
color_);
}
}
bool UIMessage::isVisible() const
{
return visible_;
}