diff --git a/src/content/Content.h b/src/content/Content.h index dc865ff2..9df2a04d 100644 --- a/src/content/Content.h +++ b/src/content/Content.h @@ -9,7 +9,7 @@ #include #include "../typedefs.h" -typedef std::set DrawGroups; +using DrawGroups = std::set; class Block; class ItemDef; diff --git a/src/frontend/gui/GUI.cpp b/src/frontend/gui/GUI.cpp index 2007d5c7..8f7118a0 100644 --- a/src/frontend/gui/GUI.cpp +++ b/src/frontend/gui/GUI.cpp @@ -13,16 +13,11 @@ #include "../../window/input.h" #include "../../window/Camera.h" -using glm::vec2; -using glm::vec3; -using std::string; -using std::shared_ptr; using namespace gui; GUI::GUI() { - container = new Container(vec2(0, 0), vec2(1000)); - - uicamera = new Camera(vec3(), Window::height); + container = std::make_shared(glm::vec2(0, 0), glm::vec2(1000)); + uicamera = std::make_unique(glm::vec3(), Window::height); uicamera->perspective = false; uicamera->flipped = true; @@ -32,8 +27,6 @@ GUI::GUI() { } GUI::~GUI() { - delete uicamera; - delete container; } std::shared_ptr GUI::getMenu() { @@ -85,7 +78,7 @@ void GUI::actMouse(float delta) { } void GUI::act(float delta) { - container->setSize(vec2(Window::width, Window::height)); + container->setSize(glm::vec2(Window::width, Window::height)); container->act(delta); auto prevfocus = focus; @@ -132,7 +125,7 @@ void GUI::draw(const GfxContext* pctx, Assets* assets) { container->draw(pctx, assets); } -shared_ptr GUI::getFocused() const { +std::shared_ptr GUI::getFocused() const { return focus; } @@ -144,19 +137,19 @@ void GUI::addBack(std::shared_ptr panel) { container->addBack(panel); } -void GUI::add(shared_ptr panel) { +void GUI::add(std::shared_ptr panel) { container->add(panel); } -void GUI::remove(shared_ptr panel) { +void GUI::remove(std::shared_ptr panel) { container->remove(panel); } -void GUI::store(string name, shared_ptr node) { +void GUI::store(std::string name, std::shared_ptr node) { storage[name] = node; } -shared_ptr GUI::get(string name) { +std::shared_ptr GUI::get(std::string name) { auto found = storage.find(name); if (found == storage.end()) { return nullptr; @@ -164,11 +157,11 @@ shared_ptr GUI::get(string name) { return found->second; } -void GUI::remove(string name) { +void GUI::remove(std::string name) { storage.erase(name); } -void GUI::setFocus(shared_ptr node) { +void GUI::setFocus(std::shared_ptr node) { if (focus) { focus->defocus(); } diff --git a/src/frontend/gui/GUI.h b/src/frontend/gui/GUI.h index 6ff90b4a..17ddd493 100644 --- a/src/frontend/gui/GUI.h +++ b/src/frontend/gui/GUI.h @@ -49,13 +49,13 @@ namespace gui { class PagesControl; class GUI { - Container* container; + std::shared_ptr container; std::shared_ptr hover = nullptr; std::shared_ptr pressed = nullptr; std::shared_ptr focus = nullptr; std::unordered_map> storage; - Camera* uicamera; + std::unique_ptr uicamera; std::shared_ptr menu; void actMouse(float delta); public: diff --git a/src/frontend/gui/UINode.cpp b/src/frontend/gui/UINode.cpp index ff4e16b6..653e0f97 100644 --- a/src/frontend/gui/UINode.cpp +++ b/src/frontend/gui/UINode.cpp @@ -88,6 +88,14 @@ void UINode::setInteractive(bool flag) { interactive = flag; } +void UINode::setResizing(bool flag) { + resizing = flag; +} + +bool UINode::isResizing() const { + return resizing; +} + vec2 UINode::calcCoord() const { if (parent) { return coord + parent->calcCoord() + parent->contentOffset(); diff --git a/src/frontend/gui/UINode.h b/src/frontend/gui/UINode.h index 487deae1..630bb54b 100644 --- a/src/frontend/gui/UINode.h +++ b/src/frontend/gui/UINode.h @@ -13,12 +13,13 @@ namespace gui { class UINode; class GUI; - typedef std::function onaction; - typedef std::function onnumberchange; + using onaction = std::function; + using onnumberchange = std::function; enum class Align { left, center, right }; + class UINode { protected: glm::vec2 coord; @@ -31,6 +32,7 @@ namespace gui { bool pressed = false; bool focused = false; bool interactive = true; + bool resizing = true; Align align = Align::left; UINode* parent = nullptr; UINode(glm::vec2 coord, glm::vec2 size); @@ -96,6 +98,9 @@ namespace gui { /* Make the element opaque (true) or transparent (false) for cursor */ virtual void setInteractive(bool flag); + virtual void setResizing(bool flag); + virtual bool isResizing() const; + /* Get inner content offset. Used for scroll */ virtual glm::vec2 contentOffset() {return glm::vec2(0.0f);}; /* Calculate screen position of the element */ diff --git a/src/frontend/gui/controls.cpp b/src/frontend/gui/controls.cpp index 979ae935..7687a667 100644 --- a/src/frontend/gui/controls.cpp +++ b/src/frontend/gui/controls.cpp @@ -49,11 +49,19 @@ void Label::draw(const GfxContext* pctx, Assets* assets) { Font* font = assets->getFont(fontName_); vec2 size = getSize(); vec2 newsize = vec2(font->calcWidth(text), font->lineHeight()); - if (newsize.x > size.x) { - setSize(newsize); - size = newsize; - } + vec2 coord = calcCoord(); + switch (align) { + case Align::left: + break; + case Align::center: + coord.x += (size.x-newsize.x)*0.5f; + break; + case Align::right: + coord.x += size.x-newsize.x; + break; + } + font->draw(batch, text, coord.x, coord.y); } @@ -62,10 +70,6 @@ Label* Label::textSupplier(wstringsupplier supplier) { return this; } -void Label::setSize(vec2 sizenew) { - UINode::setSize(vec2(UINode::getSize().x, sizenew.y)); -} - // ================================= Image ==================================== Image::Image(std::string texture, vec2 size) : UINode(vec2(), size), texture(texture) { setInteractive(false); @@ -92,9 +96,19 @@ Button::Button(std::shared_ptr content, glm::vec4 padding) setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f)); } -Button::Button(std::wstring text, glm::vec4 padding, onaction action) - : Panel(vec2(32,32), padding, 0) +Button::Button( + std::wstring text, + vec4 padding, + onaction action, + vec2 size +) : Panel(size, padding, 0) { + size = vec2( + glm::max(padding.x + padding.z + text.length()*8, size.x), + glm::max(padding.y + padding.w + 16, size.y) + ); + setSize(size); + if (action) { listenAction(action); } @@ -102,6 +116,7 @@ Button::Button(std::wstring text, glm::vec4 padding, onaction action) label = std::make_shared