frontend/gui refactor
This commit is contained in:
parent
49fdcdfb27
commit
8c722b146e
@ -17,6 +17,7 @@
|
||||
#include "window/Camera.h"
|
||||
#include "window/input.h"
|
||||
#include "graphics/Batch2D.h"
|
||||
#include "graphics/GfxContext.h"
|
||||
#include "graphics/Shader.h"
|
||||
#include "graphics/ImageData.h"
|
||||
#include "frontend/gui/GUI.h"
|
||||
@ -113,7 +114,11 @@ void Engine::mainloop() {
|
||||
|
||||
if (!Window::isIconified()) {
|
||||
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);
|
||||
} else {
|
||||
Window::swapInterval(1);
|
||||
|
||||
@ -160,22 +160,23 @@ SlotView::SlotView(
|
||||
content(content),
|
||||
stack(stack),
|
||||
layout(layout) {
|
||||
color(glm::vec4(0, 0, 0, 0.2f));
|
||||
setColor(glm::vec4(0, 0, 0, 0.2f));
|
||||
}
|
||||
|
||||
// performance disaster
|
||||
void SlotView::draw(Batch2D* batch, Assets* assets) {
|
||||
void SlotView::draw(const GfxContext* pctx, Assets* assets) {
|
||||
glm::vec2 coord = calcCoord();
|
||||
|
||||
int slotSize = InventoryView::SLOT_SIZE;
|
||||
|
||||
glm::vec4 tint(1.0f);
|
||||
glm::vec4 color = color_;
|
||||
if (hover_ || highlighted) {
|
||||
glm::vec4 color = getColor();
|
||||
if (hover || highlighted) {
|
||||
tint *= 1.333f;
|
||||
color = glm::vec4(1, 1, 1, 0.2f);
|
||||
}
|
||||
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->color = color;
|
||||
if (color.a > 0.0) {
|
||||
batch->texture(nullptr);
|
||||
@ -313,8 +314,8 @@ InventoryView::InventoryView(
|
||||
layout(std::move(layout)),
|
||||
frontend(frontend),
|
||||
interaction(interaction) {
|
||||
size(this->layout->getSize());
|
||||
color(glm::vec4(0, 0, 0, 0.0f));
|
||||
setSize(this->layout->getSize());
|
||||
setColor(glm::vec4(0, 0, 0, 0.0f));
|
||||
}
|
||||
|
||||
InventoryView::~InventoryView() {}
|
||||
@ -331,7 +332,7 @@ void InventoryView::build() {
|
||||
item, frontend, interaction, content, slot
|
||||
);
|
||||
if (!slot.background) {
|
||||
view->color(glm::vec4());
|
||||
view->setColor(glm::vec4());
|
||||
}
|
||||
slots.push_back(view.get());
|
||||
add(view, slot.position);
|
||||
@ -358,8 +359,9 @@ InventoryLayout* InventoryView::getLayout() const {
|
||||
return layout.get();
|
||||
}
|
||||
|
||||
void InventoryView::drawBackground(Batch2D* batch, Assets* assets) {
|
||||
void InventoryView::drawBackground(const GfxContext* pctx, Assets* assets) {
|
||||
glm::vec2 coord = calcCoord();
|
||||
auto batch = pctx->getBatch2D();
|
||||
|
||||
batch->texture(nullptr);
|
||||
|
||||
|
||||
@ -11,7 +11,6 @@
|
||||
#include "../items/ItemStack.h"
|
||||
#include "../typedefs.h"
|
||||
|
||||
class Batch2D;
|
||||
class Assets;
|
||||
class GfxContext;
|
||||
class Content;
|
||||
@ -102,7 +101,7 @@ public:
|
||||
const Content* content,
|
||||
SlotLayout layout);
|
||||
|
||||
virtual void draw(Batch2D* batch, Assets* assets) override;
|
||||
virtual void draw(const GfxContext* pctx, Assets* assets) override;
|
||||
|
||||
void setHighlighted(bool flag);
|
||||
bool isHighlighted() const;
|
||||
@ -135,7 +134,7 @@ public:
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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() {
|
||||
|
||||
@ -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();
|
||||
};
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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() {
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -51,18 +51,17 @@ using glm::vec3;
|
||||
using glm::vec4;
|
||||
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"-");
|
||||
label->textSupplier(supplier);
|
||||
return label;
|
||||
}
|
||||
|
||||
void HudRenderer::createDebugPanel(Engine* engine) {
|
||||
std::shared_ptr<UINode> HudRenderer::createDebugPanel(Engine* engine) {
|
||||
auto level = frontend->getLevel();
|
||||
|
||||
Panel* panel = new Panel(vec2(250, 200), vec4(5.0f), 1.0f);
|
||||
debugPanel = std::shared_ptr<UINode>(panel);
|
||||
panel->listenInterval(1.0f, [this]() {
|
||||
auto panel = std::make_shared<Panel>(vec2(250, 200), vec4(5.0f), 1.0f);
|
||||
panel->listenInterval(0.5f, [this]() {
|
||||
fpsString = std::to_wstring(fpsMax)+L" / "+std::to_wstring(fpsMin);
|
||||
fpsMin = fps;
|
||||
fpsMax = fps;
|
||||
@ -104,9 +103,9 @@ void HudRenderer::createDebugPanel(Engine* engine) {
|
||||
std::wstring str = L"x: ";
|
||||
str[0] += ax;
|
||||
Label* label = new Label(str);
|
||||
label->margin(vec4(2, 3, 2, 3));
|
||||
label->setMargin(vec4(2, 3, 2, 3));
|
||||
sub->add(label);
|
||||
sub->color(vec4(0.0f));
|
||||
sub->setColor(vec4(0.0f));
|
||||
|
||||
// Coord input
|
||||
TextBox* box = new TextBox(L"");
|
||||
@ -162,6 +161,7 @@ void HudRenderer::createDebugPanel(Engine* engine) {
|
||||
panel->add(checkbox);
|
||||
}
|
||||
panel->refresh();
|
||||
return panel;
|
||||
}
|
||||
|
||||
std::shared_ptr<InventoryView> HudRenderer::createContentAccess() {
|
||||
@ -265,14 +265,14 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
|
||||
frontend->getLevel()->content,
|
||||
SlotLayout(glm::vec2(), false, false, nullptr, nullptr)
|
||||
);
|
||||
grabbedItemView->color(glm::vec4());
|
||||
grabbedItemView->setColor(glm::vec4());
|
||||
grabbedItemView->setInteractive(false);
|
||||
|
||||
contentAccess = createContentAccess();
|
||||
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->scrollable(true);
|
||||
|
||||
@ -280,13 +280,13 @@ HudRenderer::HudRenderer(Engine* engine, LevelFrontend* frontend)
|
||||
inventoryView = createInventory();
|
||||
|
||||
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->flipped = true;
|
||||
|
||||
createDebugPanel(engine);
|
||||
debugPanel = createDebugPanel(engine);
|
||||
menu->reset();
|
||||
|
||||
gui->addBack(darkOverlay);
|
||||
@ -304,7 +304,6 @@ HudRenderer::~HudRenderer() {
|
||||
gui->remove(darkOverlay);
|
||||
gui->remove(contentAccessPanel);
|
||||
gui->remove(debugPanel);
|
||||
delete uicamera;
|
||||
}
|
||||
|
||||
void HudRenderer::drawDebug(int fps){
|
||||
@ -318,8 +317,8 @@ void HudRenderer::update(bool visible) {
|
||||
auto player = level->player;
|
||||
auto menu = gui->getMenu();
|
||||
|
||||
debugPanel->visible(player->debug && visible);
|
||||
menu->visible(pause);
|
||||
debugPanel->setVisible(player->debug && visible);
|
||||
menu->setVisible(pause);
|
||||
|
||||
if (!visible && inventoryOpen) {
|
||||
closeInventory();
|
||||
@ -351,11 +350,11 @@ void HudRenderer::update(bool visible) {
|
||||
Events::toggleCursor();
|
||||
}
|
||||
|
||||
glm::vec2 invSize = contentAccessPanel->size();
|
||||
inventoryView->visible(inventoryOpen);
|
||||
contentAccessPanel->visible(inventoryOpen);
|
||||
contentAccessPanel->size(glm::vec2(invSize.x, Window::height));
|
||||
hotbarView->visible(visible);
|
||||
vec2 invSize = contentAccessPanel->getSize();
|
||||
inventoryView->setVisible(inventoryOpen);
|
||||
contentAccessPanel->setVisible(inventoryOpen);
|
||||
contentAccessPanel->setSize(vec2(invSize.x, Window::height));
|
||||
hotbarView->setVisible(visible);
|
||||
|
||||
for (int i = keycode::NUM_1; i <= keycode::NUM_9; i++) {
|
||||
if (Events::jpressed(i)) {
|
||||
@ -374,7 +373,7 @@ void HudRenderer::update(bool visible) {
|
||||
player->setChosenSlot(slot);
|
||||
}
|
||||
|
||||
darkOverlay->visible(pause);
|
||||
darkOverlay->setVisible(pause);
|
||||
}
|
||||
|
||||
void HudRenderer::closeInventory() {
|
||||
|
||||
@ -28,7 +28,7 @@ namespace gui {
|
||||
|
||||
class HudRenderer {
|
||||
Assets* assets;
|
||||
Camera* uicamera;
|
||||
std::unique_ptr<Camera> uicamera;
|
||||
|
||||
int fps = 60;
|
||||
int fpsMin = 60;
|
||||
@ -48,7 +48,7 @@ class HudRenderer {
|
||||
gui::GUI* gui;
|
||||
LevelFrontend* frontend;
|
||||
|
||||
void createDebugPanel(Engine* engine);
|
||||
std::shared_ptr<gui::UINode> createDebugPanel(Engine* engine);
|
||||
|
||||
std::shared_ptr<InventoryView> createContentAccess();
|
||||
std::shared_ptr<InventoryView> createHotbar();
|
||||
|
||||
@ -53,12 +53,12 @@ std::shared_ptr<Panel> create_page(
|
||||
float opacity,
|
||||
int interval) {
|
||||
PagesControl* menu = engine->getGUI()->getMenu();
|
||||
Panel* panel = new Panel(vec2(width, 200), vec4(8.0f), interval);
|
||||
panel->color(vec4(0.0f, 0.0f, 0.0f, opacity));
|
||||
|
||||
std::shared_ptr<Panel> ptr (panel);
|
||||
menu->add(name, ptr);
|
||||
return ptr;
|
||||
auto panel = std::make_shared<Panel>(
|
||||
vec2(width, 200), vec4(8.0f), interval
|
||||
);
|
||||
panel->setColor(vec4(0.0f, 0.0f, 0.0f, opacity));
|
||||
menu->add(name, panel);
|
||||
return panel;
|
||||
}
|
||||
|
||||
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* 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()) {
|
||||
Panel* hpanel = new Panel(vec2(500, 30));
|
||||
hpanel->color(vec4(0.0f));
|
||||
hpanel->setColor(vec4(0.0f));
|
||||
hpanel->orientation(Orientation::horizontal);
|
||||
|
||||
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));
|
||||
Label* typelabel = new Label(L"["+contentname+L"]");
|
||||
typelabel->color(vec4(0.5f));
|
||||
typelabel->setColor(vec4(0.5f));
|
||||
hpanel->add(typelabel);
|
||||
hpanel->add(namelabel);
|
||||
subpanel->add(hpanel);
|
||||
@ -193,7 +193,7 @@ void open_world(std::string name, Engine* engine) {
|
||||
|
||||
Panel* create_worlds_panel(Engine* engine) {
|
||||
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);
|
||||
|
||||
auto paths = engine->getPaths();
|
||||
@ -203,7 +203,7 @@ Panel* create_worlds_panel(Engine* engine) {
|
||||
auto namews = util::str2wstr_utf8(name);
|
||||
|
||||
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));
|
||||
|
||||
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));
|
||||
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));
|
||||
delbtn->color(vec4(0.0f));
|
||||
delbtn->setColor(vec4(0.0f));
|
||||
delbtn->setHoverColor(vec4(1.0f, 1.0f, 1.0f, 0.17f));
|
||||
|
||||
btn->add(delbtn, vec2(330, 3));
|
||||
@ -260,7 +260,7 @@ std::shared_ptr<Panel> create_packs_panel(
|
||||
{
|
||||
auto assets = engine->getAssets();
|
||||
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->scrollable(true);
|
||||
|
||||
@ -272,8 +272,8 @@ std::shared_ptr<Panel> create_packs_panel(
|
||||
});
|
||||
}
|
||||
auto idlabel = std::make_shared<Label>("["+pack.id+"]");
|
||||
idlabel->color(vec4(1, 1, 1, 0.5f));
|
||||
packpanel->add(idlabel, vec2(PACKS_PANEL_WIDTH-40-idlabel->size().x, 2));
|
||||
idlabel->setColor(vec4(1, 1, 1, 0.5f));
|
||||
packpanel->add(idlabel, vec2(PACKS_PANEL_WIDTH-40-idlabel->getSize().x, 2));
|
||||
|
||||
auto titlelabel = std::make_shared<Label>(pack.title);
|
||||
packpanel->add(titlelabel, vec2(78, 6));
|
||||
@ -290,17 +290,17 @@ std::shared_ptr<Panel> create_packs_panel(
|
||||
|
||||
if (!pack.creator.empty()) {
|
||||
auto creatorlabel = std::make_shared<Label>("@"+pack.creator);
|
||||
creatorlabel->color(vec4(0.8f, 1.0f, 0.9f, 0.7f));
|
||||
packpanel->add(creatorlabel, vec2(PACKS_PANEL_WIDTH-40-creatorlabel->size().x, 60));
|
||||
creatorlabel->setColor(vec4(0.8f, 1.0f, 0.9f, 0.7f));
|
||||
packpanel->add(creatorlabel, vec2(PACKS_PANEL_WIDTH-40-creatorlabel->getSize().x, 60));
|
||||
}
|
||||
|
||||
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(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);
|
||||
}
|
||||
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);
|
||||
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);
|
||||
for (auto& entry : Events::bindings){
|
||||
std::string bindname = entry.first;
|
||||
|
||||
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);
|
||||
|
||||
InputBindBox* bindbox = new InputBindBox(entry.second);
|
||||
subpanel->add(bindbox);
|
||||
Label* label = new Label(langs::get(util::str2wstr_utf8(bindname)));
|
||||
label->margin(vec4(6.0f));
|
||||
label->setMargin(vec4(6.0f));
|
||||
subpanel->add(label);
|
||||
scrollPanel->add(subpanel);
|
||||
}
|
||||
|
||||
@ -9,8 +9,13 @@ GfxContext::GfxContext(const GfxContext* parent, Viewport& viewport, Batch2D* g2
|
||||
}
|
||||
|
||||
GfxContext::~GfxContext() {
|
||||
while (scissorsCount--) {
|
||||
Window::popScissor();
|
||||
}
|
||||
|
||||
if (parent == nullptr)
|
||||
return;
|
||||
|
||||
if (depthMask_ != parent->depthMask_) {
|
||||
glDepthMask(parent->depthMask_);
|
||||
}
|
||||
@ -77,3 +82,8 @@ void GfxContext::blendMode(blendmode mode) {
|
||||
blendMode_ = mode;
|
||||
Window::setBlendMode(mode);
|
||||
}
|
||||
|
||||
void GfxContext::scissors(glm::vec4 area) {
|
||||
Window::pushScissor(area);
|
||||
scissorsCount++;
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@ class GfxContext {
|
||||
bool depthTest_ = false;
|
||||
bool cullFace_ = false;
|
||||
blendmode blendMode_ = blendmode::normal;
|
||||
int scissorsCount = 0;
|
||||
public:
|
||||
GfxContext(const GfxContext* parent, Viewport& viewport, Batch2D* g2d);
|
||||
~GfxContext();
|
||||
@ -27,6 +28,7 @@ public:
|
||||
void depthTest(bool flag);
|
||||
void cullFace(bool flag);
|
||||
void blendMode(blendmode mode);
|
||||
void scissors(glm::vec4 area);
|
||||
};
|
||||
|
||||
#endif // GRAPHICS_GFX_CONTEXT_H_
|
||||
Loading…
x
Reference in New Issue
Block a user