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

271 lines
9.2 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-07-13 23:12:36 +02:00
#include <global.h>
2011-04-17 21:14:24 +02:00
#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>
2011-07-13 23:12:36 +02:00
#include <otml/otml.h>
#include <ui/uianchorlayout.h>
#include <util/translator.h>
#include <boost/algorithm/string.hpp>
2011-04-10 22:40:44 +02:00
2011-05-19 19:11:05 +02:00
UILoader g_uiLoader;
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;
2011-07-13 23:12:36 +02:00
std::string elementType = split[0].substr(1);
2011-04-10 22:40:44 +02:00
std::string elementId = split[1];
2011-07-13 23:12:36 +02:00
if(elementType == "panel")
2011-04-17 21:14:24 +02:00
element = UIElementPtr(new UIContainer(UI::Panel));
2011-07-13 23:12:36 +02:00
else if(elementType == "button")
2011-04-10 22:40:44 +02:00
element = UIElementPtr(new UIButton);
2011-07-13 23:12:36 +02:00
else if(elementType == "label")
2011-04-10 22:40:44 +02:00
element = UIElementPtr(new UILabel);
2011-07-13 23:12:36 +02:00
else if(elementType == "window")
2011-04-10 22:40:44 +02:00
element = UIElementPtr(new UIWindow);
2011-07-13 23:12:36 +02:00
else if(elementType == "textEdit")
2011-04-10 22:40:44 +02:00
element = UIElementPtr(new UITextEdit);
2011-07-13 23:12:36 +02:00
else if(elementType == "lineDecoration")
2011-04-17 21:14:24 +02:00
element = UIElementPtr(new UIElement(UI::LineDecoration));
2011-07-13 23:12:36 +02:00
else if(elementType == "checkBox")
2011-04-17 02:36:58 +02:00
element = UIElementPtr(new UICheckBox);
2011-04-10 22:40:44 +02:00
2011-07-13 23:12:36 +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::loadFromFile(std::string filePath, const UIContainerPtr& parent)
2011-04-10 22:40:44 +02:00
{
2011-05-19 19:11:05 +02:00
std::stringstream fin;
if(!g_resources.loadFile(filePath, fin)) {
2011-07-13 23:12:36 +02:00
error("ERROR: Could not load ui ", filePath);
2011-04-10 22:40:44 +02:00
return UIElementPtr();
}
2011-05-22 00:24:10 +02:00
try {
2011-07-13 23:12:36 +02:00
OTMLParser parser(fin, filePath);
OTMLNode* doc = parser.getDocument();
2011-04-10 22:40:44 +02:00
// get element id
std::string elementId = doc->front()->tag();
2011-04-10 22:40:44 +02:00
// 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-07-13 23:12:36 +02:00
error(doc->front()->generateErrorMessage("invalid root element type"));
return element;
}
2011-04-10 22:40:44 +02:00
parent->addChild(element);
// populete it
if(element->asUIContainer())
populateContainer(element->asUIContainer(), doc->front());
2011-04-10 22:40:44 +02:00
// now do the real load
loadElements(element, doc->front());
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-07-13 23:12:36 +02:00
} catch(OTMLException e) {
error("ERROR: Failed to load ui ",filePath,": ", e.what());
2011-04-10 22:40:44 +02:00
}
return UIElementPtr();
}
2011-07-13 23:12:36 +02:00
void UILoader::populateContainer(const UIContainerPtr& parent, OTMLNode* node)
2011-04-10 22:40:44 +02:00
{
// populate ordered elements
2011-07-13 23:12:36 +02:00
foreach(OTMLNode* cnode, *node) {
std::string id = cnode->tag();
2011-07-13 23:12:36 +02:00
if(id[0] == '%') {
UIElementPtr element = createElementFromId(id);
if(!element) {
2011-07-13 23:12:36 +02:00
error(cnode->generateErrorMessage("invalid element type"));
continue;
}
parent->addChild(element);
2011-04-10 22:40:44 +02:00
// also populate this element if it's a parent
if(element->asUIContainer())
populateContainer(element->asUIContainer(), cnode);
2011-04-10 22:40:44 +02:00
}
}
}
2011-07-13 23:12:36 +02:00
void UILoader::loadElements(const UIElementPtr& parent, OTMLNode* node)
2011-04-10 22:40:44 +02:00
{
loadElement(parent, node);
if(UIContainerPtr container = parent->asUIContainer()) {
foreach(const UIElementPtr& element, container->getChildren()) {
2011-07-13 23:12:36 +02:00
foreach(OTMLNode* cnode, *node) {
// node found, load it
if(boost::ends_with(cnode->tag(), "#" + element->getId())) {
loadElements(element, cnode);
break;
}
2011-04-10 22:40:44 +02:00
}
}
}
}
2011-07-13 23:12:36 +02:00
void UILoader::loadElement(const UIElementPtr& element, OTMLNode* node)
2011-04-10 22:40:44 +02:00
{
2011-04-17 02:36:58 +02:00
// set element skin
2011-07-13 23:12:36 +02:00
if(OTMLNode* cnode = node->at("skin")) {
if(cnode->hasValue()) {
element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), cnode->value()));
} 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
if(node->hasNode("size"))
element->setSize(node->readAt<Size>("size"));
2011-07-13 23:12:36 +02:00
// load margins
element->setMarginLeft(node->readAtPath("margin/left", 0));
element->setMarginRight(node->readAtPath("margin/right", 0));
element->setMarginTop(node->readAtPath("margin/top", 0));
element->setMarginBottom(node->readAtPath("margin/bottom", 0));
2011-04-10 22:40:44 +02:00
2011-07-13 23:12:36 +02:00
// load anchors
loadElementAnchor(element, UI::AnchorLeft, node->atPath("anchors/left"));
loadElementAnchor(element, UI::AnchorRight, node->atPath("anchors/right"));
loadElementAnchor(element, UI::AnchorTop, node->atPath("anchors/top"));
loadElementAnchor(element, UI::AnchorBottom, node->atPath("anchors/bottom"));
loadElementAnchor(element, UI::AnchorHorizontalCenter, node->atPath("anchors/horizontalCenter"));
loadElementAnchor(element, UI::AnchorVerticalCenter, node->atPath("anchors/verticalCenter"));
2011-04-23 05:28:23 +02:00
2011-07-13 23:12:36 +02:00
// load basic element events
loadElementScriptFunction(element, node->at("onLoad"));
loadElementScriptFunction(element, node->at("onDestroy"));
2011-04-23 22:04:49 +02:00
// load specific element type
2011-07-13 23:12:36 +02:00
switch(element->getElementType()) {
case UI::Button:
loadButton(boost::static_pointer_cast<UIButton>(element), node);
break;
case UI::Window:
loadWindow(boost::static_pointer_cast<UIWindow>(element), node);
break;
case UI::Label:
loadLabel(boost::static_pointer_cast<UILabel>(element), node);
break;
default:
break;
}
2011-04-10 22:40:44 +02:00
}
2011-07-13 23:12:36 +02:00
void UILoader::loadElementAnchor(const UIElementPtr& anchoredElement, UI::AnchorPoint anchoredEdge, OTMLNode* node)
2011-04-10 22:40:44 +02:00
{
2011-07-13 23:12:36 +02:00
if(!node)
return;
std::string anchorDescription = node->value();
if(anchorDescription.empty()) {
error(node->generateErrorMessage("anchor is empty, did you forget to fill it?"));
return;
}
UIAnchorLayoutPtr layout = boost::dynamic_pointer_cast<UIAnchorLayout>(anchoredElement->getLayout());
if(!layout) {
2011-07-13 23:12:36 +02:00
error(node->generateErrorMessage("could not add anchor, because this element does not participate of an anchor layout"));
return;
}
2011-04-10 22:40:44 +02:00
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-07-13 23:12:36 +02:00
error(node->generateErrorMessage("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-07-13 23:12:36 +02:00
error(node->generateErrorMessage("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-07-13 23:12:36 +02:00
error(node->generateErrorMessage("anchoring failed"));
2011-04-10 22:40:44 +02:00
}
2011-04-22 00:44:30 +02:00
2011-07-13 23:12:36 +02:00
void UILoader::loadElementScriptFunction(const UIElementPtr& element, OTMLNode* node)
2011-04-22 00:44:30 +02:00
{
2011-07-13 23:12:36 +02:00
if(!node)
return;
2011-04-22 00:44:30 +02:00
2011-07-13 23:12:36 +02:00
std::string functionDesc;
functionDesc += g_resources.resolvePath(node->what()) + ":" + element->getId();
functionDesc += "[" + node->tag() + "]";
if(g_lua.loadBufferAsFunction(node->value(), functionDesc))
g_lua.setScriptableField(element, node->tag());
else
error(node->generateErrorMessage("failed to parse inline lua script"));
2011-04-22 00:44:30 +02:00
}
2011-05-19 19:11:05 +02:00
2011-07-13 23:12:36 +02:00
void UILoader::loadButton(const UIButtonPtr& button, OTMLNode* node)
2011-05-19 19:11:05 +02:00
{
2011-07-13 23:12:36 +02:00
button->setText(node->valueAt("text"));
loadElementScriptFunction(button, node->at("onClick"));
2011-05-19 19:11:05 +02:00
}
2011-07-13 23:12:36 +02:00
void UILoader::loadWindow(const UIWindowPtr& window, OTMLNode* node)
{
window->setTitle(node->readAt("title", std::string()));
}
void UILoader::loadLabel(const UILabelPtr& label, OTMLNode* node)
{
label->setText(node->readAt("text", std::string()));
label->setAlign(parseAlignment(node->readAt("align", std::string("left"))));
}