From 6d3a0518ed2f96a7e81a715a051816e0c937f56f Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Mon, 20 Jul 2026 09:55:09 +0800 Subject: [PATCH] refactor(ui): generalize Label background, add ChatBox width method --- include/Cubed/ui/chat_box.hpp | 2 ++ include/Cubed/ui/label.hpp | 10 ++++------ include/Cubed/ui/text_field.hpp | 6 ++---- src/debug_collector.cpp | 10 +++++----- src/ui/chat_box.cpp | 15 ++++++++++++--- src/ui/label.cpp | 20 +++----------------- src/ui/text_field.cpp | 3 +++ src/ui/widget.cpp | 1 + src/ui/world_ui_manager.cpp | 7 ++++--- 9 files changed, 36 insertions(+), 38 deletions(-) diff --git a/include/Cubed/ui/chat_box.hpp b/include/Cubed/ui/chat_box.hpp index f074341..34ac158 100644 --- a/include/Cubed/ui/chat_box.hpp +++ b/include/Cubed/ui/chat_box.hpp @@ -23,6 +23,7 @@ public: std::string& get_input_text(); float width() const override; float height() const override; + float text_label_width() const; void set_text_field(std::unique_ptr text_field); void set_typing(bool typing, bool finished); template ChatBox& set_on_finish(F&& f) { @@ -42,6 +43,7 @@ private: bool m_scale = 1.0f; float m_spacing = 0.0f; float m_text_scale = 1.0f; + float m_text_width = 500.0f; std::deque m_messages; std::unique_ptr m_text_field; void layout(); diff --git a/include/Cubed/ui/label.hpp b/include/Cubed/ui/label.hpp index d348192..0759c80 100644 --- a/include/Cubed/ui/label.hpp +++ b/include/Cubed/ui/label.hpp @@ -1,5 +1,4 @@ #pragma once -#include "Cubed/ui/rect.hpp" #include "Cubed/ui/text.hpp" #include "Cubed/ui/ui_vertex_data.hpp" #include "Cubed/ui/widget.hpp" @@ -15,12 +14,11 @@ public: virtual ~Label() = default; Label& set_text(std::string_view text); - Label& set_color(Color color); Label& set_scale(float scale); - Label& enable_background(); - Label& set_background(Color color, float alpha); - Label& set_background_alpha(float alpha); + + Label& set_background(std::unique_ptr background); + Widget* get_background(); const UIVertexData& data() const; const TextStyle& text_style() const; @@ -41,7 +39,7 @@ protected: private: TextStyle m_text; UIVertexData m_data; - std::unique_ptr m_background; + std::unique_ptr m_background; float m_real_width = 0.0f; float m_real_height = 0.0f; float m_offset_x = 0.0f; diff --git a/include/Cubed/ui/text_field.hpp b/include/Cubed/ui/text_field.hpp index 8a247bc..2e3dd42 100644 --- a/include/Cubed/ui/text_field.hpp +++ b/include/Cubed/ui/text_field.hpp @@ -26,6 +26,7 @@ public: TextField& set_app(App* app); 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; bool handle_text_input_event(const TextInputEvent& e) override; @@ -54,11 +55,8 @@ private: bool m_typing = false; bool m_auto_scale = false; bool m_ctrl_press = false; - float m_width = NORMAL_TEXTFIELD_WIDTH; - float m_height = NORMAL_TEXTFIELD_HEIGHT; + float m_scale = DEFAULT_SCALE; - bool m_fill_width = false; - bool m_fill_height = false; std::string m_input_text; std::string m_show_text; diff --git a/src/debug_collector.cpp b/src/debug_collector.cpp index d55bf4d..2162d9c 100644 --- a/src/debug_collector.cpp +++ b/src/debug_collector.cpp @@ -2,6 +2,7 @@ #include "Cubed/tools/system_info.hpp" #include "Cubed/tools/system_window_manager.hpp" +#include "Cubed/ui/rect.hpp" #include "version.hpp" #include @@ -31,11 +32,10 @@ void DebugCollector::init(int width, int height) { auto add_label = [&](std::string_view text, std::string_view key = "") { auto& label = m_widget.add_child