tibia-client/src/framework/ui/uiloader.cpp

282 lines
10 KiB
C++
Raw Normal View History

2011-04-10 22:40:44 +02:00
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2011-04-17 21:14:24 +02:00
#include <prerequisites.h>
#include <core/resources.h>
#include <ui/ui.h>
#include <ui/uiloader.h>
2011-04-22 00:44:30 +02:00
#include <script/luascript.h>
2011-05-01 20:47:35 +02:00
#include <script/luafunctions.h>
#include "uianchorlayout.h"
2011-04-10 22:40:44 +02:00
UIElementPtr UILoader::createElementFromId(const std::string& id)
{
UIElementPtr element;
std::vector<std::string> split;
2011-04-12 01:45:18 +02:00
boost::split(split, id, boost::is_any_of(std::string("#")));
2011-04-10 22:40:44 +02:00
if(split.size() != 2)
return element;
std::string elementType = split[0];
std::string elementId = split[1];
if(elementType == "panel") {
2011-04-17 21:14:24 +02:00
element = UIElementPtr(new UIContainer(UI::Panel));
2011-04-10 22:40:44 +02:00
} else if(elementType == "button") {
element = UIElementPtr(new UIButton);
} else if(elementType == "label") {
element = UIElementPtr(new UILabel);
} else if(elementType == "window") {
element = UIElementPtr(new UIWindow);
} else if(elementType == "textEdit") {
element = UIElementPtr(new UITextEdit);
2011-04-17 02:36:58 +02:00
} else if(elementType == "lineDecoration") {
2011-04-17 21:14:24 +02:00
element = UIElementPtr(new UIElement(UI::LineDecoration));
2011-04-17 02:36:58 +02:00
} else if(elementType == "checkBox") {
element = UIElementPtr(new UICheckBox);
2011-04-10 22:40:44 +02:00
}
2011-04-16 18:08:55 +02:00
if(element) {
2011-04-10 22:40:44 +02:00
element->setId(elementId);
2011-04-16 18:08:55 +02:00
}
2011-04-10 22:40:44 +02:00
return element;
}
UIElementPtr UILoader::loadFile(const std::string& file, const UIContainerPtr& parent)
{
// try to find the file
std::string filePath = "modules/" + file;
if(!g_resources.fileExists(filePath))
filePath = "addons/" + file;
if(!g_resources.fileExists(filePath))
filePath = file;
if(!g_resources.fileExists(filePath)) {
flogError("ERROR: Could not load ui file \"%s", file.c_str());
2011-04-10 22:40:44 +02:00
return UIElementPtr();
}
std::istringstream fin(g_resources.loadTextFile(filePath));
2011-04-10 22:40:44 +02:00
try {
YAML::Parser parser(fin);
YAML::Node doc;
parser.GetNextDocument(doc);
// get element id
std::string elementId;
doc.begin().first() >> elementId;
// 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)
2011-05-12 00:16:11 +02:00
yamlThrowError(doc.begin().first(), "invalid element type");
2011-04-10 22:40:44 +02:00
parent->addChild(element);
// populete it
if(element->asUIContainer())
populateContainer(element->asUIContainer(), doc.begin().second());
// now do the real load
loadElements(element, doc.begin().second());
2011-04-11 06:08:56 +02:00
2011-04-23 05:28:23 +02:00
// report onLoad events
element->onLoad();
2011-04-11 06:08:56 +02:00
return element;
2011-04-10 22:40:44 +02:00
} catch (YAML::Exception& e) {
flogError("ERROR: Failed to load ui file \"%s\":\n %s", file.c_str() % e.what());
2011-04-10 22:40:44 +02:00
}
return UIElementPtr();
}
void UILoader::populateContainer(const UIContainerPtr& parent, const YAML::Node& node)
{
// order nodes
std::map<int, std::string> orderedNodes;
2011-04-10 22:40:44 +02:00
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];
2011-04-10 22:40:44 +02:00
UIElementPtr element = createElementFromId(id);
if(!element) {
2011-05-12 00:16:11 +02:00
logError(yamlErrorDesc(cnode, "invalid element type"));
continue;
2011-04-10 22:40:44 +02:00
}
parent->addChild(element);
// also populate this element if it's a parent
if(element->asUIContainer())
populateContainer(element->asUIContainer(), cnode);
2011-04-10 22:40:44 +02:00
}
}
void UILoader::loadElements(const UIElementPtr& parent, const YAML::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) {
// node found, load it
2011-05-14 04:21:25 +02:00
if(boost::ends_with(yamlRead<std::string>(it.first()), "#" + element->getId())) {
loadElements(element, it.second());
break;
}
2011-04-10 22:40:44 +02:00
}
}
}
}
void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
{
2011-04-17 02:36:58 +02:00
// set element skin
2011-05-12 00:16:11 +02:00
if(yamlHasValue(node, "skin")) {
const YAML::Node& cnode = node["skin"];
2011-05-14 04:21:25 +02:00
if(cnode.Type() == YAML::NodeType::Scalar) {
element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), yamlRead<std::string>(cnode)));
} else {
UIElementSkinPtr skin = UIElementSkinPtr(new UIElementSkin());
2011-05-12 00:16:11 +02:00
skin->load(cnode);
element->setSkin(skin);
}
} else // apply default skin
2011-04-17 02:36:58 +02:00
element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), "default"));
2011-04-10 22:40:44 +02:00
2011-04-17 02:36:58 +02:00
// load elements common proprieties
2011-05-12 00:16:11 +02:00
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(yamlHasValue(node, "anchors.left"))
loadElementAnchor(element, UI::AnchorLeft, node["anchors.left"]);
2011-04-10 22:40:44 +02:00
2011-05-12 00:16:11 +02:00
if(yamlHasValue(node, "anchors.right"))
loadElementAnchor(element, UI::AnchorRight, node["anchors.right"]);
2011-04-10 22:40:44 +02:00
2011-05-12 00:16:11 +02:00
if(yamlHasValue(node, "anchors.top"))
loadElementAnchor(element, UI::AnchorTop, node["anchors.top"]);
2011-04-10 22:40:44 +02:00
2011-05-12 00:16:11 +02:00
if(yamlHasValue(node, "anchors.bottom"))
loadElementAnchor(element, UI::AnchorBottom, node["anchors.bottom"]);
2011-04-10 22:40:44 +02:00
2011-05-12 00:16:11 +02:00
if(yamlHasValue(node, "anchors.horizontalCenter"))
loadElementAnchor(element, UI::AnchorHorizontalCenter, node["anchors.horizontalCenter"]);
2011-04-10 22:40:44 +02:00
2011-05-12 00:16:11 +02:00
if(yamlHasValue(node, "anchors.verticalCenter"))
loadElementAnchor(element, UI::AnchorVerticalCenter, node["anchors.verticalCenter"]);
2011-04-23 05:28:23 +02:00
// load events
2011-05-12 00:16:11 +02:00
if(yamlHasValue(node, "onLoad")) {
2011-04-23 05:28:23 +02:00
const YAML::Node& cnode = node["onLoad"];
2011-05-12 00:16:11 +02:00
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), element->getId() + ":onLoad"))
2011-05-03 00:48:41 +02:00
g_lua.setScriptableField(element, "onLoad");
else
2011-05-12 00:16:11 +02:00
logError(yamlErrorDesc(cnode, "failed to parse inline lua script"));
2011-04-23 05:28:23 +02:00
}
2011-05-12 00:16:11 +02:00
if(yamlHasValue(node, "onDestroy")) {
2011-04-23 05:28:23 +02:00
const YAML::Node& cnode = node["onDestroy"];
2011-05-12 00:16:11 +02:00
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), element->getId() + ":onDestroy"))
2011-05-03 00:48:41 +02:00
g_lua.setScriptableField(element, "onDestroy");
else
2011-05-12 00:16:11 +02:00
logError(yamlErrorDesc(cnode, "failed to parse inline lua script"));
2011-04-23 05:28:23 +02:00
}
2011-04-23 22:04:49 +02:00
// load specific element type
if(element->getElementType() == UI::Button)
loadButton(boost::static_pointer_cast<UIButton>(element), node);
else if(element->getElementType() == UI::Window) {
UIWindowPtr window = boost::static_pointer_cast<UIWindow>(element);
2011-05-12 00:16:11 +02:00
window->setTitle(yamlRead(node, "title", std::string()));
2011-04-23 22:04:49 +02:00
}
else if(element->getElementType() == UI::Label) {
UILabelPtr label = boost::static_pointer_cast<UILabel>(element);
2011-05-12 00:16:11 +02:00
label->setText(yamlRead(node, "text", std::string()));
2011-05-13 01:24:57 +02:00
label->setAlign(parseAlignment(yamlRead(node, "align", std::string("left"))));
2011-04-23 22:04:49 +02:00
}
2011-04-10 22:40:44 +02:00
}
void UILoader::loadElementAnchor(const UIElementPtr& anchoredElement, UI::AnchorPoint anchoredEdge, const YAML::Node& node)
2011-04-10 22:40:44 +02:00
{
UIAnchorLayoutPtr layout = boost::dynamic_pointer_cast<UIAnchorLayout>(anchoredElement->getLayout());
if(!layout) {
2011-05-12 00:16:11 +02:00
logError(yamlErrorDesc(node, "could not add anchor, because this element does not participate of an anchor layout"));
return;
}
2011-04-10 22:40:44 +02:00
std::string anchorDescription;
node >> anchorDescription;
std::vector<std::string> split;
2011-04-12 01:45:18 +02:00
boost::split(split, anchorDescription, boost::is_any_of(std::string(".")));
2011-04-24 01:23:52 +02:00
if(split.size() != 2) {
2011-05-12 00:16:11 +02:00
logError(yamlErrorDesc(node, "invalid anchor"));
2011-04-24 01:23:52 +02:00
return;
}
2011-04-10 22:40:44 +02:00
std::string anchorLineElementId = split[0];
UI::AnchorPoint anchorLineEdge = UIAnchorLayout::parseAnchorPoint(split[1]);
if(anchorLineEdge == UI::AnchorNone) {
2011-05-12 00:16:11 +02:00
logError(yamlErrorDesc(node, "invalid anchor type"));
2011-04-24 01:23:52 +02:00
return;
}
2011-04-10 22:40:44 +02:00
if(!layout->addAnchor(anchoredElement, anchoredEdge, AnchorLine(anchorLineElementId, anchorLineEdge)))
2011-05-12 00:16:11 +02:00
logError(yamlErrorDesc(node, "anchoring failed"));
2011-04-10 22:40:44 +02:00
}
2011-04-22 00:44:30 +02:00
void UILoader::loadButton(const UIButtonPtr& button, const YAML::Node& node)
{
2011-05-14 04:21:25 +02:00
button->setText(yamlRead<std::string>(node["text"]));
2011-04-22 00:44:30 +02:00
// set on click event
2011-05-12 00:16:11 +02:00
if(yamlHasValue(node, "onClick")) {
const YAML::Node& cnode = node["onClick"];
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), button->getId() + ":onClick"))
2011-05-03 00:48:41 +02:00
g_lua.setScriptableField(button, "onClick");
else
2011-05-12 00:16:11 +02:00
logError(yamlErrorDesc(cnode, "failed to parse inline lua script"));
2011-04-22 00:44:30 +02:00
}
}