void Draw(app::Assets &, graphics::Viewport &) noexcept override;
private:
- void Update();
+ void FixLayout() override;
private:
const graphics::Font *font;
graphics::Texture tex;
glm::vec4 fg_color;
glm::vec4 bg_color;
- bool dirty;
};
~Meter() override;
public:
- Meter *Size(const glm::vec2 &s) noexcept { size = s; return this; }
- Meter *Padding(const glm::vec2 &p) noexcept { padding = p; return this; }
- Meter *Border(float b) noexcept { border = b; return this; }
+ Meter *Size(const glm::vec2 &s) noexcept { size = s; BreakParentLayout(); return this; }
+ Meter *Padding(const glm::vec2 &p) noexcept { padding = p; BreakParentLayout(); return this; }
+ Meter *Border(float b) noexcept { border = b; BreakParentLayout(); return this; }
Meter *FillColor(const glm::vec4 &c) noexcept { fill_color = c; return this; }
Meter *BorderColor(const glm::vec4 &c) noexcept { border_color = c; return this; }
Panel *Direction(Dir);
glm::vec2 Size() override;
- void Layout();
+ void FixLayout();
void Draw(app::Assets &, graphics::Viewport &) noexcept override;
private:
Widget &operator =(Widget &&) = delete;
public:
+ void SetParent(Widget &) noexcept;
+ bool HasParent() const noexcept { return parent; }
+ Widget &GetParent() noexcept { return *parent; }
+ const Widget &GetParent() const noexcept { return *parent; }
+
Widget *Position(const glm::vec2 &p) noexcept { pos = p; return this; }
const glm::vec2 &Position() const noexcept { return pos; }
Widget *ZIndex(float z) noexcept { z_index = z; return this; }
float ZIndex() const noexcept { return z_index; }
+ bool DirtyLayout() const noexcept { return dirty_layout; }
+ void BreakLayout() noexcept;
+ void BreakParentLayout() noexcept;
+ void Layout();
+
virtual glm::vec2 Size() = 0;
virtual void Draw(app::Assets &, graphics::Viewport &) noexcept = 0;
private:
+ virtual void FixLayout() { }
+
+private:
+ Widget *parent;
glm::vec2 pos;
float z_index;
+ bool dirty_layout;
};
->Spacing(2)
->Direction(Panel::VERTICAL);
Panel *stat_meter_panel = new Panel;
+ stat_label[0]->Layout();
+ stats[0]->Layout();
stat_meter_panel
->Spacing(stat_label[0]->Size().y - stats[0]->Size().y + 2)
->Direction(Panel::VERTICAL);
const glm::vec2 margin(20.0f);
panel.Position(glm::vec2(viewport.Width() - margin.x - panel.Size().x, margin.y));
+ panel.Layout();
panel.Draw(assets, viewport);
}
const glm::vec2 margin(20.0f);
panel.Position(glm::vec2(margin.x, margin.y));
+ panel.Layout();
panel.Draw(sim.Assets(), viewport);
}
const glm::vec2 margin(20.0f);
panel.Position(glm::vec2(margin.x, viewport.Height() - margin.y - panel.Size().y));
+ panel.Layout();
panel.Draw(sim.Assets(), viewport);
}
, text()
, tex()
, fg_color(0.0f, 0.0f, 0.0f, 1.0f)
-, bg_color(0.0f, 0.0f, 0.0f, 0.0f)
-, dirty(true) {
+, bg_color(0.0f, 0.0f, 0.0f, 0.0f) {
}
Label::~Label() {
Label *Label::Text(const std::string &t) {
if (text != t) {
- dirty = true;
+ BreakLayout();
}
text = t;
return this;
Label *Label::Font(const graphics::Font &f) {
if (font != &f) {
- dirty = true;
+ BreakLayout();
}
font = &f;
return this;
if (text.empty()) {
return glm::vec2(0.0f);
}
- Update();
return tex.Size();
}
void Label::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
if (text.empty()) return;
- Update();
glm::vec2 size = Size();
assets.shaders.alpha_sprite.Activate();
assets.shaders.alpha_sprite.DrawRect();
}
-void Label::Update() {
- if (!dirty || text.empty()) return;
+void Label::FixLayout() {
+ if (text.empty()) return;
font->Render(text, tex);
- dirty = false;
}
}
Panel *Panel::Add(Widget *w) {
- std::unique_ptr<Widget> widget(w);
- glm::vec2 wsize = widget->Size();
- if (dir == HORIZONTAL) {
- size.x += wsize.x;
- size.y = std::max(size.y, wsize.y);
- } else {
- size.x = std::max(size.x, wsize.x);
- size.y += wsize.y;
- }
- widgets.emplace_back(std::move(widget));
+ w->SetParent(*this);
+ widgets.emplace_back(std::unique_ptr<Widget>(w));
+ BreakLayout();
return this;
}
Panel *Panel::Clear() {
widgets.clear();
- size = glm::vec2(0.0f);
+ BreakLayout();
return this;
}
Panel *Panel::Padding(const glm::vec2 &p) {
padding = p;
+ BreakParentLayout();
return this;
}
Panel *Panel::Spacing(float s) {
spacing = s;
+ BreakParentLayout();
return this;
}
Panel *Panel::Direction(Dir d) {
dir = d;
- Layout();
+ BreakLayout();
return this;
}
return (2.0f * padding) + space + size;
}
-void Panel::Layout() {
+void Panel::FixLayout() {
+ for (auto &w : widgets) {
+ w->Layout();
+ }
size = glm::vec2(0.0f);
if (dir == HORIZONTAL) {
for (auto &w : widgets) {
}
void Panel::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
- // TODO: separate draw and layout, it's inefficient and the wrong tree order anyway
- Layout();
if (bg_color.a > 0.0f) {
assets.shaders.canvas.Activate();
assets.shaders.canvas.ZIndex(ZIndex());
Widget::Widget()
-: pos(0.0f)
-, z_index(1.0f) {
+: parent(nullptr)
+, pos(0.0f)
+, z_index(1.0f)
+, dirty_layout(false) {
}
Widget::~Widget() {
}
+void Widget::SetParent(Widget &p) noexcept {
+ parent = &p;
+}
+
+void Widget::BreakLayout() noexcept {
+ if (dirty_layout) return;
+ dirty_layout = true;
+ BreakParentLayout();
+}
+
+void Widget::BreakParentLayout() noexcept {
+ if (HasParent()) {
+ GetParent().BreakLayout();
+ }
+}
+
+void Widget::Layout() {
+ if (dirty_layout) {
+ FixLayout();
+ dirty_layout = false;
+ }
+}
+
}
}