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

106 lines
3.9 KiB
C++
Raw Normal View History

2011-04-09 01:20: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 <graphics/textures.h>
#include <ui/uiskins.h>
#include <ui/uielementskin.h>
#include <ui/uibuttonskin.h>
#include <ui/uiwindowskin.h>
#include <ui/uitexteditskin.h>
#include <ui/uilabelskin.h>
2011-05-12 00:16:11 +02:00
#include <graphics/fonts.h>
2011-07-13 23:12:36 +02:00
#include <otml/otml.h>
2011-04-09 01:20:44 +02:00
UISkins g_uiSkins;
2011-05-19 19:11:05 +02:00
void UISkins::load(const std::string& skinName)
2011-04-10 03:13:12 +02:00
{
2011-05-19 19:11:05 +02:00
g_resources.pushCurrentPath("skins");
2011-04-09 01:20:44 +02:00
2011-05-19 19:11:05 +02:00
std::stringstream fin;
2011-07-17 02:13:53 +02:00
if(!g_resources.loadFile(skinName + ".otml", fin))
2011-07-13 23:12:36 +02:00
fatal("FATAL ERROR: Could not load skin '",skinName,"'");
2011-04-09 01:20:44 +02:00
2011-05-22 00:24:10 +02:00
try {
2011-07-13 23:12:36 +02:00
OTMLParser parser(fin, skinName);
OTMLNode* doc = parser.getDocument();
2011-04-09 01:20:44 +02:00
2011-05-22 00:24:10 +02:00
m_defaultFont = g_fonts.get(doc->valueAt("default font"));
2011-05-12 00:16:11 +02:00
if(!m_defaultFont)
2011-07-13 23:12:36 +02:00
fatal("FATAL ERROR: Could not load skin default font");
2011-05-12 00:16:11 +02:00
m_defaultFontColor = doc->readAt("default font color", Color::white);
2011-05-12 00:16:11 +02:00
std::string defaultTextureName = doc->readAt("default texture", std::string());
2011-05-12 00:16:11 +02:00
if(!defaultTextureName.empty())
2011-05-19 19:11:05 +02:00
m_defaultTexture = g_textures.get(defaultTextureName);
2011-04-09 01:20:44 +02:00
2011-07-13 23:12:36 +02:00
foreach(OTMLNode* node, *doc) {
2011-05-22 00:24:10 +02:00
UIElementSkinPtr skin;
2011-07-13 23:12:36 +02:00
foreach(OTMLNode* cnode, *node) {
2011-05-22 00:24:10 +02:00
if(node->tag() == "buttons")
skin = UIElementSkinPtr(new UIButtonSkin(cnode->tag()));
else if(node->tag() == "panels")
skin = UIElementSkinPtr(new UIElementSkin(cnode->tag(), UI::Panel));
else if(node->tag() == "windows")
skin = UIElementSkinPtr(new UIWindowSkin(cnode->tag()));
else if(node->tag() == "labels")
skin = UIElementSkinPtr(new UILabelSkin(cnode->tag()));
else if(node->tag() == "text edits")
skin = UIElementSkinPtr(new UITextEditSkin(cnode->tag()));
else if(node->tag() == "line decorations")
skin = UIElementSkinPtr(new UIElementSkin(cnode->tag(), UI::LineDecoration));
else {
break;
}
skin->load(cnode);
m_elementSkins.push_back(skin);
2011-04-09 01:20:44 +02:00
}
}
2011-07-13 23:12:36 +02:00
} catch(OTMLException e) {
fatal("FATAL ERROR: Malformed skin file '",skinName,"':\n ",e.what());
2011-04-09 01:20:44 +02:00
}
2011-05-19 19:11:05 +02:00
g_resources.popCurrentPath();
2011-04-09 01:20:44 +02:00
}
2011-04-12 01:52:37 +02:00
void UISkins::terminate()
{
2011-04-17 21:14:24 +02:00
2011-04-12 01:52:37 +02:00
}
UIElementSkinPtr UISkins::getElementSkin(UI::ElementType elementType, const std::string& name)
2011-04-09 01:20:44 +02:00
{
for(auto it = m_elementSkins.begin(); it != m_elementSkins.end(); ++it) {
2011-04-17 21:14:24 +02:00
const UIElementSkinPtr& skin = (*it);
if(elementType == skin->getElementType() && name == skin->getName())
2011-04-09 01:20:44 +02:00
return skin;
}
2011-07-13 23:12:36 +02:00
warning("Element skin '",name,"' not found");
2011-04-17 21:14:24 +02:00
return UIElementSkinPtr();
2011-04-09 01:20:44 +02:00
}