From b49cc8c88caf7d69b35b50e23a40528e71306ade Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 20 Oct 2015 12:11:13 +0200 Subject: [PATCH] uniform background for message box --- src/ui/MessageBox.hpp | 10 +++++++++- src/ui/Text.hpp | 2 ++ src/ui/widgets.cpp | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/ui/MessageBox.hpp b/src/ui/MessageBox.hpp index 2191627..b5a82c8 100644 --- a/src/ui/MessageBox.hpp +++ b/src/ui/MessageBox.hpp @@ -3,6 +3,7 @@ #include "Text.hpp" #include "../graphics/align.hpp" +#include "../graphics/PrimitiveMesh.hpp" #include #include @@ -22,7 +23,7 @@ public: void Position(const glm::vec3 &, Gravity) noexcept; void Foreground(const glm::vec4 &col) noexcept { fg = col; } - void Background(const glm::vec4 &col) noexcept { bg = col; } + void Background(const glm::vec4 &col) noexcept { bg = col; dirty = true; } void PushLine(const char *); void PushLine(const std::string &l) { @@ -31,6 +32,9 @@ public: void Render(Viewport &) noexcept; +private: + void Recalc(); + private: const Font &font; std::deque lines; @@ -38,11 +42,15 @@ private: glm::vec3 pos; glm::vec3 adv; + glm::vec2 size; glm::vec4 bg; glm::vec4 fg; + PrimitiveMesh bg_mesh; + Gravity grav; + bool dirty; }; diff --git a/src/ui/Text.hpp b/src/ui/Text.hpp index bdeb304..84a6190 100644 --- a/src/ui/Text.hpp +++ b/src/ui/Text.hpp @@ -29,6 +29,8 @@ public: dirty = true; } + const glm::vec2 &Size() const noexcept { return size; } + void Render(Viewport &) noexcept; private: diff --git a/src/ui/widgets.cpp b/src/ui/widgets.cpp index b19dbcf..533703f 100644 --- a/src/ui/widgets.cpp +++ b/src/ui/widgets.cpp @@ -19,7 +19,8 @@ MessageBox::MessageBox(const Font &f) , adv(0.0f, font.LineSkip(), 0.0f) , bg(1.0f, 1.0f, 1.0f, 0.0f) , fg(1.0f, 1.0f, 1.0f, 1.0f) -, grav(Gravity::NORTH_WEST) { +, grav(Gravity::NORTH_WEST) +, dirty(true) { } @@ -34,6 +35,7 @@ void MessageBox::Position(const glm::vec3 &p, Gravity g) noexcept { for (Text &txt : lines) { txt.Pivot(g); } + dirty = true; } void MessageBox::PushLine(const char *text) { @@ -45,13 +47,41 @@ void MessageBox::PushLine(const char *text) { while (lines.size() > max_lines) { lines.pop_back(); } + dirty = true; +} + +namespace { + +PrimitiveMesh::Buffer bg_buf; + +} + +void MessageBox::Recalc() { + size = glm::vec2(0.0f, 0.0f); + for (const Text &line : lines) { + size.x = std::max(size.x, line.Size().x); + size.y += line.Size().y; + } + bg_buf.FillRect(size.x, size.y, bg, align(grav, size)); + bg_mesh.Update(bg_buf); + bg_buf.Clear(); + dirty = false; } void MessageBox::Render(Viewport &viewport) noexcept { + viewport.SetCursor(pos, grav); + if (bg.a > std::numeric_limits::epsilon()) { + if (dirty) { + Recalc(); + } + PlainColor &prog = viewport.HUDColorProgram(); + prog.SetM(viewport.Cursor()); + bg_mesh.DrawTriangles(); + viewport.MoveCursor(glm::vec3(0.0f, 0.0f, -1.0f)); + } BlendedSprite &prog = viewport.SpriteProgram(); - prog.SetBG(bg); + prog.SetBG(glm::vec4(0.0f)); prog.SetFG(fg); - viewport.SetCursor(pos, grav); for (Text &txt : lines) { prog.SetM(viewport.Cursor()); txt.Render(viewport); -- 2.39.2