minor refactor

This commit is contained in:
MihailRis
2024-02-14 01:19:24 +03:00
parent c6bd52eed2
commit 2aa49c7470
8 changed files with 155 additions and 162 deletions
+3
View File
@@ -10,5 +10,8 @@ using wstringsupplier = std::function<std::wstring()>;
using wstringconsumer = std::function<void(const std::wstring&)>; using wstringconsumer = std::function<void(const std::wstring&)>;
using doublesupplier = std::function<double()>; using doublesupplier = std::function<double()>;
using doubleconsumer = std::function<void(double)>; using doubleconsumer = std::function<void(double)>;
using boolsupplier = std::function<bool()>;
using boolconsumer = std::function<void(bool)>;
using wstringchecker = std::function<bool(const std::wstring&)>;
#endif // DELEGATES_H_ #endif // DELEGATES_H_
+66 -69
View File
@@ -10,24 +10,20 @@
#include "../../util/stringutil.h" #include "../../util/stringutil.h"
#include "GUI.h" #include "GUI.h"
using glm::vec2;
using glm::vec3;
using glm::vec4;
using namespace gui; using namespace gui;
Label::Label(std::string text, std::string fontName) Label::Label(std::string text, std::string fontName)
: UINode(vec2(), vec2(text.length() * 8, 15)), : UINode(glm::vec2(), glm::vec2(text.length() * 8, 15)),
text(util::str2wstr_utf8(text)), text(util::str2wstr_utf8(text)),
fontName_(fontName) { fontName(fontName) {
setInteractive(false); setInteractive(false);
} }
Label::Label(std::wstring text, std::string fontName) Label::Label(std::wstring text, std::string fontName)
: UINode(vec2(), vec2(text.length() * 8, 15)), : UINode(glm::vec2(), glm::vec2(text.length() * 8, 15)),
text(text), text(text),
fontName_(fontName) { fontName(fontName) {
setInteractive(false); setInteractive(false);
} }
@@ -46,14 +42,14 @@ void Label::draw(const GfxContext* pctx, Assets* assets) {
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
batch->color = getColor(); batch->color = getColor();
Font* font = assets->getFont(fontName_); Font* font = assets->getFont(fontName);
vec2 size = getSize(); glm::vec2 size = getSize();
vec2 newsize = vec2( glm::vec2 newsize (
font->calcWidth(text), font->calcWidth(text),
font->getLineHeight()+font->getYOffset() font->getLineHeight()+font->getYOffset()
); );
vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
switch (align) { switch (align) {
case Align::left: case Align::left:
break; break;
@@ -74,13 +70,13 @@ Label* Label::textSupplier(wstringsupplier supplier) {
} }
// ================================= Image ==================================== // ================================= Image ====================================
Image::Image(std::string texture, vec2 size) : UINode(vec2(), size), texture(texture) { Image::Image(std::string texture, glm::vec2 size) : UINode(glm::vec2(), size), texture(texture) {
setInteractive(false); setInteractive(false);
} }
void Image::draw(const GfxContext* pctx, Assets* assets) { void Image::draw(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
vec4 color = getColor(); glm::vec4 color = getColor();
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
batch->texture(assets->getTexture(texture)); batch->texture(assets->getTexture(texture));
batch->color = color; batch->color = color;
@@ -90,10 +86,11 @@ void Image::draw(const GfxContext* pctx, Assets* assets) {
// ================================= Button =================================== // ================================= Button ===================================
Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding) Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
: Panel(vec2(), padding, 0) { : Panel(glm::vec2(), padding, 0) {
vec4 margin = getMargin(); glm::vec4 margin = getMargin();
setSize(content->getSize()+vec2(padding[0]+padding[2]+margin[0]+margin[2], setSize(content->getSize()+
padding[1]+padding[3]+margin[1]+margin[3])); glm::vec2(padding[0]+padding[2]+margin[0]+margin[2],
padding[1]+padding[3]+margin[1]+margin[3]));
add(content); add(content);
setScrollable(false); setScrollable(false);
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f)); setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
@@ -102,13 +99,13 @@ Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
Button::Button( Button::Button(
std::wstring text, std::wstring text,
vec4 padding, glm::vec4 padding,
onaction action, onaction action,
vec2 size glm::vec2 size
) : Panel(size, padding, 0) ) : Panel(size, padding, 0)
{ {
if (size.y < 0.0f) { if (size.y < 0.0f) {
size = vec2( size = glm::vec2(
glm::max(padding.x + padding.z + text.length()*8, size.x), glm::max(padding.x + padding.z + text.length()*8, size.x),
glm::max(padding.y + padding.w + 16, size.y) glm::max(padding.y + padding.w + 16, size.y)
); );
@@ -122,7 +119,7 @@ Button::Button(
label = std::make_shared<Label>(text); label = std::make_shared<Label>(text);
label->setAlign(Align::center); label->setAlign(Align::center);
label->setSize(size-vec2(padding.z+padding.x, padding.w+padding.y)); label->setSize(size-glm::vec2(padding.z+padding.x, padding.w+padding.y));
label->setInteractive(false); label->setInteractive(false);
add(label); add(label);
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f)); setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
@@ -151,12 +148,12 @@ Button* Button::textSupplier(wstringsupplier supplier) {
void Button::refresh() { void Button::refresh() {
Panel::refresh(); Panel::refresh();
if (label) { if (label) {
label->setSize(size-vec2(padding.z+padding.x, padding.w+padding.y)); label->setSize(size-glm::vec2(padding.z+padding.x, padding.w+padding.y));
} }
} }
void Button::drawBackground(const GfxContext* pctx, Assets* assets) { void Button::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color)); batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color));
@@ -165,7 +162,7 @@ void Button::drawBackground(const GfxContext* pctx, Assets* assets) {
void Button::mouseRelease(GUI* gui, int x, int y) { void Button::mouseRelease(GUI* gui, int x, int y) {
UINode::mouseRelease(gui, x, y); UINode::mouseRelease(gui, x, y);
if (isInside(vec2(x, y))) { if (isInside(glm::vec2(x, y))) {
for (auto callback : actions) { for (auto callback : actions) {
callback(gui); callback(gui);
} }
@@ -192,13 +189,13 @@ Align Button::getTextAlign() const {
} }
// ============================== RichButton ================================== // ============================== RichButton ==================================
RichButton::RichButton(vec2 size) : Container(vec2(), size) { RichButton::RichButton(glm::vec2 size) : Container(glm::vec2(), size) {
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f)); setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
} }
void RichButton::mouseRelease(GUI* gui, int x, int y) { void RichButton::mouseRelease(GUI* gui, int x, int y) {
UINode::mouseRelease(gui, x, y); UINode::mouseRelease(gui, x, y);
if (isInside(vec2(x, y))) { if (isInside(glm::vec2(x, y))) {
for (auto callback : actions) { for (auto callback : actions) {
callback(gui); callback(gui);
} }
@@ -211,7 +208,7 @@ RichButton* RichButton::listenAction(onaction action) {
} }
void RichButton::drawBackground(const GfxContext* pctx, Assets* assets) { void RichButton::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color)); batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color));
@@ -219,18 +216,18 @@ void RichButton::drawBackground(const GfxContext* pctx, Assets* assets) {
} }
// ================================ TextBox =================================== // ================================ TextBox ===================================
TextBox::TextBox(std::wstring placeholder, vec4 padding) TextBox::TextBox(std::wstring placeholder, glm::vec4 padding)
: Panel(vec2(200,32), padding, 0), : Panel(glm::vec2(200,32), padding, 0),
input(L""), input(L""),
placeholder(placeholder) { placeholder(placeholder) {
label = std::make_shared<Label>(L""); label = std::make_shared<Label>(L"");
label->setSize(size-vec2(padding.z+padding.x, padding.w+padding.y)); label->setSize(size-glm::vec2(padding.z+padding.x, padding.w+padding.y));
add(label); add(label);
setHoverColor(glm::vec4(0.05f, 0.1f, 0.2f, 0.75f)); setHoverColor(glm::vec4(0.05f, 0.1f, 0.2f, 0.75f));
} }
void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) { void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
@@ -253,10 +250,10 @@ void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
} }
if (input.empty()) { if (input.empty()) {
label->setColor(vec4(0.5f)); label->setColor(glm::vec4(0.5f));
label->setText(placeholder); label->setText(placeholder);
} else { } else {
label->setColor(vec4(1.0f)); label->setColor(glm::vec4(1.0f));
label->setText(input); label->setText(input);
} }
setScrollable(false); setScrollable(false);
@@ -297,7 +294,7 @@ void TextBox::focus(GUI* gui) {
void TextBox::refresh() { void TextBox::refresh() {
Panel::refresh(); Panel::refresh();
label->setSize(size-vec2(padding.z+padding.x, padding.w+padding.y)); label->setSize(size-glm::vec2(padding.z+padding.x, padding.w+padding.y));
} }
void TextBox::keyPressed(int key) { void TextBox::keyPressed(int key) {
@@ -322,7 +319,7 @@ void TextBox::keyPressed(int key) {
} }
} }
std::shared_ptr<UINode> TextBox::getAt(vec2 pos, std::shared_ptr<UINode> self) { std::shared_ptr<UINode> TextBox::getAt(glm::vec2 pos, std::shared_ptr<UINode> self) {
return UINode::getAt(pos, self); return UINode::getAt(pos, self);
} }
@@ -349,8 +346,8 @@ void TextBox::setText(std::wstring value) {
} }
// ============================== InputBindBox ================================ // ============================== InputBindBox ================================
InputBindBox::InputBindBox(Binding& binding, vec4 padding) InputBindBox::InputBindBox(Binding& binding, glm::vec4 padding)
: Panel(vec2(100,32), padding, 0), : Panel(glm::vec2(100,32), padding, 0),
binding(binding) { binding(binding) {
label = std::make_shared<Label>(L""); label = std::make_shared<Label>(L"");
add(label); add(label);
@@ -358,7 +355,7 @@ InputBindBox::InputBindBox(Binding& binding, vec4 padding)
} }
void InputBindBox::drawBackground(const GfxContext* pctx, Assets* assets) { void InputBindBox::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->color = (isFocused() ? focusedColor : (hover ? hoverColor : color)); batch->color = (isFocused() ? focusedColor : (hover ? hoverColor : color));
@@ -386,20 +383,20 @@ TrackBar::TrackBar(double min,
double value, double value,
double step, double step,
int trackWidth) int trackWidth)
: UINode(vec2(), vec2(26)), : UINode(glm::vec2(), glm::vec2(26)),
min(min), min(min),
max(max), max(max),
value(value), value(value),
step(step), step(step),
trackWidth(trackWidth) { trackWidth(trackWidth) {
setColor(vec4(0.f, 0.f, 0.f, 0.4f)); setColor(glm::vec4(0.f, 0.f, 0.f, 0.4f));
} }
void TrackBar::draw(const GfxContext* pctx, Assets* assets) { void TrackBar::draw(const GfxContext* pctx, Assets* assets) {
if (supplier_) { if (supplier) {
value = supplier_(); value = supplier();
} }
vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->color = (hover ? hoverColor : color); batch->color = (hover ? hoverColor : color);
@@ -413,16 +410,16 @@ void TrackBar::draw(const GfxContext* pctx, Assets* assets) {
batch->rect(coord.x + width * t, coord.y, actualWidth, size.y); batch->rect(coord.x + width * t, coord.y, actualWidth, size.y);
} }
void TrackBar::supplier(doublesupplier supplier) { void TrackBar::setSupplier(doublesupplier supplier) {
this->supplier_ = supplier; this->supplier = supplier;
} }
void TrackBar::consumer(doubleconsumer consumer) { void TrackBar::setConsumer(doubleconsumer consumer) {
this->consumer_ = consumer; this->consumer = consumer;
} }
void TrackBar::mouseMove(GUI*, int x, int y) { void TrackBar::mouseMove(GUI*, int x, int y) {
vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
value = x; value = x;
value -= coord.x; value -= coord.x;
value = (value)/size.x * (max-min+trackWidth*step); value = (value)/size.x * (max-min+trackWidth*step);
@@ -430,56 +427,56 @@ void TrackBar::mouseMove(GUI*, int x, int y) {
value = (value > max) ? max : value; value = (value > max) ? max : value;
value = (value < min) ? min : value; value = (value < min) ? min : value;
value = (int)(value / step) * step; value = (int)(value / step) * step;
if (consumer_) { if (consumer) {
consumer_(value); consumer(value);
} }
} }
// ================================ CheckBox ================================== // ================================ CheckBox ==================================
CheckBox::CheckBox(bool checked) : UINode(vec2(), vec2(32.0f)), checked_(checked) { CheckBox::CheckBox(bool checked) : UINode(glm::vec2(), glm::vec2(32.0f)), checked(checked) {
setColor(vec4(0.0f, 0.0f, 0.0f, 0.5f)); setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.5f));
} }
void CheckBox::draw(const GfxContext* pctx, Assets* assets) { void CheckBox::draw(const GfxContext* pctx, Assets* assets) {
if (supplier_) { if (supplier) {
checked_ = supplier_(); checked = supplier();
} }
vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->color = checked_ ? checkColor : (hover ? hoverColor : color); batch->color = checked ? checkColor : (hover ? hoverColor : color);
batch->rect(coord.x, coord.y, size.x, size.y); batch->rect(coord.x, coord.y, size.x, size.y);
} }
void CheckBox::mouseRelease(GUI*, int x, int y) { void CheckBox::mouseRelease(GUI*, int x, int y) {
checked_ = !checked_; checked = !checked;
if (consumer_) { if (consumer) {
consumer_(checked_); consumer(checked);
} }
} }
void CheckBox::supplier(boolsupplier supplier) { void CheckBox::setSupplier(boolsupplier supplier) {
supplier_ = supplier; this->supplier = supplier;
} }
void CheckBox::consumer(boolconsumer consumer) { void CheckBox::setConsumer(boolconsumer consumer) {
consumer_ = consumer; this->consumer = consumer;
} }
CheckBox* CheckBox::checked(bool flag) { CheckBox* CheckBox::setChecked(bool flag) {
checked_ = flag; checked = flag;
return this; return this;
} }
FullCheckBox::FullCheckBox(std::wstring text, glm::vec2 size, bool checked) FullCheckBox::FullCheckBox(std::wstring text, glm::vec2 size, bool checked)
: Panel(size), : Panel(size),
checkbox(std::make_shared<CheckBox>(checked)){ checkbox(std::make_shared<CheckBox>(checked)){
setColor(vec4(0.0f)); setColor(glm::vec4(0.0f));
setOrientation(Orientation::horizontal); setOrientation(Orientation::horizontal);
add(checkbox); add(checkbox);
auto label = std::make_shared<Label>(text); auto label = std::make_shared<Label>(text);
label->setMargin(vec4(5.f, 5.f, 0.f, 0.f)); label->setMargin(glm::vec4(5.f, 5.f, 0.f, 0.f));
add(label); add(label);
} }
+24 -29
View File
@@ -17,15 +17,10 @@ class Batch2D;
class Assets; class Assets;
namespace gui { namespace gui {
using boolsupplier = std::function<bool()>;
using boolconsumer = std::function<void(bool)>;
using wstringchecker = std::function<bool(const std::wstring&)>;
class Label : public UINode { class Label : public UINode {
protected: protected:
std::wstring text; std::wstring text;
std::string fontName_; std::string fontName;
wstringsupplier supplier = nullptr; wstringsupplier supplier = nullptr;
public: public:
Label(std::string text, std::string fontName="normal"); Label(std::string text, std::string fontName="normal");
@@ -145,8 +140,8 @@ namespace gui {
protected: protected:
glm::vec4 hoverColor {0.01f, 0.02f, 0.03f, 0.5f}; glm::vec4 hoverColor {0.01f, 0.02f, 0.03f, 0.5f};
glm::vec4 trackColor {1.0f, 1.0f, 1.0f, 0.4f}; glm::vec4 trackColor {1.0f, 1.0f, 1.0f, 0.4f};
doublesupplier supplier_ = nullptr; doublesupplier supplier = nullptr;
doubleconsumer consumer_ = nullptr; doubleconsumer consumer = nullptr;
double min; double min;
double max; double max;
double value; double value;
@@ -160,8 +155,8 @@ namespace gui {
int trackWidth=1); int trackWidth=1);
virtual void draw(const GfxContext* pctx, Assets* assets) override; virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void supplier(doublesupplier supplier); virtual void setSupplier(doublesupplier supplier);
virtual void consumer(doubleconsumer consumer); virtual void setConsumer(doubleconsumer consumer);
virtual void mouseMove(GUI*, int x, int y) override; virtual void mouseMove(GUI*, int x, int y) override;
}; };
@@ -170,9 +165,9 @@ namespace gui {
protected: protected:
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f}; glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 checkColor {1.0f, 1.0f, 1.0f, 0.4f}; glm::vec4 checkColor {1.0f, 1.0f, 1.0f, 0.4f};
boolsupplier supplier_ = nullptr; boolsupplier supplier = nullptr;
boolconsumer consumer_ = nullptr; boolconsumer consumer = nullptr;
bool checked_ = false; bool checked = false;
public: public:
CheckBox(bool checked=false); CheckBox(bool checked=false);
@@ -180,15 +175,15 @@ namespace gui {
virtual void mouseRelease(GUI*, int x, int y) override; virtual void mouseRelease(GUI*, int x, int y) override;
virtual void supplier(boolsupplier supplier); virtual void setSupplier(boolsupplier supplier);
virtual void consumer(boolconsumer consumer); virtual void setConsumer(boolconsumer consumer);
virtual CheckBox* checked(bool flag); virtual CheckBox* setChecked(bool flag);
virtual bool checked() const { virtual bool isChecked() const {
if (supplier_) if (supplier)
return supplier_(); return supplier();
return checked_; return checked;
} }
}; };
@@ -198,22 +193,22 @@ namespace gui {
public: public:
FullCheckBox(std::wstring text, glm::vec2 size, bool checked=false); FullCheckBox(std::wstring text, glm::vec2 size, bool checked=false);
virtual void supplier(boolsupplier supplier) { virtual void setSupplier(boolsupplier supplier) {
checkbox->supplier(supplier); checkbox->setSupplier(supplier);
} }
virtual void consumer(boolconsumer consumer) { virtual void setConsumer(boolconsumer consumer) {
checkbox->consumer(consumer); checkbox->setConsumer(consumer);
} }
virtual void checked(bool flag) { virtual void setChecked(bool flag) {
checkbox->checked(flag); checkbox->setChecked(flag);
} }
virtual bool checked() const { virtual bool isChecked() const {
return checkbox->checked(); return checkbox->isChecked();
} }
}; };
} }
#endif // FRONTEND_GUI_CONTROLS_H_ #endif // FRONTEND_GUI_CONTROLS_H_
+1 -1
View File
@@ -192,7 +192,7 @@ static std::shared_ptr<UINode> readTrackBar(UiXmlReader& reader, xml::xmlelement
element->attr("consumer").getText(), element->attr("consumer").getText(),
reader.getFilename()+".lua" reader.getFilename()+".lua"
); );
bar->consumer(consumer); bar->setConsumer(consumer);
} }
return bar; return bar;
} }
+33 -36
View File
@@ -9,15 +9,12 @@
using namespace gui; using namespace gui;
using glm::vec2; Container::Container(glm::vec2 coord, glm::vec2 size) : UINode(coord, size) {
using glm::vec4;
Container::Container(vec2 coord, vec2 size) : UINode(coord, size) {
actualLength = size.y; actualLength = size.y;
setColor(glm::vec4()); setColor(glm::vec4());
} }
std::shared_ptr<UINode> Container::getAt(vec2 pos, std::shared_ptr<UINode> self) { std::shared_ptr<UINode> Container::getAt(glm::vec2 pos, std::shared_ptr<UINode> self) {
if (!interactive) { if (!interactive) {
return nullptr; return nullptr;
} }
@@ -79,8 +76,8 @@ void Container::setScrollable(bool flag) {
} }
void Container::draw(const GfxContext* pctx, Assets* assets) { void Container::draw(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
vec2 size = getSize(); glm::vec2 size = getSize();
drawBackground(pctx, assets); drawBackground(pctx, assets);
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
@@ -88,7 +85,7 @@ void Container::draw(const GfxContext* pctx, Assets* assets) {
batch->render(); batch->render();
{ {
GfxContext ctx = pctx->sub(); GfxContext ctx = pctx->sub();
ctx.scissors(vec4(coord.x, coord.y, size.x, size.y)); ctx.scissors(glm::vec4(coord.x, coord.y, size.x, size.y));
for (auto node : nodes) { for (auto node : nodes) {
if (node->isVisible()) if (node->isVisible())
node->draw(pctx, assets); node->draw(pctx, assets);
@@ -100,7 +97,7 @@ void Container::draw(const GfxContext* pctx, Assets* assets) {
void Container::drawBackground(const GfxContext* pctx, Assets* assets) { void Container::drawBackground(const GfxContext* pctx, Assets* assets) {
if (color.a <= 0.0f) if (color.a <= 0.0f)
return; return;
vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D(); auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
@@ -154,11 +151,11 @@ const std::vector<std::shared_ptr<UINode>>& Container::getNodes() const {
return nodes; return nodes;
} }
Panel::Panel(vec2 size, glm::vec4 padding, float interval) Panel::Panel(glm::vec2 size, glm::vec4 padding, float interval)
: Container(vec2(), size), : Container(glm::vec2(), size),
padding(padding), padding(padding),
interval(interval) { interval(interval) {
setColor(vec4(0.0f, 0.0f, 0.0f, 0.75f)); setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.75f));
} }
Panel::~Panel() { Panel::~Panel() {
@@ -183,9 +180,9 @@ glm::vec4 Panel::getPadding() const {
void Panel::cropToContent() { void Panel::cropToContent() {
if (maxLength > 0.0f) { if (maxLength > 0.0f) {
setSize(vec2(getSize().x, glm::min(maxLength, actualLength))); setSize(glm::vec2(getSize().x, glm::min(maxLength, actualLength)));
} else { } else {
setSize(vec2(getSize().x, actualLength)); setSize(glm::vec2(getSize().x, actualLength));
} }
} }
@@ -199,21 +196,21 @@ void Panel::refresh() {
UINode::refresh(); UINode::refresh();
float x = padding.x; float x = padding.x;
float y = padding.y; float y = padding.y;
vec2 size = getSize(); glm::vec2 size = getSize();
if (orientation == Orientation::vertical) { if (orientation == Orientation::vertical) {
float maxw = size.x; float maxw = size.x;
for (auto& node : nodes) { for (auto& node : nodes) {
vec2 nodesize = node->getSize(); glm::vec2 nodesize = node->getSize();
const vec4 margin = node->getMargin(); const glm::vec4 margin = node->getMargin();
y += margin.y; y += margin.y;
float ex = x + margin.x; float ex = x + margin.x;
node->setCoord(vec2(ex, y)); node->setCoord(glm::vec2(ex, y));
y += nodesize.y + margin.w + interval; y += nodesize.y + margin.w + interval;
float width = size.x - padding.x - padding.z - margin.x - margin.z; float width = size.x - padding.x - padding.z - margin.x - margin.z;
if (node->isResizing()) { if (node->isResizing()) {
node->setSize(vec2(width, nodesize.y)); node->setSize(glm::vec2(width, nodesize.y));
} }
node->refresh(); node->refresh();
maxw = fmax(maxw, ex+node->getSize().x+margin.z+padding.z); maxw = fmax(maxw, ex+node->getSize().x+margin.z+padding.z);
@@ -222,10 +219,10 @@ void Panel::refresh() {
} else { } else {
float maxh = size.y; float maxh = size.y;
for (auto& node : nodes) { for (auto& node : nodes) {
vec2 nodesize = node->getSize(); glm::vec2 nodesize = node->getSize();
const vec4 margin = node->getMargin(); const glm::vec4 margin = node->getMargin();
x += margin.x; x += margin.x;
node->setCoord(vec2(x, y+margin.y)); node->setCoord(glm::vec2(x, y+margin.y));
x += nodesize.x + margin.z + interval; x += nodesize.x + margin.z + interval;
node->refresh(); node->refresh();
@@ -243,7 +240,7 @@ Orientation Panel::getOrientation() const {
return orientation; return orientation;
} }
PagesControl::PagesControl() : Container(vec2(), vec2(1)){ PagesControl::PagesControl() : Container(glm::vec2(), glm::vec2(1)){
} }
bool PagesControl::has(std::string name) { bool PagesControl::has(std::string name) {
@@ -259,16 +256,16 @@ void PagesControl::setPage(std::string name, bool history) {
if (found == pages.end()) { if (found == pages.end()) {
throw std::runtime_error("no page found"); throw std::runtime_error("no page found");
} }
if (current_.panel) { if (current.panel) {
Container::remove(current_.panel); Container::remove(current.panel);
} }
if (history) { if (history) {
pageStack.push(curname_); pageStack.push(curname);
} }
curname_ = name; curname = name;
current_ = found->second; current = found->second;
Container::add(current_.panel); Container::add(current.panel);
setSize(current_.panel->getSize()); setSize(current.panel->getSize());
} }
void PagesControl::back() { void PagesControl::back() {
@@ -279,8 +276,8 @@ void PagesControl::back() {
setPage(name, false); setPage(name, false);
} }
Page& PagesControl::current() { Page& PagesControl::getCurrent() {
return current_; return current;
} }
void PagesControl::clearHistory() { void PagesControl::clearHistory() {
@@ -289,9 +286,9 @@ void PagesControl::clearHistory() {
void PagesControl::reset() { void PagesControl::reset() {
clearHistory(); clearHistory();
if (current_.panel) { if (current.panel) {
curname_ = ""; curname = "";
Container::remove(current_.panel); Container::remove(current.panel);
current_ = Page{nullptr}; current = Page{nullptr};
} }
} }
+3 -3
View File
@@ -94,8 +94,8 @@ namespace gui {
protected: protected:
std::unordered_map<std::string, Page> pages; std::unordered_map<std::string, Page> pages;
std::stack<std::string> pageStack; std::stack<std::string> pageStack;
Page current_; Page current;
std::string curname_ = ""; std::string curname = "";
public: public:
PagesControl(); PagesControl();
@@ -106,7 +106,7 @@ namespace gui {
void clearHistory(); void clearHistory();
void reset(); void reset();
Page& current(); Page& getCurrent();
}; };
} }
#endif // FRONTEND_GUI_PANELS_H_ #endif // FRONTEND_GUI_PANELS_H_
+11 -10
View File
@@ -100,13 +100,13 @@ std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
})); }));
for (int ax = 0; ax < 3; ax++){ for (int ax = 0; ax < 3; ax++){
auto sub = std::make_shared<Panel>(vec2(10, 27), vec4(0.0f)); auto sub = std::make_shared<Container>(vec2(), vec2(250, 27));
sub->setOrientation(Orientation::horizontal);
std::wstring str = L"x: "; std::wstring str = L"x: ";
str[0] += ax; str[0] += ax;
auto label = std::make_shared<Label>(str); auto label = std::make_shared<Label>(str);
label->setMargin(vec4(2, 3, 2, 3)); label->setMargin(vec4(2, 3, 2, 3));
label->setSize(vec2(20, 27));
sub->add(label); sub->add(label);
sub->setColor(vec4(0.0f)); sub->setColor(vec4(0.0f));
@@ -128,8 +128,9 @@ std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
Hitbox* hitbox = level->player->hitbox.get(); Hitbox* hitbox = level->player->hitbox.get();
box->setText(std::to_wstring(int(hitbox->position[ax]))); box->setText(std::to_wstring(int(hitbox->position[ax])));
}); });
box->setSize(vec2(230, 27));
sub->add(box); sub->add(box, vec2(20, 0));
panel->add(sub); panel->add(sub);
} }
panel->add(create_label([=](){ panel->add(create_label([=](){
@@ -143,24 +144,24 @@ std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
})); }));
{ {
auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 1.0f, 0.005f, 8); auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 1.0f, 0.005f, 8);
bar->supplier([=]() {return level->world->daytime;}); bar->setSupplier([=]() {return level->world->daytime;});
bar->consumer([=](double val) {level->world->daytime = val;}); bar->setConsumer([=](double val) {level->world->daytime = val;});
panel->add(bar); panel->add(bar);
} }
{ {
auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 0.0f, 0.005f, 8); auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 0.0f, 0.005f, 8);
bar->supplier([=]() {return WorldRenderer::fog;}); bar->setSupplier([=]() {return WorldRenderer::fog;});
bar->consumer([=](double val) {WorldRenderer::fog = val;}); bar->setConsumer([=](double val) {WorldRenderer::fog = val;});
panel->add(bar); panel->add(bar);
} }
{ {
auto checkbox = std::make_shared<FullCheckBox>( auto checkbox = std::make_shared<FullCheckBox>(
L"Show Chunk Borders", vec2(400, 24) L"Show Chunk Borders", vec2(400, 24)
); );
checkbox->supplier([=]() { checkbox->setSupplier([=]() {
return engine->getSettings().debug.showChunkBorders; return engine->getSettings().debug.showChunkBorders;
}); });
checkbox->consumer([=](bool checked) { checkbox->setConsumer([=](bool checked) {
engine->getSettings().debug.showChunkBorders = checked; engine->getSettings().debug.showChunkBorders = checked;
}); });
panel->add(checkbox); panel->add(checkbox);
@@ -291,7 +292,7 @@ void HudRenderer::update(bool visible) {
if (!visible && inventoryOpen) { if (!visible && inventoryOpen) {
closeInventory(); closeInventory();
} }
if (pause && menu->current().panel == nullptr) { if (pause && menu->getCurrent().panel == nullptr) {
pause = false; pause = false;
} }
if (Events::jpressed(keycode::ESCAPE) && !gui->isFocusCaught()) { if (Events::jpressed(keycode::ESCAPE) && !gui->isFocusCaught()) {
+14 -14
View File
@@ -472,10 +472,10 @@ void create_controls_panel(Engine* engine) {
})); }));
auto trackbar = std::make_shared<TrackBar>(0.1, 10.0, 2.0, 0.1, 4); auto trackbar = std::make_shared<TrackBar>(0.1, 10.0, 2.0, 0.1, 4);
trackbar->supplier([=]() { trackbar->setSupplier([=]() {
return engine->getSettings().camera.sensitivity; return engine->getSettings().camera.sensitivity;
}); });
trackbar->consumer([=](double value) { trackbar->setConsumer([=](double value) {
engine->getSettings().camera.sensitivity = value; engine->getSettings().camera.sensitivity = value;
}); });
panel->add(trackbar); panel->add(trackbar);
@@ -512,10 +512,10 @@ void create_settings_panel(Engine* engine) {
})); }));
auto trackbar = std::make_shared<TrackBar>(3, 66, 10, 1, 3); auto trackbar = std::make_shared<TrackBar>(3, 66, 10, 1, 3);
trackbar->supplier([=]() { trackbar->setSupplier([=]() {
return engine->getSettings().chunks.loadDistance; return engine->getSettings().chunks.loadDistance;
}); });
trackbar->consumer([=](double value) { trackbar->setConsumer([=](double value) {
engine->getSettings().chunks.loadDistance = static_cast<uint>(value); engine->getSettings().chunks.loadDistance = static_cast<uint>(value);
}); });
panel->add(trackbar); panel->add(trackbar);
@@ -528,10 +528,10 @@ void create_settings_panel(Engine* engine) {
})); }));
auto trackbar = std::make_shared<TrackBar>(1, 32, 10, 1, 1); auto trackbar = std::make_shared<TrackBar>(1, 32, 10, 1, 1);
trackbar->supplier([=]() { trackbar->setSupplier([=]() {
return engine->getSettings().chunks.loadSpeed; return engine->getSettings().chunks.loadSpeed;
}); });
trackbar->consumer([=](double value) { trackbar->setConsumer([=](double value) {
engine->getSettings().chunks.loadSpeed = static_cast<uint>(value); engine->getSettings().chunks.loadSpeed = static_cast<uint>(value);
}); });
panel->add(trackbar); panel->add(trackbar);
@@ -545,10 +545,10 @@ void create_settings_panel(Engine* engine) {
})); }));
auto trackbar = std::make_shared<TrackBar>(1.0, 6.0, 1.0, 0.1, 2); auto trackbar = std::make_shared<TrackBar>(1.0, 6.0, 1.0, 0.1, 2);
trackbar->supplier([=]() { trackbar->setSupplier([=]() {
return engine->getSettings().graphics.fogCurve; return engine->getSettings().graphics.fogCurve;
}); });
trackbar->consumer([=](double value) { trackbar->setConsumer([=](double value) {
engine->getSettings().graphics.fogCurve = value; engine->getSettings().graphics.fogCurve = value;
}); });
panel->add(trackbar); panel->add(trackbar);
@@ -561,10 +561,10 @@ void create_settings_panel(Engine* engine) {
})); }));
auto trackbar = std::make_shared<TrackBar>(30.0, 120.0, 90, 1, 4); auto trackbar = std::make_shared<TrackBar>(30.0, 120.0, 90, 1, 4);
trackbar->supplier([=]() { trackbar->setSupplier([=]() {
return engine->getSettings().camera.fov; return engine->getSettings().camera.fov;
}); });
trackbar->consumer([=](double value) { trackbar->setConsumer([=](double value) {
engine->getSettings().camera.fov = value; engine->getSettings().camera.fov = value;
}); });
panel->add(trackbar); panel->add(trackbar);
@@ -574,10 +574,10 @@ void create_settings_panel(Engine* engine) {
auto checkbox = std::make_shared<FullCheckBox>( auto checkbox = std::make_shared<FullCheckBox>(
langs::get(L"V-Sync", L"settings"), vec2(400, 32) langs::get(L"V-Sync", L"settings"), vec2(400, 32)
); );
checkbox->supplier([=]() { checkbox->setSupplier([=]() {
return engine->getSettings().display.swapInterval != 0; return engine->getSettings().display.swapInterval != 0;
}); });
checkbox->consumer([=](bool checked) { checkbox->setConsumer([=](bool checked) {
engine->getSettings().display.swapInterval = checked; engine->getSettings().display.swapInterval = checked;
}); });
panel->add(checkbox); panel->add(checkbox);
@@ -587,10 +587,10 @@ void create_settings_panel(Engine* engine) {
auto checkbox = std::make_shared<FullCheckBox>( auto checkbox = std::make_shared<FullCheckBox>(
langs::get(L"Backlight", L"settings"), vec2(400, 32) langs::get(L"Backlight", L"settings"), vec2(400, 32)
); );
checkbox->supplier([=]() { checkbox->setSupplier([=]() {
return engine->getSettings().graphics.backlight; return engine->getSettings().graphics.backlight;
}); });
checkbox->consumer([=](bool checked) { checkbox->setConsumer([=](bool checked) {
engine->getSettings().graphics.backlight = checked; engine->getSettings().graphics.backlight = checked;
}); });
panel->add(checkbox); panel->add(checkbox);