feat(chat): implement message expiration after 5 seconds

This commit is contained in:
2026-07-20 10:16:12 +08:00
parent 6d3a0518ed
commit ce10290959
5 changed files with 23 additions and 7 deletions

View File

@@ -33,11 +33,11 @@ public:
private:
static constexpr int MAX_MESSGAES_SUM = 50;
static constexpr float DISAPPEAR_TIME = 5.0f;
struct Line {
std::unique_ptr<Label> label;
uint64_t time;
bool render = false;
bool render = true;
};
int m_lines = 10;
bool m_scale = 1.0f;

View File

@@ -26,7 +26,7 @@ public:
TextField& set_app(App* app);
TextField& set_typing(bool typing, bool finished);
TextField& clear_input();
bool is_typing() const;
bool handle_mouse_move_event(const MouseMoveEvent& e) override;
bool handle_mouse_button_event(const MouseButtonEvent& e) override;
bool handle_text_input_event(const TextInputEvent& e) override;

View File

@@ -1,6 +1,7 @@
#include "Cubed/ui/chat_box.hpp"
#include "Cubed/tools/text_tools.hpp"
#include "Cubed/tools/time_tools.hpp"
namespace Cubed {
ChatBox::ChatBox(Widget* parent) : Widget(parent) {}
@@ -101,15 +102,29 @@ void ChatBox::layout() {
it->label->set_anchor(Anchor::BOTTOM_LEFT);
// -y means upward offset
it->label->set_offset({0, -y});
it->render = true;
y += it->label->height() + m_spacing;
}
m_height = m_lines == 0 ? 0 : (y - m_spacing);
}
void ChatBox::on_update(float dt) {
Widget::on_update(dt);
auto tick = Tools::get_time_ticks();
for (auto& m : m_messages) {
m.label->update(dt);
if (!m.render) {
continue;
}
if (tick < m.time) {
Logger::error("Tick {} Less Than Line Time {}", tick, m.time);
continue;
}
auto elapsed = tick - m.time;
if (elapsed >= DISAPPEAR_TIME * 1000) {
m.render = false;
}
}
layout();
m_text_field->update(dt);
@@ -124,7 +139,7 @@ void ChatBox::on_render(Renderer& renderer) {
if (!it->label) {
continue;
}
if (it->render) {
if (m_text_field->is_typing() || it->render) {
it->label->render(renderer);
}
}

View File

@@ -173,7 +173,7 @@ TextField& TextField::clear_input() {
update_show_text();
return *this;
}
bool TextField::is_typing() const { return m_typing; }
void TextField::start_typing() {
if (m_app) {
m_app->start_text_input();

View File

@@ -39,7 +39,8 @@ void WorldUIManager::init() {
text_field->set_background(std::move(rect))
.set_app(&m_scene.scene_manager().app())
.set_fill_width(true)
.set_visible(false);
.set_visible(false)
.set_height(15.0f);
chat_box.set_text_field(std::move(text_field));
chat_box.set_spacing(0);
chat_box.set_anchor(Anchor::BOTTOM_LEFT);