6 #include "../app/Assets.hpp"
7 #include "../graphics/Font.hpp"
8 #include "../graphics/Viewport.hpp"
10 #include <glm/gtx/transform.hpp>
16 Label::Label(const graphics::Font &f)
20 , fg_color(0.0f, 0.0f, 0.0f, 1.0f)
21 , bg_color(0.0f, 0.0f, 0.0f, 0.0f)
28 Label *Label::Text(const std::string &t) {
36 Label *Label::Font(const graphics::Font &f) {
44 Label *Label::Foreground(const glm::vec4 &c) {
49 Label *Label::Background(const glm::vec4 &c) {
54 glm::vec2 Label::Size() {
56 return glm::vec2(0.0f);
62 void Label::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
64 glm::vec2 size = Size();
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();
75 void Label::Update() {
77 font->Render(text, tex);
94 glm::vec2 Meter::Size() {
95 return size + (2.0f * padding) + glm::vec2(2.0f * border);
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());
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),
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,
126 , bg_color(0.0f, 0.0f, 0.0f, 0.0f)
136 Panel *Panel::Add(Widget *w) {
137 std::unique_ptr<Widget> widget(w);
138 glm::vec2 wsize = widget->Size();
139 if (dir == HORIZONTAL) {
141 size.y = std::max(size.y, wsize.y);
143 size.x = std::max(size.x, wsize.x);
146 widgets.emplace_back(std::move(widget));
150 Panel *Panel::Clear() {
152 size = glm::vec2(0.0f);
156 Panel *Panel::Background(const glm::vec4 &c) {
161 Panel *Panel::Padding(const glm::vec2 &p) {
166 Panel *Panel::Spacing(float s) {
171 Panel *Panel::Direction(Dir d) {
177 glm::vec2 Panel::Size() {
178 return (2.0f * padding) + glm::vec2(0.0f, (widgets.size() - 1) * spacing) + size;
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();
187 size.y = std::max(size.y, wsize.y);
190 for (auto &w : widgets) {
191 glm::vec2 wsize = w->Size();
192 size.x = std::max(size.x, wsize.x);
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());
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;