233 lines
5.6 KiB
Plaintext
233 lines
5.6 KiB
Plaintext
#include "cXMLoader.hpp"
|
|
|
|
|
|
/*** Custom Header Files ***/
|
|
#include "../../UtilityEngine/cUtility.hpp"
|
|
|
|
#include "../cGUI.hpp"
|
|
#include "../cLayout.hpp"
|
|
|
|
using GUIHelpers::cXMLoader;
|
|
|
|
using UtilityEngine::cUtility;
|
|
|
|
using tinyxml2::XMLDocument;
|
|
using tinyxml2::XMLNode;
|
|
using tinyxml2::XMLVisitor;
|
|
using tinyxml2::XMLElement;
|
|
|
|
using tinyxml2::XMLError;
|
|
|
|
/*static*/ cXMLoader* cXMLoader::sp_inst = nullptr;
|
|
|
|
cXMLoader::cXMLoader()
|
|
{
|
|
|
|
}
|
|
|
|
cXMLoader::~cXMLoader()
|
|
{
|
|
|
|
}
|
|
|
|
//PUBLIC
|
|
///Functions
|
|
/*static*/ cXMLoader& cXMLoader::Inst()
|
|
{
|
|
if (sp_inst == nullptr)
|
|
sp_inst = new cXMLoader();
|
|
return *sp_inst;
|
|
}
|
|
|
|
/*static*/ void cXMLoader::Delete()
|
|
{
|
|
delete sp_inst;
|
|
sp_inst = nullptr;
|
|
}
|
|
|
|
void cXMLoader::Load( const cString& filename, const cString& dir /*= ""*/ )
|
|
{
|
|
cString file = dir + filename;
|
|
XMLDocument doc;
|
|
|
|
if (doc.LoadFile(file.c_str()) != XMLError::XML_NO_ERROR) {
|
|
cUtility::Inst().Message("Could not load file. " + file + " Error='" + doc.ErrorName() + "'.");
|
|
} else {
|
|
XMLNode* node = doc.FirstChildElement("GUI");
|
|
|
|
if (node == nullptr) {
|
|
cUtility::Inst().Message("No GUI tag found.");
|
|
} else {
|
|
GetNode(node->ToElement());
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
void cXMLoader::GetNode( tinyxml2::XMLElement* element, GUIEngine::cWindow* parent /*= nullptr*/)
|
|
{
|
|
if (element != nullptr){
|
|
cString name = element->Name();
|
|
GUIEngine::cWindow* tmp = nullptr;
|
|
if (name == "Layout")
|
|
tmp = LayoutBuild(element, parent);
|
|
if (name == "Label")
|
|
tmp = LabelBuild(element, parent);
|
|
tinyxml2::XMLElement* child = element->FirstChildElement();
|
|
if (child != nullptr)
|
|
GetNode(child, tmp);
|
|
|
|
tinyxml2::XMLElement* sibling = element->NextSiblingElement();
|
|
if (sibling != nullptr)
|
|
GetNode(sibling, parent);
|
|
|
|
if ((parent == nullptr) && (tmp != nullptr))
|
|
tmp->Resize();
|
|
}
|
|
}
|
|
|
|
const char* cXMLoader::GetAttribute(const tinyxml2::XMLElement* element, const cString& attribute) const
|
|
{
|
|
const char* rtn = element->Attribute(attribute.c_str());
|
|
if (rtn == nullptr) {
|
|
cUtility::Inst().Message("Error no attribute: " + attribute + " found in tag <" + element->Value() + ">");
|
|
rtn = "";
|
|
}
|
|
return rtn;
|
|
}
|
|
|
|
GUIEngine::cLayout* cXMLoader::LayoutBuild( tinyxml2::XMLElement* element, GUIEngine::cWindow* parent)
|
|
{
|
|
signed long int id = getID(element);
|
|
GUIHelpers::eOrientation orientation = getOrientation(element);
|
|
GUIHelpers::eAlign align = getAlign(element);
|
|
GUIHelpers::eLayout layout = getLayout(element);
|
|
GUIHelpers::Position pos = getPosition(element);
|
|
GUIHelpers::Size size = getSize(element);
|
|
GUIHelpers::Padding pad = getPadding(element);
|
|
|
|
|
|
GUIEngine::cLayout* rtn = new GUIEngine::cLayout(parent, id, orientation, align, layout, pos, size, pad);
|
|
|
|
|
|
if (parent == nullptr)
|
|
GUIEngine::cGUI::Inst().AddObject(rtn);
|
|
|
|
return rtn;
|
|
}
|
|
|
|
GUIEngine::cLabel* cXMLoader::LabelBuild(tinyxml2::XMLElement* element, GUIEngine::cWindow* parent)
|
|
{
|
|
signed long int id = getID(element);
|
|
//GUIHelpers::eOrientation orientation = getOrientation(element);
|
|
GUIHelpers::eAlign align = getAlign(element);
|
|
GUIHelpers::eLayout layout = getLayout(element);
|
|
GUIHelpers::Position pos = getPosition(element);
|
|
GUIHelpers::Size size = getSize(element);
|
|
GUIHelpers::Padding pad = getPadding(element);
|
|
|
|
cString txt = getText(element);
|
|
|
|
GUIEngine::cLabel* rtn = new GUIEngine::cLabel(parent, id, txt, align, layout, pos, size, pad);
|
|
|
|
return rtn;
|
|
}
|
|
|
|
const signed long int cXMLoader::getID(tinyxml2::XMLElement* element)
|
|
{
|
|
cString rtn = GetAttribute(element, "id");
|
|
return rtn.ToInt();
|
|
}
|
|
|
|
const GUIHelpers::eOrientation cXMLoader::getOrientation( tinyxml2::XMLElement* element )
|
|
{
|
|
GUIHelpers::eOrientation rtn = GUIHelpers::eOrientation::DEFAULT_ORIENTATION;
|
|
|
|
cString str = GetAttribute(element, "orientation");
|
|
|
|
if (str == "none")
|
|
rtn = GUIHelpers::eOrientation::NONE;
|
|
if (str == "horizontal")
|
|
rtn = GUIHelpers::eOrientation::HORIZONTAL;
|
|
if (str == "vertical")
|
|
rtn = GUIHelpers::eOrientation::VERTICAL;
|
|
|
|
return rtn;
|
|
}
|
|
|
|
const GUIHelpers::eAlign cXMLoader::getAlign(tinyxml2::XMLElement* element)
|
|
{
|
|
GUIHelpers::eAlign rtn = GUIHelpers::eAlign::DEFAULT_ALIGN;
|
|
|
|
cString str = GetAttribute(element, "align");
|
|
|
|
if (str == "center")
|
|
rtn = GUIHelpers::eAlign::CENTER;
|
|
if (str == "top")
|
|
rtn = GUIHelpers::eAlign::TOP;
|
|
if (str == "right")
|
|
rtn = GUIHelpers::eAlign::RIGHT;
|
|
if (str == "bottom")
|
|
rtn = GUIHelpers::eAlign::BOTTOM;
|
|
if (str == "left")
|
|
rtn = GUIHelpers::eAlign::LEFT;
|
|
|
|
return rtn;
|
|
}
|
|
|
|
const GUIHelpers::eLayout cXMLoader::getLayout(tinyxml2::XMLElement* element)
|
|
{
|
|
GUIHelpers::eLayout rtn = GUIHelpers::eLayout::DEFAULT_LAYOUT;
|
|
|
|
cString str = GetAttribute(element, "layout");
|
|
|
|
if (str == "fill_parent")
|
|
rtn = GUIHelpers::eLayout::FILL_PARENT;
|
|
if (str == "match_parent")
|
|
rtn = GUIHelpers::eLayout::MATCH_PARENT;
|
|
if (str == "wrap_content")
|
|
rtn = GUIHelpers::eLayout::WRAP_CONTENT;
|
|
|
|
return rtn;
|
|
}
|
|
|
|
const GUIHelpers::Position cXMLoader::getPosition(tinyxml2::XMLElement* element)
|
|
{
|
|
GUIHelpers::Position rtn = { 0, 0 };
|
|
|
|
/*cString str = GetAttribute(element, "position");*/
|
|
|
|
return rtn;
|
|
}
|
|
|
|
const GUIHelpers::Size cXMLoader::getSize(tinyxml2::XMLElement* element)
|
|
{
|
|
GUIHelpers::Size rtn = { 0, 0 };
|
|
|
|
/*cString str = GetAttribute(element, "position");*/
|
|
|
|
return rtn;
|
|
}
|
|
|
|
const GUIHelpers::Padding cXMLoader::getPadding(tinyxml2::XMLElement* element)
|
|
{
|
|
GUIHelpers::Padding rtn = { -1, -1, -1, -1 };
|
|
|
|
cString str = GetAttribute(element, "padding");
|
|
|
|
if (str != "") {
|
|
std::vector<cString> strV = str.split(",");
|
|
|
|
rtn.x = strV[0].ToInt();
|
|
rtn.y = strV[1].ToInt();
|
|
rtn.w = strV[2].ToInt();
|
|
rtn.z = strV[3].ToInt();
|
|
}
|
|
|
|
return rtn;
|
|
}
|
|
|
|
const cString cXMLoader::getText(tinyxml2::XMLElement* element)
|
|
{
|
|
return GetAttribute(element, "text");
|
|
} |