mirror of
https://github.com/edubart/otclient.git
synced 2025-10-20 06:23:26 +02:00
make otml simpler and easier to use, improve error handling/exceptions
This commit is contained in:
@@ -61,17 +61,17 @@ BorderImagePtr BorderImage::loadFromOTML(const OTMLNodePtr& borderImageNode)
|
||||
|
||||
// load basic border confs
|
||||
size = texture->getSize();
|
||||
size = borderImageNode->readAt("size", size);
|
||||
offset = borderImageNode->readAt("offset", offset);
|
||||
border = borderImageNode->readAt("border", 0);
|
||||
size = borderImageNode->valueAt("size", size);
|
||||
offset = borderImageNode->valueAt("offset", offset);
|
||||
border = borderImageNode->valueAt("border", 0);
|
||||
subRect = Rect(offset, size);
|
||||
|
||||
// load border margins
|
||||
top = bottom = left = right = border;
|
||||
top = borderImageNode->readAt("border.top", top);
|
||||
bottom = borderImageNode->readAt("border.bottom", bottom);
|
||||
left = borderImageNode->readAt("border.left", left);
|
||||
right = borderImageNode->readAt("border.right", right);
|
||||
top = borderImageNode->valueAt("border.top", top);
|
||||
bottom = borderImageNode->valueAt("border.bottom", bottom);
|
||||
left = borderImageNode->valueAt("border.left", left);
|
||||
right = borderImageNode->valueAt("border.right", right);
|
||||
|
||||
// calculates border coords
|
||||
leftBorder = Rect(subRect.left(), subRect.top() + top, left, subRect.height() - top - bottom);
|
||||
@@ -86,15 +86,15 @@ BorderImagePtr BorderImage::loadFromOTML(const OTMLNodePtr& borderImageNode)
|
||||
|
||||
// load individual border conf if supplied
|
||||
/*
|
||||
leftBorder = borderImageNode->readAt("left border", leftBorder);
|
||||
rightBorder = borderImageNode->readAt("right border", rightBorder);
|
||||
topBorder = borderImageNode->readAt("top border", topBorder);
|
||||
bottomBorder = borderImageNode->readAt("bottom border", bottomBorder);
|
||||
topLeftCorner = borderImageNode->readAt("top left corner", topLeftCorner);
|
||||
topRightCorner = borderImageNode->readAt("top right corner", topRightCorner);
|
||||
bottomLeftCorner = borderImageNode->readAt("bottom left corner", bottomLeftCorner);
|
||||
bottomRightCorner = borderImageNode->readAt("bottom right corner", bottomRightCorner);
|
||||
center = borderImageNode->readAt("center", center);
|
||||
leftBorder = borderImageNode->valueAt("left border", leftBorder);
|
||||
rightBorder = borderImageNode->valueAt("right border", rightBorder);
|
||||
topBorder = borderImageNode->valueAt("top border", topBorder);
|
||||
bottomBorder = borderImageNode->valueAt("bottom border", bottomBorder);
|
||||
topLeftCorner = borderImageNode->valueAt("top left corner", topLeftCorner);
|
||||
topRightCorner = borderImageNode->valueAt("top right corner", topRightCorner);
|
||||
bottomLeftCorner = borderImageNode->valueAt("bottom left corner", bottomLeftCorner);
|
||||
bottomRightCorner = borderImageNode->valueAt("bottom right corner", bottomRightCorner);
|
||||
center = borderImageNode->valueAt("center", center);
|
||||
*/
|
||||
|
||||
return BorderImagePtr(new BorderImage(texture,
|
||||
|
@@ -7,7 +7,6 @@ class Texture;
|
||||
class Font;
|
||||
class Image;
|
||||
class BorderImage;
|
||||
class TextArea;
|
||||
class FrameBuffer;
|
||||
|
||||
typedef std::weak_ptr<Texture> TextureWeakPtr;
|
||||
@@ -16,7 +15,6 @@ typedef std::shared_ptr<Texture> TexturePtr;
|
||||
typedef std::shared_ptr<Font> FontPtr;
|
||||
typedef std::shared_ptr<Image> ImagePtr;
|
||||
typedef std::shared_ptr<BorderImage> BorderImagePtr;
|
||||
typedef std::shared_ptr<TextArea> TextAreaPtr;
|
||||
typedef std::shared_ptr<FrameBuffer> FrameBufferPtr;
|
||||
|
||||
#endif
|
||||
|
@@ -6,25 +6,25 @@
|
||||
|
||||
void Font::load(const OTMLNodePtr& fontNode)
|
||||
{
|
||||
std::string textureName = fontNode->readAt<std::string>("texture");
|
||||
Size glyphSize = fontNode->readAt<Size>("glyph size");
|
||||
m_glyphHeight = fontNode->readAt<int>("height");
|
||||
m_topMargin = fontNode->readAt("top margin", 0);
|
||||
m_firstGlyph = fontNode->readAt("first glyph", 32);
|
||||
m_glyphSpacing = fontNode->readAt("spacing", Size(0,0));
|
||||
std::string textureName = fontNode->valueAt("texture");
|
||||
Size glyphSize = fontNode->valueAt<Size>("glyph size");
|
||||
m_glyphHeight = fontNode->valueAt<int>("height");
|
||||
m_topMargin = fontNode->valueAt("top margin", 0);
|
||||
m_firstGlyph = fontNode->valueAt("first glyph", 32);
|
||||
m_glyphSpacing = fontNode->valueAt("spacing", Size(0,0));
|
||||
|
||||
// load font texture
|
||||
m_texture = g_textures.getTexture(textureName);
|
||||
if(!m_texture)
|
||||
throw OTMLException(fontNode, "failed to load texture for font");
|
||||
throw std::runtime_error("failed to load texture for font");
|
||||
|
||||
// auto calculate widths
|
||||
calculateGlyphsWidthsAutomatically(glyphSize);
|
||||
|
||||
// read custom widths
|
||||
if(OTMLNodePtr node = fontNode->get("glyph widths")) {
|
||||
for(const OTMLNodePtr& child : node->childNodes())
|
||||
m_glyphsSize[fw::safe_cast<int>(child->tag())].setWidth(child->read<int>());
|
||||
for(const OTMLNodePtr& child : node->children())
|
||||
m_glyphsSize[fw::safe_cast<int>(child->tag())].setWidth(child->value<int>());
|
||||
}
|
||||
|
||||
// calculate glyphs texture coords
|
||||
|
@@ -20,9 +20,9 @@ bool FontManager::importFont(std::string fontFile)
|
||||
OTMLDocumentPtr doc = OTMLDocument::parse(fontFile);
|
||||
OTMLNodePtr fontNode = doc->at("Font");
|
||||
|
||||
std::string name = fontNode->readAt<std::string>("name");
|
||||
std::string name = fontNode->valueAt("name");
|
||||
if(fontExists(name))
|
||||
throw OTMLException(fontNode, "a font with the same name is already imported, did you duplicate font names?");
|
||||
throw std::runtime_error("a font with the same name is already imported, did you duplicate font names?");
|
||||
|
||||
FontPtr font(new Font(name));
|
||||
font->load(fontNode);
|
||||
@@ -34,7 +34,7 @@ bool FontManager::importFont(std::string fontFile)
|
||||
|
||||
return true;
|
||||
} catch(std::exception& e) {
|
||||
logError("ERROR: could not load font '", fontFile, "': ", e.what());
|
||||
logError("ERROR: could not load font from '", fontFile, "': ", e.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -17,9 +17,9 @@ Image::Image(TexturePtr texture, Rect textureCoords)
|
||||
ImagePtr Image::loadFromOTML(const OTMLNodePtr& imageNode)
|
||||
{
|
||||
// load configs from otml node
|
||||
std::string source = imageNode->hasValue() ? imageNode->read<std::string>() : imageNode->readAt<std::string>("source");
|
||||
bool smooth = imageNode->readAt("smooth", false);
|
||||
Rect textureCoords = imageNode->readAt("coords", Rect());
|
||||
std::string source = imageNode->hasValue() ? imageNode->value() : imageNode->valueAt("source");
|
||||
bool smooth = imageNode->valueAt("smooth", false);
|
||||
Rect textureCoords = imageNode->valueAt("coords", Rect());
|
||||
|
||||
// load texture
|
||||
TexturePtr texture = g_textures.getTexture(source);
|
||||
|
@@ -24,7 +24,7 @@ TexturePtr TextureManager::getTexture(const std::string& textureFile)
|
||||
try {
|
||||
// currently only png textures are supported
|
||||
if(!boost::ends_with(textureFile, ".png"))
|
||||
throw std::logic_error("texture file format no supported");
|
||||
throw std::runtime_error("texture file format no supported");
|
||||
|
||||
// load texture file data
|
||||
std::stringstream fin;
|
||||
|
Reference in New Issue
Block a user