1 #include "FixedText.hpp"
2 #include "MessageBox.hpp"
3 #include "Progress.hpp"
6 #include "../graphics/Font.hpp"
7 #include "../graphics/Viewport.hpp"
14 MessageBox::MessageBox(const Font &f)
19 , adv(0.0f, font.LineSkip(), 0.0f)
20 , bg(1.0f, 1.0f, 1.0f, 0.0f)
21 , fg(1.0f, 1.0f, 1.0f, 1.0f)
22 , grav(Gravity::NORTH_WEST) {
26 void MessageBox::Position(const glm::vec3 &p, Gravity g) noexcept {
29 if (get_y(g) == Align::END) {
30 adv.y = -font.LineSkip();
32 adv.y = font.LineSkip();
34 for (Text &txt : lines) {
39 void MessageBox::PushLine(const char *text) {
40 lines.emplace_front();
41 Text &txt = lines.front();
45 while (lines.size() > max_lines) {
50 void MessageBox::Render(Viewport &viewport) noexcept {
51 BlendedSprite &prog = viewport.SpriteProgram();
54 viewport.SetCursor(pos, grav);
55 for (Text &txt : lines) {
56 prog.SetM(viewport.Cursor());
58 viewport.MoveCursor(adv);
63 Progress::Progress(Font &font) noexcept
66 , tpl("%d/%d (%d%%)") {
72 char buf[128] = { '\0' };
76 void Progress::Update(int current, int total) {
77 std::snprintf(buf, sizeof(buf), tpl, current, total, current * 100 / total);
81 void Progress::Render(Viewport &viewport) noexcept {
82 text.Render(viewport);
90 , pivot(Gravity::NORTH_WEST)
95 FixedText::FixedText() noexcept
97 , bg(1.0f, 1.0f, 1.0f, 0.0f)
98 , fg(1.0f, 1.0f, 1.0f, 1.0f)
100 , grav(Gravity::NORTH_WEST)
105 void Text::Set(const Font &font, const char *text) {
106 font.Render(text, tex);
107 size = font.TextSize(text);
113 SpriteModel::Buffer sprite_buf;
117 void Text::Update() {
118 sprite_buf.LoadRect(size.x, size.y, align(pivot, size));
119 sprite.Update(sprite_buf);
123 void FixedText::Render(Viewport &viewport) noexcept {
124 BlendedSprite &prog = viewport.SpriteProgram();
125 viewport.SetCursor(pos, grav);
126 prog.SetM(viewport.Cursor());
129 Text::Render(viewport);
132 void Text::Render(Viewport &viewport) noexcept {
136 BlendedSprite &prog = viewport.SpriteProgram();
137 prog.SetTexture(tex);