mirror of
https://github.com/edubart/otclient.git
synced 2025-10-17 21:13:26 +02:00
replace YAML with custom made library for reading text files named FML
This commit is contained in:
@@ -67,93 +67,81 @@ UIElementPtr UILoader::createElementFromId(const std::string& id)
|
||||
return element;
|
||||
}
|
||||
|
||||
UIElementPtr UILoader::loadFromYAML(std::string filePath, const UIContainerPtr& parent)
|
||||
UIElementPtr UILoader::loadFromFile(std::string filePath, const UIContainerPtr& parent)
|
||||
{
|
||||
std::stringstream fin;
|
||||
if(!g_resources.loadFile(filePath, fin)) {
|
||||
flogError("ERROR: Could not load ui file \"%s", filePath.c_str());
|
||||
flogError("ERROR: Could not load ui file \"%s", filePath.c_str());
|
||||
return UIElementPtr();
|
||||
}
|
||||
|
||||
m_currentFile = filePath;
|
||||
|
||||
try {
|
||||
YAML::Parser parser(fin);
|
||||
|
||||
YAML::Node doc;
|
||||
parser.GetNextDocument(doc);
|
||||
FML::Parser parser(fin);
|
||||
if(!parser.hasError()) {
|
||||
FML::Node* doc = parser.getDocument();
|
||||
|
||||
// get element id
|
||||
std::string elementId;
|
||||
doc.begin().first() >> elementId;
|
||||
std::string elementId = doc->front()->tag();
|
||||
|
||||
// first we should populate all elements
|
||||
// only after that we can load anchors
|
||||
|
||||
// create element interpreting it's id
|
||||
UIElementPtr element = createElementFromId(elementId);
|
||||
if(!element)
|
||||
yamlThrowError(doc.begin().first(), "invalid element type");
|
||||
if(!element) {
|
||||
logError(doc->front()->generateErrorMessage("invalid root element type"));
|
||||
return element;
|
||||
}
|
||||
parent->addChild(element);
|
||||
|
||||
// populete it
|
||||
if(element->asUIContainer())
|
||||
populateContainer(element->asUIContainer(), doc.begin().second());
|
||||
populateContainer(element->asUIContainer(), doc->front());
|
||||
|
||||
// now do the real load
|
||||
loadElements(element, doc.begin().second());
|
||||
loadElements(element, doc->front());
|
||||
|
||||
// report onLoad events
|
||||
element->onLoad();
|
||||
return element;
|
||||
} catch (YAML::Exception& e) {
|
||||
flogError("ERROR: Failed to load ui file \"%s\":\n %s", filePath.c_str() % e.what());
|
||||
} else {
|
||||
flogError("ERROR: Failed to load ui file \"%s\":\n %s", filePath.c_str() % parser.getErrorMessage());
|
||||
}
|
||||
|
||||
return UIElementPtr();
|
||||
}
|
||||
|
||||
void UILoader::populateContainer(const UIContainerPtr& parent, const YAML::Node& node)
|
||||
void UILoader::populateContainer(const UIContainerPtr& parent, FML::Node* node)
|
||||
{
|
||||
// order nodes
|
||||
std::map<int, std::string> orderedNodes;
|
||||
for(auto it = node.begin(); it != node.end(); ++it) {
|
||||
std::string id;
|
||||
it.first() >> id;
|
||||
|
||||
// check if it's an element id
|
||||
if(id.find("#") != std::string::npos)
|
||||
orderedNodes[it.first().GetMark().pos] = id;
|
||||
}
|
||||
|
||||
// populate ordered elements
|
||||
foreach(auto pair, orderedNodes) {
|
||||
std::string id = pair.second;
|
||||
const YAML::Node& cnode = node[id];
|
||||
foreach(FML::Node* cnode, *node) {
|
||||
std::string id = cnode->tag();
|
||||
if(id.find("#") != std::string::npos) {
|
||||
UIElementPtr element = createElementFromId(id);
|
||||
if(!element) {
|
||||
logError(cnode->generateErrorMessage("invalid element type"));
|
||||
continue;
|
||||
}
|
||||
parent->addChild(element);
|
||||
|
||||
UIElementPtr element = createElementFromId(id);
|
||||
if(!element) {
|
||||
logError(yamlErrorDesc(cnode, "invalid element type"));
|
||||
continue;
|
||||
// also populate this element if it's a parent
|
||||
if(element->asUIContainer())
|
||||
populateContainer(element->asUIContainer(), cnode);
|
||||
}
|
||||
parent->addChild(element);
|
||||
|
||||
// also populate this element if it's a parent
|
||||
if(element->asUIContainer())
|
||||
populateContainer(element->asUIContainer(), cnode);
|
||||
}
|
||||
}
|
||||
|
||||
void UILoader::loadElements(const UIElementPtr& parent, const YAML::Node& node)
|
||||
void UILoader::loadElements(const UIElementPtr& parent, FML::Node* node)
|
||||
{
|
||||
loadElement(parent, node);
|
||||
|
||||
if(UIContainerPtr container = parent->asUIContainer()) {
|
||||
foreach(const UIElementPtr& element, container->getChildren()) {
|
||||
for(auto it = node.begin(); it != node.end(); ++it) {
|
||||
foreach(FML::Node* cnode, *node) {
|
||||
// node found, load it
|
||||
if(boost::ends_with(yamlRead<std::string>(it.first()), "#" + element->getId())) {
|
||||
loadElements(element, it.second());
|
||||
if(boost::ends_with(cnode->tag(), "#" + element->getId())) {
|
||||
loadElements(element, cnode);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -161,13 +149,12 @@ void UILoader::loadElements(const UIElementPtr& parent, const YAML::Node& node)
|
||||
}
|
||||
}
|
||||
|
||||
void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
|
||||
void UILoader::loadElement(const UIElementPtr& element, FML::Node* node)
|
||||
{
|
||||
// set element skin
|
||||
if(yamlHasValue(node, "skin")) {
|
||||
const YAML::Node& cnode = node["skin"];
|
||||
if(cnode.Type() == YAML::NodeType::Scalar) {
|
||||
element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), yamlRead<std::string>(cnode)));
|
||||
if(FML::Node* cnode = node->at("skin")) {
|
||||
if(cnode->hasValue()) {
|
||||
element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), cnode->value()));
|
||||
} else {
|
||||
UIElementSkinPtr skin = UIElementSkinPtr(new UIElementSkin());
|
||||
skin->load(cnode);
|
||||
@@ -177,46 +164,45 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
|
||||
element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), "default"));
|
||||
|
||||
// load elements common proprieties
|
||||
if(yamlHasValue(node, "size"))
|
||||
element->setSize(yamlRead<Size>(node, "size"));
|
||||
element->setMarginLeft(yamlRead(node, "margin.left", 0));
|
||||
element->setMarginRight(yamlRead(node, "margin.right", 0));
|
||||
element->setMarginTop(yamlRead(node, "margin.top", 0));
|
||||
element->setMarginBottom(yamlRead(node, "margin.bottom", 0));
|
||||
if(node->hasNode("size"))
|
||||
element->setSize(node->readAt<Size>("size"));
|
||||
|
||||
if(yamlHasValue(node, "anchors.left"))
|
||||
loadElementAnchor(element, UI::AnchorLeft, node["anchors.left"]);
|
||||
element->setMarginLeft(node->readAt("margin.left", 0));
|
||||
element->setMarginRight(node->readAt("margin.right", 0));
|
||||
element->setMarginTop(node->readAt("margin.top", 0));
|
||||
element->setMarginBottom(node->readAt("margin.bottom", 0));
|
||||
|
||||
if(yamlHasValue(node, "anchors.right"))
|
||||
loadElementAnchor(element, UI::AnchorRight, node["anchors.right"]);
|
||||
if(node->hasNode("anchors.left"))
|
||||
loadElementAnchor(element, UI::AnchorLeft, node->at("anchors.left"));
|
||||
|
||||
if(yamlHasValue(node, "anchors.top"))
|
||||
loadElementAnchor(element, UI::AnchorTop, node["anchors.top"]);
|
||||
if(node->hasNode("anchors.right"))
|
||||
loadElementAnchor(element, UI::AnchorRight, node->at("anchors.right"));
|
||||
|
||||
if(yamlHasValue(node, "anchors.bottom"))
|
||||
loadElementAnchor(element, UI::AnchorBottom, node["anchors.bottom"]);
|
||||
if(node->hasNode("anchors.top"))
|
||||
loadElementAnchor(element, UI::AnchorTop, node->at("anchors.top"));
|
||||
|
||||
if(yamlHasValue(node, "anchors.horizontalCenter"))
|
||||
loadElementAnchor(element, UI::AnchorHorizontalCenter, node["anchors.horizontalCenter"]);
|
||||
if(node->hasNode("anchors.bottom"))
|
||||
loadElementAnchor(element, UI::AnchorBottom, node->at("anchors.bottom"));
|
||||
|
||||
if(yamlHasValue(node, "anchors.verticalCenter"))
|
||||
loadElementAnchor(element, UI::AnchorVerticalCenter, node["anchors.verticalCenter"]);
|
||||
if(node->hasNode("anchors.horizontalCenter"))
|
||||
loadElementAnchor(element, UI::AnchorHorizontalCenter, node->at("anchors.horizontalCenter"));
|
||||
|
||||
if(node->hasNode("anchors.verticalCenter"))
|
||||
loadElementAnchor(element, UI::AnchorVerticalCenter, node->at("anchors.verticalCenter"));
|
||||
|
||||
// load events
|
||||
if(yamlHasValue(node, "onLoad")) {
|
||||
const YAML::Node& cnode = node["onLoad"];
|
||||
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), getElementSourceDesc(element, "onLoad")))
|
||||
if(FML::Node* cnode = node->at("onLoad")) {
|
||||
if(g_lua.loadBufferAsFunction(cnode->value(), getElementSourceDesc(element, "onLoad")))
|
||||
g_lua.setScriptableField(element, "onLoad");
|
||||
else
|
||||
logError(yamlErrorDesc(cnode, "failed to parse inline lua script"));
|
||||
logError(cnode->generateErrorMessage("failed to parse inline lua script"));
|
||||
}
|
||||
|
||||
if(yamlHasValue(node, "onDestroy")) {
|
||||
const YAML::Node& cnode = node["onDestroy"];
|
||||
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), getElementSourceDesc(element, "onDestroy")))
|
||||
if(FML::Node* cnode = node->at("onDestroy")) {
|
||||
if(g_lua.loadBufferAsFunction(cnode->value(), getElementSourceDesc(element, "onDestroy")))
|
||||
g_lua.setScriptableField(element, "onDestroy");
|
||||
else
|
||||
logError(yamlErrorDesc(cnode, "failed to parse inline lua script"));
|
||||
logError(cnode->generateErrorMessage("failed to parse inline lua script"));
|
||||
}
|
||||
|
||||
// load specific element type
|
||||
@@ -224,30 +210,29 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
|
||||
loadButton(boost::static_pointer_cast<UIButton>(element), node);
|
||||
else if(element->getElementType() == UI::Window) {
|
||||
UIWindowPtr window = boost::static_pointer_cast<UIWindow>(element);
|
||||
window->setTitle(yamlRead(node, "title", std::string()));
|
||||
window->setTitle(node->readAt("title", std::string()));
|
||||
}
|
||||
else if(element->getElementType() == UI::Label) {
|
||||
UILabelPtr label = boost::static_pointer_cast<UILabel>(element);
|
||||
label->setText(yamlRead(node, "text", std::string()));
|
||||
label->setAlign(parseAlignment(yamlRead(node, "align", std::string("left"))));
|
||||
label->setText(node->readAt("text", std::string()));
|
||||
label->setAlign(parseAlignment(node->readAt("align", std::string("left"))));
|
||||
}
|
||||
}
|
||||
|
||||
void UILoader::loadElementAnchor(const UIElementPtr& anchoredElement, UI::AnchorPoint anchoredEdge, const YAML::Node& node)
|
||||
void UILoader::loadElementAnchor(const UIElementPtr& anchoredElement, UI::AnchorPoint anchoredEdge, FML::Node* node)
|
||||
{
|
||||
UIAnchorLayoutPtr layout = boost::dynamic_pointer_cast<UIAnchorLayout>(anchoredElement->getLayout());
|
||||
if(!layout) {
|
||||
logError(yamlErrorDesc(node, "could not add anchor, because this element does not participate of an anchor layout"));
|
||||
logError(node->generateErrorMessage("could not add anchor, because this element does not participate of an anchor layout"));
|
||||
return;
|
||||
}
|
||||
|
||||
std::string anchorDescription;
|
||||
node >> anchorDescription;
|
||||
std::string anchorDescription = node->value();
|
||||
|
||||
std::vector<std::string> split;
|
||||
boost::split(split, anchorDescription, boost::is_any_of(std::string(".")));
|
||||
if(split.size() != 2) {
|
||||
logError(yamlErrorDesc(node, "invalid anchor"));
|
||||
logError(node->generateErrorMessage("invalid anchor"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -255,25 +240,24 @@ void UILoader::loadElementAnchor(const UIElementPtr& anchoredElement, UI::Anchor
|
||||
UI::AnchorPoint anchorLineEdge = UIAnchorLayout::parseAnchorPoint(split[1]);
|
||||
|
||||
if(anchorLineEdge == UI::AnchorNone) {
|
||||
logError(yamlErrorDesc(node, "invalid anchor type"));
|
||||
logError(node->generateErrorMessage("invalid anchor type"));
|
||||
return;
|
||||
}
|
||||
|
||||
if(!layout->addAnchor(anchoredElement, anchoredEdge, AnchorLine(anchorLineElementId, anchorLineEdge)))
|
||||
logError(yamlErrorDesc(node, "anchoring failed"));
|
||||
logError(node->generateErrorMessage("anchoring failed"));
|
||||
}
|
||||
|
||||
void UILoader::loadButton(const UIButtonPtr& button, const YAML::Node& node)
|
||||
void UILoader::loadButton(const UIButtonPtr& button, FML::Node* node)
|
||||
{
|
||||
button->setText(yamlRead<std::string>(node["text"]));
|
||||
button->setText(node->valueAt("text"));
|
||||
|
||||
// set on click event
|
||||
if(yamlHasValue(node, "onClick")) {
|
||||
const YAML::Node& cnode = node["onClick"];
|
||||
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), getElementSourceDesc(button, "onClick")))
|
||||
if(FML::Node* cnode = node->at("onClick")) {
|
||||
if(g_lua.loadBufferAsFunction(cnode->value(), getElementSourceDesc(button, "onClick")))
|
||||
g_lua.setScriptableField(button, "onClick");
|
||||
else
|
||||
logError(yamlErrorDesc(cnode, "failed to parse inline lua script"));
|
||||
logError(cnode->generateErrorMessage("failed to parse inline lua script"));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user