X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fui%2Fwidgets.cpp;h=554a6854876f22f0f5bd9425046342a1251318d4;hb=a19fdf3d9f0d7ecbf6eeeec817856d85049a8336;hp=6ce92dbc2c894d029db36c33aa6d630b7fe2e82d;hpb=fd08d1defb5c42d2847a9001e1921898e3d526bf;p=blobs.git diff --git a/src/ui/widgets.cpp b/src/ui/widgets.cpp index 6ce92db..554a685 100644 --- a/src/ui/widgets.cpp +++ b/src/ui/widgets.cpp @@ -7,6 +7,8 @@ #include "../graphics/Font.hpp" #include "../graphics/Viewport.hpp" +#include +#include #include @@ -18,8 +20,7 @@ Label::Label(const graphics::Font &f) , 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() { @@ -27,7 +28,7 @@ Label::~Label() { Label *Label::Text(const std::string &t) { if (text != t) { - dirty = true; + BreakLayout(); } text = t; return this; @@ -35,7 +36,7 @@ Label *Label::Text(const std::string &t) { Label *Label::Font(const graphics::Font &f) { if (font != &f) { - dirty = true; + BreakLayout(); } font = &f; return this; @@ -55,12 +56,11 @@ glm::vec2 Label::Size() { if (text.empty()) { return glm::vec2(0.0f); } - Update(); return tex.Size(); } void Label::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept { - Update(); + if (text.empty()) return; glm::vec2 size = Size(); assets.shaders.alpha_sprite.Activate(); @@ -72,10 +72,9 @@ void Label::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept { assets.shaders.alpha_sprite.DrawRect(); } -void Label::Update() { - if (!dirty) return; +void Label::FixLayout() { + if (text.empty()) return; font->Render(text, tex); - dirty = false; } @@ -110,10 +109,12 @@ void Meter::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept { } if (value > 0.0f) { + glm::vec2 bottom_right = Position() + fullsize - glm::vec2(border) - padding; + bottom_right.x -= size.x * (1.0f - value); assets.shaders.canvas.SetColor(fill_color); assets.shaders.canvas.FillRect( Position() + glm::vec2(border) + padding, - Position() + fullsize - glm::vec2(border) - padding + bottom_right ); } } @@ -132,22 +133,15 @@ Panel::~Panel() { } Panel *Panel::Add(Widget *w) { - std::unique_ptr 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(w)); + BreakLayout(); return this; } Panel *Panel::Clear() { widgets.clear(); - size = glm::vec2(0.0f); + BreakLayout(); return this; } @@ -158,25 +152,32 @@ Panel *Panel::Background(const glm::vec4 &c) { 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; - Relayout(); + BreakLayout(); return this; } glm::vec2 Panel::Size() { - return (2.0f * padding) + glm::vec2(0.0f, (widgets.size() - 1) * spacing) + size; + glm::vec2 space(0.0f); + space[dir] = (widgets.size() - 1) * spacing; + return (2.0f * padding) + space + size; } -void Panel::Relayout() { +void Panel::FixLayout() { + for (auto &w : widgets) { + w->Layout(); + } size = glm::vec2(0.0f); if (dir == HORIZONTAL) { for (auto &w : widgets) { @@ -201,9 +202,7 @@ void Panel::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept { assets.shaders.canvas.FillRect(Position(), Position() + Size()); } - glm::vec2 cursor = Position(); - cursor.x += padding.x; - cursor.y += padding.y; + glm::vec2 cursor = Position() + padding; for (auto &w : widgets) { w->Position(cursor)->ZIndex(ZIndex() + 1.0f); w->Draw(assets, viewport); @@ -213,12 +212,37 @@ void Panel::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept { 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; + } +} + } }