From 2fd9f0b8549186c7036cd4e5335f6e431f9407db Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sun, 19 Jul 2026 20:15:21 +0800 Subject: [PATCH] refactor(ui): decouple ChatBox from image dependencies, allow custom TextField backgrounds TextField's background is now a generic `Widget` pointer, enabling use of any widget (e.g., `Rect`, `Image`) as background. `ChatBox` no longer creates its own `TextField`; it accepts one via `set_text_field`. Removed `set_d_image` and `set_default_image` in favor of `set_background`. Added a shared default image constant in `default_image.hpp`. Updated `host_game_ui.cpp`, `join_game_ui.cpp`, and `world_ui_manager.cpp` to construct backgrounds accordingly. --- include/Cubed/ui/chat_box.hpp | 3 +- include/Cubed/ui/default_image.hpp | 5 +++ include/Cubed/ui/text_field.hpp | 13 ++++---- src/ui/chat_box.cpp | 37 +++++++++++++--------- src/ui/host_game_ui.cpp | 11 +++++-- src/ui/join_game_ui.cpp | 6 +++- src/ui/text_field.cpp | 49 +++++++++++++++++++++--------- src/ui/world_ui_manager.cpp | 11 +++++-- 8 files changed, 93 insertions(+), 42 deletions(-) create mode 100644 include/Cubed/ui/default_image.hpp diff --git a/include/Cubed/ui/chat_box.hpp b/include/Cubed/ui/chat_box.hpp index 17ac318..1fef375 100644 --- a/include/Cubed/ui/chat_box.hpp +++ b/include/Cubed/ui/chat_box.hpp @@ -24,9 +24,8 @@ public: std::string& get_input_text(); float width() const override; float height() const override; - void set_d_image(TextureManager& m); + void set_text_field(std::unique_ptr text_field); 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)); return *this; diff --git a/include/Cubed/ui/default_image.hpp b/include/Cubed/ui/default_image.hpp new file mode 100644 index 0000000..3fbd870 --- /dev/null +++ b/include/Cubed/ui/default_image.hpp @@ -0,0 +1,5 @@ +#pragma once +namespace Cubed { +static constexpr const char* DEFAULT_TEXT_FIELD_IMAGE = + "texture/ui/textfield001.png"; +} \ No newline at end of file diff --git a/include/Cubed/ui/text_field.hpp b/include/Cubed/ui/text_field.hpp index 9eb332a..90f09bd 100644 --- a/include/Cubed/ui/text_field.hpp +++ b/include/Cubed/ui/text_field.hpp @@ -1,6 +1,5 @@ #pragma once -#include "Cubed/ui/image.hpp" #include "Cubed/ui/label.hpp" #include "Cubed/ui/rect.hpp" #include "Cubed/ui/widget.hpp" @@ -22,13 +21,13 @@ public: TextField& set_width(float width); TextField& set_height(float height); TextField& set_show_text(const std::string& text); - TextField& set_background_image(const std::string& path, - TextureManager& texture_manager); - TextField& set_default_image(TextureManager& texture_manager); + TextField& set_background(std::unique_ptr background); TextField& set_auto_scale(bool auto_scale); TextField& set_app(App* app); TextField& set_typing(bool typing, bool finished); TextField& clear_input(); + TextField& set_fill_width(bool fill); + TextField& set_fill_height(bool fill); 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; @@ -46,11 +45,9 @@ private: static constexpr float DEFAULT_SCALE = 3.0f; static constexpr float TEXT_SCALE = 0.6f; static constexpr float CURSOR_INTERVAL = 0.5f; - static constexpr const char* DEFAULT_TEXT_FIELD_IMAGE = - "texture/ui/textfield001.png"; App* m_app = nullptr; - std::unique_ptr m_background; + std::unique_ptr m_background; std::unique_ptr