From c0c385b8d6e474b930821486515e2c7c5d1d6550 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sat, 18 Jul 2026 20:49:47 +0800 Subject: [PATCH] feat(chat): distinguish between send and cancel in chat input --- include/Cubed/scene/world_scene.hpp | 2 +- include/Cubed/ui/chat_box.hpp | 2 +- include/Cubed/ui/text_field.hpp | 4 ++-- include/Cubed/ui/world_ui_manager.hpp | 2 +- src/scene/world_scene.cpp | 33 ++++++++++++++++++++------- src/ui/chat_box.cpp | 4 +++- src/ui/text_field.cpp | 26 ++++++++++----------- src/ui/world_ui_manager.cpp | 6 ++--- 8 files changed, 49 insertions(+), 30 deletions(-) diff --git a/include/Cubed/scene/world_scene.hpp b/include/Cubed/scene/world_scene.hpp index 283642e..94d7a7b 100644 --- a/include/Cubed/scene/world_scene.hpp +++ b/include/Cubed/scene/world_scene.hpp @@ -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); diff --git a/include/Cubed/ui/chat_box.hpp b/include/Cubed/ui/chat_box.hpp index 4f4330f..17ac318 100644 --- a/include/Cubed/ui/chat_box.hpp +++ b/include/Cubed/ui/chat_box.hpp @@ -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 ChatBox& set_on_finish(F&& f) { m_text_field->set_on_finish(std::forward(f)); diff --git a/include/Cubed/ui/text_field.hpp b/include/Cubed/ui/text_field.hpp index db8a3bd..9eb332a 100644 --- a/include/Cubed/ui/text_field.hpp +++ b/include/Cubed/ui/text_field.hpp @@ -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 \ No newline at end of file diff --git a/include/Cubed/ui/world_ui_manager.hpp b/include/Cubed/ui/world_ui_manager.hpp index e2b4ae7..29ad6b1 100644 --- a/include/Cubed/ui/world_ui_manager.hpp +++ b/include/Cubed/ui/world_ui_manager.hpp @@ -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); diff --git a/src/scene/world_scene.cpp b/src/scene/world_scene.cpp index fb5748e..a65f51e 100644 --- a/src/scene/world_scene.cpp +++ b/src/scene/world_scene.cpp @@ -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 diff --git a/src/ui/chat_box.cpp b/src/ui/chat_box.cpp index 4506a97..c9a7f22 100644 --- a/src/ui/chat_box.cpp +++ b/src/ui/chat_box.cpp @@ -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() { diff --git a/src/ui/text_field.cpp b/src/ui/text_field.cpp index ced9fcb..86d9fd4 100644 --- a/src/ui/text_field.cpp +++ b/src/ui/text_field.cpp @@ -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()); diff --git a/src/ui/world_ui_manager.cpp b/src/ui/world_ui_manager.cpp index fce04c9..a458366 100644 --- a/src/ui/world_ui_manager.cpp +++ b/src/ui/world_ui_manager.cpp @@ -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) {