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

247 lines
7.9 KiB
C++
Raw Normal View History

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-07-17 02:13:53 +02:00
#include <script/scriptcontext.h>
#include <script/scriptfunctions.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-07-17 02:13:53 +02:00
if(split.size() != 2) {
2011-04-10 22:40:44 +02:00
return element;
2011-07-17 02:13:53 +02:00
}
2011-04-10 22:40:44 +02:00
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-07-17 08:56:57 +02:00
UIElementPtr element;
2011-05-19 19:11:05 +02:00
std::stringstream fin;
2011-07-17 08:56:57 +02:00
if(!g_resources.loadFile(filePath, fin))
return element;
2011-04-10 22:40:44 +02:00
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
2011-07-17 08:56:57 +02:00
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-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
}
2011-07-17 08:56:57 +02:00
return element;
2011-04-10 22:40:44 +02:00
}
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-07-17 08:56:57 +02:00
element->applyDefaultSkin();
2011-04-10 22:40:44 +02:00
2011-04-17 02:36:58 +02:00
// load elements common proprieties
2011-07-17 02:13:53 +02:00
if(node->hasChild("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
2011-07-17 08:56:57 +02:00
loadElementAnchor(element, AnchorLeft, node->atPath("anchors/left"));
loadElementAnchor(element, AnchorRight, node->atPath("anchors/right"));
loadElementAnchor(element, AnchorTop, node->atPath("anchors/top"));
loadElementAnchor(element, AnchorBottom, node->atPath("anchors/bottom"));
loadElementAnchor(element, AnchorHorizontalCenter, node->atPath("anchors/horizontalCenter"));
loadElementAnchor(element, 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-17 08:56:57 +02:00
void UILoader::loadElementAnchor(const UIElementPtr& anchoredElement, 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];
2011-07-17 08:56:57 +02:00
AnchorPoint anchorLineEdge = UIAnchorLayout::parseAnchorPoint(split[1]);
2011-07-17 08:56:57 +02:00
if(anchorLineEdge == 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))
2011-07-17 02:13:53 +02:00
g_lua.setScriptObjectField(element, node->tag());
2011-07-13 23:12:36 +02:00
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"))));
}