feat(ui): add Rect widget with fill, color, and parent-relative sizing

This commit is contained in:
2026-07-13 20:48:51 +08:00
parent acb0b94d7b
commit 80ba6dad89
15 changed files with 215 additions and 42 deletions

View File

@@ -12,6 +12,7 @@
#include "Cubed/shader.hpp"
#include "Cubed/ui/image.hpp"
#include "Cubed/ui/label.hpp"
#include "Cubed/ui/rect.hpp"
#include <glm/glm.hpp>
#include <vector>
@@ -33,7 +34,7 @@ public:
void render_world(ClientWorld& world);
void render_lable(const Label& label);
void render_image(const Image& image);
void render_rect(const Rect& rect);
void begin_render_ui();
void end_render_ui();

View File

@@ -40,6 +40,9 @@ public:
} else if constexpr (is_same_v<dT, glm::mat4>) {
glUniformMatrix4fv(loc(location), 1, GL_FALSE,
glm::value_ptr(value));
} else if constexpr (is_same_v<dT, glm::vec4>) {
glUniform4fv(loc(location), 1, glm::value_ptr(value));
} else {
static_assert(always_false<dT>::value, "Unknown Type");
}

View File

@@ -12,6 +12,9 @@ public:
void update(float dt) override;
float width() const override;
float height() const override;
void set_spacing(int spacing);
// No need for parent node pointer; do not modify children's anchors and
// scale.

View File

@@ -25,7 +25,8 @@ public:
private:
MainMenuScene& m_scene;
std::unordered_map<std::string, std::unique_ptr<Widget>> m_widgets;
std::unique_ptr<Widget> m_root_widget;
std::unordered_map<std::string, Widget*> m_widgets;
bool handle_mouse_move_event(const MouseMoveEvent& e);
bool handle_mouse_button_event(const MouseButtonEvent& e);
bool handle_window_resize_event(const WindowResizeEvent& e);

41
include/Cubed/ui/rect.hpp Normal file
View File

@@ -0,0 +1,41 @@
#pragma once
#include "Cubed/ui/color.hpp"
#include "Cubed/ui/widget.hpp"
namespace Cubed {
class Rect : public Widget {
public:
Rect(const Rect&) = delete;
Rect(Rect&&) = delete;
Rect& operator=(const Rect&) = delete;
Rect& operator=(Rect&&) = delete;
Rect(Widget* parent);
~Rect();
void update(float dt) override;
void render(Renderer& renderer) override;
float width() const override;
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);
Color color() const;
private:
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:
virtual Widget& set_offset(glm::ivec2 offset);
virtual void set_window_size(int width, int height);
// Returns the final display size
virtual float width() const;
virtual float height() const;
virtual float width() const = 0;
virtual float height() const = 0;
virtual glm::vec2 pos() const;
virtual bool handle_key_event(const KeyEvent& e);