double Health() const noexcept { return health; }
void AddNeed(const Need &n) { needs.push_back(n); }
+ const std::vector<Need> &Needs() const noexcept { return needs; }
void Tick(double dt);
#ifndef BLOBS_UI_CREATUREPANEL_HPP_
#define BLOBS_UI_CREATUREPANEL_HPP_
-#include "Label.hpp"
#include "Panel.hpp"
}
namespace ui {
+class Label;
+class Meter;
+
class CreaturePanel {
public:
void Draw(app::Assets &, graphics::Viewport &) noexcept;
private:
+ void CreateNeeds();
+
+private:
+ const app::Assets &assets;
creature::Creature *c;
Label *name;
+ Panel *needs;
Panel panel;
+ std::vector<Meter *> meters;
+
};
}
~Label() override;
public:
- Label &Text(const std::string &);
- Label &Font(const graphics::Font &);
- Label &Foreground(const glm::vec4 &);
- Label &Background(const glm::vec4 &);
+ Label *Text(const std::string &);
+ Label *Font(const graphics::Font &);
+ Label *Foreground(const glm::vec4 &);
+ Label *Background(const glm::vec4 &);
glm::vec2 Size() override;
void Draw(app::Assets &, graphics::Viewport &) noexcept override;
--- /dev/null
+#ifndef BLOBS_UI_METER_HPP_
+#define BLOBS_UI_METER_HPP_
+
+#include "Widget.hpp"
+
+
+namespace blobs {
+namespace ui {
+
+class Meter
+: public Widget {
+
+public:
+ Meter();
+ ~Meter() override;
+
+public:
+ Meter *Size(const glm::vec2 &s) noexcept { size = s; return this; }
+ Meter *Padding(const glm::vec2 &p) noexcept { padding = p; return this; }
+ // TODO: gl line width is deprecated, use polys instead
+ Meter *Border(float b) noexcept { border = b; return this; }
+
+ Meter *FillColor(const glm::vec4 &c) noexcept { fill_color = c; return this; }
+ Meter *BorderColor(const glm::vec4 &c) noexcept { border_color = c; return this; }
+
+ Meter *Value(float v) noexcept { value = v; return this; }
+
+ glm::vec2 Size() override;
+ void Draw(app::Assets &, graphics::Viewport &) noexcept override;
+
+private:
+ glm::vec4 fill_color;
+ glm::vec4 border_color;
+
+ glm::vec2 size;
+ glm::vec2 padding;
+ float border;
+
+ float value;
+
+};
+
+}
+}
+
+#endif
HORIZONTAL,
VERTICAL,
};
+ using Children_t = std::vector<std::unique_ptr<Widget>>;
public:
Panel();
public:
// panel takes ownership
- Panel &Add(Widget *);
+ Panel *Add(Widget *);
+ Panel *Clear();
+ Panel *Reserve(int n) { widgets.reserve(n); return this; }
+ const Children_t &Children() const noexcept { return widgets; }
- Panel &Background(const glm::vec4 &);
- Panel &Padding(const glm::vec2 &);
- Panel &Spacing(float);
- Panel &Direction(Dir);
+ Panel *Background(const glm::vec4 &);
+ Panel *Padding(const glm::vec2 &);
+ Panel *Spacing(float);
+ Panel *Direction(Dir);
glm::vec2 Size() override;
void Relayout();
void Draw(app::Assets &, graphics::Viewport &) noexcept override;
private:
- std::vector<std::unique_ptr<Widget>> widgets;
+ Children_t widgets;
glm::vec4 bg_color;
glm::vec2 padding;
float spacing;
Widget &operator =(Widget &&) = delete;
public:
- Widget &Position(const glm::vec3 &p) noexcept { pos = p; return *this; }
+ Widget *Position(const glm::vec3 &p) noexcept { pos = p; return this; }
const glm::vec3 &Position() const noexcept { return pos; }
glm::vec3 AlignedPosition() noexcept;
glm::vec3 TopLeft() noexcept;
- Widget &Origin(Gravity o) noexcept { origin = o; return *this; }
+ Widget *Origin(Gravity o) noexcept { origin = o; return this; }
Gravity Origin() const noexcept { return origin; }
virtual glm::vec2 Size() = 0;
#include "CreaturePanel.hpp"
+#include "Label.hpp"
+#include "Meter.hpp"
#include "../app/Assets.hpp"
#include "../creature/Creature.hpp"
#include "../graphics/Viewport.hpp"
namespace ui {
CreaturePanel::CreaturePanel(const app::Assets &assets)
-: c(nullptr)
+: assets(assets)
+, c(nullptr)
, name(new Label(assets.fonts.large))
+, needs(new Panel)
, panel() {
panel
.Add(name)
- .Padding(glm::vec2(10.0f))
- .Spacing(10.0f)
- .Direction(Panel::VERTICAL)
- .Background(glm::vec4(0.7f, 0.7f, 0.7f, 1.0f))
- .Origin(Gravity::NORTH_EAST);
+ ->Add(needs)
+ ->Padding(glm::vec2(10.0f))
+ ->Spacing(10.0f)
+ ->Direction(Panel::VERTICAL)
+ ->Background(glm::vec4(0.7f, 0.7f, 0.7f, 1.0f))
+ ->Origin(Gravity::NORTH_EAST);
}
CreaturePanel::~CreaturePanel() {
void CreaturePanel::Show(creature::Creature &cr) {
c = &cr;
name->Text(c->Name());
+ CreateNeeds();
+}
+
+void CreaturePanel::CreateNeeds() {
+ needs->Clear()->Reserve(c->Needs().size());
+ meters.clear();
+ meters.reserve(c->Needs().size());
+ for (auto &need : c->Needs()) {
+ Label *label = new Label(assets.fonts.medium);
+ label
+ ->Text(assets.data.resources[need.resource].label);
+ Meter *meter = new Meter;
+ meter
+ ->Value(1.0f - need.value)
+ ->Size(glm::vec2(100.0f, assets.fonts.medium.Height()))
+ ->Padding(glm::vec2(1.0f))
+ ->Border(1.0f)
+ ->FillColor(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f))
+ ->BorderColor(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f));
+ Panel *need_panel = new Panel;
+ need_panel
+ ->Direction(Panel::HORIZONTAL)
+ ->Spacing(10.0f)
+ ->Add(label)
+ ->Add(meter);
+ needs->Add(need_panel);
+ meters.push_back(meter);
+ }
panel.Relayout();
}
void CreaturePanel::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
if (!c) return;
+ if (meters.size() != c->Needs().size()) {
+ CreateNeeds();
+ } else {
+ auto need = c->Needs().begin();
+ auto need_end = c->Needs().end();
+ auto meter = meters.begin();
+ for (; need != need_end; ++need, ++meter) {
+ (*meter)->Value(1.0f - need->value)->FillColor(need->IsSatisfied()
+ ? glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)
+ : glm::vec4(1.0f, 0.0f, 0.0f, 1.0f));
+ }
+ }
+
const glm::vec2 margin(20.0f);
panel.Position(glm::vec3(viewport.Width() - margin.x, margin.y, 0.0f));
#include "Label.hpp"
+#include "Meter.hpp"
#include "Panel.hpp"
#include "Widget.hpp"
Label::~Label() {
}
-Label &Label::Text(const std::string &t) {
+Label *Label::Text(const std::string &t) {
if (text != t) {
dirty = true;
}
text = t;
- return *this;
+ return this;
}
-Label &Label::Font(const graphics::Font &f) {
+Label *Label::Font(const graphics::Font &f) {
if (font != &f) {
dirty = true;
}
font = &f;
- return *this;
+ return this;
}
-Label &Label::Foreground(const glm::vec4 &c) {
+Label *Label::Foreground(const glm::vec4 &c) {
fg_color = c;
- return *this;
+ return this;
}
-Label &Label::Background(const glm::vec4 &c) {
+Label *Label::Background(const glm::vec4 &c) {
bg_color = c;
- return *this;
+ return this;
}
glm::vec2 Label::Size() {
}
+Meter::Meter()
+: fill_color(1.0f)
+, border_color(1.0f)
+, size(3.0f)
+, padding(1.0f)
+, border(1.0f)
+, value(0.0f) {
+}
+
+Meter::~Meter() {
+}
+
+glm::vec2 Meter::Size() {
+ return size + (2.0f * padding) + (2.0f * border);
+}
+
+void Meter::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
+ glm::vec2 fullsize = Size();
+ assets.shaders.plain_color.Activate();
+
+ if (border > 0.0f) {
+ assets.shaders.plain_color.SetM(glm::translate(AlignedPosition())
+ * glm::scale(glm::vec3(fullsize.x, fullsize.y, 1.0f)));
+ assets.shaders.plain_color.SetColor(border_color);
+ assets.shaders.plain_color.OutlineRect();
+ }
+
+ if (value > 0.0f) {
+ glm::vec3 top_left(glm::vec2(TopLeft()) + padding + glm::vec2(border), Position().z);
+ glm::vec3 actual_size(size.x * value, size.y, 1.0f);
+
+ assets.shaders.plain_color.SetM(glm::translate(align(Gravity::NORTH_WEST, actual_size, top_left))
+ * glm::scale(actual_size));
+ assets.shaders.plain_color.SetColor(fill_color);
+ assets.shaders.plain_color.DrawRect();
+ }
+}
+
+
Panel::Panel()
: widgets()
, bg_color(0.0f, 0.0f, 0.0f, 0.0f)
Panel::~Panel() {
}
-Panel &Panel::Add(Widget *w) {
+Panel *Panel::Add(Widget *w) {
std::unique_ptr<Widget> widget(w);
glm::vec2 wsize = widget->Size();
if (dir == HORIZONTAL) {
size.y += wsize.y;
}
widgets.emplace_back(std::move(widget));
- return *this;
+ return this;
+}
+
+Panel *Panel::Clear() {
+ widgets.clear();
+ size = glm::vec2(0.0f);
+ return this;
}
-Panel &Panel::Background(const glm::vec4 &c) {
+Panel *Panel::Background(const glm::vec4 &c) {
bg_color = c;
- return *this;
+ return this;
}
-Panel &Panel::Padding(const glm::vec2 &p) {
+Panel *Panel::Padding(const glm::vec2 &p) {
padding = p;
- return *this;
+ return this;
}
-Panel &Panel::Spacing(float s) {
+Panel *Panel::Spacing(float s) {
spacing = s;
- return *this;
+ return this;
}
-Panel &Panel::Direction(Dir d) {
+Panel *Panel::Direction(Dir d) {
dir = d;
Relayout();
- return *this;
+ return this;
}
glm::vec2 Panel::Size() {
- return 2.0f * padding + glm::vec2(0.0f, (widgets.size() - 1) * spacing) + size;
+ return (2.0f * padding) + glm::vec2(0.0f, (widgets.size() - 1) * spacing) + size;
}
void Panel::Relayout() {
cursor.y += padding.y;
cursor.z -= 1.0f;
for (auto &w : widgets) {
- w->Position(cursor).Origin(Gravity::NORTH_WEST);
+ w->Position(cursor)->Origin(Gravity::NORTH_WEST);
w->Draw(assets, viewport);
cursor[dir] += w->Size()[dir] + spacing;
}