refactor(ui): move width, height, and fill properties to base Widget

Consolidate width, height, and fill (fill_parent, fill_width, fill_height) into the base Widget class. Replace per-widget set_fill(bool) with set_fill_parent(bool), set_fill_width(bool), and set_fill_height(bool).
This commit is contained in:
2026-07-19 20:56:31 +08:00
parent 2fd9f0b854
commit 8c79808b86
25 changed files with 183 additions and 156 deletions

View File

@@ -23,8 +23,8 @@ public:
float width() const override;
float height() const override;
Button& set_width(float width);
Button& set_height(float height);
Button& set_width(float width) override;
Button& set_height(float height) override;
Button& set_background_image(const std::string& path,
TextureManager& texture_manager);
Button& set_default_image(TextureManager& texture_manager);
@@ -54,8 +54,6 @@ private:
"texture/ui/button001.png";
std::function<void()> m_clicked;
float m_width = NORMAL_BUTTON_WIDTH;
float m_height = NORMAL_BUTTON_HEIGHT;
float m_scale = DEFAULT_SCALE;
bool m_auto_scale = false;
void on_render(Renderer& renderer) override;

View File

@@ -17,7 +17,6 @@ public:
ChatBox& set_text_scale(float scale);
// Only the width of TextField can be set; the height is calculated
// dynamically
ChatBox& set_width(float width);
ChatBox& set_show_lines(int lines);
ChatBox& set_spacing(float spacing);
ChatBox& clear_input();
@@ -42,8 +41,6 @@ private:
int m_lines = 10;
bool m_scale = 1.0f;
float m_spacing = 0.0f;
float m_width = 0.0f;
float m_height = 0.0f;
float m_text_scale = 1.0f;
std::deque<Line> m_messages;
std::unique_ptr<TextField> m_text_field;

View File

@@ -26,8 +26,8 @@ public:
private:
int m_spacing = 0;
float m_content_height = 0;
float m_content_width = 0;
float m_content_height = 0.0f;
float m_content_width = 0.0f;
ColumnLayoutAnchor m_layout_anchor = ColumnLayoutAnchor::CENTER;
};
} // namespace Cubed

View File

@@ -21,17 +21,9 @@ public:
Image& set_scale(float scale);
float scale() const;
Image& set_height(float height);
Image& set_width(float width);
Image& set_fill(bool fill);
private:
void on_render(Renderer& renderer) override;
void on_update(float dt) override;
float m_width = 0.0f;
float m_height = 0.0f;
bool m_fill = false;
const Texture* m_texture = nullptr;
glm::vec2 m_pos{0.0f, 0.0f};

View File

@@ -16,11 +16,6 @@ public:
float height() const override;
float alpha() const;
Rect& set_width(float width);
Rect& set_height(float height);
Rect& set_fill(bool fill);
Rect& set_scale(float scale);
Rect& set_color(Color color);
Rect& set_alpha(float alpha);
@@ -32,10 +27,7 @@ private:
void on_render(Renderer& renderer) override;
Color m_color = Color::WHITE;
float m_width = 0.0f;
float m_height = 0.0f;
float m_scale = 1.0f;
float m_alpha = 1.0f;
bool m_fill = false;
};
} // namespace Cubed

View File

@@ -21,8 +21,8 @@ public:
float height() const override;
Slider& set_scale(float scale);
Slider& set_width(float width);
Slider& set_height(float h);
Slider& set_width(float width) override;
Slider& set_height(float h) override;
Slider& set_slider_text(const std::string& key,
const std::string& variable);
Slider& set_track_image(const std::string& path,
@@ -48,8 +48,7 @@ private:
std::unique_ptr<Label> m_label;
ValueType m_type = ValueType::FLOAT;
float m_scale = DEFAULT_SCALE;
float m_width = NORMAL_SLIDER_WIDTH;
float m_height = NORMAL_SLIDER_HEIGHT;
float m_xpos = 0.0f;
bool m_dragging = false;
bool m_inside = false;

View File

@@ -18,16 +18,14 @@ public:
float width() const override;
float height() const override;
TextField& set_width(float width);
TextField& set_height(float height);
TextField& set_width(float width) override;
TextField& set_height(float height) override;
TextField& set_show_text(const std::string& text);
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;

View File

@@ -34,8 +34,16 @@ public:
virtual Widget& set_window_size(int width, int height);
virtual Widget& set_visible(bool visible);
// Returns the final display size
virtual float width() const;
virtual float height() const;
virtual Widget& set_width(float width);
virtual Widget& set_height(float height);
virtual Widget& set_fill_parent(bool fill);
virtual Widget& set_fill_width(bool fill);
virtual Widget& set_fill_height(bool fill);
virtual glm::vec2 pos() const;
virtual bool handle_key_event(const KeyEvent& e);
@@ -44,7 +52,6 @@ public:
virtual bool handle_window_resize_event(const WindowResizeEvent& e);
virtual bool handle_mouse_move_event(const MouseMoveEvent& e);
virtual bool handle_text_input_event(const TextInputEvent& e);
void set_order(TraversalOrder order);
template <typename T, typename... Args> T& add_child(Args&&... args) {
@@ -56,12 +63,16 @@ public:
protected:
Widget* m_parent = nullptr;
float m_window_height = 0;
float m_window_width = 0;
float m_window_height = 0.0f;
float m_window_width = 0.0f;
float m_width = 0.0f;
float m_height = 0.0f;
// Center is at the top-left corner, position is at the top-left corner
Anchor m_anchor = Anchor::TOP_LEFT;
bool m_visible = true;
bool m_fill_parent = false;
bool m_fill_height = false;
bool m_fill_width = false;
glm::ivec2 m_offset{0, 0};
std::vector<std::unique_ptr<Widget>>& children();