mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
feat(ui): add child anchor and content sizing to column layout
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
#include "Cubed/ui/widget.hpp"
|
||||
namespace Cubed {
|
||||
|
||||
enum class ColumnLayoutAnchor { LEFT, CENTER, RIGHT };
|
||||
|
||||
class ColumnLayout : public Widget {
|
||||
public:
|
||||
ColumnLayout(const ColumnLayout&) = delete;
|
||||
@@ -18,10 +21,13 @@ public:
|
||||
ColumnLayout& set_spacing(int spacing);
|
||||
// No need for parent node pointer; do not modify children's anchors and
|
||||
// scale.
|
||||
|
||||
ColumnLayout& set_child_anchor(ColumnLayoutAnchor anchor);
|
||||
void layout();
|
||||
|
||||
private:
|
||||
int m_spacing = 0;
|
||||
float m_content_height = 0;
|
||||
float m_content_width = 0;
|
||||
ColumnLayoutAnchor m_layout_anchor = ColumnLayoutAnchor::CENTER;
|
||||
};
|
||||
} // namespace Cubed
|
||||
@@ -19,7 +19,7 @@ void DebugCollector::init(int width, int height) {
|
||||
m_widget.set_spacing(15);
|
||||
m_widget.set_anchor(Anchor::TOP_LEFT);
|
||||
m_widget.set_offset({0, 5});
|
||||
|
||||
m_widget.set_child_anchor(ColumnLayoutAnchor::LEFT);
|
||||
// version_text
|
||||
auto& version_text = m_widget.add_child<Label>("version");
|
||||
std::string version{"Version: " CUBED_VERSION};
|
||||
|
||||
@@ -9,32 +9,39 @@ void ColumnLayout::update(float dt) {
|
||||
Widget::update(dt);
|
||||
}
|
||||
|
||||
float ColumnLayout::width() const {
|
||||
if (m_parent) {
|
||||
return m_parent->width();
|
||||
}
|
||||
return m_window_width;
|
||||
}
|
||||
float ColumnLayout::height() const {
|
||||
if (m_parent) {
|
||||
return m_parent->height();
|
||||
}
|
||||
return m_window_height;
|
||||
}
|
||||
float ColumnLayout::width() const { return m_content_width; }
|
||||
float ColumnLayout::height() const { return m_content_height; }
|
||||
|
||||
ColumnLayout& ColumnLayout::set_spacing(int spacing) {
|
||||
m_spacing = spacing;
|
||||
return *this;
|
||||
}
|
||||
|
||||
ColumnLayout& ColumnLayout::set_child_anchor(ColumnLayoutAnchor anchor) {
|
||||
m_layout_anchor = anchor;
|
||||
return *this;
|
||||
}
|
||||
void ColumnLayout::layout() {
|
||||
auto& children = Widget::children();
|
||||
int y = 0;
|
||||
for (auto& child : children) {
|
||||
child->set_offset({0, y});
|
||||
child->set_anchor(m_anchor);
|
||||
y += child->height() + m_spacing;
|
||||
Anchor anchor = Anchor::TOP_LEFT;
|
||||
switch (m_layout_anchor) {
|
||||
case ColumnLayoutAnchor::LEFT:
|
||||
anchor = Anchor::TOP_LEFT;
|
||||
break;
|
||||
case ColumnLayoutAnchor::CENTER:
|
||||
anchor = Anchor::TOP_CENTER;
|
||||
break;
|
||||
case ColumnLayoutAnchor::RIGHT:
|
||||
anchor = Anchor::TOP_RIGHT;
|
||||
break;
|
||||
}
|
||||
for (auto& child : children) {
|
||||
child->set_anchor(anchor);
|
||||
child->set_offset({0, y});
|
||||
y += child->height() + m_spacing;
|
||||
m_content_width = std::max(m_content_width, child->width());
|
||||
}
|
||||
m_content_height = children.empty() ? 0 : (y - m_spacing);
|
||||
}
|
||||
|
||||
} // namespace Cubed
|
||||
@@ -21,6 +21,11 @@ void PauseMenuUIManager::init() {
|
||||
auto& layout = rect->add_child<ColumnLayout>();
|
||||
layout.set_anchor(Anchor::CENTER);
|
||||
layout.set_spacing(20);
|
||||
{
|
||||
auto& title = layout.add_child<Label>();
|
||||
title.set_text("Pause Menu");
|
||||
title.set_scale(0.75f);
|
||||
}
|
||||
{
|
||||
auto& button = layout.add_child<Button>();
|
||||
button.set_default_image(texture_manager);
|
||||
|
||||
@@ -37,7 +37,10 @@ void SettingsUI::init() {
|
||||
slider.set_track_image("texture/ui/slider_track001.png",
|
||||
texture_manager);
|
||||
};
|
||||
|
||||
{
|
||||
auto& title = layout.add_child<Label>();
|
||||
title.set_text("Setting");
|
||||
}
|
||||
{
|
||||
auto& fov = layout.add_child<Slider>();
|
||||
fov.set_slider(&v.fov, 20.0f, 120.0f);
|
||||
|
||||
Reference in New Issue
Block a user