diff --git a/src/graphics/ui/GUI.cpp b/src/graphics/ui/GUI.cpp index 828315e6..4de82509 100644 --- a/src/graphics/ui/GUI.cpp +++ b/src/graphics/ui/GUI.cpp @@ -368,3 +368,7 @@ Window& GUI::getWindow() { devtools::Editor& GUI::getEditor() { return engine.getEditor(); } + +Engine& GUI::getEngine() { + return engine; +} diff --git a/src/graphics/ui/GUI.hpp b/src/graphics/ui/GUI.hpp index b5c70bdd..c8bb20e2 100644 --- a/src/graphics/ui/GUI.hpp +++ b/src/graphics/ui/GUI.hpp @@ -164,5 +164,6 @@ namespace gui { Input& getInput(); Window& getWindow(); devtools::Editor& getEditor(); + Engine& getEngine(); }; } diff --git a/src/graphics/ui/elements/InlineFrame.cpp b/src/graphics/ui/elements/InlineFrame.cpp new file mode 100644 index 00000000..d81d499f --- /dev/null +++ b/src/graphics/ui/elements/InlineFrame.cpp @@ -0,0 +1,51 @@ +#include "InlineFrame.hpp" +#include "frontend/UiDocument.hpp" +#include "logic/scripting/scripting.hpp" +#include "assets/Assets.hpp" +#include "engine/Engine.hpp" +#include "../GUI.hpp" + +using namespace gui; + +InlineFrame::InlineFrame(GUI& gui) : Container(gui, glm::vec2(1)) {} +InlineFrame::~InlineFrame() = default; + +void InlineFrame::setSrc(const std::string& src) { + this->src = src; + if (document) { + scripting::on_ui_close(document.get(), nullptr); + document = nullptr; + root = nullptr; + } +} + +void InlineFrame::setDocument(const std::shared_ptr& document) { + clear(); + if (document == nullptr) { + return; + } + this->document = document; + this->root = document->getRoot(); + add(root); + + root->setSize(size); + + gui.postRunnable([this]() { + scripting::on_ui_open(this->document.get(), {}); + }); +} + +void InlineFrame::act(float delta) { + if (document || src.empty()) { + return; + } + const auto& assets = *gui.getEngine().getAssets(); + setDocument(assets.getShared(src)); +} + +void InlineFrame::setSize(glm::vec2 size) { + Container::setSize(size); + if (root) { + root->setSize(size); + } +} diff --git a/src/graphics/ui/elements/InlineFrame.hpp b/src/graphics/ui/elements/InlineFrame.hpp new file mode 100644 index 00000000..fd8a6967 --- /dev/null +++ b/src/graphics/ui/elements/InlineFrame.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "Container.hpp" + +class UiDocument; + +namespace gui { + class InlineFrame : public Container { + public: + explicit InlineFrame(GUI& gui); + virtual ~InlineFrame(); + + void setSrc(const std::string& src); + void setDocument(const std::shared_ptr& document); + + void act(float delta) override; + void setSize(glm::vec2 size) override; + private: + std::string src; + std::shared_ptr document; + std::shared_ptr root; + }; +} diff --git a/src/graphics/ui/gui_xml.cpp b/src/graphics/ui/gui_xml.cpp index 31c7bd56..151d1239 100644 --- a/src/graphics/ui/gui_xml.cpp +++ b/src/graphics/ui/gui_xml.cpp @@ -12,6 +12,7 @@ #include "elements/SplitBox.hpp" #include "elements/TrackBar.hpp" #include "elements/Image.hpp" +#include "elements/InlineFrame.hpp" #include "elements/InputBindBox.hpp" #include "elements/InventoryView.hpp" #include "elements/Menu.hpp" @@ -292,7 +293,7 @@ static std::wstring parse_inner_text( return text; } -static std::shared_ptr readLabel( +static std::shared_ptr read_label( const UiXmlReader& reader, const xml::xmlelement& element ) { std::wstring text = parse_inner_text(element, reader.getContext()); @@ -739,11 +740,24 @@ static std::shared_ptr read_page_box( return menu; } +static std::shared_ptr read_iframe( + UiXmlReader& reader, const xml::xmlelement& element +) { + auto& gui = reader.getGUI(); + auto iframe = std::make_shared(gui); + read_container_impl(reader, element, *iframe); + + std::string src = element.attr("src", "").getText(); + iframe->setSrc(src); + return iframe; +} + UiXmlReader::UiXmlReader(gui::GUI& gui, const scriptenv& env) : gui(gui), env(env) { contextStack.emplace(""); add("image", read_image); add("canvas", read_canvas); - add("label", readLabel); + add("iframe", read_iframe); + add("label", read_label); add("panel", read_panel); add("button", read_button); add("textbox", read_text_box);