#include "../graphics/Font.hpp"
#include "../graphics/Viewport.hpp"
+#include <iomanip>
+#include <sstream>
#include <glm/gtx/transform.hpp>
, 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 {
- Update();
+ if (text.empty()) return;
glm::vec2 size = Size();
assets.shaders.alpha_sprite.Activate();
assets.shaders.alpha_sprite.DrawRect();
}
-void Label::Update() {
- if (!dirty) return;
+void Label::FixLayout() {
+ if (text.empty()) return;
font->Render(text, tex);
- dirty = false;
}
}
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
);
}
}
}
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;
- 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) {
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);
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;
+ }
+}
+
}
}