feat(chat): distinguish between send and cancel in chat input

This commit is contained in:
2026-07-18 20:49:47 +08:00
parent 75f71a537f
commit c0c385b8d6
8 changed files with 49 additions and 30 deletions

View File

@@ -34,7 +34,7 @@ public:
void set_pause(bool pause);
void set_error(std::string_view error);
void set_mouse(bool pause);
void set_chatting(bool chatting);
void set_chatting(bool chatting, bool send);
// Not thread safe
void handle_chat_message(ChatMessage& message);

View File

@@ -25,7 +25,7 @@ public:
float width() const override;
float height() const override;
void set_d_image(TextureManager& m);
void set_typing(bool typing);
void set_typing(bool typing, bool finished);
void set_app(App* app);
template <typename F> ChatBox& set_on_finish(F&& f) {
m_text_field->set_on_finish(std::forward<F>(f));

View File

@@ -27,7 +27,7 @@ public:
TextField& set_default_image(TextureManager& texture_manager);
TextField& set_auto_scale(bool auto_scale);
TextField& set_app(App* app);
TextField& set_typing(bool typing);
TextField& set_typing(bool typing, bool finished);
TextField& clear_input();
bool handle_mouse_move_event(const MouseMoveEvent& e) override;
bool handle_mouse_button_event(const MouseButtonEvent& e) override;
@@ -74,6 +74,6 @@ private:
void update_input_area();
void start_typing();
void stop_typing();
void stop_typing(bool finished);
};
} // namespace Cubed

View File

@@ -19,7 +19,7 @@ public:
void init() override;
void render(Renderer& renderer) override;
void set_chatting(bool chantting);
void set_chatting(bool chantting, bool sned);
void add_chat_message(ChatMessage& message);

View File

@@ -171,6 +171,9 @@ void WorldScene::on_re_enter() {
}
bool WorldScene::handle_mouse_move_event(const MouseMoveEvent& e) {
if (m_chatting) {
return true;
}
if (m_paused) {
if (m_pasue_menu.handle_event(e)) {
return true;
@@ -191,6 +194,9 @@ bool WorldScene::handle_mouse_move_event(const MouseMoveEvent& e) {
return false;
}
bool WorldScene::handle_mouse_button_event(const MouseButtonEvent& e) {
if (m_chatting) {
return true;
}
if (m_paused) {
if (m_pasue_menu.handle_event(e)) {
return true;
@@ -228,6 +234,9 @@ bool WorldScene::handle_window_resize_event(const WindowResizeEvent& e) {
return false;
}
bool WorldScene::handle_mouse_wheel_event(const MouseWheelEvent& e) {
if (m_chatting) {
return true;
}
if (m_paused) {
if (m_pasue_menu.handle_event(e)) {
return true;
@@ -249,6 +258,10 @@ bool WorldScene::handle_mouse_wheel_event(const MouseWheelEvent& e) {
bool WorldScene::handle_key_event(const KeyEvent& e) {
if (e.key == Key::ESCAPE && e.action == KeyAction::PRESS) {
if (m_chatting && !m_paused) {
set_chatting(false, false);
return true;
}
bool pasued = pause();
pasued = !pasued;
set_pause(pasued);
@@ -264,13 +277,17 @@ bool WorldScene::handle_key_event(const KeyEvent& e) {
}
if (e.key == Key::T && e.action == KeyAction::PRESS) {
if (m_chatting) {
set_chatting(false);
} else {
set_chatting(true);
if (!m_chatting) {
set_chatting(true, true);
return true;
}
}
return true;
if (e.key == Key::ENTER && e.action == KeyAction::PRESS) {
if (m_chatting) {
set_chatting(false, true);
return true;
}
}
if (m_paused) {
@@ -324,14 +341,14 @@ void WorldScene::set_mouse(bool enable) {
}
}
void WorldScene::set_chatting(bool chatting) {
void WorldScene::set_chatting(bool chatting, bool send) {
if (m_paused) {
m_hud_ui.set_chatting(false, false);
return;
}
m_chatting = chatting;
m_hud_ui.set_chatting(chatting, send);
set_mouse(chatting);
m_hud_ui.set_chatting(chatting);
Logger::info("World Scene Chatting {}", chatting);
}
// Not thread safe

View File

@@ -67,7 +67,9 @@ void ChatBox::set_d_image(TextureManager& m) {
m_text_field->set_default_image(m);
}
void ChatBox::set_typing(bool typing) { m_text_field->set_typing(typing); }
void ChatBox::set_typing(bool typing, bool finished) {
m_text_field->set_typing(typing, finished);
}
void ChatBox::set_app(App* app) { m_text_field->set_app(app); }
void ChatBox::layout() {

View File

@@ -80,7 +80,7 @@ void TextField::on_update(float dt) {
void TextField::update_show_text() {
if (!m_typing) {
if (!m_typing && m_input_text.empty()) {
m_foreground->set_text(m_show_text);
m_foreground->set_color(Color::GRAY);
} else {
@@ -147,13 +147,12 @@ TextField& TextField::set_app(App* app) {
return *this;
}
TextField& TextField::set_typing(bool typing) {
Logger::info("Typing {}", typing);
TextField& TextField::set_typing(bool typing, bool finished) {
m_typing = typing;
if (m_typing) {
start_typing();
} else {
stop_typing();
stop_typing(finished);
}
return *this;
@@ -171,11 +170,11 @@ void TextField::start_typing() {
}
update_show_text();
}
void TextField::stop_typing() {
void TextField::stop_typing(bool finished) {
if (m_app) {
m_app->stop_text_input();
}
if (m_on_finished) {
if (m_on_finished && finished) {
m_on_finished();
}
}
@@ -202,7 +201,7 @@ bool TextField::handle_mouse_button_event(const MouseButtonEvent& e) {
} else {
if (m_typing) {
m_typing = false;
stop_typing();
stop_typing(true);
return false;
}
@@ -223,12 +222,7 @@ bool TextField::handle_key_event(const KeyEvent& e) {
if (e.key == Key::ENTER && e.action == KeyAction::PRESS) {
if (m_typing) {
m_typing = false;
if (m_app) {
m_app->stop_text_input();
}
if (m_on_finished) {
m_on_finished();
}
stop_typing(true);
return true;
}
@@ -251,6 +245,12 @@ bool TextField::handle_key_event(const KeyEvent& e) {
}
}
if (e.key == Key::ESCAPE && e.action == KeyAction::PRESS) {
stop_typing(false);
clear_input();
return true;
}
if (e.key == Key::V && e.action == KeyAction::PRESS && m_ctrl_press) {
if (m_app && m_typing) {
m_input_text.append(m_app->get_clipboard_text());

View File

@@ -57,9 +57,9 @@ void WorldUIManager::render(Renderer& renderer) {
renderer.end_render_ui();
}
void WorldUIManager::set_chatting(bool chantting) {
Logger::info("WorldUIManager Chatting {}", chantting);
m_chat_box->set_typing(chantting);
void WorldUIManager::set_chatting(bool chantting, bool send) {
m_chat_box->set_typing(chantting, send);
}
void WorldUIManager::add_chat_message(ChatMessage& message) {