frontend/gui refactor

This commit is contained in:
MihailRis
2024-01-31 04:26:35 +03:00
parent 49fdcdfb27
commit 8c722b146e
17 changed files with 331 additions and 332 deletions
+14 -10
View File
@@ -8,6 +8,7 @@
#include "../../assets/Assets.h"
#include "../../graphics/Batch2D.h"
#include "../../graphics/Shader.h"
#include "../../graphics/GfxContext.h"
#include "../../window/Events.h"
#include "../../window/input.h"
#include "../../window/Camera.h"
@@ -42,10 +43,10 @@ PagesControl* GUI::getMenu() {
void GUI::actMouse(float delta) {
auto hover = container->getAt(Events::cursor, nullptr);
if (this->hover && this->hover != hover) {
this->hover->hover(false);
this->hover->setHover(false);
}
if (hover) {
hover->hover(true);
hover->setHover(true);
if (Events::scroll) {
hover->scrolled(Events::scroll);
}
@@ -84,7 +85,7 @@ void GUI::actMouse(float delta) {
}
void GUI::act(float delta) {
container->size(vec2(Window::width, Window::height));
container->setSize(vec2(Window::width, Window::height));
container->act(delta);
auto prevfocus = focus;
@@ -111,21 +112,24 @@ void GUI::act(float delta) {
}
}
}
if (focus && !focus->isfocused()) {
if (focus && !focus->isFocused()) {
focus = nullptr;
}
}
void GUI::draw(Batch2D* batch, Assets* assets) {
menu->setCoord((Window::size() - menu->size()) / 2.0f);
uicamera->setFov(Window::height);
void GUI::draw(const GfxContext* pctx, Assets* assets) {
auto& viewport = pctx->getViewport();
glm::vec2 wsize = viewport.size();
menu->setCoord((wsize - menu->getSize()) / 2.0f);
uicamera->setFov(wsize.y);
Shader* uishader = assets->getShader("ui");
uishader->use();
uishader->uniformMatrix("u_projview", uicamera->getProjection()*uicamera->getView());
batch->begin();
container->draw(batch, assets);
pctx->getBatch2D()->begin();
container->draw(pctx, assets);
}
shared_ptr<UINode> GUI::getFocused() const {
@@ -133,7 +137,7 @@ shared_ptr<UINode> GUI::getFocused() const {
}
bool GUI::isFocusCaught() const {
return focus && focus->isfocuskeeper();
return focus && focus->isFocuskeeper();
}
void GUI::addBack(std::shared_ptr<UINode> panel) {
+2 -2
View File
@@ -8,7 +8,7 @@
#include <functional>
#include <unordered_map>
class Batch2D;
class GfxContext;
class Assets;
class Camera;
@@ -71,7 +71,7 @@ namespace gui {
bool isFocusCaught() const;
void act(float delta);
void draw(Batch2D* batch, Assets* assets);
void draw(const GfxContext* pctx, Assets* assets);
void addBack(std::shared_ptr<UINode> panel);
void add(std::shared_ptr<UINode> panel);
void remove(std::shared_ptr<UINode> panel);
+35 -45
View File
@@ -2,42 +2,40 @@
#include "../../graphics/Batch2D.h"
using std::shared_ptr;
using gui::UINode;
using gui::Align;
using glm::vec2;
using glm::vec4;
UINode::UINode(vec2 coord, vec2 size) : coord(coord), size_(size) {
UINode::UINode(vec2 coord, vec2 size) : coord(coord), size(size) {
}
UINode::~UINode() {
}
bool UINode::visible() const {
return isvisible;
bool UINode::isVisible() const {
return visible;
}
void UINode::visible(bool flag) {
isvisible = flag;
void UINode::setVisible(bool flag) {
visible = flag;
}
Align UINode::align() const {
return align_;
Align UINode::getAlign() const {
return align;
}
void UINode::align(Align align) {
align_ = align;
void UINode::setAlign(Align align) {
this->align = align;
}
void UINode::hover(bool flag) {
hover_ = flag;
void UINode::setHover(bool flag) {
hover = flag;
}
bool UINode::hover() const {
return hover_;
bool UINode::isHover() const {
return hover;
}
void UINode::setParent(UINode* node) {
@@ -49,33 +47,33 @@ UINode* UINode::getParent() const {
}
void UINode::click(GUI*, int x, int y) {
pressed_ = true;
pressed = true;
}
void UINode::mouseRelease(GUI*, int x, int y) {
pressed_ = false;
pressed = false;
}
bool UINode::ispressed() const {
return pressed_;
bool UINode::isPressed() const {
return pressed;
}
void UINode::defocus() {
focused_ = false;
focused = false;
}
bool UINode::isfocused() const {
return focused_;
bool UINode::isFocused() const {
return focused;
}
bool UINode::isInside(glm::vec2 pos) {
vec2 coord = calcCoord();
vec2 size = this->size();
vec2 size = getSize();
return (pos.x >= coord.x && pos.y >= coord.y &&
pos.x < coord.x + size.x && pos.y < coord.y + size.y);
}
shared_ptr<UINode> UINode::getAt(vec2 pos, shared_ptr<UINode> self) {
std::shared_ptr<UINode> UINode::getAt(vec2 pos, std::shared_ptr<UINode> self) {
if (!interactive) {
return nullptr;
}
@@ -83,7 +81,7 @@ shared_ptr<UINode> UINode::getAt(vec2 pos, shared_ptr<UINode> self) {
}
bool UINode::isInteractive() const {
return interactive && visible();
return interactive && isVisible();
}
void UINode::setInteractive(bool flag) {
@@ -107,36 +105,28 @@ void UINode::setCoord(vec2 coord) {
this->coord = coord;
}
vec2 UINode::size() const {
return size_;
vec2 UINode::getSize() const {
return size;
}
void UINode::size(vec2 size) {
if (sizelock)
return;
this->size_ = size;
void UINode::setSize(vec2 size) {
this->size = size;
}
void UINode::_size(vec2 size) {
if (sizelock)
return;
this->size_ = size;
void UINode::setColor(vec4 color) {
this->color = color;
}
void UINode::color(vec4 color) {
this->color_ = color;
vec4 UINode::getColor() const {
return color;
}
vec4 UINode::color() const {
return color_;
void UINode::setMargin(vec4 margin) {
this->margin = margin;
}
void UINode::margin(vec4 margin) {
this->margin_ = margin;
}
vec4 UINode::margin() const {
return margin_;
vec4 UINode::getMargin() const {
return margin;
}
void UINode::lock() {
+27 -29
View File
@@ -6,7 +6,7 @@
#include <memory>
#include <functional>
class Batch2D;
class GfxContext;
class Assets;
namespace gui {
@@ -22,52 +22,51 @@ namespace gui {
class UINode {
protected:
glm::vec2 coord;
glm::vec2 size_;
glm::vec4 color_ {1.0f};
glm::vec4 margin_ {1.0f};
bool isvisible = true;
bool sizelock = false;
bool hover_ = false;
bool pressed_ = false;
bool focused_ = false;
glm::vec2 size;
glm::vec4 color {1.0f};
glm::vec4 margin {1.0f};
bool visible = true;
bool hover = false;
bool pressed = false;
bool focused = false;
bool interactive = true;
Align align_ = Align::left;
Align align = Align::left;
UINode* parent = nullptr;
UINode(glm::vec2 coord, glm::vec2 size);
public:
virtual ~UINode();
virtual void act(float delta) {};
virtual void draw(Batch2D* batch, Assets* assets) = 0;
virtual void draw(const GfxContext* pctx, Assets* assets) = 0;
virtual void visible(bool flag);
bool visible() const;
virtual void setVisible(bool flag);
bool isVisible() const;
virtual void align(Align align);
Align align() const;
virtual void setAlign(Align align);
Align getAlign() const;
virtual void hover(bool flag);
bool hover() const;
virtual void setHover(bool flag);
bool isHover() const;
virtual void setParent(UINode* node);
UINode* getParent() const;
virtual void color(glm::vec4 newColor);
glm::vec4 color() const;
virtual void setColor(glm::vec4 newColor);
glm::vec4 getColor() const;
virtual void margin(glm::vec4 margin);
glm::vec4 margin() const;
virtual void setMargin(glm::vec4 margin);
glm::vec4 getMargin() const;
virtual void focus(GUI*) {focused_ = true;}
virtual void focus(GUI*) {focused = true;}
virtual void click(GUI*, int x, int y);
virtual void clicked(GUI*, int button) {}
virtual void mouseMove(GUI*, int x, int y) {};
virtual void mouseRelease(GUI*, int x, int y);
virtual void scrolled(int value);
bool ispressed() const;
bool isPressed() const;
void defocus();
bool isfocused() const;
virtual bool isfocuskeeper() const {return false;}
bool isFocused() const;
virtual bool isFocuskeeper() const {return false;}
virtual void typed(unsigned int codepoint) {};
virtual void keyPressed(int key) {};
@@ -79,11 +78,10 @@ namespace gui {
virtual void setInteractive(bool flag);
virtual glm::vec2 contentOffset() {return glm::vec2(0.0f);};
glm::vec2 calcCoord() const;
virtual glm::vec2 calcCoord() const;
virtual void setCoord(glm::vec2 coord);
virtual glm::vec2 size() const;
virtual void size(glm::vec2 size);
void _size(glm::vec2 size);
virtual glm::vec2 getSize() const;
virtual void setSize(glm::vec2 size);
virtual void refresh() {};
virtual void lock();
};
+104 -107
View File
@@ -6,58 +6,53 @@
#include "../../assets/Assets.h"
#include "../../graphics/Batch2D.h"
#include "../../graphics/Font.h"
#include "../../graphics/GfxContext.h"
#include "../../util/stringutil.h"
#include "GUI.h"
using std::string;
using std::wstring;
using std::shared_ptr;
using glm::vec2;
using glm::vec3;
using glm::vec4;
const uint KEY_ESCAPE = 256;
const uint KEY_ENTER = 257;
const uint KEY_BACKSPACE = 259;
using namespace gui;
Label::Label(string text, string fontName)
Label::Label(std::string text, std::string fontName)
: UINode(vec2(), vec2(text.length() * 8, 15)),
text_(util::str2wstr_utf8(text)),
text(util::str2wstr_utf8(text)),
fontName_(fontName) {
}
Label::Label(wstring text, string fontName)
Label::Label(std::wstring text, std::string fontName)
: UINode(vec2(), vec2(text.length() * 8, 15)),
text_(text),
text(text),
fontName_(fontName) {
}
Label& Label::text(wstring text) {
this->text_ = text;
return *this;
void Label::setText(std::wstring text) {
this->text = text;
}
wstring Label::text() const {
return text_;
std::wstring Label::getText() const {
return text;
}
void Label::draw(Batch2D* batch, Assets* assets) {
void Label::draw(const GfxContext* pctx, Assets* assets) {
if (supplier) {
text(supplier());
setText(supplier());
}
batch->color = color_;
auto batch = pctx->getBatch2D();
batch->color = getColor();
Font* font = assets->getFont(fontName_);
vec2 size = UINode::size();
vec2 newsize = vec2(font->calcWidth(text_), font->lineHeight());
vec2 size = getSize();
vec2 newsize = vec2(font->calcWidth(text), font->lineHeight());
if (newsize.x > size.x) {
this->size(newsize);
setSize(newsize);
size = newsize;
}
vec2 coord = calcCoord();
font->draw(batch, text_, coord.x, coord.y);
font->draw(batch, text, coord.x, coord.y);
}
Label* Label::textSupplier(wstringsupplier supplier) {
@@ -65,58 +60,61 @@ Label* Label::textSupplier(wstringsupplier supplier) {
return this;
}
void Label::size(vec2 sizenew) {
UINode::size(vec2(UINode::size().x, sizenew.y));
void Label::setSize(vec2 sizenew) {
UINode::setSize(vec2(UINode::getSize().x, sizenew.y));
}
// ================================= Image ====================================
Image::Image(string texture, vec2 size) : UINode(vec2(), size), texture(texture) {
Image::Image(std::string texture, vec2 size) : UINode(vec2(), size), texture(texture) {
setInteractive(false);
}
void Image::draw(Batch2D* batch, Assets* assets) {
void Image::draw(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
vec4 color = getColor();
auto batch = pctx->getBatch2D();
batch->texture(assets->getTexture(texture));
batch->color = color_;
batch->rect(coord.x, coord.y, size_.x, size_.y, 0, 0, 0, UVRegion(), false, true, color_);
batch->color = color;
batch->rect(coord.x, coord.y, size.x, size.y,
0, 0, 0, UVRegion(), false, true, color);
}
// ================================= Button ===================================
Button::Button(shared_ptr<UINode> content, glm::vec4 padding)
: Panel(content->size()+vec2(padding[0]+padding[2]+content->margin()[0]+content->margin()[2],
padding[1]+padding[3]+content->margin()[1]+content->margin()[3]), padding, 0) {
Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
: Panel(vec2(), padding, 0) {
vec4 margin = getMargin();
setSize(content->getSize()+vec2(padding[0]+padding[2]+margin[0]+margin[2],
padding[1]+padding[3]+margin[1]+margin[3]));
add(content);
scrollable(false);
}
Button::Button(wstring text, glm::vec4 padding, glm::vec4 margin)
: Panel(vec2(32,32), padding, 0) {
this->margin(margin);
Label* label = new Label(text);
label->align(Align::center);
this->label = shared_ptr<UINode>(label);
add(this->label);
Button::Button(std::wstring text, glm::vec4 padding, glm::vec4 margin)
: Panel(vec2(32,32), padding, 0)
{
setMargin(margin);
scrollable(false);
label = std::make_shared<Label>(text);
label->setAlign(Align::center);
add(label);
}
void Button::text(std::wstring text) {
void Button::setText(std::wstring text) {
if (label) {
Label* label = (Label*)(this->label.get());
label->text(text);
label->setText(text);
}
}
wstring Button::text() const {
std::wstring Button::getText() const {
if (label) {
Label* label = (Label*)(this->label.get());
return label->text();
return label->getText();
}
return L"";
}
Button* Button::textSupplier(wstringsupplier supplier) {
if (label) {
Label* label = (Label*)(this->label.get());
label->textSupplier(supplier);
}
return this;
@@ -126,14 +124,15 @@ void Button::setHoverColor(glm::vec4 color) {
hoverColor = color;
}
void Button::drawBackground(Batch2D* batch, Assets* assets) {
void Button::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = (ispressed() ? pressedColor : (hover_ ? hoverColor : color_));
batch->rect(coord.x, coord.y, size_.x, size_.y);
batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color));
batch->rect(coord.x, coord.y, size.x, size.y);
}
shared_ptr<UINode> Button::getAt(vec2 pos, shared_ptr<UINode> self) {
std::shared_ptr<UINode> Button::getAt(vec2 pos, std::shared_ptr<UINode> self) {
return UINode::getAt(pos, self);
}
@@ -153,8 +152,7 @@ Button* Button::listenAction(onaction action) {
void Button::textAlign(Align align) {
if (label) {
Label* label = (Label*)(this->label.get());
label->align(align);
label->setAlign(align);
refresh();
}
}
@@ -181,55 +179,58 @@ void RichButton::setHoverColor(glm::vec4 color) {
hoverColor = color;
}
void RichButton::drawBackground(Batch2D* batch, Assets* assets) {
void RichButton::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = (ispressed() ? pressedColor : (hover_ ? hoverColor : color_));
batch->rect(coord.x, coord.y, size_.x, size_.y);
batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color));
batch->rect(coord.x, coord.y, size.x, size.y);
}
// ================================ TextBox ===================================
TextBox::TextBox(wstring placeholder, vec4 padding)
TextBox::TextBox(std::wstring placeholder, vec4 padding)
: Panel(vec2(200,32), padding, 0, false),
input(L""),
placeholder(placeholder) {
label = new Label(L"");
add(shared_ptr<UINode>(label));
label = std::make_shared<Label>(L"");
add(label);
}
void TextBox::drawBackground(Batch2D* batch, Assets* assets) {
void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
if (valid) {
if (isfocused()) {
if (isFocused()) {
batch->color = focusedColor;
} else if (hover_) {
} else if (hover) {
batch->color = hoverColor;
} else {
batch->color = color_;
batch->color = color;
}
} else {
batch->color = invalidColor;
}
batch->rect(coord.x, coord.y, size_.x, size_.y);
if (!focused_ && supplier) {
batch->rect(coord.x, coord.y, size.x, size.y);
if (!isFocused() && supplier) {
input = supplier();
}
if (input.empty()) {
label->color(vec4(0.5f));
label->text(placeholder);
label->setColor(vec4(0.5f));
label->setText(placeholder);
} else {
label->color(vec4(1.0f));
label->text(input);
label->setColor(vec4(1.0f));
label->setText(input);
}
scrollable(false);
}
void TextBox::typed(unsigned int codepoint) {
input += wstring({(wchar_t)codepoint});
input += std::wstring({(wchar_t)codepoint});
validate();
}
@@ -262,19 +263,16 @@ void TextBox::focus(GUI* gui) {
}
void TextBox::keyPressed(int key) {
switch (key) {
case KEY_BACKSPACE:
if (!input.empty()){
input = input.substr(0, input.length()-1);
validate();
}
break;
case KEY_ENTER:
if (validate() && consumer) {
consumer(label->text());
}
defocus();
break;
if (key == keycode::BACKSPACE) {
if (!input.empty()){
input = input.substr(0, input.length()-1);
validate();
}
} else if (key == keycode::ENTER) {
if (validate() && consumer) {
consumer(label->getText());
}
defocus();
}
// Pasting text from clipboard
if (key == keycode::V && Events::pressed(keycode::LEFT_CONTROL)) {
@@ -286,7 +284,7 @@ void TextBox::keyPressed(int key) {
}
}
shared_ptr<UINode> TextBox::getAt(vec2 pos, shared_ptr<UINode> self) {
std::shared_ptr<UINode> TextBox::getAt(vec2 pos, std::shared_ptr<UINode> self) {
return UINode::getAt(pos, self);
}
@@ -302,7 +300,7 @@ void TextBox::textValidator(wstringchecker validator) {
this->validator = validator;
}
wstring TextBox::text() const {
std::wstring TextBox::text() const {
if (input.empty())
return placeholder;
return input;
@@ -321,16 +319,13 @@ InputBindBox::InputBindBox(Binding& binding, vec4 padding)
scrollable(false);
}
shared_ptr<UINode> InputBindBox::getAt(vec2 pos, shared_ptr<UINode> self) {
return UINode::getAt(pos, self);
}
void InputBindBox::drawBackground(Batch2D* batch, Assets* assets) {
void InputBindBox::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = (isfocused() ? focusedColor : (hover_ ? hoverColor : color_));
batch->rect(coord.x, coord.y, size_.x, size_.y);
label->text(util::str2wstr_utf8(binding.text()));
batch->color = (isFocused() ? focusedColor : (hover ? hoverColor : color));
batch->rect(coord.x, coord.y, size.x, size.y);
label->setText(util::str2wstr_utf8(binding.text()));
}
void InputBindBox::clicked(GUI*, int button) {
@@ -359,24 +354,25 @@ TrackBar::TrackBar(double min,
value(value),
step(step),
trackWidth(trackWidth) {
color(vec4(0.f, 0.f, 0.f, 0.4f));
setColor(vec4(0.f, 0.f, 0.f, 0.4f));
}
void TrackBar::draw(Batch2D* batch, Assets* assets) {
void TrackBar::draw(const GfxContext* pctx, Assets* assets) {
if (supplier_) {
value = supplier_();
}
vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = (hover_ ? hoverColor : color_);
batch->rect(coord.x, coord.y, size_.x, size_.y);
batch->color = (hover ? hoverColor : color);
batch->rect(coord.x, coord.y, size.x, size.y);
float width = size_.x;
float width = size.x;
float t = (value - min) / (max-min+trackWidth*step);
batch->color = trackColor;
int actualWidth = size_.x * (trackWidth / (max-min+trackWidth*step) * step);
batch->rect(coord.x + width * t, coord.y, actualWidth, size_.y);
int actualWidth = size.x * (trackWidth / (max-min+trackWidth*step) * step);
batch->rect(coord.x + width * t, coord.y, actualWidth, size.y);
}
void TrackBar::supplier(doublesupplier supplier) {
@@ -391,7 +387,7 @@ void TrackBar::mouseMove(GUI*, int x, int y) {
vec2 coord = calcCoord();
value = x;
value -= coord.x;
value = (value)/size_.x * (max-min+trackWidth*step);
value = (value)/size.x * (max-min+trackWidth*step);
value += min;
value = (value > max) ? max : value;
value = (value < min) ? min : value;
@@ -403,17 +399,18 @@ void TrackBar::mouseMove(GUI*, int x, int y) {
// ================================ CheckBox ==================================
CheckBox::CheckBox(bool checked) : UINode(vec2(), vec2(32.0f)), checked_(checked) {
color(vec4(0.0f, 0.0f, 0.0f, 0.5f));
setColor(vec4(0.0f, 0.0f, 0.0f, 0.5f));
}
void CheckBox::draw(Batch2D* batch, Assets* assets) {
void CheckBox::draw(const GfxContext* pctx, Assets* assets) {
if (supplier_) {
checked_ = supplier_();
}
vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = checked_ ? checkColor : (hover_ ? hoverColor : color_);
batch->rect(coord.x, coord.y, size_.x, size_.y);
batch->color = checked_ ? checkColor : (hover ? hoverColor : color);
batch->rect(coord.x, coord.y, size.x, size.y);
}
void CheckBox::mouseRelease(GUI*, int x, int y) {
@@ -439,12 +436,12 @@ CheckBox* CheckBox::checked(bool flag) {
FullCheckBox::FullCheckBox(std::wstring text, glm::vec2 size, bool checked)
: Panel(size),
checkbox(std::make_shared<CheckBox>(checked)){
color(vec4(0.0f));
setColor(vec4(0.0f));
orientation(Orientation::horizontal);
add(checkbox);
auto label = std::make_shared<Label>(text);
label->margin(vec4(5.0f, 5.0f, 0.0f, 0.0f));
label->setMargin(vec4(5.f, 5.f, 0.f, 0.f));
add(label);
}
+18 -22
View File
@@ -28,23 +28,20 @@ namespace gui {
class Label : public UINode {
protected:
std::wstring text_;
std::wstring text;
std::string fontName_;
wstringsupplier supplier = nullptr;
public:
Label(std::string text, std::string fontName="normal");
Label(std::wstring text, std::string fontName="normal");
virtual Label& text(std::wstring text);
std::wstring text() const;
virtual void setText(std::wstring text);
std::wstring getText() const;
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual Label* textSupplier(wstringsupplier supplier);
virtual glm::vec2 size() const override {
return UINode::size();
}
virtual void size(glm::vec2 size) override;
virtual void setSize(glm::vec2 size) override;
};
class Image : public UINode {
@@ -53,7 +50,7 @@ namespace gui {
public:
Image(std::string texture, glm::vec2 size);
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual void draw(const GfxContext* pctx, Assets* assets) override;
};
class Button : public Panel {
@@ -61,14 +58,14 @@ namespace gui {
glm::vec4 hoverColor {0.05f, 0.1f, 0.15f, 0.75f};
glm::vec4 pressedColor {0.0f, 0.0f, 0.0f, 0.95f};
std::vector<onaction> actions;
std::shared_ptr<UINode> label = nullptr;
std::shared_ptr<Label> label = nullptr;
public:
Button(std::shared_ptr<UINode> content, glm::vec4 padding=glm::vec4(2.0f));
Button(std::wstring text,
glm::vec4 padding=glm::vec4(2.0f),
glm::vec4 margin=glm::vec4(1.0f));
virtual void drawBackground(Batch2D* batch, Assets* assets) override;
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
@@ -77,8 +74,8 @@ namespace gui {
virtual void textAlign(Align align);
virtual void text(std::wstring text);
virtual std::wstring text() const;
virtual void setText(std::wstring text);
virtual std::wstring getText() const;
virtual Button* textSupplier(wstringsupplier supplier);
@@ -93,7 +90,7 @@ namespace gui {
public:
RichButton(glm::vec2 size);
virtual void drawBackground(Batch2D* batch, Assets* assets) override;
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void mouseRelease(GUI*, int x, int y) override;
virtual RichButton* listenAction(onaction action);
@@ -106,7 +103,7 @@ namespace gui {
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};
glm::vec4 invalidColor {0.1f, 0.05f, 0.03f, 1.0f};
Label* label;
std::shared_ptr<Label> label;
std::wstring input;
std::wstring placeholder;
wstringsupplier supplier = nullptr;
@@ -120,13 +117,13 @@ namespace gui {
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void drawBackground(Batch2D* batch, Assets* assets) override;
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void typed(unsigned int codepoint) override;
virtual void keyPressed(int key) override;
virtual void textSupplier(wstringsupplier supplier);
virtual void textConsumer(wstringconsumer consumer);
virtual void textValidator(wstringchecker validator);
virtual bool isfocuskeeper() const override {return true;}
virtual bool isFocuskeeper() const override {return true;}
virtual std::wstring text() const;
virtual void text(std::wstring value);
virtual bool validate();
@@ -144,12 +141,11 @@ namespace gui {
Binding& binding;
public:
InputBindBox(Binding& binding, glm::vec4 padding=glm::vec4(6.0f));
virtual void drawBackground(Batch2D* batch, Assets* assets) override;
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void clicked(GUI*, int button) override;
virtual void keyPressed(int key) override;
virtual bool isfocuskeeper() const override {return true;}
virtual bool isFocuskeeper() const override {return true;}
};
class TrackBar : public UINode {
@@ -169,7 +165,7 @@ namespace gui {
double value,
double step=1.0,
int trackWidth=1);
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void supplier(doublesupplier supplier);
virtual void consumer(doubleconsumer consumer);
@@ -187,7 +183,7 @@ namespace gui {
public:
CheckBox(bool checked=false);
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void mouseRelease(GUI*, int x, int y) override;
+3 -3
View File
@@ -29,7 +29,7 @@ Button* guiutil::gotoButton(
void guiutil::alert(GUI* gui, const std::wstring& text, gui::runnable on_hidden) {
PagesControl* menu = gui->getMenu();
Panel* panel = new Panel(vec2(500, 200), vec4(8.0f), 8.0f);
panel->color(vec4(0.0f, 0.0f, 0.0f, 0.5f));
panel->setColor(vec4(0.0f, 0.0f, 0.0f, 0.5f));
// TODO: implement built-in text wrapping
const int wrap_length = 60;
@@ -70,10 +70,10 @@ void guiutil::confirm(
PagesControl* menu = gui->getMenu();
Panel* panel = new Panel(vec2(600, 200), vec4(8.0f), 8.0f);
panel->color(vec4(0.0f, 0.0f, 0.0f, 0.5f));
panel->setColor(vec4(0.0f, 0.0f, 0.0f, 0.5f));
panel->add(new Label(text));
Panel* subpanel = new Panel(vec2(600, 53));
subpanel->color(vec4(0));
subpanel->setColor(vec4(0));
subpanel->add((new Button(yestext, vec4(8.0f)))->listenAction([=](GUI*){
if (on_confirm)
on_confirm();
+48 -50
View File
@@ -5,8 +5,7 @@
#include "../../window/Window.h"
#include "../../assets/Assets.h"
#include "../../graphics/Batch2D.h"
using std::shared_ptr;
#include "../../graphics/GfxContext.h"
using namespace gui;
@@ -17,7 +16,7 @@ Container::Container(vec2 coord, vec2 size) : UINode(coord, size) {
actualLength = size.y;
}
shared_ptr<UINode> Container::getAt(vec2 pos, shared_ptr<UINode> self) {
std::shared_ptr<UINode> Container::getAt(vec2 pos, std::shared_ptr<UINode> self) {
if (!interactive) {
return nullptr;
}
@@ -25,7 +24,7 @@ shared_ptr<UINode> Container::getAt(vec2 pos, shared_ptr<UINode> self) {
for (int i = nodes.size()-1; i >= 0; i--) {
auto& node = nodes[i];
if (!node->visible())
if (!node->isVisible())
continue;
auto hover = node->getAt(pos, node);
if (hover != nullptr) {
@@ -54,14 +53,14 @@ void Container::act(float delta) {
), intervalEvents.end());
for (auto node : nodes) {
if (node->visible()) {
if (node->isVisible()) {
node->act(delta);
}
}
}
void Container::scrolled(int value) {
int diff = (actualLength-size().y);
int diff = (actualLength-getSize().y);
if (diff > 0 && scrollable_) {
scroll += value * 40;
if (scroll > 0)
@@ -78,46 +77,50 @@ void Container::scrollable(bool flag) {
scrollable_ = flag;
}
void Container::draw(Batch2D* batch, Assets* assets) {
void Container::draw(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
vec2 size = this->size();
drawBackground(batch, assets);
vec2 size = getSize();
drawBackground(pctx, assets);
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->render();
Window::pushScissor(vec4(coord.x, coord.y, size.x, size.y));
for (auto node : nodes) {
if (node->visible())
node->draw(batch, assets);
{
GfxContext ctx = pctx->sub();
ctx.scissors(vec4(coord.x, coord.y, size.x, size.y));
for (auto node : nodes) {
if (node->isVisible())
node->draw(pctx, assets);
}
batch->render();
}
batch->render();
Window::popScissor();
}
void Container::addBack(shared_ptr<UINode> node) {
void Container::addBack(std::shared_ptr<UINode> node) {
nodes.insert(nodes.begin(), node);
node->setParent(this);
refresh();
}
void Container::add(shared_ptr<UINode> node) {
void Container::add(std::shared_ptr<UINode> node) {
nodes.push_back(node);
node->setParent(this);
refresh();
}
void Container::add(UINode* node) {
add(shared_ptr<UINode>(node));
add(std::shared_ptr<UINode>(node));
}
void Container::add(shared_ptr<UINode> node, glm::vec2 coord) {
void Container::add(std::shared_ptr<UINode> node, glm::vec2 coord) {
node->setCoord(coord);
add(node);
}
void Container::remove(shared_ptr<UINode> selected) {
void Container::remove(std::shared_ptr<UINode> selected) {
selected->setParent(nullptr);
nodes.erase(std::remove_if(nodes.begin(), nodes.end(),
[selected](const shared_ptr<UINode> node) {
[selected](const std::shared_ptr<UINode> node) {
return node == selected;
}
), nodes.end());
@@ -133,17 +136,19 @@ Panel::Panel(vec2 size, glm::vec4 padding, float interval, bool resizing)
padding(padding),
interval(interval),
resizing_(resizing) {
color_ = vec4(0.0f, 0.0f, 0.0f, 0.75f);
setColor(vec4(0.0f, 0.0f, 0.0f, 0.75f));
}
Panel::~Panel() {
}
void Panel::drawBackground(Batch2D* batch, Assets* assets) {
void Panel::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->color = color_;
batch->rect(coord.x, coord.y, size_.x, size_.y);
batch->color = color;
batch->rect(coord.x, coord.y, size.x, size.y);
}
void Panel::maxLength(int value) {
@@ -157,22 +162,22 @@ int Panel::maxLength() const {
void Panel::refresh() {
float x = padding.x;
float y = padding.y;
vec2 size = this->size();
vec2 size = getSize();
if (orientation_ == Orientation::vertical) {
float maxw = size.x;
for (auto& node : nodes) {
vec2 nodesize = node->size();
const vec4 margin = node->margin();
vec2 nodesize = node->getSize();
const vec4 margin = node->getMargin();
y += margin.y;
float ex;
float spacex = size.x - margin.z - padding.z;
switch (node->align()) {
switch (node->getAlign()) {
case Align::center:
ex = x + fmax(0.0f, spacex - node->size().x) / 2.0f;
ex = x + fmax(0.0f, spacex - nodesize.x) / 2.0f;
break;
case Align::right:
ex = x + spacex - node->size().x;
ex = x + spacex - nodesize.x;
break;
default:
ex = x + margin.x;
@@ -181,36 +186,36 @@ void Panel::refresh() {
y += nodesize.y + margin.w + interval;
float width = size.x - padding.x - padding.z - margin.x - margin.z;
node->size(vec2(width, nodesize.y));;
node->setSize(vec2(width, nodesize.y));;
node->refresh();
maxw = fmax(maxw, ex+node->size().x+margin.z+padding.z);
maxw = fmax(maxw, ex+node->getSize().x+margin.z+padding.z);
}
if (resizing_) {
if (maxLength_)
this->size(vec2(size.x, glm::min(maxLength_, (int)(y+padding.w))));
setSize(vec2(size.x, glm::min(maxLength_, (int)(y+padding.w))));
else
this->size(vec2(size.x, y+padding.w));
setSize(vec2(size.x, y+padding.w));
}
actualLength = y + padding.w;
} else {
float maxh = size.y;
for (auto& node : nodes) {
vec2 nodesize = node->size();
const vec4 margin = node->margin();
vec2 nodesize = node->getSize();
const vec4 margin = node->getMargin();
x += margin.x;
node->setCoord(vec2(x, y+margin.y));
x += nodesize.x + margin.z + interval;
float height = size.y - padding.y - padding.w - margin.y - margin.w;
node->size(vec2(nodesize.x, height));
node->setSize(vec2(nodesize.x, height));
node->refresh();
maxh = fmax(maxh, y+margin.y+node->size().y+margin.w+padding.w);
maxh = fmax(maxh, y+margin.y+node->getSize().y+margin.w+padding.w);
}
if (resizing_) {
if (maxLength_)
this->size(vec2(glm::min(maxLength_, (int)(x+padding.z)), size.y));
setSize(vec2(glm::min(maxLength_, (int)(x+padding.z)), size.y));
else
this->size(vec2(x+padding.z, size.y));
setSize(vec2(x+padding.z, size.y));
}
actualLength = size.y;
}
@@ -224,13 +229,6 @@ Orientation Panel::orientation() const {
return orientation_;
}
void Panel::lock(){
for (auto node : nodes) {
node->lock();
}
resizing_ = false;
}
PagesControl::PagesControl() : Container(vec2(), vec2(1)){
}
@@ -243,7 +241,7 @@ void PagesControl::add(std::string name, std::shared_ptr<UINode> panel) {
}
void PagesControl::add(std::string name, UINode* panel) {
add(name, shared_ptr<UINode>(panel));
add(name, std::shared_ptr<UINode>(panel));
}
void PagesControl::set(std::string name, bool history) {
@@ -260,7 +258,7 @@ void PagesControl::set(std::string name, bool history) {
curname_ = name;
current_ = found->second;
Container::add(current_.panel);
size(current_.panel->size());
setSize(current_.panel->getSize());
}
void PagesControl::back() {
+3 -4
View File
@@ -34,8 +34,8 @@ namespace gui {
Container(glm::vec2 coord, glm::vec2 size);
virtual void act(float delta) override;
virtual void drawBackground(Batch2D* batch, Assets* assets) {};
virtual void draw(Batch2D* batch, Assets* assets) override;
virtual void drawBackground(const GfxContext* pctx, Assets* assets) {};
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void addBack(std::shared_ptr<UINode> node);
virtual void add(std::shared_ptr<UINode> node);
@@ -59,13 +59,12 @@ namespace gui {
Panel(glm::vec2 size, glm::vec4 padding=glm::vec4(2.0f), float interval=2.0f, bool resizing=true);
virtual ~Panel();
virtual void drawBackground(Batch2D* batch, Assets* assets) override;
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void orientation(Orientation orientation);
Orientation orientation() const;
virtual void refresh() override;
virtual void lock() override;
virtual void maxLength(int value);
int maxLength() const;