mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
feat(ui): introduce ColumnLayout widget and refactor widget parenting
- Add ColumnLayout widget that arranges children vertically with spacing. - Refactor Widget::add_child to automatically pass `this` as parent. - Update DebugCollector to use ColumnLayout for consistent spacing. - Expose children() accessor in Widget for layout management.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "Cubed/ui/column_layout.hpp"
|
||||||
#include "Cubed/ui/label.hpp"
|
#include "Cubed/ui/label.hpp"
|
||||||
#include "Cubed/ui/widget.hpp"
|
#include "Cubed/ui/widget.hpp"
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ public:
|
|||||||
bool handle_event(const Event& e);
|
bool handle_event(const Event& e);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Widget m_widget;
|
ColumnLayout m_widget;
|
||||||
std::unordered_map<std::string, Label*> m_component;
|
std::unordered_map<std::string, Label*> m_component;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
24
include/Cubed/ui/column_layout.hpp
Normal file
24
include/Cubed/ui/column_layout.hpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Cubed/ui/widget.hpp"
|
||||||
|
namespace Cubed {
|
||||||
|
class ColumnLayout : public Widget {
|
||||||
|
public:
|
||||||
|
ColumnLayout(const ColumnLayout&) = delete;
|
||||||
|
ColumnLayout(ColumnLayout&&) = delete;
|
||||||
|
ColumnLayout& operator=(const ColumnLayout&) = delete;
|
||||||
|
ColumnLayout& operator=(ColumnLayout&&) = delete;
|
||||||
|
ColumnLayout(Widget* parent);
|
||||||
|
~ColumnLayout();
|
||||||
|
|
||||||
|
void update(float dt) override;
|
||||||
|
|
||||||
|
void set_spacing(int spacing);
|
||||||
|
// No need for parent node pointer; do not modify children's anchors and
|
||||||
|
// scale.
|
||||||
|
|
||||||
|
void layout();
|
||||||
|
|
||||||
|
private:
|
||||||
|
int m_spacing = 0;
|
||||||
|
};
|
||||||
|
} // namespace Cubed
|
||||||
@@ -32,16 +32,13 @@ public:
|
|||||||
virtual bool handle_mouse_move_event(const MouseMoveEvent& e);
|
virtual bool handle_mouse_move_event(const MouseMoveEvent& e);
|
||||||
|
|
||||||
template <typename T, typename... Args> T& add_child(Args&&... args) {
|
template <typename T, typename... Args> T& add_child(Args&&... args) {
|
||||||
auto widget = std::make_unique<T>(std::forward<Args>(args)...);
|
auto widget = std::make_unique<T>(std::forward<Args>(args)..., this);
|
||||||
T& ref = *widget;
|
T& ref = *widget;
|
||||||
m_children.emplace_back(std::move(widget));
|
m_children.emplace_back(std::move(widget));
|
||||||
return ref;
|
return ref;
|
||||||
};
|
};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void on_update(float dt);
|
|
||||||
virtual void on_render(Renderer& renderer);
|
|
||||||
virtual glm::vec2 compute_position() const;
|
|
||||||
Widget* m_parent = nullptr;
|
Widget* m_parent = nullptr;
|
||||||
std::string m_id;
|
std::string m_id;
|
||||||
float m_window_height = 0;
|
float m_window_height = 0;
|
||||||
@@ -50,6 +47,14 @@ protected:
|
|||||||
Anchor m_anchor = Anchor::TOP_LEFT;
|
Anchor m_anchor = Anchor::TOP_LEFT;
|
||||||
glm::ivec2 m_offset{0, 0};
|
glm::ivec2 m_offset{0, 0};
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<Widget>>& children();
|
||||||
|
|
||||||
|
const std::vector<std::unique_ptr<Widget>>& children() const;
|
||||||
|
|
||||||
|
virtual void on_update(float dt);
|
||||||
|
virtual void on_render(Renderer& renderer);
|
||||||
|
virtual glm::vec2 compute_position() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::unique_ptr<Widget>> m_children;
|
std::vector<std::unique_ptr<Widget>> m_children;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -67,4 +67,5 @@ target_sources(${PROJECT_NAME}
|
|||||||
ui/world_ui_manager.cpp
|
ui/world_ui_manager.cpp
|
||||||
ui/main_menu_ui_manager.cpp
|
ui/main_menu_ui_manager.cpp
|
||||||
scene/main_menu_scene.cpp
|
scene/main_menu_scene.cpp
|
||||||
|
ui/column_layout.cpp
|
||||||
)
|
)
|
||||||
@@ -13,61 +13,50 @@ DebugCollector& DebugCollector::get() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DebugCollector::init(int width, int height) {
|
void DebugCollector::init(int width, int height) {
|
||||||
constexpr int SPACE = 50;
|
constexpr float SCALE = 0.6f;
|
||||||
// version_text
|
|
||||||
m_widget.set_window_size(width, height);
|
m_widget.set_window_size(width, height);
|
||||||
auto& version_text = m_widget.add_child<Label>("version", &m_widget);
|
m_widget.set_spacing(15);
|
||||||
|
m_widget.set_anchor(Anchor::TOP_LEFT);
|
||||||
|
m_widget.set_offset({0, 5});
|
||||||
|
|
||||||
|
// version_text
|
||||||
|
auto& version_text = m_widget.add_child<Label>("version");
|
||||||
std::string version{"Version: " CUBED_VERSION};
|
std::string version{"Version: " CUBED_VERSION};
|
||||||
|
|
||||||
#ifdef DEBUG_MODE
|
#ifdef DEBUG_MODE
|
||||||
version.append("-debug");
|
version.append("-debug");
|
||||||
#else
|
#else
|
||||||
version.append("-release");
|
version.append("-release");
|
||||||
#endif
|
#endif
|
||||||
version_text.set_color(Color::WHITE)
|
|
||||||
.set_text(version)
|
version_text.set_color(Color::WHITE).set_text(version).set_scale(SCALE);
|
||||||
.set_scale(0.8f)
|
|
||||||
.set_anchor(Anchor::TOP_LEFT);
|
|
||||||
version_text.set_offset({0, SPACE});
|
|
||||||
m_component.try_emplace(version_text.id(), &version_text);
|
m_component.try_emplace(version_text.id(), &version_text);
|
||||||
|
|
||||||
// fps
|
// fps
|
||||||
auto& fps_text = m_widget.add_child<Label>("fps", &version_text);
|
auto& fps_text = m_widget.add_child<Label>("fps");
|
||||||
fps_text.set_text("FPS: 0")
|
fps_text.set_text("FPS: 0").set_scale(SCALE);
|
||||||
.set_scale(0.8f)
|
|
||||||
.set_anchor(Anchor::TOP_LEFT)
|
|
||||||
.set_offset({0, SPACE});
|
|
||||||
m_component.try_emplace(fps_text.id(), &fps_text);
|
m_component.try_emplace(fps_text.id(), &fps_text);
|
||||||
|
|
||||||
// player_pos
|
// player_pos
|
||||||
auto& player_pos_text = m_widget.add_child<Label>("player_pos", &fps_text);
|
auto& player_pos_text = m_widget.add_child<Label>("player_pos");
|
||||||
player_pos_text.set_text("x: 0.00 y: 0.00 z: 0.00")
|
player_pos_text.set_text("x: 0.00 y: 0.00 z: 0.00").set_scale(SCALE);
|
||||||
.set_scale(0.8f)
|
|
||||||
.set_anchor(Anchor::TOP_LEFT)
|
|
||||||
.set_offset({0, SPACE});
|
|
||||||
m_component.try_emplace(player_pos_text.id(), &player_pos_text);
|
m_component.try_emplace(player_pos_text.id(), &player_pos_text);
|
||||||
|
|
||||||
// rendered_chunk
|
// rendered_chunk
|
||||||
auto& rendered_chunk_text =
|
auto& rendered_chunk_text = m_widget.add_child<Label>("rendered_chunk");
|
||||||
m_widget.add_child<Label>("rendered_chunk", &player_pos_text);
|
rendered_chunk_text.set_text("Rendered Chunk: 0").set_scale(SCALE);
|
||||||
rendered_chunk_text.set_text("Rendered Chunk: 0")
|
|
||||||
.set_scale(0.8f)
|
|
||||||
.set_anchor(Anchor::TOP_LEFT)
|
|
||||||
.set_offset({0, SPACE});
|
|
||||||
m_component.try_emplace(rendered_chunk_text.id(), &rendered_chunk_text);
|
m_component.try_emplace(rendered_chunk_text.id(), &rendered_chunk_text);
|
||||||
|
|
||||||
// rss
|
// rss
|
||||||
auto& rss_text = m_widget.add_child<Label>("rss", &rendered_chunk_text);
|
auto& rss_text = m_widget.add_child<Label>("rss");
|
||||||
rss_text.set_text("RSS: 0mb")
|
rss_text.set_text("RSS: 0mb").set_scale(SCALE);
|
||||||
.set_scale(0.8f)
|
|
||||||
.set_anchor(Anchor::TOP_LEFT)
|
|
||||||
.set_offset({0, SPACE});
|
|
||||||
|
|
||||||
m_component.try_emplace(rss_text.id(), &rss_text);
|
m_component.try_emplace(rss_text.id(), &rss_text);
|
||||||
|
|
||||||
// os
|
// os
|
||||||
std::string os;
|
std::string os;
|
||||||
auto& os_text = m_widget.add_child<Label>("os", &rss_text);
|
auto& os_text = m_widget.add_child<Label>("os");
|
||||||
os_text.set_scale(0.8f).set_anchor(Anchor::TOP_LEFT).set_offset({0, SPACE});
|
os_text.set_scale(SCALE);
|
||||||
if (Tools::get_os_version(os)) {
|
if (Tools::get_os_version(os)) {
|
||||||
os_text.set_text("OS: " + os);
|
os_text.set_text("OS: " + os);
|
||||||
Logger::info("System: {}", os);
|
Logger::info("System: {}", os);
|
||||||
@@ -77,48 +66,34 @@ void DebugCollector::init(int width, int height) {
|
|||||||
m_component.try_emplace(os_text.id(), &os_text);
|
m_component.try_emplace(os_text.id(), &os_text);
|
||||||
|
|
||||||
// cpu
|
// cpu
|
||||||
auto& cpu_text = m_widget.add_child<Label>("cpu", &os_text);
|
auto& cpu_text = m_widget.add_child<Label>("cpu");
|
||||||
cpu_text.set_text("CPU: " + Tools::get_cpu_info())
|
cpu_text.set_text("CPU: " + Tools::get_cpu_info()).set_scale(SCALE);
|
||||||
.set_scale(0.8f)
|
|
||||||
.set_anchor(Anchor::TOP_LEFT)
|
|
||||||
.set_offset({0, SPACE});
|
|
||||||
m_component.try_emplace(cpu_text.id(), &cpu_text);
|
m_component.try_emplace(cpu_text.id(), &cpu_text);
|
||||||
|
|
||||||
// gpu
|
// gpu
|
||||||
auto& gpu_text = m_widget.add_child<Label>("gpu", &cpu_text);
|
auto& gpu_text = m_widget.add_child<Label>("gpu");
|
||||||
gpu_text
|
gpu_text
|
||||||
.set_text(std::string{"GPU: "} +
|
.set_text(std::string{"GPU: "} +
|
||||||
reinterpret_cast<const char*>(glGetString(GL_RENDERER)))
|
reinterpret_cast<const char*>(glGetString(GL_RENDERER)))
|
||||||
.set_scale(0.7f)
|
.set_scale(SCALE);
|
||||||
.set_anchor(Anchor::TOP_LEFT)
|
|
||||||
.set_offset({0, SPACE});
|
|
||||||
m_component.try_emplace(gpu_text.id(), &gpu_text);
|
m_component.try_emplace(gpu_text.id(), &gpu_text);
|
||||||
|
|
||||||
// opengl_version
|
// opengl_version
|
||||||
auto& opengl_version_text =
|
auto& opengl_version_text = m_widget.add_child<Label>("opengl_version");
|
||||||
m_widget.add_child<Label>("opengl_version", &gpu_text);
|
|
||||||
opengl_version_text
|
opengl_version_text
|
||||||
.set_text("OpenGL: " + std::to_string(GLVersion.major) + "." +
|
.set_text("OpenGL: " + std::to_string(GLVersion.major) + "." +
|
||||||
std::to_string(GLVersion.minor))
|
std::to_string(GLVersion.minor))
|
||||||
.set_scale(0.7f)
|
.set_scale(SCALE);
|
||||||
.set_anchor(Anchor::TOP_LEFT)
|
|
||||||
.set_offset({0, SPACE});
|
|
||||||
m_component.try_emplace(opengl_version_text.id(), &opengl_version_text);
|
m_component.try_emplace(opengl_version_text.id(), &opengl_version_text);
|
||||||
|
|
||||||
// biome
|
// biome
|
||||||
auto& biome_text = m_widget.add_child<Label>("biome", &opengl_version_text);
|
auto& biome_text = m_widget.add_child<Label>("biome");
|
||||||
biome_text.set_text("Biome: ")
|
biome_text.set_text("Biome: ").set_scale(SCALE);
|
||||||
.set_scale(0.8f)
|
|
||||||
.set_anchor(Anchor::TOP_LEFT)
|
|
||||||
.set_offset({0, SPACE});
|
|
||||||
m_component.try_emplace(biome_text.id(), &biome_text);
|
m_component.try_emplace(biome_text.id(), &biome_text);
|
||||||
|
|
||||||
// speed
|
// speed
|
||||||
auto& speed_text = m_widget.add_child<Label>("speed", &biome_text);
|
auto& speed_text = m_widget.add_child<Label>("speed");
|
||||||
speed_text.set_text("Speed: 0 m/s")
|
speed_text.set_text("Speed: 0 m/s").set_scale(SCALE);
|
||||||
.set_scale(0.8f)
|
|
||||||
.set_anchor(Anchor::TOP_LEFT)
|
|
||||||
.set_offset({0, SPACE});
|
|
||||||
m_component.try_emplace(speed_text.id(), &speed_text);
|
m_component.try_emplace(speed_text.id(), &speed_text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
24
src/ui/column_layout.cpp
Normal file
24
src/ui/column_layout.cpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#include "Cubed/ui/column_layout.hpp"
|
||||||
|
|
||||||
|
namespace Cubed {
|
||||||
|
ColumnLayout::ColumnLayout(Widget* parent) : Widget(parent) {}
|
||||||
|
ColumnLayout::~ColumnLayout() {}
|
||||||
|
|
||||||
|
void ColumnLayout::update(float dt) {
|
||||||
|
Widget::update(dt);
|
||||||
|
layout();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ColumnLayout::set_spacing(int spacing) { m_spacing = spacing; }
|
||||||
|
|
||||||
|
void ColumnLayout::layout() {
|
||||||
|
auto& children = Widget::children();
|
||||||
|
int y = 0;
|
||||||
|
for (auto& child : children) {
|
||||||
|
child->set_offset({0, y});
|
||||||
|
child->set_anchor(Anchor::TOP_LEFT);
|
||||||
|
y += child->height() + m_spacing;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Cubed
|
||||||
@@ -25,6 +25,12 @@ void Widget::render(Renderer& renderer) {
|
|||||||
void Widget::on_update(float) {}
|
void Widget::on_update(float) {}
|
||||||
void Widget::on_render(Renderer&) {}
|
void Widget::on_render(Renderer&) {}
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<Widget>>& Widget::children() { return m_children; }
|
||||||
|
|
||||||
|
const std::vector<std::unique_ptr<Widget>>& Widget::children() const {
|
||||||
|
return m_children;
|
||||||
|
}
|
||||||
|
|
||||||
glm::vec2 Widget::compute_position() const {
|
glm::vec2 Widget::compute_position() const {
|
||||||
glm::vec2 pos{0, 0};
|
glm::vec2 pos{0, 0};
|
||||||
float parent_w;
|
float parent_w;
|
||||||
|
|||||||
Reference in New Issue
Block a user