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 assets.shaders.canvas.SetColor(fill_color);
114 assets.shaders.canvas.FillRect(
115 Position() + glm::vec2(border) + padding,
116 Position() + fullsize - glm::vec2(border) - padding
124 , bg_color(0.0f, 0.0f, 0.0f, 0.0f)
134 Panel *Panel::Add(Widget *w) {
135 std::unique_ptr<Widget> widget(w);
136 glm::vec2 wsize = widget->Size();
137 if (dir == HORIZONTAL) {
139 size.y = std::max(size.y, wsize.y);
141 size.x = std::max(size.x, wsize.x);
144 widgets.emplace_back(std::move(widget));
148 Panel *Panel::Clear() {
150 size = glm::vec2(0.0f);
154 Panel *Panel::Background(const glm::vec4 &c) {
159 Panel *Panel::Padding(const glm::vec2 &p) {
164 Panel *Panel::Spacing(float s) {
169 Panel *Panel::Direction(Dir d) {
175 glm::vec2 Panel::Size() {
176 return (2.0f * padding) + glm::vec2(0.0f, (widgets.size() - 1) * spacing) + size;
179 void Panel::Relayout() {
180 size = glm::vec2(0.0f);
181 if (dir == HORIZONTAL) {
182 for (auto &w : widgets) {
183 glm::vec2 wsize = w->Size();
185 size.y = std::max(size.y, wsize.y);
188 for (auto &w : widgets) {
189 glm::vec2 wsize = w->Size();
190 size.x = std::max(size.x, wsize.x);
196 void Panel::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
197 if (bg_color.a > 0.0f) {
198 assets.shaders.canvas.Activate();
199 assets.shaders.canvas.ZIndex(ZIndex());
200 assets.shaders.canvas.SetColor(bg_color);
201 assets.shaders.canvas.FillRect(Position(), Position() + Size());
204 glm::vec2 cursor = Position();
205 cursor.x += padding.x;
206 cursor.y += padding.y;
207 for (auto &w : widgets) {
208 w->Position(cursor)->ZIndex(ZIndex() + 1.0f);
209 w->Draw(assets, viewport);
210 cursor[dir] += w->Size()[dir] + spacing;