You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.2 KiB

/* 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.
*/
#include <global.h>
#include <ui/uiskins.h>
#include <ui/uielement.h>
#include <ui/uielementskin.h>
#include <graphics/borderedimage.h>
#include <graphics/textures.h>
#include <graphics/fonts.h>
void UIElementSkin::load(OTMLNode* node)
{
m_defaultSize = node->readAt("default size", Size());
m_defaultImage = loadImage(node);
m_font = loadFont(node);
m_fontColor = node->readAt("font color", g_uiSkins.getDefaultFontColor());
}
void UIElementSkin::apply(UIElement* element)
{
if(!element->getRect().isValid() && m_defaultSize.isValid())
element->setSize(m_defaultSize);
}
void UIElementSkin::draw(UIElement *element)
{
if(m_defaultImage)
m_defaultImage->draw(element->getRect());
}
ImagePtr UIElementSkin::loadImage(OTMLNode* node)
{
ImagePtr image;
TexturePtr texture;
if(OTMLNode* cnode = node->at("bordered image")) {
Rect left = cnode->readAt("left border", Rect());
Rect right = cnode->readAt("right border", Rect());
Rect top = cnode->readAt("top border", Rect());
Rect bottom = cnode->readAt("bottom border", Rect());
Rect topLeft = cnode->readAt("top left corner", Rect());
Rect topRight = cnode->readAt("top right corner", Rect());
Rect bottomLeft = cnode->readAt("bottom left corner", Rect());
Rect bottomRight = cnode->readAt("bottom right corner", Rect());
Rect center = cnode->readAt("center", Rect());
std::string textureName = cnode->readAt("source", std::string());
if(!textureName.empty())
texture = g_textures.get(textureName);
else
texture = g_uiSkins.getDefaultTexture();
if(texture) {
image = ImagePtr(new BorderedImage(texture,
left,
right,
top,
bottom,
topLeft,
topRight,
bottomLeft,
bottomRight,
center));
}
if(!image)
error(node->generateErrorMessage("failed to load bordered image"));
} else if(OTMLNode* cnode = node->at("image")) {
texture = g_textures.get(cnode->value());
if(texture)
image = ImagePtr(new Image(texture));
if(!m_defaultSize.isValid())
m_defaultSize = texture->getSize();
if(!image)
error(cnode->generateErrorMessage("failed to load image"));
}
if(texture) {
bool antialised = node->readAt("antialised", false);
if(antialised)
texture->enableBilinearFilter();
}
return image;
}
FontPtr UIElementSkin::loadFont(OTMLNode* node)
{
FontPtr font;
if(OTMLNode* cnode = node->at("font"))
font = g_fonts.get(cnode->value());
if(!font)
font = g_uiSkins.getDefaultFont();
return font;
}