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.
This commit is contained in:
2026-07-19 20:15:21 +08:00
parent dae57c9163
commit 2fd9f0b854
8 changed files with 93 additions and 42 deletions

View File

@@ -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<TextField> text_field);
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));
return *this;

View File

@@ -0,0 +1,5 @@
#pragma once
namespace Cubed {
static constexpr const char* DEFAULT_TEXT_FIELD_IMAGE =
"texture/ui/textfield001.png";
}

View File

@@ -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<Widget> 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<Image> m_background;
std::unique_ptr<Widget> m_background;
std::unique_ptr<Label> m_foreground;
std::unique_ptr<Rect> m_cursor;
float m_cursor_timer = 0.0f;
@@ -62,6 +59,8 @@ private:
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;