diff --git a/src/engine.cpp b/src/engine.cpp index ac6958c6..0b6e979b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -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); diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp index c7488958..acae9e55 100644 --- a/src/frontend/InventoryView.cpp +++ b/src/frontend/InventoryView.cpp @@ -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); diff --git a/src/frontend/InventoryView.h b/src/frontend/InventoryView.h index 3f85d187..7c6b26db 100644 --- a/src/frontend/InventoryView.h +++ b/src/frontend/InventoryView.h @@ -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); diff --git a/src/frontend/gui/GUI.cpp b/src/frontend/gui/GUI.cpp index 5dd189c9..114693d3 100644 --- a/src/frontend/gui/GUI.cpp +++ b/src/frontend/gui/GUI.cpp @@ -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 GUI::getFocused() const { @@ -133,7 +137,7 @@ shared_ptr GUI::getFocused() const { } bool GUI::isFocusCaught() const { - return focus && focus->isfocuskeeper(); + return focus && focus->isFocuskeeper(); } void GUI::addBack(std::shared_ptr panel) { diff --git a/src/frontend/gui/GUI.h b/src/frontend/gui/GUI.h index 3e159f4a..6b8876f9 100644 --- a/src/frontend/gui/GUI.h +++ b/src/frontend/gui/GUI.h @@ -8,7 +8,7 @@ #include #include -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 panel); void add(std::shared_ptr panel); void remove(std::shared_ptr panel); diff --git a/src/frontend/gui/UINode.cpp b/src/frontend/gui/UINode.cpp index 239da047..d87e1c9e 100644 --- a/src/frontend/gui/UINode.cpp +++ b/src/frontend/gui/UINode.cpp @@ -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::getAt(vec2 pos, shared_ptr self) { +std::shared_ptr UINode::getAt(vec2 pos, std::shared_ptr self) { if (!interactive) { return nullptr; } @@ -83,7 +81,7 @@ shared_ptr UINode::getAt(vec2 pos, shared_ptr 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() { diff --git a/src/frontend/gui/UINode.h b/src/frontend/gui/UINode.h index 6f909ba5..56859cd7 100644 --- a/src/frontend/gui/UINode.h +++ b/src/frontend/gui/UINode.h @@ -6,7 +6,7 @@ #include #include -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(); }; diff --git a/src/frontend/gui/controls.cpp b/src/frontend/gui/controls.cpp index 54188b43..b5fc5b65 100644 --- a/src/frontend/gui/controls.cpp +++ b/src/frontend/gui/controls.cpp @@ -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 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 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(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