From 38f9805ca77022d69b6ca27b47a1b89e0e2dd384 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 16 Feb 2024 22:51:47 +0300 Subject: [PATCH] minor refactor + Block.uiLayout --- src/content/ContentLoader.cpp | 1 + src/frontend/InventoryView.cpp | 6 +---- src/frontend/InventoryView.h | 2 +- src/frontend/UiDocument.cpp | 2 +- src/frontend/gui/GUI.cpp | 2 +- src/frontend/gui/GUI.h | 3 ++- src/frontend/gui/UINode.h | 27 ++++++++++++------- .../gui/{panels.cpp => containers.cpp} | 3 ++- src/frontend/gui/{panels.h => containers.h} | 6 ++--- src/frontend/gui/controls.cpp | 6 ++--- src/frontend/gui/controls.h | 10 ++++--- src/frontend/gui/gui_util.cpp | 2 +- src/frontend/gui/gui_xml.cpp | 4 +-- src/frontend/hud.cpp | 6 ++--- src/frontend/menu.cpp | 4 +-- src/frontend/screens.cpp | 2 +- 16 files changed, 48 insertions(+), 38 deletions(-) rename src/frontend/gui/{panels.cpp => containers.cpp} (95%) rename src/frontend/gui/{panels.h => containers.h} (93%) diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 740ad1de..00a325d8 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -173,6 +173,7 @@ void ContentLoader::loadBlock(Block& def, std::string name, fs::path file) { root->num("draw-group", def.drawGroup); root->str("picking-item", def.pickingItem); root->str("script-name", def.scriptName); + root->str("ui-layout", def.uiLayout); root->num("inventory-size", def.inventorySize); } diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp index 05cd88fd..2cf41869 100644 --- a/src/frontend/InventoryView.cpp +++ b/src/frontend/InventoryView.cpp @@ -19,7 +19,7 @@ #include "../maths/voxmaths.h" #include "../objects/Player.h" #include "../voxels/Block.h" -#include "../frontend/gui/panels.h" +#include "../frontend/gui/containers.h" #include "../frontend/gui/controls.h" #include "../util/stringutil.h" #include "../world/Level.h" @@ -416,10 +416,6 @@ static void readSlotsGrid(InventoryView* view, gui::UiXmlReader& reader, xml::xm } layout.padding = padding; - glm::vec2 size ( - cols * slotSize + (cols - 1) * interval + padding * 2, - rows * slotSize + (rows - 1) * interval + padding * 2 - ); int idx = 0; for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++, idx++) { diff --git a/src/frontend/InventoryView.h b/src/frontend/InventoryView.h index 0a4cc0ba..d72a75f5 100644 --- a/src/frontend/InventoryView.h +++ b/src/frontend/InventoryView.h @@ -6,7 +6,7 @@ #include #include "../frontend/gui/UINode.h" -#include "../frontend/gui/panels.h" +#include "../frontend/gui/containers.h" #include "../frontend/gui/controls.h" #include "../items/ItemStack.h" #include "../typedefs.h" diff --git a/src/frontend/UiDocument.cpp b/src/frontend/UiDocument.cpp index 28b90825..0690eaa9 100644 --- a/src/frontend/UiDocument.cpp +++ b/src/frontend/UiDocument.cpp @@ -2,7 +2,7 @@ #include #include "gui/UINode.h" -#include "gui/panels.h" +#include "gui/containers.h" #include "InventoryView.h" #include "../logic/scripting/scripting.h" #include "../files/files.h" diff --git a/src/frontend/gui/GUI.cpp b/src/frontend/gui/GUI.cpp index 0886014c..a2ce3699 100644 --- a/src/frontend/gui/GUI.cpp +++ b/src/frontend/gui/GUI.cpp @@ -1,6 +1,6 @@ #include "GUI.h" #include "UINode.h" -#include "panels.h" +#include "containers.h" #include #include diff --git a/src/frontend/gui/GUI.h b/src/frontend/gui/GUI.h index b893372a..6258de66 100644 --- a/src/frontend/gui/GUI.h +++ b/src/frontend/gui/GUI.h @@ -48,6 +48,7 @@ namespace gui { class Container; class PagesControl; + /** The main UI controller */ class GUI { std::shared_ptr container; std::shared_ptr hover = nullptr; @@ -81,4 +82,4 @@ namespace gui { }; } -#endif // FRONTEND_GUI_GUI_H_ \ No newline at end of file +#endif // FRONTEND_GUI_GUI_H_ diff --git a/src/frontend/gui/UINode.h b/src/frontend/gui/UINode.h index 25c3133a..cdc380d9 100644 --- a/src/frontend/gui/UINode.h +++ b/src/frontend/gui/UINode.h @@ -23,6 +23,9 @@ namespace gui { }; class UINode { + /** + * element identifier used for direct access in UiDocument + */ std::string id = ""; protected: glm::vec2 coord; @@ -44,6 +47,9 @@ namespace gui { UINode(glm::vec2 coord, glm::vec2 size); public: virtual ~UINode(); + /** Called every frame for all visible elements + * @param delta delta time + */ virtual void act(float delta) {}; virtual void draw(const GfxContext* pctx, Assets* assets) = 0; @@ -59,11 +65,11 @@ namespace gui { virtual void setParent(UINode* node); UINode* getParent() const; - /* Set element color (doesn't affect inner elements). + /** Set element color (doesn't affect inner elements). Also replaces hover color to avoid adding extra properties. */ virtual void setColor(glm::vec4 newColor); - /* Get element color */ + /** Get element color (float R,G,B,A in range [0.0, 1.0])*/ glm::vec4 getColor() const; virtual void setHoverColor(glm::vec4 newColor); @@ -72,6 +78,8 @@ namespace gui { virtual void setMargin(glm::vec4 margin); glm::vec4 getMargin() const; + /** Influences container elements sort order + Doesn't work in Panel */ virtual void setZIndex(int idx); int getZIndex() const; @@ -86,20 +94,20 @@ namespace gui { void defocus(); bool isFocused() const; - /* Check if elements catches all user input when focused */ + /** Check if element catches all user input when focused */ virtual bool isFocuskeeper() const {return false;} virtual void typed(unsigned int codepoint) {}; virtual void keyPressed(int key) {}; - /* Check if screen position is inside of the element - @param pos screen position */ + /** Check if screen position is inside of the element + * @param pos screen position */ virtual bool isInside(glm::vec2 pos); - /* Get element under the cursor. - @param pos cursor screen position - @param self shared pointer to element - @return self, sub-element or nullptr if element is not interractive */ + /** Get element under the cursor. + * @param pos cursor screen position + * @param self shared pointer to element + * @return self, sub-element or nullptr if element is not interractive */ virtual std::shared_ptr getAt(glm::vec2 pos, std::shared_ptr self); /* Check if element is opaque for cursor */ @@ -120,6 +128,7 @@ namespace gui { virtual void setSize(glm::vec2 size); virtual glm::vec2 getMinSize() const; virtual void setMinSize(glm::vec2 size); + /* Called in containers when new element added */ virtual void refresh() {}; virtual void lock(); diff --git a/src/frontend/gui/panels.cpp b/src/frontend/gui/containers.cpp similarity index 95% rename from src/frontend/gui/panels.cpp rename to src/frontend/gui/containers.cpp index 4f7b2737..bea33bec 100644 --- a/src/frontend/gui/panels.cpp +++ b/src/frontend/gui/containers.cpp @@ -1,4 +1,4 @@ -#include "panels.h" +#include "containers.h" #include #include @@ -301,3 +301,4 @@ void PagesControl::reset() { current = Page{nullptr}; } } + diff --git a/src/frontend/gui/panels.h b/src/frontend/gui/containers.h similarity index 93% rename from src/frontend/gui/panels.h rename to src/frontend/gui/containers.h index 6056cb49..6ebe21b4 100644 --- a/src/frontend/gui/panels.h +++ b/src/frontend/gui/containers.h @@ -1,5 +1,5 @@ -#ifndef FRONTEND_GUI_PANELS_H_ -#define FRONTEND_GUI_PANELS_H_ +#ifndef FRONTEND_GUI_CONTAINERS_H_ +#define FRONTEND_GUI_CONTAINERS_H_ #include #include @@ -109,4 +109,4 @@ namespace gui { Page& getCurrent(); }; } -#endif // FRONTEND_GUI_PANELS_H_ \ No newline at end of file +#endif // FRONTEND_GUI_CONTAINERS_H_ diff --git a/src/frontend/gui/controls.cpp b/src/frontend/gui/controls.cpp index 481a8c64..9979b6f1 100644 --- a/src/frontend/gui/controls.cpp +++ b/src/frontend/gui/controls.cpp @@ -335,15 +335,15 @@ std::shared_ptr TextBox::getAt(glm::vec2 pos, std::shared_ptr se return UINode::getAt(pos, self); } -void TextBox::textSupplier(wstringsupplier supplier) { +void TextBox::setTextSupplier(wstringsupplier supplier) { this->supplier = supplier; } -void TextBox::textConsumer(wstringconsumer consumer) { +void TextBox::setTextConsumer(wstringconsumer consumer) { this->consumer = consumer; } -void TextBox::textValidator(wstringchecker validator) { +void TextBox::setTextValidator(wstringchecker validator) { this->validator = validator; } diff --git a/src/frontend/gui/controls.h b/src/frontend/gui/controls.h index 7ee35c1f..72aeb5cd 100644 --- a/src/frontend/gui/controls.h +++ b/src/frontend/gui/controls.h @@ -9,7 +9,7 @@ #include "GUI.h" #include "UINode.h" -#include "panels.h" +#include "containers.h" #include "../../window/input.h" #include "../../delegates.h" @@ -111,11 +111,13 @@ namespace gui { 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 void setTextSupplier(wstringsupplier supplier); + virtual void setTextConsumer(wstringconsumer consumer); + virtual void setTextValidator(wstringchecker validator); virtual bool isFocuskeeper() const override {return true;} + /* Get TextBox content text or placeholder if empty */ virtual std::wstring getText() const; + /* Set TextBox content text */ virtual void setText(std::wstring value); virtual bool validate(); virtual void setValid(bool valid); diff --git a/src/frontend/gui/gui_util.cpp b/src/frontend/gui/gui_util.cpp index 73b01d39..81ebb693 100644 --- a/src/frontend/gui/gui_util.cpp +++ b/src/frontend/gui/gui_util.cpp @@ -1,6 +1,6 @@ #include "gui_util.h" #include "controls.h" -#include "panels.h" +#include "containers.h" #include diff --git a/src/frontend/gui/gui_xml.cpp b/src/frontend/gui/gui_xml.cpp index 7463937d..8f395dfa 100644 --- a/src/frontend/gui/gui_xml.cpp +++ b/src/frontend/gui/gui_xml.cpp @@ -3,7 +3,7 @@ #include #include -#include "panels.h" +#include "containers.h" #include "controls.h" #include "../../assets/AssetsLoader.h" @@ -173,7 +173,7 @@ static std::shared_ptr readTextBox(UiXmlReader& reader, xml::xmlelement element->attr("consumer").getText(), reader.getFilename()+".lua" ); - textbox->textConsumer(consumer); + textbox->setTextConsumer(consumer); } return textbox; } diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index b42450f8..3826f866 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -33,7 +33,7 @@ #include "../physics/Hitbox.h" #include "../maths/voxmaths.h" #include "gui/controls.h" -#include "gui/panels.h" +#include "gui/containers.h" #include "gui/UINode.h" #include "gui/GUI.h" #include "ContentGfxCache.h" @@ -147,11 +147,11 @@ std::shared_ptr Hud::createDebugPanel(Engine* engine) { // Coord input auto box = std::make_shared(L""); - box->textSupplier([=]() { + box->setTextSupplier([=]() { Hitbox* hitbox = level->player->hitbox.get(); return util::to_wstring(hitbox->position[ax], 2); }); - box->textConsumer([=](std::wstring text) { + box->setTextConsumer([=](std::wstring text) { try { glm::vec3 position = level->player->hitbox->position; position[ax] = std::stoi(text); diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index 56aa500b..ce1ab9cc 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -9,7 +9,7 @@ #include #include "gui/GUI.h" -#include "gui/panels.h" +#include "gui/containers.h" #include "gui/controls.h" #include "screens.h" @@ -401,7 +401,7 @@ void create_new_world_panel(Engine* engine) { panel->add(std::make_shared