mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat(chat): implement message expiration after 5 seconds
This commit is contained in:
@@ -33,11 +33,11 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr int MAX_MESSGAES_SUM = 50;
|
static constexpr int MAX_MESSGAES_SUM = 50;
|
||||||
|
static constexpr float DISAPPEAR_TIME = 5.0f;
|
||||||
struct Line {
|
struct Line {
|
||||||
std::unique_ptr<Label> label;
|
std::unique_ptr<Label> label;
|
||||||
uint64_t time;
|
uint64_t time;
|
||||||
bool render = false;
|
bool render = true;
|
||||||
};
|
};
|
||||||
int m_lines = 10;
|
int m_lines = 10;
|
||||||
bool m_scale = 1.0f;
|
bool m_scale = 1.0f;
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ public:
|
|||||||
TextField& set_app(App* app);
|
TextField& set_app(App* app);
|
||||||
TextField& set_typing(bool typing, bool finished);
|
TextField& set_typing(bool typing, bool finished);
|
||||||
TextField& clear_input();
|
TextField& clear_input();
|
||||||
|
bool is_typing() const;
|
||||||
bool handle_mouse_move_event(const MouseMoveEvent& e) override;
|
bool handle_mouse_move_event(const MouseMoveEvent& e) override;
|
||||||
bool handle_mouse_button_event(const MouseButtonEvent& e) override;
|
bool handle_mouse_button_event(const MouseButtonEvent& e) override;
|
||||||
bool handle_text_input_event(const TextInputEvent& e) override;
|
bool handle_text_input_event(const TextInputEvent& e) override;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "Cubed/ui/chat_box.hpp"
|
#include "Cubed/ui/chat_box.hpp"
|
||||||
|
|
||||||
#include "Cubed/tools/text_tools.hpp"
|
#include "Cubed/tools/text_tools.hpp"
|
||||||
|
#include "Cubed/tools/time_tools.hpp"
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
ChatBox::ChatBox(Widget* parent) : Widget(parent) {}
|
ChatBox::ChatBox(Widget* parent) : Widget(parent) {}
|
||||||
|
|
||||||
@@ -101,15 +102,29 @@ void ChatBox::layout() {
|
|||||||
it->label->set_anchor(Anchor::BOTTOM_LEFT);
|
it->label->set_anchor(Anchor::BOTTOM_LEFT);
|
||||||
// -y means upward offset
|
// -y means upward offset
|
||||||
it->label->set_offset({0, -y});
|
it->label->set_offset({0, -y});
|
||||||
it->render = true;
|
|
||||||
y += it->label->height() + m_spacing;
|
y += it->label->height() + m_spacing;
|
||||||
}
|
}
|
||||||
m_height = m_lines == 0 ? 0 : (y - m_spacing);
|
m_height = m_lines == 0 ? 0 : (y - m_spacing);
|
||||||
}
|
}
|
||||||
void ChatBox::on_update(float dt) {
|
void ChatBox::on_update(float dt) {
|
||||||
Widget::on_update(dt);
|
Widget::on_update(dt);
|
||||||
|
auto tick = Tools::get_time_ticks();
|
||||||
for (auto& m : m_messages) {
|
for (auto& m : m_messages) {
|
||||||
m.label->update(dt);
|
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();
|
layout();
|
||||||
m_text_field->update(dt);
|
m_text_field->update(dt);
|
||||||
@@ -124,7 +139,7 @@ void ChatBox::on_render(Renderer& renderer) {
|
|||||||
if (!it->label) {
|
if (!it->label) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (it->render) {
|
if (m_text_field->is_typing() || it->render) {
|
||||||
it->label->render(renderer);
|
it->label->render(renderer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ TextField& TextField::clear_input() {
|
|||||||
update_show_text();
|
update_show_text();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
bool TextField::is_typing() const { return m_typing; }
|
||||||
void TextField::start_typing() {
|
void TextField::start_typing() {
|
||||||
if (m_app) {
|
if (m_app) {
|
||||||
m_app->start_text_input();
|
m_app->start_text_input();
|
||||||
|
|||||||
@@ -39,7 +39,8 @@ void WorldUIManager::init() {
|
|||||||
text_field->set_background(std::move(rect))
|
text_field->set_background(std::move(rect))
|
||||||
.set_app(&m_scene.scene_manager().app())
|
.set_app(&m_scene.scene_manager().app())
|
||||||
.set_fill_width(true)
|
.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_text_field(std::move(text_field));
|
||||||
chat_box.set_spacing(0);
|
chat_box.set_spacing(0);
|
||||||
chat_box.set_anchor(Anchor::BOTTOM_LEFT);
|
chat_box.set_anchor(Anchor::BOTTOM_LEFT);
|
||||||
|
|||||||
Reference in New Issue
Block a user