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

View File

@ -17,6 +17,7 @@
#include "window/Camera.h" #include "window/Camera.h"
#include "window/input.h" #include "window/input.h"
#include "graphics/Batch2D.h" #include "graphics/Batch2D.h"
#include "graphics/GfxContext.h"
#include "graphics/Shader.h" #include "graphics/Shader.h"
#include "graphics/ImageData.h" #include "graphics/ImageData.h"
#include "frontend/gui/GUI.h" #include "frontend/gui/GUI.h"
@ -113,7 +114,11 @@ void Engine::mainloop() {
if (!Window::isIconified()) { if (!Window::isIconified()) {
screen->draw(delta); screen->draw(delta);
gui->draw(&batch, assets.get());
Viewport viewport(Window::width, Window::height);
GfxContext ctx(nullptr, viewport, &batch);
gui->draw(&ctx, assets.get());
Window::swapInterval(settings.display.swapInterval); Window::swapInterval(settings.display.swapInterval);
} else { } else {
Window::swapInterval(1); Window::swapInterval(1);

View File

@ -160,22 +160,23 @@ SlotView::SlotView(
content(content), content(content),
stack(stack), stack(stack),
layout(layout) { layout(layout) {
color(glm::vec4(0, 0, 0, 0.2f)); setColor(glm::vec4(0, 0, 0, 0.2f));
} }
// performance disaster // performance disaster
void SlotView::draw(Batch2D* batch, Assets* assets) { void SlotView::draw(const GfxContext* pctx, Assets* assets) {
glm::vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
int slotSize = InventoryView::SLOT_SIZE; int slotSize = InventoryView::SLOT_SIZE;
glm::vec4 tint(1.0f); glm::vec4 tint(1.0f);
glm::vec4 color = color_; glm::vec4 color = getColor();
if (hover_ || highlighted) { if (hover || highlighted) {
tint *= 1.333f; tint *= 1.333f;
color = glm::vec4(1, 1, 1, 0.2f); color = glm::vec4(1, 1, 1, 0.2f);
} }
auto batch = pctx->getBatch2D();
batch->color = color; batch->color = color;
if (color.a > 0.0) { if (color.a > 0.0) {
batch->texture(nullptr); batch->texture(nullptr);
@ -313,8 +314,8 @@ InventoryView::InventoryView(
layout(std::move(layout)), layout(std::move(layout)),
frontend(frontend), frontend(frontend),
interaction(interaction) { interaction(interaction) {
size(this->layout->getSize()); setSize(this->layout->getSize());
color(glm::vec4(0, 0, 0, 0.0f)); setColor(glm::vec4(0, 0, 0, 0.0f));
} }
InventoryView::~InventoryView() {} InventoryView::~InventoryView() {}
@ -331,7 +332,7 @@ void InventoryView::build() {
item, frontend, interaction, content, slot item, frontend, interaction, content, slot
); );
if (!slot.background) { if (!slot.background) {
view->color(glm::vec4()); view->setColor(glm::vec4());
} }
slots.push_back(view.get()); slots.push_back(view.get());
add(view, slot.position); add(view, slot.position);
@ -358,8 +359,9 @@ InventoryLayout* InventoryView::getLayout() const {
return layout.get(); return layout.get();
} }
void InventoryView::drawBackground(Batch2D* batch, Assets* assets) { void InventoryView::drawBackground(const GfxContext* pctx, Assets* assets) {
glm::vec2 coord = calcCoord(); glm::vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);

View File

@ -11,7 +11,6 @@
#include "../items/ItemStack.h" #include "../items/ItemStack.h"
#include "../typedefs.h" #include "../typedefs.h"
class Batch2D;
class Assets; class Assets;
class GfxContext; class GfxContext;
class Content; class Content;
@ -102,7 +101,7 @@ public:
const Content* content, const Content* content,
SlotLayout layout); SlotLayout layout);
virtual void draw(Batch2D* batch, Assets* assets) override; virtual void draw(const GfxContext* pctx, Assets* assets) override;
void setHighlighted(bool flag); void setHighlighted(bool flag);
bool isHighlighted() const; bool isHighlighted() const;
@ -135,7 +134,7 @@ public:
void build(); void build();
virtual void drawBackground(Batch2D* batch, Assets* assets) override; virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
void setInventory(std::shared_ptr<Inventory> inventory); void setInventory(std::shared_ptr<Inventory> inventory);

View File

@ -8,6 +8,7 @@
#include "../../assets/Assets.h" #include "../../assets/Assets.h"
#include "../../graphics/Batch2D.h" #include "../../graphics/Batch2D.h"
#include "../../graphics/Shader.h" #include "../../graphics/Shader.h"
#include "../../graphics/GfxContext.h"
#include "../../window/Events.h" #include "../../window/Events.h"
#include "../../window/input.h" #include "../../window/input.h"
#include "../../window/Camera.h" #include "../../window/Camera.h"
@ -42,10 +43,10 @@ PagesControl* GUI::getMenu() {
void GUI::actMouse(float delta) { void GUI::actMouse(float delta) {
auto hover = container->getAt(Events::cursor, nullptr); auto hover = container->getAt(Events::cursor, nullptr);
if (this->hover && this->hover != hover) { if (this->hover && this->hover != hover) {
this->hover->hover(false); this->hover->setHover(false);
} }
if (hover) { if (hover) {
hover->hover(true); hover->setHover(true);
if (Events::scroll) { if (Events::scroll) {
hover->scrolled(Events::scroll); hover->scrolled(Events::scroll);
} }
@ -84,7 +85,7 @@ void GUI::actMouse(float delta) {
} }
void GUI::act(float delta) { void GUI::act(float delta) {
container->size(vec2(Window::width, Window::height)); container->setSize(vec2(Window::width, Window::height));
container->act(delta); container->act(delta);
auto prevfocus = focus; auto prevfocus = focus;
@ -111,21 +112,24 @@ void GUI::act(float delta) {
} }
} }
} }
if (focus && !focus->isfocused()) { if (focus && !focus->isFocused()) {
focus = nullptr; focus = nullptr;
} }
} }
void GUI::draw(Batch2D* batch, Assets* assets) { void GUI::draw(const GfxContext* pctx, Assets* assets) {
menu->setCoord((Window::size() - menu->size()) / 2.0f); auto& viewport = pctx->getViewport();
uicamera->setFov(Window::height); glm::vec2 wsize = viewport.size();
menu->setCoord((wsize - menu->getSize()) / 2.0f);
uicamera->setFov(wsize.y);
Shader* uishader = assets->getShader("ui"); Shader* uishader = assets->getShader("ui");
uishader->use(); uishader->use();
uishader->uniformMatrix("u_projview", uicamera->getProjection()*uicamera->getView()); uishader->uniformMatrix("u_projview", uicamera->getProjection()*uicamera->getView());
batch->begin(); pctx->getBatch2D()->begin();
container->draw(batch, assets); container->draw(pctx, assets);
} }
shared_ptr<UINode> GUI::getFocused() const { shared_ptr<UINode> GUI::getFocused() const {
@ -133,7 +137,7 @@ shared_ptr<UINode> GUI::getFocused() const {
} }
bool GUI::isFocusCaught() const { bool GUI::isFocusCaught() const {
return focus && focus->isfocuskeeper(); return focus && focus->isFocuskeeper();
} }
void GUI::addBack(std::shared_ptr<UINode> panel) { void GUI::addBack(std::shared_ptr<UINode> panel) {

View File

@ -8,7 +8,7 @@
#include <functional> #include <functional>
#include <unordered_map> #include <unordered_map>
class Batch2D; class GfxContext;
class Assets; class Assets;
class Camera; class Camera;
@ -71,7 +71,7 @@ namespace gui {
bool isFocusCaught() const; bool isFocusCaught() const;
void act(float delta); 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 addBack(std::shared_ptr<UINode> panel);
void add(std::shared_ptr<UINode> panel); void add(std::shared_ptr<UINode> panel);
void remove(std::shared_ptr<UINode> panel); void remove(std::shared_ptr<UINode> panel);

View File

@ -2,42 +2,40 @@
#include "../../graphics/Batch2D.h" #include "../../graphics/Batch2D.h"
using std::shared_ptr;
using gui::UINode; using gui::UINode;
using gui::Align; using gui::Align;
using glm::vec2; using glm::vec2;
using glm::vec4; 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() { UINode::~UINode() {
} }
bool UINode::visible() const { bool UINode::isVisible() const {
return isvisible; return visible;
} }
void UINode::visible(bool flag) { void UINode::setVisible(bool flag) {
isvisible = flag; visible = flag;
} }
Align UINode::align() const { Align UINode::getAlign() const {
return align_; return align;
} }
void UINode::align(Align align) { void UINode::setAlign(Align align) {
align_ = align; this->align = align;
} }
void UINode::hover(bool flag) { void UINode::setHover(bool flag) {
hover_ = flag; hover = flag;
} }
bool UINode::hover() const { bool UINode::isHover() const {
return hover_; return hover;
} }
void UINode::setParent(UINode* node) { void UINode::setParent(UINode* node) {
@ -49,33 +47,33 @@ UINode* UINode::getParent() const {
} }
void UINode::click(GUI*, int x, int y) { void UINode::click(GUI*, int x, int y) {
pressed_ = true; pressed = true;
} }
void UINode::mouseRelease(GUI*, int x, int y) { void UINode::mouseRelease(GUI*, int x, int y) {
pressed_ = false; pressed = false;
} }
bool UINode::ispressed() const { bool UINode::isPressed() const {
return pressed_; return pressed;
} }
void UINode::defocus() { void UINode::defocus() {
focused_ = false; focused = false;
} }
bool UINode::isfocused() const { bool UINode::isFocused() const {
return focused_; return focused;
} }
bool UINode::isInside(glm::vec2 pos) { bool UINode::isInside(glm::vec2 pos) {
vec2 coord = calcCoord(); vec2 coord = calcCoord();
vec2 size = this->size(); vec2 size = getSize();
return (pos.x >= coord.x && pos.y >= coord.y && return (pos.x >= coord.x && pos.y >= coord.y &&
pos.x < coord.x + size.x && pos.y < coord.y + size.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) { if (!interactive) {
return nullptr; return nullptr;
} }
@ -83,7 +81,7 @@ shared_ptr<UINode> UINode::getAt(vec2 pos, shared_ptr<UINode> self) {
} }
bool UINode::isInteractive() const { bool UINode::isInteractive() const {
return interactive && visible(); return interactive && isVisible();
} }
void UINode::setInteractive(bool flag) { void UINode::setInteractive(bool flag) {
@ -107,36 +105,28 @@ void UINode::setCoord(vec2 coord) {
this->coord = coord; this->coord = coord;
} }
vec2 UINode::size() const { vec2 UINode::getSize() const {
return size_; return size;
} }
void UINode::size(vec2 size) { void UINode::setSize(vec2 size) {
if (sizelock) this->size = size;
return;
this->size_ = size;
} }
void UINode::_size(vec2 size) { void UINode::setColor(vec4 color) {
if (sizelock) this->color = color;
return;
this->size_ = size;
} }
void UINode::color(vec4 color) { vec4 UINode::getColor() const {
this->color_ = color; return color;
} }
vec4 UINode::color() const { void UINode::setMargin(vec4 margin) {
return color_; this->margin = margin;
} }
void UINode::margin(vec4 margin) { vec4 UINode::getMargin() const {
this->margin_ = margin; return margin;
}
vec4 UINode::margin() const {
return margin_;
} }
void UINode::lock() { void UINode::lock() {

View File

@ -6,7 +6,7 @@
#include <memory> #include <memory>
#include <functional> #include <functional>
class Batch2D; class GfxContext;
class Assets; class Assets;
namespace gui { namespace gui {
@ -22,52 +22,51 @@ namespace gui {
class UINode { class UINode {
protected: protected:
glm::vec2 coord; glm::vec2 coord;
glm::vec2 size_; glm::vec2 size;
glm::vec4 color_ {1.0f}; glm::vec4 color {1.0f};
glm::vec4 margin_ {1.0f}; glm::vec4 margin {1.0f};
bool isvisible = true; bool visible = true;
bool sizelock = false; bool hover = false;
bool hover_ = false; bool pressed = false;
bool pressed_ = false; bool focused = false;
bool focused_ = false;
bool interactive = true; bool interactive = true;
Align align_ = Align::left; Align align = Align::left;
UINode* parent = nullptr; UINode* parent = nullptr;
UINode(glm::vec2 coord, glm::vec2 size); UINode(glm::vec2 coord, glm::vec2 size);
public: public:
virtual ~UINode(); virtual ~UINode();
virtual void act(float delta) {}; 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); virtual void setVisible(bool flag);
bool visible() const; bool isVisible() const;
virtual void align(Align align); virtual void setAlign(Align align);
Align align() const; Align getAlign() const;
virtual void hover(bool flag); virtual void setHover(bool flag);
bool hover() const; bool isHover() const;
virtual void setParent(UINode* node); virtual void setParent(UINode* node);
UINode* getParent() const; UINode* getParent() const;
virtual void color(glm::vec4 newColor); virtual void setColor(glm::vec4 newColor);
glm::vec4 color() const; glm::vec4 getColor() const;
virtual void margin(glm::vec4 margin); virtual void setMargin(glm::vec4 margin);
glm::vec4 margin() const; 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 click(GUI*, int x, int y);
virtual void clicked(GUI*, int button) {} virtual void clicked(GUI*, int button) {}
virtual void mouseMove(GUI*, int x, int y) {}; virtual void mouseMove(GUI*, int x, int y) {};
virtual void mouseRelease(GUI*, int x, int y); virtual void mouseRelease(GUI*, int x, int y);
virtual void scrolled(int value); virtual void scrolled(int value);
bool ispressed() const; bool isPressed() const;
void defocus(); void defocus();
bool isfocused() const; bool isFocused() const;
virtual bool isfocuskeeper() const {return false;} virtual bool isFocuskeeper() const {return false;}
virtual void typed(unsigned int codepoint) {}; virtual void typed(unsigned int codepoint) {};
virtual void keyPressed(int key) {}; virtual void keyPressed(int key) {};
@ -79,11 +78,10 @@ namespace gui {
virtual void setInteractive(bool flag); virtual void setInteractive(bool flag);
virtual glm::vec2 contentOffset() {return glm::vec2(0.0f);}; 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 void setCoord(glm::vec2 coord);
virtual glm::vec2 size() const; virtual glm::vec2 getSize() const;
virtual void size(glm::vec2 size); virtual void setSize(glm::vec2 size);
void _size(glm::vec2 size);
virtual void refresh() {}; virtual void refresh() {};
virtual void lock(); virtual void lock();
}; };

View File

@ -6,58 +6,53 @@
#include "../../assets/Assets.h" #include "../../assets/Assets.h"
#include "../../graphics/Batch2D.h" #include "../../graphics/Batch2D.h"
#include "../../graphics/Font.h" #include "../../graphics/Font.h"
#include "../../graphics/GfxContext.h"
#include "../../util/stringutil.h" #include "../../util/stringutil.h"
#include "GUI.h" #include "GUI.h"
using std::string;
using std::wstring;
using std::shared_ptr;
using glm::vec2; using glm::vec2;
using glm::vec3; using glm::vec3;
using glm::vec4; using glm::vec4;
const uint KEY_ESCAPE = 256;
const uint KEY_ENTER = 257;
const uint KEY_BACKSPACE = 259;
using namespace gui; using namespace gui;
Label::Label(string text, string fontName) Label::Label(std::string text, std::string fontName)
: UINode(vec2(), vec2(text.length() * 8, 15)), : UINode(vec2(), vec2(text.length() * 8, 15)),
text_(util::str2wstr_utf8(text)), text(util::str2wstr_utf8(text)),
fontName_(fontName) { fontName_(fontName) {
} }
Label::Label(wstring text, string fontName) Label::Label(std::wstring text, std::string fontName)
: UINode(vec2(), vec2(text.length() * 8, 15)), : UINode(vec2(), vec2(text.length() * 8, 15)),
text_(text), text(text),
fontName_(fontName) { fontName_(fontName) {
} }
Label& Label::text(wstring text) { void Label::setText(std::wstring text) {
this->text_ = text; this->text = text;
return *this;
} }
wstring Label::text() const { std::wstring Label::getText() const {
return text_; return text;
} }
void Label::draw(Batch2D* batch, Assets* assets) { void Label::draw(const GfxContext* pctx, Assets* assets) {
if (supplier) { if (supplier) {
text(supplier()); setText(supplier());
} }
batch->color = color_;
auto batch = pctx->getBatch2D();
batch->color = getColor();
Font* font = assets->getFont(fontName_); Font* font = assets->getFont(fontName_);
vec2 size = UINode::size(); vec2 size = getSize();
vec2 newsize = vec2(font->calcWidth(text_), font->lineHeight()); vec2 newsize = vec2(font->calcWidth(text), font->lineHeight());
if (newsize.x > size.x) { if (newsize.x > size.x) {
this->size(newsize); setSize(newsize);
size = newsize; size = newsize;
} }
vec2 coord = calcCoord(); vec2 coord = calcCoord();
font->draw(batch, text_, coord.x, coord.y); font->draw(batch, text, coord.x, coord.y);
} }
Label* Label::textSupplier(wstringsupplier supplier) { Label* Label::textSupplier(wstringsupplier supplier) {
@ -65,58 +60,61 @@ Label* Label::textSupplier(wstringsupplier supplier) {
return this; return this;
} }
void Label::size(vec2 sizenew) { void Label::setSize(vec2 sizenew) {
UINode::size(vec2(UINode::size().x, sizenew.y)); UINode::setSize(vec2(UINode::getSize().x, sizenew.y));
} }
// ================================= Image ==================================== // ================================= 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); setInteractive(false);
} }
void Image::draw(Batch2D* batch, Assets* assets) { void Image::draw(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord(); vec2 coord = calcCoord();
vec4 color = getColor();
auto batch = pctx->getBatch2D();
batch->texture(assets->getTexture(texture)); batch->texture(assets->getTexture(texture));
batch->color = color_; batch->color = color;
batch->rect(coord.x, coord.y, size_.x, size_.y, 0, 0, 0, UVRegion(), false, true, color_); batch->rect(coord.x, coord.y, size.x, size.y,
0, 0, 0, UVRegion(), false, true, color);
} }
// ================================= Button =================================== // ================================= Button ===================================
Button::Button(shared_ptr<UINode> content, glm::vec4 padding) Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
: Panel(content->size()+vec2(padding[0]+padding[2]+content->margin()[0]+content->margin()[2], : Panel(vec2(), padding, 0) {
padding[1]+padding[3]+content->margin()[1]+content->margin()[3]), 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); add(content);
scrollable(false); scrollable(false);
} }
Button::Button(wstring text, glm::vec4 padding, glm::vec4 margin) Button::Button(std::wstring text, glm::vec4 padding, glm::vec4 margin)
: Panel(vec2(32,32), padding, 0) { : Panel(vec2(32,32), padding, 0)
this->margin(margin); {
Label* label = new Label(text); setMargin(margin);
label->align(Align::center);
this->label = shared_ptr<UINode>(label);
add(this->label);
scrollable(false); 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) { if (label) {
Label* label = (Label*)(this->label.get()); label->setText(text);
label->text(text);
} }
} }
wstring Button::text() const { std::wstring Button::getText() const {
if (label) { if (label) {
Label* label = (Label*)(this->label.get()); return label->getText();
return label->text();
} }
return L""; return L"";
} }
Button* Button::textSupplier(wstringsupplier supplier) { Button* Button::textSupplier(wstringsupplier supplier) {
if (label) { if (label) {
Label* label = (Label*)(this->label.get());
label->textSupplier(supplier); label->textSupplier(supplier);
} }
return this; return this;
@ -126,14 +124,15 @@ void Button::setHoverColor(glm::vec4 color) {
hoverColor = color; hoverColor = color;
} }
void Button::drawBackground(Batch2D* batch, Assets* assets) { void Button::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord(); vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->color = (ispressed() ? pressedColor : (hover_ ? hoverColor : color_)); batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color));
batch->rect(coord.x, coord.y, size_.x, size_.y); 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); return UINode::getAt(pos, self);
} }
@ -153,8 +152,7 @@ Button* Button::listenAction(onaction action) {
void Button::textAlign(Align align) { void Button::textAlign(Align align) {
if (label) { if (label) {
Label* label = (Label*)(this->label.get()); label->setAlign(align);
label->align(align);
refresh(); refresh();
} }
} }
@ -181,55 +179,58 @@ void RichButton::setHoverColor(glm::vec4 color) {
hoverColor = color; hoverColor = color;
} }
void RichButton::drawBackground(Batch2D* batch, Assets* assets) { void RichButton::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord(); vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->color = (ispressed() ? pressedColor : (hover_ ? hoverColor : color_)); batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color));
batch->rect(coord.x, coord.y, size_.x, size_.y); batch->rect(coord.x, coord.y, size.x, size.y);
} }
// ================================ TextBox =================================== // ================================ TextBox ===================================
TextBox::TextBox(wstring placeholder, vec4 padding) TextBox::TextBox(std::wstring placeholder, vec4 padding)
: Panel(vec2(200,32), padding, 0, false), : Panel(vec2(200,32), padding, 0, false),
input(L""), input(L""),
placeholder(placeholder) { placeholder(placeholder) {
label = new Label(L""); label = std::make_shared<Label>(L"");
add(shared_ptr<UINode>(label)); add(label);
} }
void TextBox::drawBackground(Batch2D* batch, Assets* assets) { void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord(); vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
if (valid) { if (valid) {
if (isfocused()) { if (isFocused()) {
batch->color = focusedColor; batch->color = focusedColor;
} else if (hover_) { } else if (hover) {
batch->color = hoverColor; batch->color = hoverColor;
} else { } else {
batch->color = color_; batch->color = color;
} }
} else { } else {
batch->color = invalidColor; batch->color = invalidColor;
} }
batch->rect(coord.x, coord.y, size_.x, size_.y); batch->rect(coord.x, coord.y, size.x, size.y);
if (!focused_ && supplier) { if (!isFocused() && supplier) {
input = supplier(); input = supplier();
} }
if (input.empty()) { if (input.empty()) {
label->color(vec4(0.5f)); label->setColor(vec4(0.5f));
label->text(placeholder); label->setText(placeholder);
} else { } else {
label->color(vec4(1.0f)); label->setColor(vec4(1.0f));
label->text(input); label->setText(input);
} }
scrollable(false); scrollable(false);
} }
void TextBox::typed(unsigned int codepoint) { void TextBox::typed(unsigned int codepoint) {
input += wstring({(wchar_t)codepoint}); input += std::wstring({(wchar_t)codepoint});
validate(); validate();
} }
@ -262,19 +263,16 @@ void TextBox::focus(GUI* gui) {
} }
void TextBox::keyPressed(int key) { void TextBox::keyPressed(int key) {
switch (key) { if (key == keycode::BACKSPACE) {
case KEY_BACKSPACE:
if (!input.empty()){ if (!input.empty()){
input = input.substr(0, input.length()-1); input = input.substr(0, input.length()-1);
validate(); validate();
} }
break; } else if (key == keycode::ENTER) {
case KEY_ENTER:
if (validate() && consumer) { if (validate() && consumer) {
consumer(label->text()); consumer(label->getText());
} }
defocus(); defocus();
break;
} }
// Pasting text from clipboard // Pasting text from clipboard
if (key == keycode::V && Events::pressed(keycode::LEFT_CONTROL)) { 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); return UINode::getAt(pos, self);
} }
@ -302,7 +300,7 @@ void TextBox::textValidator(wstringchecker validator) {
this->validator = validator; this->validator = validator;
} }
wstring TextBox::text() const { std::wstring TextBox::text() const {
if (input.empty()) if (input.empty())
return placeholder; return placeholder;
return input; return input;
@ -321,16 +319,13 @@ InputBindBox::InputBindBox(Binding& binding, vec4 padding)
scrollable(false); scrollable(false);
} }
shared_ptr<UINode> InputBindBox::getAt(vec2 pos, shared_ptr<UINode> self) { void InputBindBox::drawBackground(const GfxContext* pctx, Assets* assets) {
return UINode::getAt(pos, self);
}
void InputBindBox::drawBackground(Batch2D* batch, Assets* assets) {
vec2 coord = calcCoord(); vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->color = (isfocused() ? focusedColor : (hover_ ? hoverColor : color_)); batch->color = (isFocused() ? focusedColor : (hover ? hoverColor : color));
batch->rect(coord.x, coord.y, size_.x, size_.y); batch->rect(coord.x, coord.y, size.x, size.y);
label->text(util::str2wstr_utf8(binding.text())); label->setText(util::str2wstr_utf8(binding.text()));
} }
void InputBindBox::clicked(GUI*, int button) { void InputBindBox::clicked(GUI*, int button) {
@ -359,24 +354,25 @@ TrackBar::TrackBar(double min,
value(value), value(value),
step(step), step(step),
trackWidth(trackWidth) { 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_) { if (supplier_) {
value = supplier_(); value = supplier_();
} }
vec2 coord = calcCoord(); vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->color = (hover_ ? hoverColor : color_); batch->color = (hover ? hoverColor : color);
batch->rect(coord.x, coord.y, size_.x, size_.y); 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); float t = (value - min) / (max-min+trackWidth*step);
batch->color = trackColor; batch->color = trackColor;
int actualWidth = size_.x * (trackWidth / (max-min+trackWidth*step) * step); int actualWidth = size.x * (trackWidth / (max-min+trackWidth*step) * step);
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::supplier(doublesupplier supplier) {
@ -391,7 +387,7 @@ void TrackBar::mouseMove(GUI*, int x, int y) {
vec2 coord = calcCoord(); 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);
value += min; value += min;
value = (value > max) ? max : value; value = (value > max) ? max : value;
value = (value < min) ? min : value; value = (value < min) ? min : value;
@ -403,17 +399,18 @@ void TrackBar::mouseMove(GUI*, int x, int y) {
// ================================ CheckBox ================================== // ================================ CheckBox ==================================
CheckBox::CheckBox(bool checked) : UINode(vec2(), vec2(32.0f)), checked_(checked) { 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_) { if (supplier_) {
checked_ = supplier_(); checked_ = supplier_();
} }
vec2 coord = calcCoord(); vec2 coord = calcCoord();
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) {
@ -439,12 +436,12 @@ CheckBox* CheckBox::checked(bool flag) {
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)){
color(vec4(0.0f)); setColor(vec4(0.0f));
orientation(Orientation::horizontal); orientation(Orientation::horizontal);
add(checkbox); add(checkbox);
auto label = std::make_shared<Label>(text); 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); add(label);
} }

View File

@ -28,23 +28,20 @@ namespace gui {
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");
Label(std::wstring text, std::string fontName="normal"); Label(std::wstring text, std::string fontName="normal");
virtual Label& text(std::wstring text); virtual void setText(std::wstring text);
std::wstring text() const; 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 Label* textSupplier(wstringsupplier supplier);
virtual glm::vec2 size() const override { virtual void setSize(glm::vec2 size) override;
return UINode::size();
}
virtual void size(glm::vec2 size) override;
}; };
class Image : public UINode { class Image : public UINode {
@ -53,7 +50,7 @@ namespace gui {
public: public:
Image(std::string texture, glm::vec2 size); 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 { class Button : public Panel {
@ -61,14 +58,14 @@ namespace gui {
glm::vec4 hoverColor {0.05f, 0.1f, 0.15f, 0.75f}; glm::vec4 hoverColor {0.05f, 0.1f, 0.15f, 0.75f};
glm::vec4 pressedColor {0.0f, 0.0f, 0.0f, 0.95f}; glm::vec4 pressedColor {0.0f, 0.0f, 0.0f, 0.95f};
std::vector<onaction> actions; std::vector<onaction> actions;
std::shared_ptr<UINode> label = nullptr; std::shared_ptr<Label> label = nullptr;
public: public:
Button(std::shared_ptr<UINode> content, glm::vec4 padding=glm::vec4(2.0f)); Button(std::shared_ptr<UINode> content, glm::vec4 padding=glm::vec4(2.0f));
Button(std::wstring text, Button(std::wstring text,
glm::vec4 padding=glm::vec4(2.0f), glm::vec4 padding=glm::vec4(2.0f),
glm::vec4 margin=glm::vec4(1.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; 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 textAlign(Align align);
virtual void text(std::wstring text); virtual void setText(std::wstring text);
virtual std::wstring text() const; virtual std::wstring getText() const;
virtual Button* textSupplier(wstringsupplier supplier); virtual Button* textSupplier(wstringsupplier supplier);
@ -93,7 +90,7 @@ namespace gui {
public: public:
RichButton(glm::vec2 size); 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 void mouseRelease(GUI*, int x, int y) override;
virtual RichButton* listenAction(onaction action); virtual RichButton* listenAction(onaction action);
@ -106,7 +103,7 @@ namespace gui {
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f}; glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f}; glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};
glm::vec4 invalidColor {0.1f, 0.05f, 0.03f, 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 input;
std::wstring placeholder; std::wstring placeholder;
wstringsupplier supplier = nullptr; 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 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 typed(unsigned int codepoint) override;
virtual void keyPressed(int key) override; virtual void keyPressed(int key) override;
virtual void textSupplier(wstringsupplier supplier); virtual void textSupplier(wstringsupplier supplier);
virtual void textConsumer(wstringconsumer consumer); virtual void textConsumer(wstringconsumer consumer);
virtual void textValidator(wstringchecker validator); 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 std::wstring text() const;
virtual void text(std::wstring value); virtual void text(std::wstring value);
virtual bool validate(); virtual bool validate();
@ -144,12 +141,11 @@ namespace gui {
Binding& binding; Binding& binding;
public: public:
InputBindBox(Binding& binding, glm::vec4 padding=glm::vec4(6.0f)); InputBindBox(Binding& binding, glm::vec4 padding=glm::vec4(6.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;
virtual void clicked(GUI*, int button) override; virtual void clicked(GUI*, int button) override;
virtual void keyPressed(int key) 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 { class TrackBar : public UINode {
@ -169,7 +165,7 @@ namespace gui {
double value, double value,
double step=1.0, double step=1.0,
int trackWidth=1); 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 supplier(doublesupplier supplier);
virtual void consumer(doubleconsumer consumer); virtual void consumer(doubleconsumer consumer);
@ -187,7 +183,7 @@ namespace gui {
public: public:
CheckBox(bool checked=false); 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; virtual void mouseRelease(GUI*, int x, int y) override;

View File

@ -29,7 +29,7 @@ Button* guiutil::gotoButton(
void guiutil::alert(GUI* gui, const std::wstring& text, gui::runnable on_hidden) { void guiutil::alert(GUI* gui, const std::wstring& text, gui::runnable on_hidden) {
PagesControl* menu = gui->getMenu(); PagesControl* menu = gui->getMenu();
Panel* panel = new Panel(vec2(500, 200), vec4(8.0f), 8.0f); 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 // TODO: implement built-in text wrapping
const int wrap_length = 60; const int wrap_length = 60;
@ -70,10 +70,10 @@ void guiutil::confirm(
PagesControl* menu = gui->getMenu(); PagesControl* menu = gui->getMenu();
Panel* panel = new Panel(vec2(600, 200), vec4(8.0f), 8.0f); 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->add(new Label(text));
Panel* subpanel = new Panel(vec2(600, 53)); 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*){ subpanel->add((new Button(yestext, vec4(8.0f)))->listenAction([=](GUI*){
if (on_confirm) if (on_confirm)
on_confirm(); on_confirm();

View File

@ -5,8 +5,7 @@
#include "../../window/Window.h" #include "../../window/Window.h"
#include "../../assets/Assets.h" #include "../../assets/Assets.h"
#include "../../graphics/Batch2D.h" #include "../../graphics/Batch2D.h"
#include "../../graphics/GfxContext.h"
using std::shared_ptr;
using namespace gui; using namespace gui;
@ -17,7 +16,7 @@ Container::Container(vec2 coord, vec2 size) : UINode(coord, size) {
actualLength = size.y; 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) { if (!interactive) {
return nullptr; 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--) { for (int i = nodes.size()-1; i >= 0; i--) {
auto& node = nodes[i]; auto& node = nodes[i];
if (!node->visible()) if (!node->isVisible())
continue; continue;
auto hover = node->getAt(pos, node); auto hover = node->getAt(pos, node);
if (hover != nullptr) { if (hover != nullptr) {
@ -54,14 +53,14 @@ void Container::act(float delta) {
), intervalEvents.end()); ), intervalEvents.end());
for (auto node : nodes) { for (auto node : nodes) {
if (node->visible()) { if (node->isVisible()) {
node->act(delta); node->act(delta);
} }
} }
} }
void Container::scrolled(int value) { void Container::scrolled(int value) {
int diff = (actualLength-size().y); int diff = (actualLength-getSize().y);
if (diff > 0 && scrollable_) { if (diff > 0 && scrollable_) {
scroll += value * 40; scroll += value * 40;
if (scroll > 0) if (scroll > 0)
@ -78,46 +77,50 @@ void Container::scrollable(bool flag) {
scrollable_ = flag; scrollable_ = flag;
} }
void Container::draw(Batch2D* batch, Assets* assets) { void Container::draw(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord(); vec2 coord = calcCoord();
vec2 size = this->size(); vec2 size = getSize();
drawBackground(batch, assets); drawBackground(pctx, assets);
auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->render(); batch->render();
Window::pushScissor(vec4(coord.x, coord.y, size.x, size.y)); {
GfxContext ctx = pctx->sub();
ctx.scissors(vec4(coord.x, coord.y, size.x, size.y));
for (auto node : nodes) { for (auto node : nodes) {
if (node->visible()) if (node->isVisible())
node->draw(batch, assets); 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); nodes.insert(nodes.begin(), node);
node->setParent(this); node->setParent(this);
refresh(); refresh();
} }
void Container::add(shared_ptr<UINode> node) { void Container::add(std::shared_ptr<UINode> node) {
nodes.push_back(node); nodes.push_back(node);
node->setParent(this); node->setParent(this);
refresh(); refresh();
} }
void Container::add(UINode* node) { 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); node->setCoord(coord);
add(node); add(node);
} }
void Container::remove(shared_ptr<UINode> selected) { void Container::remove(std::shared_ptr<UINode> selected) {
selected->setParent(nullptr); selected->setParent(nullptr);
nodes.erase(std::remove_if(nodes.begin(), nodes.end(), 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; return node == selected;
} }
), nodes.end()); ), nodes.end());
@ -133,17 +136,19 @@ Panel::Panel(vec2 size, glm::vec4 padding, float interval, bool resizing)
padding(padding), padding(padding),
interval(interval), interval(interval),
resizing_(resizing) { resizing_(resizing) {
color_ = vec4(0.0f, 0.0f, 0.0f, 0.75f); setColor(vec4(0.0f, 0.0f, 0.0f, 0.75f));
} }
Panel::~Panel() { Panel::~Panel() {
} }
void Panel::drawBackground(Batch2D* batch, Assets* assets) { void Panel::drawBackground(const GfxContext* pctx, Assets* assets) {
vec2 coord = calcCoord(); vec2 coord = calcCoord();
auto batch = pctx->getBatch2D();
batch->texture(nullptr); batch->texture(nullptr);
batch->color = color_; batch->color = color;
batch->rect(coord.x, coord.y, size_.x, size_.y); batch->rect(coord.x, coord.y, size.x, size.y);
} }
void Panel::maxLength(int value) { void Panel::maxLength(int value) {
@ -157,22 +162,22 @@ int Panel::maxLength() const {
void Panel::refresh() { void Panel::refresh() {
float x = padding.x; float x = padding.x;
float y = padding.y; float y = padding.y;
vec2 size = this->size(); 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->size(); vec2 nodesize = node->getSize();
const vec4 margin = node->margin(); const vec4 margin = node->getMargin();
y += margin.y; y += margin.y;
float ex; float ex;
float spacex = size.x - margin.z - padding.z; float spacex = size.x - margin.z - padding.z;
switch (node->align()) { switch (node->getAlign()) {
case Align::center: 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; break;
case Align::right: case Align::right:
ex = x + spacex - node->size().x; ex = x + spacex - nodesize.x;
break; break;
default: default:
ex = x + margin.x; ex = x + margin.x;
@ -181,36 +186,36 @@ void Panel::refresh() {
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;
node->size(vec2(width, nodesize.y));; node->setSize(vec2(width, nodesize.y));;
node->refresh(); 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 (resizing_) {
if (maxLength_) 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 else
this->size(vec2(size.x, y+padding.w)); setSize(vec2(size.x, y+padding.w));
} }
actualLength = y + padding.w; actualLength = y + padding.w;
} else { } else {
float maxh = size.y; float maxh = size.y;
for (auto& node : nodes) { for (auto& node : nodes) {
vec2 nodesize = node->size(); vec2 nodesize = node->getSize();
const vec4 margin = node->margin(); const vec4 margin = node->getMargin();
x += margin.x; x += margin.x;
node->setCoord(vec2(x, y+margin.y)); node->setCoord(vec2(x, y+margin.y));
x += nodesize.x + margin.z + interval; x += nodesize.x + margin.z + interval;
float height = size.y - padding.y - padding.w - margin.y - margin.w; 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(); 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 (resizing_) {
if (maxLength_) 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 else
this->size(vec2(x+padding.z, size.y)); setSize(vec2(x+padding.z, size.y));
} }
actualLength = size.y; actualLength = size.y;
} }
@ -224,13 +229,6 @@ Orientation Panel::orientation() const {
return orientation_; return orientation_;
} }
void Panel::lock(){
for (auto node : nodes) {
node->lock();
}
resizing_ = false;
}
PagesControl::PagesControl() : Container(vec2(), vec2(1)){ 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) { 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) { void PagesControl::set(std::string name, bool history) {
@ -260,7 +258,7 @@ void PagesControl::set(std::string name, bool history) {
curname_ = name; curname_ = name;
current_ = found->second; current_ = found->second;
Container::add(current_.panel); Container::add(current_.panel);
size(current_.panel->size()); setSize(current_.panel->getSize());
} }
void PagesControl::back() { void PagesControl::back() {

View File

@ -34,8 +34,8 @@ namespace gui {
Container(glm::vec2 coord, glm::vec2 size); Container(glm::vec2 coord, glm::vec2 size);
virtual void act(float delta) override; virtual void act(float delta) override;
virtual void drawBackground(Batch2D* batch, Assets* assets) {}; virtual void drawBackground(const GfxContext* pctx, Assets* assets) {};
virtual void draw(Batch2D* batch, Assets* assets) override; 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 std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
virtual void addBack(std::shared_ptr<UINode> node); virtual void addBack(std::shared_ptr<UINode> node);
virtual void add(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); Panel(glm::vec2 size, glm::vec4 padding=glm::vec4(2.0f), float interval=2.0f, bool resizing=true);
virtual ~Panel(); virtual ~Panel();
virtual void drawBackground(Batch2D* batch, Assets* assets) override; virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void orientation(Orientation orientation); virtual void orientation(Orientation orientation);
Orientation orientation() const; Orientation orientation() const;
virtual void refresh() override; virtual void refresh() override;
virtual void lock() override;
virtual void maxLength(int value); virtual void maxLength(int value);
int maxLength() const; int maxLength() const;

View File

@ -51,18 +51,17 @@ using glm::vec3;
using glm::vec4; using glm::vec4;
using namespace gui; using namespace gui;
inline std::shared_ptr<Label> create_label(gui::wstringsupplier supplier) { static std::shared_ptr<Label> create_label(gui::wstringsupplier supplier) {
auto label = std::make_shared<Label>(L"-"); auto label = std::make_shared<Label>(L"-");
label->textSupplier(supplier); label->textSupplier(supplier);
return label; return label;
} }
void HudRenderer::createDebugPanel(Engine* engine) { std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
auto level = frontend->getLevel(); auto level = frontend->getLevel();
Panel* panel = new Panel(vec2(250, 200), vec4(5.0f), 1.0f); auto panel = std::make_shared<Panel>(vec2(250, 200), vec4(5.0f), 1.0f);
debugPanel = std::shared_ptr<UINode>(panel); panel->listenInterval(0.5f, [this]() {
panel->listenInterval(1.0f, [this]() {
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin); fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
fpsMin = fps; fpsMin = fps;
fpsMax = fps; fpsMax = fps;
@ -104,9 +103,9 @@ void HudRenderer::createDebugPanel(Engine* engine) {
std::wstring str = L"x: "; std::wstring str = L"x: ";
str[0] += ax; str[0] += ax;
Label* label = new Label(str); Label* label = new Label(str);
label->margin(vec4(2, 3, 2, 3)); label->setMargin(vec4(2, 3, 2, 3));
sub->add(label); sub->add(label);
sub->color(vec4(0.0f)); sub->setColor(vec4(0.0f));
// Coord input // Coord input
TextBox* box = new TextBox(L""); TextBox* box = new TextBox(L"");
@ -162,6 +161,7 @@ void HudRenderer::createDebugPanel(Engine* engine) {
panel->add(checkbox); panel->add(checkbox);
} }
panel->refresh(); panel->refresh();
return panel;
} }
std::shared_ptr<InventoryView> HudRenderer::createContentAccess() { std::shared_ptr<InventoryView> HudRenderer::createContentAccess() {
@ -265,14 +265,14 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
frontend->getLevel()->content, frontend->getLevel()->content,
SlotLayout(glm::vec2(), false, false, nullptr, nullptr) SlotLayout(glm::vec2(), false, false, nullptr, nullptr)
); );
grabbedItemView->color(glm::vec4()); grabbedItemView->setColor(glm::vec4());
grabbedItemView->setInteractive(false); grabbedItemView->setInteractive(false);
contentAccess = createContentAccess(); contentAccess = createContentAccess();
contentAccessPanel = std::make_shared<Panel>( contentAccessPanel = std::make_shared<Panel>(
contentAccess->size(), vec4(0.0f), 0.0f contentAccess->getSize(), vec4(0.0f), 0.0f
); );
contentAccessPanel->color(glm::vec4()); contentAccessPanel->setColor(glm::vec4());
contentAccessPanel->add(contentAccess); contentAccessPanel->add(contentAccess);
contentAccessPanel->scrollable(true); contentAccessPanel->scrollable(true);
@ -280,13 +280,13 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
inventoryView = createInventory(); inventoryView = createInventory();
darkOverlay = std::make_unique<Panel>(glm::vec2(4000.0f)); darkOverlay = std::make_unique<Panel>(glm::vec2(4000.0f));
darkOverlay->color(glm::vec4(0, 0, 0, 0.5f)); darkOverlay->setColor(glm::vec4(0, 0, 0, 0.5f));
uicamera = new Camera(vec3(), 1); uicamera = std::make_unique<Camera>(vec3(), 1);
uicamera->perspective = false; uicamera->perspective = false;
uicamera->flipped = true; uicamera->flipped = true;
createDebugPanel(engine); debugPanel = createDebugPanel(engine);
menu->reset(); menu->reset();
gui->addBack(darkOverlay); gui->addBack(darkOverlay);
@ -304,7 +304,6 @@ HudRenderer::~HudRenderer() {
gui->remove(darkOverlay); gui->remove(darkOverlay);
gui->remove(contentAccessPanel); gui->remove(contentAccessPanel);
gui->remove(debugPanel); gui->remove(debugPanel);
delete uicamera;
} }
void HudRenderer::drawDebug(int fps){ void HudRenderer::drawDebug(int fps){
@ -318,8 +317,8 @@ void HudRenderer::update(bool visible) {
auto player = level->player; auto player = level->player;
auto menu = gui->getMenu(); auto menu = gui->getMenu();
debugPanel->visible(player->debug && visible); debugPanel->setVisible(player->debug && visible);
menu->visible(pause); menu->setVisible(pause);
if (!visible && inventoryOpen) { if (!visible && inventoryOpen) {
closeInventory(); closeInventory();
@ -351,11 +350,11 @@ void HudRenderer::update(bool visible) {
Events::toggleCursor(); Events::toggleCursor();
} }
glm::vec2 invSize = contentAccessPanel->size(); vec2 invSize = contentAccessPanel->getSize();
inventoryView->visible(inventoryOpen); inventoryView->setVisible(inventoryOpen);
contentAccessPanel->visible(inventoryOpen); contentAccessPanel->setVisible(inventoryOpen);
contentAccessPanel->size(glm::vec2(invSize.x, Window::height)); contentAccessPanel->setSize(vec2(invSize.x, Window::height));
hotbarView->visible(visible); hotbarView->setVisible(visible);
for (int i = keycode::NUM_1; i <= keycode::NUM_9; i++) { for (int i = keycode::NUM_1; i <= keycode::NUM_9; i++) {
if (Events::jpressed(i)) { if (Events::jpressed(i)) {
@ -374,7 +373,7 @@ void HudRenderer::update(bool visible) {
player->setChosenSlot(slot); player->setChosenSlot(slot);
} }
darkOverlay->visible(pause); darkOverlay->setVisible(pause);
} }
void HudRenderer::closeInventory() { void HudRenderer::closeInventory() {

View File

@ -28,7 +28,7 @@ namespace gui {
class HudRenderer { class HudRenderer {
Assets* assets; Assets* assets;
Camera* uicamera; std::unique_ptr<Camera> uicamera;
int fps = 60; int fps = 60;
int fpsMin = 60; int fpsMin = 60;
@ -48,7 +48,7 @@ class HudRenderer {
gui::GUI* gui; gui::GUI* gui;
LevelFrontend* frontend; LevelFrontend* frontend;
void createDebugPanel(Engine* engine); std::shared_ptr<gui::UINode> createDebugPanel(Engine* engine);
std::shared_ptr<InventoryView> createContentAccess(); std::shared_ptr<InventoryView> createContentAccess();
std::shared_ptr<InventoryView> createHotbar(); std::shared_ptr<InventoryView> createHotbar();

View File

@ -53,12 +53,12 @@ std::shared_ptr<Panel> create_page(
float opacity, float opacity,
int interval) { int interval) {
PagesControl* menu = engine->getGUI()->getMenu(); PagesControl* menu = engine->getGUI()->getMenu();
Panel* panel = new Panel(vec2(width, 200), vec4(8.0f), interval); auto panel = std::make_shared<Panel>(
panel->color(vec4(0.0f, 0.0f, 0.0f, opacity)); vec2(width, 200), vec4(8.0f), interval
);
std::shared_ptr<Panel> ptr (panel); panel->setColor(vec4(0.0f, 0.0f, 0.0f, opacity));
menu->add(name, ptr); menu->add(name, panel);
return ptr; return panel;
} }
Button* create_button(std::wstring text, Button* create_button(std::wstring text,
@ -81,19 +81,19 @@ void show_content_missing(Engine* engine, const Content* content,
panel->add(new Label(langs::get(L"menu.missing-content"))); panel->add(new Label(langs::get(L"menu.missing-content")));
Panel* subpanel = new Panel(vec2(500, 100)); Panel* subpanel = new Panel(vec2(500, 100));
subpanel->color(vec4(0.0f, 0.0f, 0.0f, 0.5f)); subpanel->setColor(vec4(0.0f, 0.0f, 0.0f, 0.5f));
for (auto& entry : lut->getMissingContent()) { for (auto& entry : lut->getMissingContent()) {
Panel* hpanel = new Panel(vec2(500, 30)); Panel* hpanel = new Panel(vec2(500, 30));
hpanel->color(vec4(0.0f)); hpanel->setColor(vec4(0.0f));
hpanel->orientation(Orientation::horizontal); hpanel->orientation(Orientation::horizontal);
Label* namelabel = new Label(util::str2wstr_utf8(entry.name)); Label* namelabel = new Label(util::str2wstr_utf8(entry.name));
namelabel->color(vec4(1.0f, 0.2f, 0.2f, 0.5f)); namelabel->setColor(vec4(1.0f, 0.2f, 0.2f, 0.5f));
auto contentname = util::str2wstr_utf8(contenttype_name(entry.type)); auto contentname = util::str2wstr_utf8(contenttype_name(entry.type));
Label* typelabel = new Label(L"["+contentname+L"]"); Label* typelabel = new Label(L"["+contentname+L"]");
typelabel->color(vec4(0.5f)); typelabel->setColor(vec4(0.5f));
hpanel->add(typelabel); hpanel->add(typelabel);
hpanel->add(namelabel); hpanel->add(namelabel);
subpanel->add(hpanel); subpanel->add(hpanel);
@ -193,7 +193,7 @@ void open_world(std::string name, Engine* engine) {
Panel* create_worlds_panel(Engine* engine) { Panel* create_worlds_panel(Engine* engine) {
auto panel = new Panel(vec2(390, 200), vec4(5.0f)); auto panel = new Panel(vec2(390, 200), vec4(5.0f));
panel->color(vec4(1.0f, 1.0f, 1.0f, 0.07f)); panel->setColor(vec4(1.0f, 1.0f, 1.0f, 0.07f));
panel->maxLength(400); panel->maxLength(400);
auto paths = engine->getPaths(); auto paths = engine->getPaths();
@ -203,7 +203,7 @@ Panel* create_worlds_panel(Engine* engine) {
auto namews = util::str2wstr_utf8(name); auto namews = util::str2wstr_utf8(name);
auto btn = std::make_shared<RichButton>(vec2(390, 46)); auto btn = std::make_shared<RichButton>(vec2(390, 46));
btn->color(vec4(1.0f, 1.0f, 1.0f, 0.1f)); btn->setColor(vec4(1.0f, 1.0f, 1.0f, 0.1f));
btn->setHoverColor(vec4(1.0f, 1.0f, 1.0f, 0.17f)); btn->setHoverColor(vec4(1.0f, 1.0f, 1.0f, 0.17f));
auto label = std::make_shared<Label>(namews); auto label = std::make_shared<Label>(namews);
@ -214,10 +214,10 @@ Panel* create_worlds_panel(Engine* engine) {
}); });
auto image = std::make_shared<Image>("gui/delete_icon", vec2(32, 32)); auto image = std::make_shared<Image>("gui/delete_icon", vec2(32, 32));
image->color(vec4(1, 1, 1, 0.5f)); image->setColor(vec4(1, 1, 1, 0.5f));
auto delbtn = std::make_shared<Button>(image, vec4(2)); auto delbtn = std::make_shared<Button>(image, vec4(2));
delbtn->color(vec4(0.0f)); delbtn->setColor(vec4(0.0f));
delbtn->setHoverColor(vec4(1.0f, 1.0f, 1.0f, 0.17f)); delbtn->setHoverColor(vec4(1.0f, 1.0f, 1.0f, 0.17f));
btn->add(delbtn, vec2(330, 3)); btn->add(delbtn, vec2(330, 3));
@ -260,7 +260,7 @@ std::shared_ptr<Panel> create_packs_panel(
{ {
auto assets = engine->getAssets(); auto assets = engine->getAssets();
auto panel = std::make_shared<Panel>(vec2(PACKS_PANEL_WIDTH, 200), vec4(5.0f)); auto panel = std::make_shared<Panel>(vec2(PACKS_PANEL_WIDTH, 200), vec4(5.0f));
panel->color(vec4(1.0f, 1.0f, 1.0f, 0.07f)); panel->setColor(vec4(1.0f, 1.0f, 1.0f, 0.07f));
panel->maxLength(400); panel->maxLength(400);
panel->scrollable(true); panel->scrollable(true);
@ -272,8 +272,8 @@ std::shared_ptr<Panel> create_packs_panel(
}); });
} }
auto idlabel = std::make_shared<Label>("["+pack.id+"]"); auto idlabel = std::make_shared<Label>("["+pack.id+"]");
idlabel->color(vec4(1, 1, 1, 0.5f)); idlabel->setColor(vec4(1, 1, 1, 0.5f));
packpanel->add(idlabel, vec2(PACKS_PANEL_WIDTH-40-idlabel->size().x, 2)); packpanel->add(idlabel, vec2(PACKS_PANEL_WIDTH-40-idlabel->getSize().x, 2));
auto titlelabel = std::make_shared<Label>(pack.title); auto titlelabel = std::make_shared<Label>(pack.title);
packpanel->add(titlelabel, vec2(78, 6)); packpanel->add(titlelabel, vec2(78, 6));
@ -290,17 +290,17 @@ std::shared_ptr<Panel> create_packs_panel(
if (!pack.creator.empty()) { if (!pack.creator.empty()) {
auto creatorlabel = std::make_shared<Label>("@"+pack.creator); auto creatorlabel = std::make_shared<Label>("@"+pack.creator);
creatorlabel->color(vec4(0.8f, 1.0f, 0.9f, 0.7f)); creatorlabel->setColor(vec4(0.8f, 1.0f, 0.9f, 0.7f));
packpanel->add(creatorlabel, vec2(PACKS_PANEL_WIDTH-40-creatorlabel->size().x, 60)); packpanel->add(creatorlabel, vec2(PACKS_PANEL_WIDTH-40-creatorlabel->getSize().x, 60));
} }
auto descriptionlabel = std::make_shared<Label>(pack.description); auto descriptionlabel = std::make_shared<Label>(pack.description);
descriptionlabel->color(vec4(1, 1, 1, 0.7f)); descriptionlabel->setColor(vec4(1, 1, 1, 0.7f));
packpanel->add(descriptionlabel, vec2(80, 28)); packpanel->add(descriptionlabel, vec2(80, 28));
packpanel->add(std::make_shared<Image>(icon, glm::vec2(64)), vec2(8)); packpanel->add(std::make_shared<Image>(icon, glm::vec2(64)), vec2(8));
packpanel->color(vec4(0.06f, 0.12f, 0.18f, 0.7f)); packpanel->setColor(vec4(0.06f, 0.12f, 0.18f, 0.7f));
panel->add(packpanel); panel->add(packpanel);
} }
if (backbutton) { if (backbutton) {
@ -456,19 +456,19 @@ void create_controls_panel(Engine* engine, PagesControl* menu) {
} }
Panel* scrollPanel = new Panel(vec2(400, 200), vec4(2.0f), 1.0f); Panel* scrollPanel = new Panel(vec2(400, 200), vec4(2.0f), 1.0f);
scrollPanel->color(vec4(0.0f, 0.0f, 0.0f, 0.3f)); scrollPanel->setColor(vec4(0.0f, 0.0f, 0.0f, 0.3f));
scrollPanel->maxLength(400); scrollPanel->maxLength(400);
for (auto& entry : Events::bindings){ for (auto& entry : Events::bindings){
std::string bindname = entry.first; std::string bindname = entry.first;
Panel* subpanel = new Panel(vec2(400, 40), vec4(5.0f), 1.0f); Panel* subpanel = new Panel(vec2(400, 40), vec4(5.0f), 1.0f);
subpanel->color(vec4(0.0f)); subpanel->setColor(vec4(0.0f));
subpanel->orientation(Orientation::horizontal); subpanel->orientation(Orientation::horizontal);
InputBindBox* bindbox = new InputBindBox(entry.second); InputBindBox* bindbox = new InputBindBox(entry.second);
subpanel->add(bindbox); subpanel->add(bindbox);
Label* label = new Label(langs::get(util::str2wstr_utf8(bindname))); Label* label = new Label(langs::get(util::str2wstr_utf8(bindname)));
label->margin(vec4(6.0f)); label->setMargin(vec4(6.0f));
subpanel->add(label); subpanel->add(label);
scrollPanel->add(subpanel); scrollPanel->add(subpanel);
} }

View File

@ -9,8 +9,13 @@ GfxContext::GfxContext(const GfxContext* parent, Viewport& viewport, Batch2D* g2
} }
GfxContext::~GfxContext() { GfxContext::~GfxContext() {
while (scissorsCount--) {
Window::popScissor();
}
if (parent == nullptr) if (parent == nullptr)
return; return;
if (depthMask_ != parent->depthMask_) { if (depthMask_ != parent->depthMask_) {
glDepthMask(parent->depthMask_); glDepthMask(parent->depthMask_);
} }
@ -77,3 +82,8 @@ void GfxContext::blendMode(blendmode mode) {
blendMode_ = mode; blendMode_ = mode;
Window::setBlendMode(mode); Window::setBlendMode(mode);
} }
void GfxContext::scissors(glm::vec4 area) {
Window::pushScissor(area);
scissorsCount++;
}

View File

@ -15,6 +15,7 @@ class GfxContext {
bool depthTest_ = false; bool depthTest_ = false;
bool cullFace_ = false; bool cullFace_ = false;
blendmode blendMode_ = blendmode::normal; blendmode blendMode_ = blendmode::normal;
int scissorsCount = 0;
public: public:
GfxContext(const GfxContext* parent, Viewport& viewport, Batch2D* g2d); GfxContext(const GfxContext* parent, Viewport& viewport, Batch2D* g2d);
~GfxContext(); ~GfxContext();
@ -27,6 +28,7 @@ public:
void depthTest(bool flag); void depthTest(bool flag);
void cullFace(bool flag); void cullFace(bool flag);
void blendMode(blendmode mode); void blendMode(blendmode mode);
void scissors(glm::vec4 area);
}; };
#endif // GRAPHICS_GFX_CONTEXT_H_ #endif // GRAPHICS_GFX_CONTEXT_H_