6 #include "../app/Assets.hpp"
7 #include "../graphics/Font.hpp"
8 #include "../graphics/Viewport.hpp"
12 #include <glm/gtx/transform.hpp>
18 Label::Label(const graphics::Font &f)
22 , fg_color(0.0f, 0.0f, 0.0f, 1.0f)
23 , bg_color(0.0f, 0.0f, 0.0f, 0.0f)
30 Label *Label::Text(const std::string &t) {
38 Label *Label::Font(const graphics::Font &f) {
46 Label *Label::Foreground(const glm::vec4 &c) {
51 Label *Label::Background(const glm::vec4 &c) {
56 glm::vec2 Label::Size() {
58 return glm::vec2(0.0f);
64 void Label::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
65 if (text.empty()) return;
67 glm::vec2 size = Size();
69 assets.shaders.alpha_sprite.Activate();
70 assets.shaders.alpha_sprite.SetM(glm::translate(glm::vec3(Position() + (size * 0.5f), -ZIndex()))
71 * glm::scale(glm::vec3(size.x, size.y, 1.0f)));
72 assets.shaders.alpha_sprite.SetTexture(tex);
73 assets.shaders.alpha_sprite.SetFgColor(fg_color);
74 assets.shaders.alpha_sprite.SetBgColor(bg_color);
75 assets.shaders.alpha_sprite.DrawRect();
78 void Label::Update() {
79 if (!dirty || text.empty()) return;
80 font->Render(text, tex);
97 glm::vec2 Meter::Size() {
98 return size + (2.0f * padding) + glm::vec2(2.0f * border);
101 void Meter::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
102 glm::vec2 fullsize = Size();
103 assets.shaders.canvas.Activate();
104 assets.shaders.canvas.ZIndex(ZIndex());
107 assets.shaders.canvas.SetColor(border_color);
108 assets.shaders.canvas.DrawRect(
109 Position() + glm::vec2(border * 0.5f),
110 Position() + fullsize - glm::vec2(border * 0.5f),
116 glm::vec2 bottom_right = Position() + fullsize - glm::vec2(border) - padding;
117 bottom_right.x -= size.x * (1.0f - value);
118 assets.shaders.canvas.SetColor(fill_color);
119 assets.shaders.canvas.FillRect(
120 Position() + glm::vec2(border) + padding,
129 , bg_color(0.0f, 0.0f, 0.0f, 0.0f)
139 Panel *Panel::Add(Widget *w) {
140 std::unique_ptr<Widget> widget(w);
141 glm::vec2 wsize = widget->Size();
142 if (dir == HORIZONTAL) {
144 size.y = std::max(size.y, wsize.y);
146 size.x = std::max(size.x, wsize.x);
149 widgets.emplace_back(std::move(widget));
153 Panel *Panel::Clear() {
155 size = glm::vec2(0.0f);
159 Panel *Panel::Background(const glm::vec4 &c) {
164 Panel *Panel::Padding(const glm::vec2 &p) {
169 Panel *Panel::Spacing(float s) {
174 Panel *Panel::Direction(Dir d) {
180 glm::vec2 Panel::Size() {
181 glm::vec2 space(0.0f);
182 space[dir] = (widgets.size() - 1) * spacing;
183 return (2.0f * padding) + space + size;
186 void Panel::Layout() {
187 size = glm::vec2(0.0f);
188 if (dir == HORIZONTAL) {
189 for (auto &w : widgets) {
190 glm::vec2 wsize = w->Size();
192 size.y = std::max(size.y, wsize.y);
195 for (auto &w : widgets) {
196 glm::vec2 wsize = w->Size();
197 size.x = std::max(size.x, wsize.x);
203 void Panel::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
204 // TODO: separate draw and layout, it's inefficient and the wrong tree order anyway
206 if (bg_color.a > 0.0f) {
207 assets.shaders.canvas.Activate();
208 assets.shaders.canvas.ZIndex(ZIndex());
209 assets.shaders.canvas.SetColor(bg_color);
210 assets.shaders.canvas.FillRect(Position(), Position() + Size());
213 glm::vec2 cursor = Position() + padding;
214 for (auto &w : widgets) {
215 w->Position(cursor)->ZIndex(ZIndex() + 1.0f);
216 w->Draw(assets, viewport);
217 cursor[dir] += w->Size()[dir] + spacing;