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
|
#pragma once
|
||||||
#include "Cubed/ui/widget.hpp"
|
#include "Cubed/ui/widget.hpp"
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
|
|
||||||
|
enum class ColumnLayoutAnchor { LEFT, CENTER, RIGHT };
|
||||||
|
|
||||||
class ColumnLayout : public Widget {
|
class ColumnLayout : public Widget {
|
||||||
public:
|
public:
|
||||||
ColumnLayout(const ColumnLayout&) = delete;
|
ColumnLayout(const ColumnLayout&) = delete;
|
||||||
@@ -18,10 +21,13 @@ public:
|
|||||||
ColumnLayout& set_spacing(int spacing);
|
ColumnLayout& set_spacing(int spacing);
|
||||||
// No need for parent node pointer; do not modify children's anchors and
|
// No need for parent node pointer; do not modify children's anchors and
|
||||||
// scale.
|
// scale.
|
||||||
|
ColumnLayout& set_child_anchor(ColumnLayoutAnchor anchor);
|
||||||
void layout();
|
void layout();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_spacing = 0;
|
int m_spacing = 0;
|
||||||
|
float m_content_height = 0;
|
||||||
|
float m_content_width = 0;
|
||||||
|
ColumnLayoutAnchor m_layout_anchor = ColumnLayoutAnchor::CENTER;
|
||||||
};
|
};
|
||||||
} // namespace Cubed
|
} // namespace Cubed
|
||||||
@@ -19,7 +19,7 @@ void DebugCollector::init(int width, int height) {
|
|||||||
m_widget.set_spacing(15);
|
m_widget.set_spacing(15);
|
||||||
m_widget.set_anchor(Anchor::TOP_LEFT);
|
m_widget.set_anchor(Anchor::TOP_LEFT);
|
||||||
m_widget.set_offset({0, 5});
|
m_widget.set_offset({0, 5});
|
||||||
|
m_widget.set_child_anchor(ColumnLayoutAnchor::LEFT);
|
||||||
// version_text
|
// version_text
|
||||||
auto& version_text = m_widget.add_child<Label>("version");
|
auto& version_text = m_widget.add_child<Label>("version");
|
||||||
std::string version{"Version: " CUBED_VERSION};
|
std::string version{"Version: " CUBED_VERSION};
|
||||||
|
|||||||
@@ -9,32 +9,39 @@ void ColumnLayout::update(float dt) {
|
|||||||
Widget::update(dt);
|
Widget::update(dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
float ColumnLayout::width() const {
|
float ColumnLayout::width() const { return m_content_width; }
|
||||||
if (m_parent) {
|
float ColumnLayout::height() const { return m_content_height; }
|
||||||
return m_parent->width();
|
|
||||||
}
|
|
||||||
return m_window_width;
|
|
||||||
}
|
|
||||||
float ColumnLayout::height() const {
|
|
||||||
if (m_parent) {
|
|
||||||
return m_parent->height();
|
|
||||||
}
|
|
||||||
return m_window_height;
|
|
||||||
}
|
|
||||||
|
|
||||||
ColumnLayout& ColumnLayout::set_spacing(int spacing) {
|
ColumnLayout& ColumnLayout::set_spacing(int spacing) {
|
||||||
m_spacing = spacing;
|
m_spacing = spacing;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
ColumnLayout& ColumnLayout::set_child_anchor(ColumnLayoutAnchor anchor) {
|
||||||
|
m_layout_anchor = anchor;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
void ColumnLayout::layout() {
|
void ColumnLayout::layout() {
|
||||||
auto& children = Widget::children();
|
auto& children = Widget::children();
|
||||||
int y = 0;
|
int y = 0;
|
||||||
for (auto& child : children) {
|
Anchor anchor = Anchor::TOP_LEFT;
|
||||||
child->set_offset({0, y});
|
switch (m_layout_anchor) {
|
||||||
child->set_anchor(m_anchor);
|
case ColumnLayoutAnchor::LEFT:
|
||||||
y += child->height() + m_spacing;
|
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
|
} // namespace Cubed
|
||||||
@@ -21,6 +21,11 @@ void PauseMenuUIManager::init() {
|
|||||||
auto& layout = rect->add_child<ColumnLayout>();
|
auto& layout = rect->add_child<ColumnLayout>();
|
||||||
layout.set_anchor(Anchor::CENTER);
|
layout.set_anchor(Anchor::CENTER);
|
||||||
layout.set_spacing(20);
|
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>();
|
auto& button = layout.add_child<Button>();
|
||||||
button.set_default_image(texture_manager);
|
button.set_default_image(texture_manager);
|
||||||
|
|||||||
@@ -37,7 +37,10 @@ void SettingsUI::init() {
|
|||||||
slider.set_track_image("texture/ui/slider_track001.png",
|
slider.set_track_image("texture/ui/slider_track001.png",
|
||||||
texture_manager);
|
texture_manager);
|
||||||
};
|
};
|
||||||
|
{
|
||||||
|
auto& title = layout.add_child<Label>();
|
||||||
|
title.set_text("Setting");
|
||||||
|
}
|
||||||
{
|
{
|
||||||
auto& fov = layout.add_child<Slider>();
|
auto& fov = layout.add_child<Slider>();
|
||||||
fov.set_slider(&v.fov, 20.0f, 120.0f);
|
fov.set_slider(&v.fov, 20.0f, 120.0f);
|
||||||
|
|||||||
Reference in New Issue
Block a user