]> git.localhorst.tv Git - blobs.git/blob - src/ui/widgets.cpp
remote outdated TODO
[blobs.git] / src / ui / widgets.cpp
1 #include "Label.hpp"
2 #include "Meter.hpp"
3 #include "Panel.hpp"
4 #include "Widget.hpp"
5
6 #include "../app/Assets.hpp"
7 #include "../graphics/Font.hpp"
8 #include "../graphics/Viewport.hpp"
9
10 #include <glm/gtx/transform.hpp>
11
12
13 namespace blobs {
14 namespace ui {
15
16 Label::Label(const graphics::Font &f)
17 : font(&f)
18 , text()
19 , tex()
20 , fg_color(0.0f, 0.0f, 0.0f, 1.0f)
21 , bg_color(0.0f, 0.0f, 0.0f, 0.0f)
22 , dirty(true) {
23 }
24
25 Label::~Label() {
26 }
27
28 Label *Label::Text(const std::string &t) {
29         if (text != t) {
30                 dirty = true;
31         }
32         text = t;
33         return this;
34 }
35
36 Label *Label::Font(const graphics::Font &f) {
37         if (font != &f) {
38                 dirty = true;
39         }
40         font = &f;
41         return this;
42 }
43
44 Label *Label::Foreground(const glm::vec4 &c) {
45         fg_color = c;
46         return this;
47 }
48
49 Label *Label::Background(const glm::vec4 &c) {
50         bg_color = c;
51         return this;
52 }
53
54 glm::vec2 Label::Size() {
55         if (text.empty()) {
56                 return glm::vec2(0.0f);
57         }
58         Update();
59         return tex.Size();
60 }
61
62 void Label::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
63         Update();
64         glm::vec2 size = Size();
65
66         assets.shaders.alpha_sprite.Activate();
67         assets.shaders.alpha_sprite.SetM(glm::translate(glm::vec3(Position() + (size * 0.5f), -ZIndex()))
68                 * glm::scale(glm::vec3(size.x, size.y, 1.0f)));
69         assets.shaders.alpha_sprite.SetTexture(tex);
70         assets.shaders.alpha_sprite.SetFgColor(fg_color);
71         assets.shaders.alpha_sprite.SetBgColor(bg_color);
72         assets.shaders.alpha_sprite.DrawRect();
73 }
74
75 void Label::Update() {
76         if (!dirty) return;
77         font->Render(text, tex);
78         dirty = false;
79 }
80
81
82 Meter::Meter()
83 : fill_color(1.0f)
84 , border_color(1.0f)
85 , size(3.0f)
86 , padding(1.0f)
87 , border(1.0f)
88 , value(0.0f) {
89 }
90
91 Meter::~Meter() {
92 }
93
94 glm::vec2 Meter::Size() {
95         return size + (2.0f * padding) + glm::vec2(2.0f * border);
96 }
97
98 void Meter::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
99         glm::vec2 fullsize = Size();
100         assets.shaders.canvas.Activate();
101         assets.shaders.canvas.ZIndex(ZIndex());
102
103         if (border > 0.0f) {
104                 assets.shaders.canvas.SetColor(border_color);
105                 assets.shaders.canvas.DrawRect(
106                         Position() + glm::vec2(border * 0.5f),
107                         Position() + fullsize - glm::vec2(border * 0.5f),
108                         border
109                 );
110         }
111
112         if (value > 0.0f) {
113                 glm::vec2 bottom_right = Position() + fullsize - glm::vec2(border) - padding;
114                 bottom_right.x -= size.x * (1.0f - value);
115                 assets.shaders.canvas.SetColor(fill_color);
116                 assets.shaders.canvas.FillRect(
117                         Position() + glm::vec2(border) + padding,
118                         bottom_right
119                 );
120         }
121 }
122
123
124 Panel::Panel()
125 : widgets()
126 , bg_color(0.0f, 0.0f, 0.0f, 0.0f)
127 , padding(0.0f)
128 , spacing(0.0f)
129 , dir(VERTICAL)
130 , size(0.0f, 0.0f) {
131 }
132
133 Panel::~Panel() {
134 }
135
136 Panel *Panel::Add(Widget *w) {
137         std::unique_ptr<Widget> widget(w);
138         glm::vec2 wsize = widget->Size();
139         if (dir == HORIZONTAL) {
140                 size.x += wsize.x;
141                 size.y = std::max(size.y, wsize.y);
142         } else {
143                 size.x = std::max(size.x, wsize.x);
144                 size.y += wsize.y;
145         }
146         widgets.emplace_back(std::move(widget));
147         return this;
148 }
149
150 Panel *Panel::Clear() {
151         widgets.clear();
152         size = glm::vec2(0.0f);
153         return this;
154 }
155
156 Panel *Panel::Background(const glm::vec4 &c) {
157         bg_color = c;
158         return this;
159 }
160
161 Panel *Panel::Padding(const glm::vec2 &p) {
162         padding = p;
163         return this;
164 }
165
166 Panel *Panel::Spacing(float s) {
167         spacing = s;
168         return this;
169 }
170
171 Panel *Panel::Direction(Dir d) {
172         dir = d;
173         Relayout();
174         return this;
175 }
176
177 glm::vec2 Panel::Size() {
178         return (2.0f * padding) + glm::vec2(0.0f, (widgets.size() - 1) * spacing) + size;
179 }
180
181 void Panel::Relayout() {
182         size = glm::vec2(0.0f);
183         if (dir == HORIZONTAL) {
184                 for (auto &w : widgets) {
185                         glm::vec2 wsize = w->Size();
186                         size.x += wsize.x;
187                         size.y = std::max(size.y, wsize.y);
188                 }
189         } else {
190                 for (auto &w : widgets) {
191                         glm::vec2 wsize = w->Size();
192                         size.x = std::max(size.x, wsize.x);
193                         size.y += wsize.y;
194                 }
195         }
196 }
197
198 void Panel::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
199         if (bg_color.a > 0.0f) {
200                 assets.shaders.canvas.Activate();
201                 assets.shaders.canvas.ZIndex(ZIndex());
202                 assets.shaders.canvas.SetColor(bg_color);
203                 assets.shaders.canvas.FillRect(Position(), Position() + Size());
204         }
205
206         glm::vec2 cursor = Position();
207         cursor.x += padding.x;
208         cursor.y += padding.y;
209         for (auto &w : widgets) {
210                 w->Position(cursor)->ZIndex(ZIndex() + 1.0f);
211                 w->Draw(assets, viewport);
212                 cursor[dir] += w->Size()[dir] + spacing;
213         }
214 }
215
216
217 Widget::Widget()
218 : pos(0.0f)
219 , z_index(1.0f)  {
220 }
221
222 Widget::~Widget() {
223 }
224
225 }
226 }