]> git.localhorst.tv Git - blobs.git/blob - src/ui/ui.cpp
remote outdated TODO
[blobs.git] / src / ui / ui.cpp
1 #include "CreaturePanel.hpp"
2
3 #include "Label.hpp"
4 #include "Meter.hpp"
5 #include "../app/Assets.hpp"
6 #include "../creature/Creature.hpp"
7 #include "../creature/Need.hpp"
8 #include "../graphics/Viewport.hpp"
9
10 #include <glm/gtx/transform.hpp>
11
12
13 namespace blobs {
14 namespace ui {
15
16 CreaturePanel::CreaturePanel(const app::Assets &assets)
17 : assets(assets)
18 , c(nullptr)
19 , name(new Label(assets.fonts.large))
20 , needs(new Panel)
21 , panel()
22 , health_meter(new Meter)
23 , need_meters() {
24         Label *health_label = new Label(assets.fonts.medium);
25         health_label->Text("Health");
26         health_meter
27                 ->Size(glm::vec2(100.0f, assets.fonts.medium.Height() + assets.fonts.medium.Descent()))
28                 ->Padding(glm::vec2(1.0f))
29                 ->Border(1.0f)
30                 ->FillColor(glm::vec4(0.9f, 0.0f, 0.0f, 1.0f))
31                 ->BorderColor(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f));
32         Panel *health_panel = new Panel;
33         health_panel
34                 ->Add(health_label)
35                 ->Add(health_meter)
36                 ->Direction(Panel::HORIZONTAL);
37         panel
38                 .Add(name)
39                 ->Add(health_panel)
40                 ->Add(needs)
41                 ->Padding(glm::vec2(10.0f))
42                 ->Spacing(10.0f)
43                 ->Direction(Panel::VERTICAL)
44                 ->Background(glm::vec4(0.7f, 0.7f, 0.7f, 1.0f));
45 }
46
47 CreaturePanel::~CreaturePanel() {
48 }
49
50
51 void CreaturePanel::Show(creature::Creature &cr) {
52         c = &cr;
53         name->Text(c->Name());
54         CreateNeeds();
55 }
56
57 void CreaturePanel::CreateNeeds() {
58         needs->Clear()->Reserve(c->Needs().size());
59         need_meters.clear();
60         need_meters.reserve(c->Needs().size());
61         for (auto &need : c->Needs()) {
62                 Label *label = new Label(assets.fonts.medium);
63                 label->Text(need->name);
64                 Meter *meter = new Meter;
65                 meter
66                         ->Value(1.0f - need->value)
67                         ->Size(glm::vec2(100.0f, assets.fonts.medium.Height() + assets.fonts.medium.Descent()))
68                         ->Padding(glm::vec2(1.0f))
69                         ->Border(1.0f)
70                         ->FillColor(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f))
71                         ->BorderColor(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f));
72                 Panel *need_panel = new Panel;
73                 need_panel
74                         ->Direction(Panel::HORIZONTAL)
75                         ->Spacing(10.0f)
76                         ->Add(label)
77                         ->Add(meter);
78                 needs->Add(need_panel);
79                 need_meters.push_back(meter);
80         }
81         panel.Relayout();
82 }
83
84 void CreaturePanel::Hide() noexcept {
85         c = nullptr;
86 }
87
88 void CreaturePanel::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
89         if (!c) return;
90
91         health_meter->Value(c->Health());
92
93         if (need_meters.size() != c->Needs().size()) {
94                 CreateNeeds();
95         } else {
96                 auto need = c->Needs().begin();
97                 auto need_end = c->Needs().end();
98                 auto meter = need_meters.begin();
99                 for (; need != need_end; ++need, ++meter) {
100                         (*meter)->Value(1.0f - (*need)->value);
101                         if ((*need)->IsSatisfied()) {
102                                 (*meter)->FillColor(glm::vec4(0.0f, 0.7f, 0.0f, 1.0f));
103                         } else if ((*need)->IsInconvenient()) {
104                                 (*meter)->FillColor(glm::vec4(0.7f, 0.5f, 0.0f, 1.0f));
105                         } else {
106                                 (*meter)->FillColor(glm::vec4(0.9f, 0.0f, 0.0f, 1.0f));
107                         }
108                 }
109         }
110
111         const glm::vec2 margin(20.0f);
112
113         panel.Position(glm::vec2(viewport.Width() - margin.x - panel.Size().x, margin.y));
114         panel.Draw(assets, viewport);
115 }
116
117 }
118 }