#include "gui_xml.h" #include #include #include "containers.h" #include "controls.h" #include "../../assets/AssetsLoader.h" #include "../locale/langs.h" #include "../../logic/scripting/scripting.h" #include "../../util/stringutil.h" using namespace gui; static Align align_from_string(const std::string& str, Align def) { if (str == "left") return Align::left; if (str == "center") return Align::center; if (str == "right") return Align::right; return def; } /* Read basic UINode properties */ static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& node) { if (element->has("id")) { node.setId(element->attr("id").getText()); } if (element->has("pos")) { node.setCoord(element->attr("pos").asVec2()); } if (element->has("size")) { node.setSize(element->attr("size").asVec2()); } if (element->has("color")) { glm::vec4 color = element->attr("color").asColor(); glm::vec4 hoverColor = color; if (element->has("hover-color")) { hoverColor = node.getHoverColor(); } node.setColor(color); node.setHoverColor(hoverColor); } if (element->has("margin")) { node.setMargin(element->attr("margin").asVec4()); } if (element->has("z-index")) { node.setZIndex(element->attr("z-index").asInt()); } if (element->has("interactive")) { node.setInteractive(element->attr("interactive").asBool()); } if (element->has("visible")) { node.setVisible(element->attr("visible").asBool()); } if (element->has("position-func")) { auto supplier = scripting::create_vec2_supplier( reader.getEnvironment().getId(), element->attr("position-func").getText(), reader.getFilename()+".lua" ); node.setPositionFunc(supplier); } if (element->has("hover-color")) { node.setHoverColor(element->attr("hover-color").asColor()); } std::string alignName = element->attr("align", "").getText(); node.setAlign(align_from_string(alignName, node.getAlign())); } static void _readContainer(UiXmlReader& reader, xml::xmlelement element, Container& container) { _readUINode(reader, element, container); if (element->has("scrollable")) { container.setScrollable(element->attr("scrollable").asBool()); } for (auto& sub : element->getElements()) { if (sub->isText()) continue; auto subnode = reader.readUINode(sub); if (subnode) { container.add(subnode); } } } void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, Container& container) { _readContainer(reader, element, container); } void UiXmlReader::readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& node) { _readUINode(reader, element, node); } static void _readPanel(UiXmlReader& reader, xml::xmlelement element, Panel& panel) { _readUINode(reader, element, panel); if (element->has("padding")) { glm::vec4 padding = element->attr("padding").asVec4(); panel.setPadding(padding); glm::vec2 size = panel.getSize(); panel.setSize(glm::vec2( size.x + padding.x + padding.z, size.y + padding.y + padding.w )); } if (element->has("size")) { panel.setResizing(false); } if (element->has("max-length")) { panel.setMaxLength(element->attr("max-length").asInt()); } for (auto& sub : element->getElements()) { if (sub->isText()) continue; auto subnode = reader.readUINode(sub); if (subnode) { panel.add(subnode); } } } static std::wstring readAndProcessInnerText(xml::xmlelement element) { std::wstring text = L""; if (element->size() == 1) { std::string source = element->sub(0)->attr("#").getText(); util::trim(source); text = util::str2wstr_utf8(source); if (text[0] == '@') { text = langs::get(text.substr(1)); } } return text; } static std::shared_ptr readLabel(UiXmlReader& reader, xml::xmlelement element) { std::wstring text = readAndProcessInnerText(element); auto label = std::make_shared