Henrique 13 years ago
commit 48abbe0ed4

@ -10,7 +10,6 @@ SET(Boost_USE_MULTITHREADED OFF)
FIND_PACKAGE(Boost COMPONENTS system signals REQUIRED) FIND_PACKAGE(Boost COMPONENTS system signals REQUIRED)
FIND_PACKAGE(OpenGL REQUIRED) FIND_PACKAGE(OpenGL REQUIRED)
FIND_PACKAGE(Lua REQUIRED) FIND_PACKAGE(Lua REQUIRED)
FIND_PACKAGE(YamlCpp REQUIRED)
FIND_PACKAGE(PhysFS REQUIRED) FIND_PACKAGE(PhysFS REQUIRED)
FIND_PACKAGE(GMP REQUIRED) FIND_PACKAGE(GMP REQUIRED)
FIND_PACKAGE(ZLIB REQUIRED) FIND_PACKAGE(ZLIB REQUIRED)
@ -33,7 +32,6 @@ INCLUDE_DIRECTORIES(
${Boost_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS}
${OPENGL_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR}
${LUA_INCLUDE_DIR} ${LUA_INCLUDE_DIR}
${YAMLCPP_INCLUDE_DIR}
${PHYSFS_INCLUDE_DIR} ${PHYSFS_INCLUDE_DIR}
${GMP_INCLUDE_DIR} ${GMP_INCLUDE_DIR}
${ZLIB_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR}
@ -71,6 +69,7 @@ SET(SOURCES
src/framework/util/logger.cpp src/framework/util/logger.cpp
src/framework/util/rsa.cpp src/framework/util/rsa.cpp
src/framework/util/apngloader.cpp src/framework/util/apngloader.cpp
src/framework/util/fml.cpp
# framework graphics # framework graphics
src/framework/graphics/image.cpp src/framework/graphics/image.cpp
@ -131,7 +130,6 @@ TARGET_LINK_LIBRARIES(otclient
${Boost_LIBRARIES} ${Boost_LIBRARIES}
${OPENGL_LIBRARIES} ${OPENGL_LIBRARIES}
${LUA_LIBRARIES} ${LUA_LIBRARIES}
${YAMLCPP_LIBRARY}
${PHYSFS_LIBRARY} ${PHYSFS_LIBRARY}
${GMP_LIBRARY} ${GMP_LIBRARY}
${ZLIB_LIBRARY} ${ZLIB_LIBRARY}

@ -1,34 +1,34 @@
window#enterGameWindow: window#enterGameWindow
title: Enter Game title: Enter Game
size: [236, 178] size: [236, 178]
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
onLoad: self.locked = true onLoad: self.locked = true
label#accountNameLabel: label#accountNameLabel
text: Account name text: Account name
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
margin.left: 18 margin.left: 18
margin.top: 33 margin.top: 33
label#passwordLabel: label#passwordLabel
text: "Password:" text: "Password:"
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
margin.left: 18 margin.left: 18
margin.top: 62 margin.top: 62
label#createAccountLabel: label#createAccountLabel
text: | text: |
If you don't have If you don't have
an account yet an account yet
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
margin.left: 18 margin.left: 18
margin.top: 87 margin.top: 87
button#createAccountButton: button#createAccountButton
text: Create Account text: Create Account
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
@ -36,7 +36,7 @@ window#enterGameWindow:
margin.left: 132 margin.left: 132
onClick: messageBox("Error", "Not implemented yet") onClick: messageBox("Error", "Not implemented yet")
button#okButton: button#okButton
text: Ok text: Ok
size: [43, 20] size: [43, 20]
anchors.right: parent.right anchors.right: parent.right
@ -44,7 +44,7 @@ window#enterGameWindow:
margin.bottom: 10 margin.bottom: 10
margin.right: 66 margin.right: 66
button#cancelButton: button#cancelButton
text: Cancel text: Cancel
size: [43, 20] size: [43, 20]
anchors.right: parent.right anchors.right: parent.right
@ -53,13 +53,13 @@ window#enterGameWindow:
margin.right: 13 margin.right: 13
onClick: self.parent:destroy() onClick: self.parent:destroy()
textEdit#accountNameTextEdit: textEdit#accountNameTextEdit
anchors.right: parent.right anchors.right: parent.right
anchors.top: parent.top anchors.top: parent.top
margin.top: 32 margin.top: 32
margin.right: 18 margin.right: 18
textEdit#passwordTextEdit: textEdit#passwordTextEdit
anchors.right: parent.right anchors.right: parent.right
anchors.top: parent.top anchors.top: parent.top
margin.top: 61 margin.top: 61

@ -1,11 +1,11 @@
window#infoWindow: window#infoWindow
title: Info title: Info
size: [244, 221] size: [244, 221]
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
onLoad: self.locked = true onLoad: self.locked = true
panel#infoPanel: panel#infoPanel
skin: flatPanel skin: flatPanel
size: [208, 129] size: [208, 129]
anchors.left: parent.left anchors.left: parent.left
@ -13,31 +13,31 @@ window#infoWindow:
margin.top: 32 margin.top: 32
margin.left: 18 margin.left: 18
label#infoLabel: label#infoLabel
align: center align: center
text: |- text: |-
OTClient OTClient
Version 0.2.0 Version 0.2.0
Created by edubart Created by edubart
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top anchors.top: parent.top
margin.top: 20 margin.top: 20
lineDecoration#bottomSeparator: lineDecoration#bottomSeparator
size: [190,2] size: [190, 2]
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
margin.top: 83 margin.top: 83
margin.left: 9 margin.left: 9
label#websiteLabel: label#websiteLabel
text: Official Website text: Official Website
anchors.left: parent.left anchors.left: parent.left
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
margin.bottom: 14 margin.bottom: 14
margin.left: 9 margin.left: 9
button#websiteButton: button#websiteButton
text: Github Page text: Github Page
size: [80,22] size: [80,22]
anchors.right: parent.right anchors.right: parent.right
@ -45,7 +45,7 @@ window#infoWindow:
margin.bottom: 9 margin.bottom: 9
margin.right: 9 margin.right: 9
lineDecoration#bottomSeparator: lineDecoration#bottomSeparator
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
@ -53,7 +53,7 @@ window#infoWindow:
margin.left: 13 margin.left: 13
margin.right: 13 margin.right: 13
button#okButton: button#okButton
text: Ok text: Ok
size: [43, 20] size: [43, 20]
anchors.left: parent.left anchors.left: parent.left

@ -1,11 +1,11 @@
panel#background: panel#background
skin: mainMenuBackground skin: mainMenuBackground
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
anchors.top: parent.top anchors.top: parent.top
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
panel#icos4d: panel#icos4d
skin: skin:
image: /skins/lightness/mouse.png image: /skins/lightness/mouse.png
anchors.left: parent.left anchors.left: parent.left
@ -13,7 +13,7 @@ panel#background:
margin.left: -2 margin.left: -2
margin.top: 70 margin.top: 70
panel#mouse: panel#mouse
skin: skin:
image: /skins/lightness/icos4d.png image: /skins/lightness/icos4d.png
anchors.right: parent.right anchors.right: parent.right
@ -21,7 +21,7 @@ panel#background:
margin.left: 60 margin.left: 60
margin.top: 70 margin.top: 70
panel#mainMenu: panel#mainMenu
skin: roundedGridPanel skin: roundedGridPanel
size: [117, 171] size: [117, 171]
anchors.left: parent.left anchors.left: parent.left
@ -29,35 +29,35 @@ panel#background:
margin.left: 60 margin.left: 60
margin.bottom: 70 margin.bottom: 70
button#enterGameButton: button#enterGameButton
text: Enter Game text: Enter Game
anchors.top: parent.top anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
margin.top: 16 margin.top: 16
onClick: UI.load("entergamewindow.yml") onClick: UI.load("entergamewindow.yml")
button#accessAccountButton: button#accessAccountButton
text: Access Account text: Access Account
anchors.top: parent.top anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
margin.top: 46 margin.top: 46
onClick: messageBox("Error", "Not implemented yet") onClick: messageBox("Error", "Not implemented yet")
button#optionsButton: button#optionsButton
text: Options text: Options
anchors.top: parent.top anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
margin.top: 76 margin.top: 76
onClick: UI.load("optionswindow.yml") onClick: UI.load("optionswindow.yml")
button#infoButton: button#infoButton
text: Info text: Info
anchors.top: parent.top anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
margin.top: 106 margin.top: 106
onClick: UI.load("infowindow.yml") onClick: UI.load("infowindow.yml")
button#exitGameButton: button#exitGameButton
text: Exit text: Exit
anchors.top: parent.top anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter

@ -1,4 +1,4 @@
window#optionsWindow: window#optionsWindow
title: Options title: Options
size: [286, 262] size: [286, 262]
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
@ -6,48 +6,48 @@ window#optionsWindow:
onLoad: self.locked = true onLoad: self.locked = true
# general # general
button#generalButton: button#generalButton
text: General text: General
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
margin.left: 18 margin.left: 18
margin.top: 32 margin.top: 32
label#generalLabel: label#generalLabel
text: |- text: |-
Change general Change general
game options game options
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
margin.left: 117 margin.left: 117
margin.top: 29 margin.top: 29
# graphics # graphics
button#graphicsButton: button#graphicsButton
text: Graphics text: Graphics
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
margin.left: 18 margin.left: 18
margin.top: 65 margin.top: 65
label#graphicsLabel: label#graphicsLabel
text: |- text: |-
Change graphics and Change graphics and
performance settings performance settings
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
margin.left: 117 margin.left: 117
margin.top: 62 margin.top: 62
# console # console
button#consoleButton: button#consoleButton
text: Console text: Console
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
margin.left: 18 margin.left: 18
margin.top: 98 margin.top: 98
label#consoleLabel: label#consoleLabel
text: Customise the console text: Customise the console
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
@ -55,21 +55,21 @@ window#optionsWindow:
margin.top: 95 margin.top: 95
# hotkeys # hotkeys
button#hotkeysButton: button#hotkeysButton
text: Hotkeys text: Hotkeys
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
margin.left: 18 margin.left: 18
margin.top: 131 margin.top: 131
label#hotkeysLabel: label#hotkeysLabel
text: Edit your hotkey texts text: Edit your hotkey texts
anchors.left: parent.left anchors.left: parent.left
anchors.top: parent.top anchors.top: parent.top
margin.left: 117 margin.left: 117
margin.top: 128 margin.top: 128
lineDecoration#middleSeparator: lineDecoration#middleSeparator
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
@ -78,23 +78,23 @@ window#optionsWindow:
margin.right: 18 margin.right: 18
# motd # motd
button#motdButton: button#motdButton
text: Motd text: Motd
anchors.left: parent.left anchors.left: parent.left
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
margin.left: 18 margin.left: 18
margin.bottom: 60 margin.bottom: 60
label#motdLabel: label#motdLabel
text: |- text: |-
Show the most recent Show the most recent
Message of the Day Message of the Day
anchors.left: parent.left anchors.left: parent.left
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
margin.left: 117 margin.left: 117
margin.bottom: 56 margin.bottom: 56
lineDecoration#bottomSeparator: lineDecoration#bottomSeparator
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
@ -103,7 +103,7 @@ window#optionsWindow:
margin.right: 13 margin.right: 13
# ok button # ok button
button#okButton: button#okButton
text: Ok text: Ok
size: [43, 20] size: [43, 20]
anchors.right: parent.right anchors.right: parent.right

@ -7,5 +7,5 @@ function messageBox(title, text)
msgBox.locked = true msgBox.locked = true
msgBox.title = title msgBox.title = title
msgBox:child("textLabel").text = text msgBox:child("textLabel").text = text
msgBox:child("okButton").onClick = autoDestroyParentz msgBox:child("okButton").onClick = autoDestroyParent
end end

@ -36,27 +36,28 @@ bool Configs::load(const std::string& fileName)
if(!g_resources.loadFile(fileName, fin)) if(!g_resources.loadFile(fileName, fin))
return false; return false;
try { FML::Parser parser;
YAML::Node doc; if(parser.load(fin)) {
YAML::Parser parser(fin); FML::Node* doc = parser.getDocument();
parser.GetNextDocument(doc);
foreach(FML::Node* node, *doc)
for(auto it = doc.begin(); it != doc.end(); ++it) m_confsMap[node->tag()] = node->value();
m_confsMap[yamlRead<std::string>(it.first())] = yamlRead<std::string>(it.second()); } else {
flogError("ERROR: Malformed config file: %s", parser.getErrorMessage());
return true;
} catch (YAML::Exception& e) {
flogError("ERROR: Malformed config file: %s", e.what());
return false; return false;
} }
return true;
} }
void Configs::save() void Configs::save()
{ {
if(!m_fileName.empty()) { if(!m_fileName.empty()) {
YAML::Emitter out; std::stringstream out;
out << m_confsMap; foreach(auto pair, m_confsMap) {
g_resources.saveFile(m_fileName, (const uchar*)out.c_str(), out.size()); out << pair.first << ": " << pair.second << std::endl;
}
g_resources.saveFile(m_fileName, out);
} }
} }

@ -41,7 +41,7 @@ public:
bool load(const std::string& fileName); bool load(const std::string& fileName);
void save(); void save();
template<class T> void setValue(const std::string& key, const T& value) { m_confsMap[key] = convert_cast<std::string>(value); } template<class T> void set(const std::string& key, const T& value) { m_confsMap[key] = convert_cast<std::string>(value); }
ConfigValueProxy get(const std::string& key) { return ConfigValueProxy{m_confsMap[key]}; } ConfigValueProxy get(const std::string& key) { return ConfigValueProxy{m_confsMap[key]}; }
private: private:

@ -105,10 +105,11 @@ bool Resources::directoryExists(const std::string& directoryName)
bool Resources::loadFile(const std::string& fileName, std::iostream& out) bool Resources::loadFile(const std::string& fileName, std::iostream& out)
{ {
std::string fullPath = resolvePath(fileName);
out.clear(std::ios::goodbit); out.clear(std::ios::goodbit);
PHYSFS_file *file = PHYSFS_openRead(resolvePath(fileName).c_str()); PHYSFS_file *file = PHYSFS_openRead(fullPath.c_str());
if(!file) { if(!file) {
flogError("ERROR: Failed to load file \"%s\": %s", fileName.c_str() % PHYSFS_getLastError()); flogError("ERROR: Failed to load file \"%s\": %s", fullPath.c_str() % PHYSFS_getLastError());
out.clear(std::ios::failbit); out.clear(std::ios::failbit);
return false; return false;
} else { } else {

@ -77,20 +77,17 @@ bool Font::load(const std::string& file)
std::string textureName; std::string textureName;
Size glyphSize; Size glyphSize;
try { FML::Parser parser(fin);
YAML::Parser parser(fin); if(!parser.hasError()) {
FML::Node* doc = parser.getDocument();
YAML::Node doc;
parser.GetNextDocument(doc);
// required values // required values
doc["glyph height"] >> m_glyphHeight; textureName = doc->valueAt("image");
doc["image glyph size"] >> glyphSize; glyphSize = doc->readAt("image glyph size", Size(16, 16));
textureName = yamlRead<std::string>(doc, "image"); m_glyphHeight = doc->readAt("glyph height", 11);
m_glyphHeight = yamlRead<int>(doc, "glyph height"); m_firstGlyph = doc->readAt("first glyph", 32);
m_firstGlyph = yamlRead<int>(doc, "first glyph", 32); m_topMargin = doc->readAt("top margin", 0);
m_topMargin = yamlRead<int>(doc, "top margin", 0); m_glyphSpacing = doc->readAt("glyph spacing", Size(0,0));
m_glyphSpacing = yamlRead(doc, "glyph spacing", Size(0,0));
// load texture // load texture
m_texture = g_textures.get(textureName); m_texture = g_textures.get(textureName);
@ -103,9 +100,12 @@ bool Font::load(const std::string& file)
calculateGlyphsWidthsAutomatically(glyphSize); calculateGlyphsWidthsAutomatically(glyphSize);
// read custom widths // read custom widths
std::map<int, int> glyphWidths = yamlReadMap<int, int>(doc, "glyph widths"); if(doc->hasNode("glyph widths")) {
foreach(const auto& pair, glyphWidths) std::map<int, int> glyphWidths;
m_glyphsSize[pair.first].setWidth(pair.second); (*(doc->at("glyph widths"))) >> glyphWidths;
foreach(const auto& pair, glyphWidths)
m_glyphsSize[pair.first].setWidth(pair.second);
}
// calculate glyphs texture coords // calculate glyphs texture coords
int numHorizontalGlyphs = m_texture->getSize().width() / glyphSize.width(); int numHorizontalGlyphs = m_texture->getSize().width() / glyphSize.width();
@ -115,8 +115,8 @@ bool Font::load(const std::string& file)
m_glyphsSize[glyph].width(), m_glyphsSize[glyph].width(),
m_glyphHeight); m_glyphHeight);
} }
} catch (YAML::Exception& e) { } else {
flogError("ERROR: Malformed font file \"%s\":\n %s", file.c_str() % e.what()); flogError("ERROR: Malformed font file \"%s\":\n %s", file.c_str() % parser.getErrorMessage());
return false; return false;
} }

@ -50,6 +50,9 @@ struct Win32PlatformPrivate {
void Platform::init(const char *appName) void Platform::init(const char *appName)
{ {
// seend random numbers
std::srand(std::time(NULL));
win32.appName = appName; win32.appName = appName;
win32.instance = GetModuleHandle(NULL); win32.instance = GetModuleHandle(NULL);

@ -69,6 +69,9 @@ struct X11PlatformPrivate {
void Platform::init(const char *appName) void Platform::init(const char *appName)
{ {
// seend random numbers
std::srand(std::time(NULL));
x11.appName = appName; x11.appName = appName;
x11.display = NULL; x11.display = NULL;
x11.visual = NULL; x11.visual = NULL;

@ -85,11 +85,11 @@ typedef boost::function<void()> SimpleCallback;
// common utilities // common utilities
#include <util/util.h> #include <util/util.h>
#include <util/fml.h>
#include <util/logger.h> #include <util/logger.h>
#include <util/color.h> #include <util/color.h>
#include <util/point.h> #include <util/point.h>
#include <util/size.h> #include <util/size.h>
#include <util/rect.h> #include <util/rect.h>
#include <util/yaml.h>
#endif // PREREQUISITES_H #endif // PREREQUISITES_H

@ -98,7 +98,7 @@ int lua_UI_load()
UIElementPtr element; UIElementPtr element;
if(parent) if(parent)
element = g_uiLoader.loadFromYAML(uiFile.c_str(), parent); element = g_uiLoader.loadFromFile(uiFile.c_str(), parent);
else else
g_lua.reportErrorWithTraceback("invalid parent container"); g_lua.reportErrorWithTraceback("invalid parent container");

@ -379,20 +379,25 @@ void LuaScript::pushRef(int ref)
lua_rawgeti(L, LUA_REGISTRYINDEX, ref); lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
} }
std::string LuaScript::getFunctionSourcePath() std::string LuaScript::getFunctionSourcePath(bool functionIsOnStack, int level)
{ {
std::string path; std::string path;
lua_Debug ar; lua_Debug ar;
memset(&ar, 0, sizeof(ar)); memset(&ar, 0, sizeof(ar));
lua_getinfo(L, ">Sn", &ar); if(functionIsOnStack)
lua_getinfo(L, ">Sn", &ar);
else {
if(lua_getstack(L, level-1, &ar) == 1)
lua_getinfo(L, "Sn", &ar);
}
// c function, we must get information of level above // c function, we must get information of a level above
if(strcmp("C", ar.what) == 0) { if(strcmp("C", ar.what) == 0) {
memset(&ar, 0, sizeof(ar)); memset(&ar, 0, sizeof(ar));
if(lua_getstack(L, 1, &ar) == 1) { if(lua_getstack(L, level, &ar) == 1) {
lua_getinfo(L, "f", &ar); lua_getinfo(L, "f", &ar);
return getFunctionSourcePath(); return getFunctionSourcePath(true, level+1);
} }
} else { } else {
@ -411,7 +416,7 @@ std::string LuaScript::getFunctionSourcePath()
bool LuaScript::callFunction(int numArgs, int numRets) bool LuaScript::callFunction(int numArgs, int numRets)
{ {
pushValue(-numArgs - 1); pushValue(-numArgs - 1);
g_resources.pushCurrentPath(getFunctionSourcePath()); g_resources.pushCurrentPath(getFunctionSourcePath(true));
int size = getStackSize(); int size = getStackSize();
int errorIndex = -numArgs - 2; int errorIndex = -numArgs - 2;
@ -726,8 +731,14 @@ int LuaScript::luaFunctionCallback(lua_State* L)
{ {
// look for function id // look for function id
int id = lua_tonumber(L, lua_upvalueindex(1)); int id = lua_tonumber(L, lua_upvalueindex(1));
g_resources.pushCurrentPath(g_lua.getFunctionSourcePath(false));
// call the function // call the function
return (*(g_lua.m_functions[id]))(); int ret = (*(g_lua.m_functions[id]))();
g_resources.popCurrentPath();
return ret;
} }
int LuaScript::luaErrorHandler(lua_State *L) int LuaScript::luaErrorHandler(lua_State *L)

@ -104,7 +104,7 @@ public:
void pushValue(int index = -1); void pushValue(int index = -1);
void pushRef(int ref); void pushRef(int ref);
std::string getFunctionSourcePath(); std::string getFunctionSourcePath(bool functionIsOnStack, int level = 1);
bool callFunction(int numArgs = 0, int numRets = 0); bool callFunction(int numArgs = 0, int numRets = 0);
void callModuleField(const std::string& module, const std::string& field); void callModuleField(const std::string& module, const std::string& field);

@ -27,24 +27,22 @@
#include <ui/uibuttonskin.h> #include <ui/uibuttonskin.h>
#include <ui/uibutton.h> #include <ui/uibutton.h>
void UIButtonSkin::load(const YAML::Node& node) void UIButtonSkin::load(FML::Node* node)
{ {
UIElementSkin::load(node); UIElementSkin::load(node);
m_buttonDownTextColor = getFontColor(); m_buttonDownTextColor = getFontColor();
m_buttonHoverTextColor = getFontColor(); m_buttonHoverTextColor = getFontColor();
if(yamlHasValue(node, "down state")) { if(FML::Node* cnode = node->at("down state")) {
const YAML::Node& cnode = node["down state"];
m_buttonDownImage = loadImage(cnode); m_buttonDownImage = loadImage(cnode);
m_buttonDownTranslate = yamlRead(cnode, "text translate", Point()); m_buttonDownTranslate = cnode->readAt("text translate", Point());
m_buttonDownTextColor = yamlRead(cnode, "font color", getFontColor()); m_buttonDownTextColor = cnode->readAt("font color", getFontColor());
} }
if(yamlHasValue(node, "hover state")) { if(FML::Node* cnode = node->at("hover state")) {
const YAML::Node& cnode = node["hover state"];
m_buttonHoverImage = loadImage(cnode); m_buttonHoverImage = loadImage(cnode);
m_buttonHoverTextColor = yamlRead(cnode, "font color", getFontColor()); m_buttonHoverTextColor = cnode->readAt("font color", getFontColor());
} }
} }

@ -37,7 +37,7 @@ public:
UIButtonSkin(const std::string& name) : UIButtonSkin(const std::string& name) :
UIElementSkin(name, UI::Button) { } UIElementSkin(name, UI::Button) { }
void load(const YAML::Node& node); void load(FML::Node* node);
void draw(UIElement *element); void draw(UIElement *element);
private: private:

@ -30,12 +30,12 @@
#include <graphics/textures.h> #include <graphics/textures.h>
#include <graphics/fonts.h> #include <graphics/fonts.h>
void UIElementSkin::load(const YAML::Node& node) void UIElementSkin::load(FML::Node* node)
{ {
m_defaultSize = yamlRead(node, "default size", Size()); m_defaultSize = node->readAt("default size", Size());
m_defaultImage = loadImage(node); m_defaultImage = loadImage(node);
m_font = loadFont(node); m_font = loadFont(node);
m_fontColor = yamlRead(node, "font color", g_uiSkins.getDefaultFontColor()); m_fontColor = node->readAt("font color", g_uiSkins.getDefaultFontColor());
} }
void UIElementSkin::apply(UIElement* element) void UIElementSkin::apply(UIElement* element)
@ -50,23 +50,22 @@ void UIElementSkin::draw(UIElement *element)
m_defaultImage->draw(element->getRect()); m_defaultImage->draw(element->getRect());
} }
ImagePtr UIElementSkin::loadImage(const YAML::Node& node) ImagePtr UIElementSkin::loadImage(FML::Node* node)
{ {
ImagePtr image; ImagePtr image;
TexturePtr texture; TexturePtr texture;
if(yamlHasValue(node, "bordered image")) { if(FML::Node* cnode = node->at("bordered image")) {
const YAML::Node& cnode = node["bordered image"]; Rect left = cnode->readAt("left border", Rect());
Rect left = yamlRead(cnode, "left border", Rect()); Rect right = cnode->readAt("right border", Rect());
Rect right = yamlRead(cnode, "right border", Rect()); Rect top = cnode->readAt("top border", Rect());
Rect top = yamlRead(cnode, "top border", Rect()); Rect bottom = cnode->readAt("bottom border", Rect());
Rect bottom = yamlRead(cnode, "bottom border", Rect()); Rect topLeft = cnode->readAt("top left corner", Rect());
Rect topLeft = yamlRead(cnode, "top left corner", Rect()); Rect topRight = cnode->readAt("top right corner", Rect());
Rect topRight = yamlRead(cnode, "top right corner", Rect()); Rect bottomLeft = cnode->readAt("bottom left corner", Rect());
Rect bottomLeft = yamlRead(cnode, "bottom left corner", Rect()); Rect bottomRight = cnode->readAt("bottom right corner", Rect());
Rect bottomRight = yamlRead(cnode, "bottom right corner", Rect()); Rect center = cnode->readAt("center", Rect());
Rect center = yamlRead(cnode, "center", Rect()); std::string textureName = cnode->readAt("source", std::string());
std::string textureName = yamlRead(cnode, "source", std::string());
if(!textureName.empty()) if(!textureName.empty())
texture = g_textures.get(textureName); texture = g_textures.get(textureName);
@ -87,20 +86,20 @@ ImagePtr UIElementSkin::loadImage(const YAML::Node& node)
} }
if(!image) if(!image)
logError(yamlErrorDesc(cnode, "failed to load bordered image")); logError(node->generateErrorMessage("failed to load bordered image"));
} else if(yamlHasValue(node, "image")) { } else if(FML::Node* cnode = node->at("image")) {
texture = g_textures.get(yamlRead<std::string>(node, "image")); texture = g_textures.get(cnode->value());
if(texture) if(texture)
image = ImagePtr(new Image(texture)); image = ImagePtr(new Image(texture));
if(!m_defaultSize.isValid()) if(!m_defaultSize.isValid())
m_defaultSize = texture->getSize(); m_defaultSize = texture->getSize();
if(!image) if(!image)
logError(yamlErrorDesc(node["image"], "failed to load image")); logError(cnode->generateErrorMessage("failed to load image"));
} }
if(texture) { if(texture) {
bool antialised = yamlRead(node, "antialised", false); bool antialised = node->readAt("antialised", false);
if(antialised) if(antialised)
texture->enableBilinearFilter(); texture->enableBilinearFilter();
} }
@ -108,11 +107,11 @@ ImagePtr UIElementSkin::loadImage(const YAML::Node& node)
return image; return image;
} }
FontPtr UIElementSkin::loadFont(const YAML::Node& node) FontPtr UIElementSkin::loadFont(FML::Node* node)
{ {
FontPtr font; FontPtr font;
if(yamlHasValue(node, "font")) if(FML::Node* cnode = node->at("font"))
font = g_fonts.get(yamlRead<std::string>(node, "font")); font = g_fonts.get(cnode->value());
if(!font) if(!font)
font = g_uiSkins.getDefaultFont(); font = g_uiSkins.getDefaultFont();
return font; return font;

@ -41,8 +41,8 @@ public:
UIElementSkin() : m_elementType(UI::Element) { } UIElementSkin() : m_elementType(UI::Element) { }
virtual ~UIElementSkin() { } virtual ~UIElementSkin() { }
/// Load the skin from a YAML node /// Load the skin from a FML node
virtual void load(const YAML::Node& node); virtual void load(FML::Node* node);
/// Apply the skin to an element /// Apply the skin to an element
virtual void apply(UIElement *element); virtual void apply(UIElement *element);
/// Draw the skinned element /// Draw the skinned element
@ -56,8 +56,8 @@ public:
Color getFontColor() const { return m_fontColor; } Color getFontColor() const { return m_fontColor; }
protected: protected:
ImagePtr loadImage(const YAML::Node& node); ImagePtr loadImage(FML::Node* node);
FontPtr loadFont(const YAML::Node &node); FontPtr loadFont(FML::Node* node);
private: private:
std::string m_name; std::string m_name;

@ -27,7 +27,7 @@
#include <ui/uilabelskin.h> #include <ui/uilabelskin.h>
#include <ui/uilabel.h> #include <ui/uilabel.h>
void UILabelSkin::load(const YAML::Node& node) void UILabelSkin::load(FML::Node* node)
{ {
UIElementSkin::load(node); UIElementSkin::load(node);
} }

@ -35,7 +35,7 @@ public:
UILabelSkin(const std::string& name) : UILabelSkin(const std::string& name) :
UIElementSkin(name, UI::Label) { } UIElementSkin(name, UI::Label) { }
void load(const YAML::Node& node); void load(FML::Node* node);
void apply(UIElement *element); void apply(UIElement *element);
void draw(UIElement *element); void draw(UIElement *element);
}; };

@ -67,93 +67,81 @@ UIElementPtr UILoader::createElementFromId(const std::string& id)
return element; return element;
} }
UIElementPtr UILoader::loadFromYAML(std::string filePath, const UIContainerPtr& parent) UIElementPtr UILoader::loadFromFile(std::string filePath, const UIContainerPtr& parent)
{ {
std::stringstream fin; std::stringstream fin;
if(!g_resources.loadFile(filePath, fin)) { if(!g_resources.loadFile(filePath, fin)) {
flogError("ERROR: Could not load ui file \"%s", filePath.c_str()); flogError("ERROR: Could not load ui %s", filePath.c_str());
return UIElementPtr(); return UIElementPtr();
} }
m_currentFile = filePath; m_currentFile = filePath;
try { FML::Parser parser(fin);
YAML::Parser parser(fin); if(!parser.hasError()) {
FML::Node* doc = parser.getDocument();
YAML::Node doc;
parser.GetNextDocument(doc);
// get element id // get element id
std::string elementId; std::string elementId = doc->front()->tag();
doc.begin().first() >> elementId;
// first we should populate all elements // first we should populate all elements
// only after that we can load anchors // only after that we can load anchors
// create element interpreting it's id // create element interpreting it's id
UIElementPtr element = createElementFromId(elementId); UIElementPtr element = createElementFromId(elementId);
if(!element) if(!element) {
yamlThrowError(doc.begin().first(), "invalid element type"); logError(doc->front()->generateErrorMessage("invalid root element type"));
return element;
}
parent->addChild(element); parent->addChild(element);
// populete it // populete it
if(element->asUIContainer()) if(element->asUIContainer())
populateContainer(element->asUIContainer(), doc.begin().second()); populateContainer(element->asUIContainer(), doc->front());
// now do the real load // now do the real load
loadElements(element, doc.begin().second()); loadElements(element, doc->front());
// report onLoad events // report onLoad events
element->onLoad(); element->onLoad();
return element; return element;
} catch (YAML::Exception& e) { } else {
flogError("ERROR: Failed to load ui file \"%s\":\n %s", filePath.c_str() % e.what()); flogError("ERROR: Failed to load ui %s: %s", filePath.c_str() % parser.getErrorMessage());
} }
return UIElementPtr(); return UIElementPtr();
} }
void UILoader::populateContainer(const UIContainerPtr& parent, const YAML::Node& node) void UILoader::populateContainer(const UIContainerPtr& parent, FML::Node* node)
{ {
// order nodes
std::map<int, std::string> orderedNodes;
for(auto it = node.begin(); it != node.end(); ++it) {
std::string id;
it.first() >> id;
// check if it's an element id
if(id.find("#") != std::string::npos)
orderedNodes[it.first().GetMark().pos] = id;
}
// populate ordered elements // populate ordered elements
foreach(auto pair, orderedNodes) { foreach(FML::Node* cnode, *node) {
std::string id = pair.second; std::string id = cnode->tag();
const YAML::Node& cnode = node[id]; if(id.find("#") != std::string::npos) {
UIElementPtr element = createElementFromId(id);
if(!element) {
logError(cnode->generateErrorMessage("invalid element type"));
continue;
}
parent->addChild(element);
UIElementPtr element = createElementFromId(id); // also populate this element if it's a parent
if(!element) { if(element->asUIContainer())
logError(yamlErrorDesc(cnode, "invalid element type")); populateContainer(element->asUIContainer(), cnode);
continue;
} }
parent->addChild(element);
// also populate this element if it's a parent
if(element->asUIContainer())
populateContainer(element->asUIContainer(), cnode);
} }
} }
void UILoader::loadElements(const UIElementPtr& parent, const YAML::Node& node) void UILoader::loadElements(const UIElementPtr& parent, FML::Node* node)
{ {
loadElement(parent, node); loadElement(parent, node);
if(UIContainerPtr container = parent->asUIContainer()) { if(UIContainerPtr container = parent->asUIContainer()) {
foreach(const UIElementPtr& element, container->getChildren()) { foreach(const UIElementPtr& element, container->getChildren()) {
for(auto it = node.begin(); it != node.end(); ++it) { foreach(FML::Node* cnode, *node) {
// node found, load it // node found, load it
if(boost::ends_with(yamlRead<std::string>(it.first()), "#" + element->getId())) { if(boost::ends_with(cnode->tag(), "#" + element->getId())) {
loadElements(element, it.second()); loadElements(element, cnode);
break; break;
} }
} }
@ -161,13 +149,12 @@ void UILoader::loadElements(const UIElementPtr& parent, const YAML::Node& node)
} }
} }
void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node) void UILoader::loadElement(const UIElementPtr& element, FML::Node* node)
{ {
// set element skin // set element skin
if(yamlHasValue(node, "skin")) { if(FML::Node* cnode = node->at("skin")) {
const YAML::Node& cnode = node["skin"]; if(cnode->hasValue()) {
if(cnode.Type() == YAML::NodeType::Scalar) { element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), cnode->value()));
element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), yamlRead<std::string>(cnode)));
} else { } else {
UIElementSkinPtr skin = UIElementSkinPtr(new UIElementSkin()); UIElementSkinPtr skin = UIElementSkinPtr(new UIElementSkin());
skin->load(cnode); skin->load(cnode);
@ -177,46 +164,45 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), "default")); element->setSkin(g_uiSkins.getElementSkin(element->getElementType(), "default"));
// load elements common proprieties // load elements common proprieties
if(yamlHasValue(node, "size")) if(node->hasNode("size"))
element->setSize(yamlRead<Size>(node, "size")); element->setSize(node->readAt<Size>("size"));
element->setMarginLeft(yamlRead(node, "margin.left", 0));
element->setMarginRight(yamlRead(node, "margin.right", 0)); element->setMarginLeft(node->readAt("margin.left", 0));
element->setMarginTop(yamlRead(node, "margin.top", 0)); element->setMarginRight(node->readAt("margin.right", 0));
element->setMarginBottom(yamlRead(node, "margin.bottom", 0)); element->setMarginTop(node->readAt("margin.top", 0));
element->setMarginBottom(node->readAt("margin.bottom", 0));
if(yamlHasValue(node, "anchors.left")) if(node->hasNode("anchors.left"))
loadElementAnchor(element, UI::AnchorLeft, node["anchors.left"]); loadElementAnchor(element, UI::AnchorLeft, node->at("anchors.left"));
if(yamlHasValue(node, "anchors.right")) if(node->hasNode("anchors.right"))
loadElementAnchor(element, UI::AnchorRight, node["anchors.right"]); loadElementAnchor(element, UI::AnchorRight, node->at("anchors.right"));
if(yamlHasValue(node, "anchors.top")) if(node->hasNode("anchors.top"))
loadElementAnchor(element, UI::AnchorTop, node["anchors.top"]); loadElementAnchor(element, UI::AnchorTop, node->at("anchors.top"));
if(yamlHasValue(node, "anchors.bottom")) if(node->hasNode("anchors.bottom"))
loadElementAnchor(element, UI::AnchorBottom, node["anchors.bottom"]); loadElementAnchor(element, UI::AnchorBottom, node->at("anchors.bottom"));
if(yamlHasValue(node, "anchors.horizontalCenter")) if(node->hasNode("anchors.horizontalCenter"))
loadElementAnchor(element, UI::AnchorHorizontalCenter, node["anchors.horizontalCenter"]); loadElementAnchor(element, UI::AnchorHorizontalCenter, node->at("anchors.horizontalCenter"));
if(yamlHasValue(node, "anchors.verticalCenter")) if(node->hasNode("anchors.verticalCenter"))
loadElementAnchor(element, UI::AnchorVerticalCenter, node["anchors.verticalCenter"]); loadElementAnchor(element, UI::AnchorVerticalCenter, node->at("anchors.verticalCenter"));
// load events // load events
if(yamlHasValue(node, "onLoad")) { if(FML::Node* cnode = node->at("onLoad")) {
const YAML::Node& cnode = node["onLoad"]; if(g_lua.loadBufferAsFunction(cnode->value(), getElementSourceDesc(element, "onLoad")))
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), getElementSourceDesc(element, "onLoad")))
g_lua.setScriptableField(element, "onLoad"); g_lua.setScriptableField(element, "onLoad");
else else
logError(yamlErrorDesc(cnode, "failed to parse inline lua script")); logError(cnode->generateErrorMessage("failed to parse inline lua script"));
} }
if(yamlHasValue(node, "onDestroy")) { if(FML::Node* cnode = node->at("onDestroy")) {
const YAML::Node& cnode = node["onDestroy"]; if(g_lua.loadBufferAsFunction(cnode->value(), getElementSourceDesc(element, "onDestroy")))
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), getElementSourceDesc(element, "onDestroy")))
g_lua.setScriptableField(element, "onDestroy"); g_lua.setScriptableField(element, "onDestroy");
else else
logError(yamlErrorDesc(cnode, "failed to parse inline lua script")); logError(cnode->generateErrorMessage("failed to parse inline lua script"));
} }
// load specific element type // load specific element type
@ -224,30 +210,29 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
loadButton(boost::static_pointer_cast<UIButton>(element), node); loadButton(boost::static_pointer_cast<UIButton>(element), node);
else if(element->getElementType() == UI::Window) { else if(element->getElementType() == UI::Window) {
UIWindowPtr window = boost::static_pointer_cast<UIWindow>(element); UIWindowPtr window = boost::static_pointer_cast<UIWindow>(element);
window->setTitle(yamlRead(node, "title", std::string())); window->setTitle(node->readAt("title", std::string()));
} }
else if(element->getElementType() == UI::Label) { else if(element->getElementType() == UI::Label) {
UILabelPtr label = boost::static_pointer_cast<UILabel>(element); UILabelPtr label = boost::static_pointer_cast<UILabel>(element);
label->setText(yamlRead(node, "text", std::string())); label->setText(node->readAt("text", std::string()));
label->setAlign(parseAlignment(yamlRead(node, "align", std::string("left")))); label->setAlign(parseAlignment(node->readAt("align", std::string("left"))));
} }
} }
void UILoader::loadElementAnchor(const UIElementPtr& anchoredElement, UI::AnchorPoint anchoredEdge, const YAML::Node& node) void UILoader::loadElementAnchor(const UIElementPtr& anchoredElement, UI::AnchorPoint anchoredEdge, FML::Node* node)
{ {
UIAnchorLayoutPtr layout = boost::dynamic_pointer_cast<UIAnchorLayout>(anchoredElement->getLayout()); UIAnchorLayoutPtr layout = boost::dynamic_pointer_cast<UIAnchorLayout>(anchoredElement->getLayout());
if(!layout) { if(!layout) {
logError(yamlErrorDesc(node, "could not add anchor, because this element does not participate of an anchor layout")); logError(node->generateErrorMessage("could not add anchor, because this element does not participate of an anchor layout"));
return; return;
} }
std::string anchorDescription; std::string anchorDescription = node->value();
node >> anchorDescription;
std::vector<std::string> split; std::vector<std::string> split;
boost::split(split, anchorDescription, boost::is_any_of(std::string("."))); boost::split(split, anchorDescription, boost::is_any_of(std::string(".")));
if(split.size() != 2) { if(split.size() != 2) {
logError(yamlErrorDesc(node, "invalid anchor")); logError(node->generateErrorMessage("invalid anchor"));
return; return;
} }
@ -255,25 +240,24 @@ void UILoader::loadElementAnchor(const UIElementPtr& anchoredElement, UI::Anchor
UI::AnchorPoint anchorLineEdge = UIAnchorLayout::parseAnchorPoint(split[1]); UI::AnchorPoint anchorLineEdge = UIAnchorLayout::parseAnchorPoint(split[1]);
if(anchorLineEdge == UI::AnchorNone) { if(anchorLineEdge == UI::AnchorNone) {
logError(yamlErrorDesc(node, "invalid anchor type")); logError(node->generateErrorMessage("invalid anchor type"));
return; return;
} }
if(!layout->addAnchor(anchoredElement, anchoredEdge, AnchorLine(anchorLineElementId, anchorLineEdge))) if(!layout->addAnchor(anchoredElement, anchoredEdge, AnchorLine(anchorLineElementId, anchorLineEdge)))
logError(yamlErrorDesc(node, "anchoring failed")); logError(node->generateErrorMessage("anchoring failed"));
} }
void UILoader::loadButton(const UIButtonPtr& button, const YAML::Node& node) void UILoader::loadButton(const UIButtonPtr& button, FML::Node* node)
{ {
button->setText(yamlRead<std::string>(node["text"])); button->setText(node->valueAt("text"));
// set on click event // set on click event
if(yamlHasValue(node, "onClick")) { if(FML::Node* cnode = node->at("onClick")) {
const YAML::Node& cnode = node["onClick"]; if(g_lua.loadBufferAsFunction(cnode->value(), getElementSourceDesc(button, "onClick")))
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), getElementSourceDesc(button, "onClick")))
g_lua.setScriptableField(button, "onClick"); g_lua.setScriptableField(button, "onClick");
else else
logError(yamlErrorDesc(cnode, "failed to parse inline lua script")); logError(cnode->generateErrorMessage("failed to parse inline lua script"));
} }
} }

@ -33,27 +33,27 @@
class UILoader class UILoader
{ {
public: public:
/// Loads an UIElement and it's children from a YAML file /// Loads an UIElement and it's children from a FML file
UIElementPtr loadFromYAML(std::string filePath, const UIContainerPtr& parent = UIContainer::getRoot()); UIElementPtr loadFromFile(std::string filePath, const UIContainerPtr& parent = UIContainer::getRoot());
private: private:
/// Detect element type and create it /// Detect element type and create it
UIElementPtr createElementFromId(const std::string& id); UIElementPtr createElementFromId(const std::string& id);
/// Populate container children from a YAML node /// Populate container children from a FML node
void populateContainer(const UIContainerPtr& parent, const YAML::Node& node); void populateContainer(const UIContainerPtr& parent, FML::Node* node);
/// Load element and its children from a YAML node /// Load element and its children from a FML node
void loadElements(const UIElementPtr& parent, const YAML::Node& node); void loadElements(const UIElementPtr& parent, FML::Node* node);
/// Load element proprieties from a YAML node /// Load element proprieties from a FML node
void loadElement(const UIElementPtr& element, const YAML::Node& node); void loadElement(const UIElementPtr& element, FML::Node* node);
/// Load anchor from a YAML node /// Load anchor from a FML node
void loadElementAnchor(const UIElementPtr& anchoredElement, UI::AnchorPoint anchoredEdge, const YAML::Node& node); void loadElementAnchor(const UIElementPtr& anchoredElement, UI::AnchorPoint anchoredEdge, FML::Node* node);
// specific elements loading // specific elements loading
void loadButton(const UIButtonPtr& button, const YAML::Node& node); void loadButton(const UIButtonPtr& button, FML::Node* node);
std::string getElementSourceDesc(const UIElementPtr& element, const std::string& key = ""); std::string getElementSourceDesc(const UIElementPtr& element, const std::string& key = "");

@ -43,97 +43,62 @@ void UISkins::load(const std::string& skinName)
if(!g_resources.loadFile(skinName + ".yml", fin)) if(!g_resources.loadFile(skinName + ".yml", fin))
flogFatal("FATAL ERROR: Could not load skin \"%s", skinName.c_str()); flogFatal("FATAL ERROR: Could not load skin \"%s", skinName.c_str());
try { FML::Parser parser(fin);
YAML::Parser parser(fin); if(!parser.hasError()) {
FML::Node* doc = parser.getDocument();
YAML::Node doc; m_defaultFont = g_fonts.get(doc->readAt<std::string>("default font"));
parser.GetNextDocument(doc);
m_defaultFont = g_fonts.get(yamlRead<std::string>(doc, "default font"));
if(!m_defaultFont) if(!m_defaultFont)
logFatal("FATAL ERROR: Could not load skin default font"); logFatal("FATAL ERROR: Could not load skin default font");
m_defaultFontColor = yamlRead(doc, "default font color", Color::white); m_defaultFontColor = doc->readAt("default font color", Color::white);
std::string defaultTextureName = yamlRead(doc, "default texture", std::string()); std::string defaultTextureName = doc->readAt("default texture", std::string());
if(!defaultTextureName.empty()) if(!defaultTextureName.empty())
m_defaultTexture = g_textures.get(defaultTextureName); m_defaultTexture = g_textures.get(defaultTextureName);
{ if(FML::Node* node = doc->at("buttons")) {
const YAML::Node& node = doc["buttons"]; foreach(FML::Node* cnode, *node) {
for(auto it = node.begin(); it != node.end(); ++it) { UIElementSkinPtr skin = UIElementSkinPtr(new UIButtonSkin(cnode->tag()));
std::string name; skin->load(cnode);
it.first() >> name;
UIElementSkinPtr skin = UIElementSkinPtr(new UIButtonSkin(name));
skin->load(it.second());
m_elementSkins.push_back(skin);
}
}
{
const YAML::Node& node = doc["panels"];
for(auto it = node.begin(); it != node.end(); ++it) {
std::string name;
it.first() >> name;
UIElementSkinPtr skin = UIElementSkinPtr(new UIElementSkin(name, UI::Panel));
skin->load(it.second());
m_elementSkins.push_back(skin); m_elementSkins.push_back(skin);
} }
} }
{ if(FML::Node* node = doc->at("panels")) {
const YAML::Node& node = doc["windows"]; foreach(FML::Node* cnode, *node) {
for(auto it = node.begin(); it != node.end(); ++it) { UIElementSkinPtr skin = UIElementSkinPtr(new UIElementSkin(cnode->tag(), UI::Panel));
std::string name; skin->load(cnode);
it.first() >> name;
UIElementSkinPtr skin = UIElementSkinPtr(new UIWindowSkin(name));
skin->load(it.second());
m_elementSkins.push_back(skin); m_elementSkins.push_back(skin);
} }
} }
{ if(FML::Node* node = doc->at("windows")) {
const YAML::Node& node = doc["labels"]; foreach(FML::Node* cnode, *node) {
for(auto it = node.begin(); it != node.end(); ++it) { UIElementSkinPtr skin = UIElementSkinPtr(new UIWindowSkin(cnode->tag()));
std::string name; skin->load(cnode);
it.first() >> name;
UIElementSkinPtr skin = UIElementSkinPtr(new UILabelSkin(name));
skin->load(it.second());
m_elementSkins.push_back(skin); m_elementSkins.push_back(skin);
} }
} }
if(FML::Node* node = doc->at("labels")) {
{ foreach(FML::Node* cnode, *node) {
const YAML::Node& node = doc["text edits"]; UIElementSkinPtr skin = UIElementSkinPtr(new UILabelSkin(cnode->tag()));
for(auto it = node.begin(); it != node.end(); ++it) { skin->load(cnode);
std::string name;
it.first() >> name;
UIElementSkinPtr skin = UIElementSkinPtr(new UITextEditSkin(name));
skin->load(it.second());
m_elementSkins.push_back(skin); m_elementSkins.push_back(skin);
} }
} }
{ if(FML::Node* node = doc->at("text edits")) {
const YAML::Node& node = doc["line decorations"]; foreach(FML::Node* cnode, *node) {
for(auto it = node.begin(); it != node.end(); ++it) { UIElementSkinPtr skin = UIElementSkinPtr(new UITextEditSkin(cnode->tag()));
std::string name; skin->load(cnode);
it.first() >> name;
UIElementSkinPtr skin = UIElementSkinPtr(new UIElementSkin(name, UI::LineDecoration));
skin->load(it.second());
m_elementSkins.push_back(skin); m_elementSkins.push_back(skin);
} }
} }
} catch (YAML::Exception& e) { } else {
flogFatal("FATAL ERROR: Malformed skin file \"%s\":\n %s", skinName.c_str() % e.what()); flogFatal("FATAL ERROR: Malformed skin file \"%s\":\n %s", skinName.c_str() % parser.getErrorMessage());
} }
g_resources.popCurrentPath(); g_resources.popCurrentPath();

@ -27,10 +27,10 @@
#include <ui/uitexteditskin.h> #include <ui/uitexteditskin.h>
#include <ui/uitextedit.h> #include <ui/uitextedit.h>
void UITextEditSkin::load(const YAML::Node& node) void UITextEditSkin::load(FML::Node* node)
{ {
UIElementSkin::load(node); UIElementSkin::load(node);
m_textMargin = yamlRead(node, "text margin", 2); m_textMargin = node->readAt("text margin", 2);
} }
void UITextEditSkin::apply(UIElement* element) void UITextEditSkin::apply(UIElement* element)

@ -36,7 +36,7 @@ public:
UITextEditSkin(const std::string& name) : UITextEditSkin(const std::string& name) :
UIElementSkin(name, UI::TextEdit) { } UIElementSkin(name, UI::TextEdit) { }
void load(const YAML::Node& node); void load(FML::Node* node);
void apply(UIElement *element); void apply(UIElement *element);
void draw(UIElement *element); void draw(UIElement *element);

@ -26,17 +26,17 @@
#include <ui/uiwindowskin.h> #include <ui/uiwindowskin.h>
#include <ui/uiwindow.h> #include <ui/uiwindow.h>
void UIWindowSkin::load(const YAML::Node& node) void UIWindowSkin::load(FML::Node* node)
{ {
UIElementSkin::load(node); UIElementSkin::load(node);
const YAML::Node& headNode = node["head"]; FML::Node* headNode = node->at("head");
const YAML::Node& bodyNode = node["body"]; FML::Node* bodyNode = node->at("body");
m_headImage = boost::dynamic_pointer_cast<BorderedImage>(loadImage(headNode)); m_headImage = boost::dynamic_pointer_cast<BorderedImage>(loadImage(headNode));
m_headHeight = yamlRead(headNode, "height", m_headImage->getDefaultSize().height()); m_headHeight = headNode->readAt("height", m_headImage->getDefaultSize().height());
m_headMargin = yamlRead(headNode, "margin", 0); m_headMargin = headNode->readAt("margin", 0);
m_titleAlign = parseAlignment(yamlRead(headNode, "text align", std::string("center"))); m_titleAlign = parseAlignment(headNode->readAt("text align", std::string("center")));
m_bodyImage = loadImage(bodyNode); m_bodyImage = loadImage(bodyNode);
} }

@ -35,7 +35,7 @@ public:
UIWindowSkin(const std::string& name) : UIWindowSkin(const std::string& name) :
UIElementSkin(name, UI::Window) { } UIElementSkin(name, UI::Window) { }
void load(const YAML::Node& node); void load(FML::Node* node);
void draw(UIElement *element); void draw(UIElement *element);
int getHeadHeight() const { return m_headHeight; } int getHeadHeight() const { return m_headHeight; }

@ -76,4 +76,14 @@ inline std::ostream& operator<<(std::ostream& out, const Color& color)
return out; return out;
} }
inline void operator>>(const FML::Node& node, Color& color)
{
int r, g, b, a;
*node.at(0) >> r;
*node.at(1) >> g;
*node.at(2) >> b;
*node.at(3) >> a;
color.setRGBA(r,g,b,a);
}
#endif // COLOR_H #endif // COLOR_H

@ -0,0 +1,359 @@
#include "fml.h"
#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>
namespace FML {
///////////////////////////////////////////////////////////////////////////////
// Utilities
bool fml_convert(const std::string& input, bool& b)
{
std::string names[5][2] = { { "1", "0" },
{ "y", "n" },
{ "yes", "no" },
{ "true", "false" },
{ "on", "off" } };
std::string processedInput = input;
boost::trim(processedInput);
boost::to_lower(processedInput);
for(int i=0;i<5;i++) {
if(names[i][0] == processedInput) {
b = true;
return true;
}
if(names[i][1] == processedInput) {
b = false;
return true;
}
}
return false;
}
bool fml_convert(const std::string& input, std::string& output) {
output = input;
return true;
}
///////////////////////////////////////////////////////////////////////////////
// Node
Node::~Node()
{
for(NodeList::iterator it = m_children.begin(); it != m_children.end(); ++it)
delete (*it);
}
Node* Node::at(const std::string& childTag) const
{
for(NodeList::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
if((*it)->tag() == childTag)
return (*it);
}
return NULL;
}
Node* Node::at(int pos) const
{
if(pos < 0 || pos >= size())
return NULL;
return m_children[pos];
}
std::string Node::value(const std::string& def) const
{
if(!m_value.empty())
return m_value;
return def;
}
std::string Node::valueAt(const std::string childTag, const std::string& def) const
{
for(NodeList::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
if((*it)->tag() == childTag)
return (*it)->value();
}
return def;
}
void Node::addNode(Node *node)
{
if(node->hasTag() && node->hasValue()) {
// remove nodes wit the same tag
for(NodeList::iterator it = m_children.begin(); it != m_children.end(); ++it) {
if((*it)->tag() == node->tag()) {
delete (*it);
m_children.erase(it);
break;
}
}
}
m_children.push_back(node);
node->setParent(this);
}
std::string Node::generateErrorMessage(const std::string& message) const
{
std::stringstream ss;
ss << "FML error ";
if(m_parser && !m_parser->getWhat().empty())
ss << "in " << m_parser->getWhat();
if(m_line > 0)
ss << "at line " << m_line;
ss << ": " << message;
return ss.str();
}
///////////////////////////////////////////////////////////////////////////////
// Parser
Parser::~Parser()
{
if(m_rootNode)
delete m_rootNode;
}
bool Parser::load(std::istream& in)
{
// initialize root node
if(m_rootNode)
delete m_rootNode;
m_rootNode = new Node();
m_rootNode->setTag("root");
m_currentParent = m_rootNode;
m_currentDepth = 0;
m_currentLine = 0;
m_multilineMode = DONT_MULTILINE;
m_multilineData.clear();
while(in.good() && !in.eof()) {
m_currentLine++;
std::string line;
std::getline(in, line);
parseLine(line);
if(hasError())
return false;
}
// stop multilining if enabled
if(isMultilining())
stopMultilining();
return !hasError();
}
void Parser::parseLine(std::string& line)
{
// process multiline data first
if(isMultilining() && parseMultiline(line))
return;
// calculate depth
std::size_t numSpaces = line.find_first_not_of(' ');
// trim left whitespaces
boost::trim_left(line);
// skip comment lines
if(line[0] == '#')
return;
// skip empty lines
if(line.empty())
return;
int depth = 0;
if(numSpaces != std::string::npos) {
depth = numSpaces / 2;
// check for syntax error
if(numSpaces % 2 != 0) {
setErrorMessage("file must be idented every 2 whitespaces", m_currentLine);
return;
}
}
// a depth above
if(depth == m_currentDepth+1) {
// change current parent to the previous added node
m_currentParent = m_previousNode;
// a depth below, change parent to previus parent and add new node inside previuos parent
} else if(depth < m_currentDepth) {
// change current parent to the the new depth parent
for(int i=0;i<m_currentDepth-depth;++i)
m_currentParent = m_currentParent->parent();
// else if nots the same depth it's a syntax error
} else if(depth != m_currentDepth) {
setErrorMessage("invalid indentation level", m_currentLine);
return;
}
// update current depth
m_currentDepth = depth;
// add node
Node *node = parseNode(line);
m_currentParent->addNode(node);
m_previousNode = node;
}
Node *Parser::parseNode(std::string& line)
{
// determine node tag and value
std::string tag;
std::string value;
// its a node that has tag and possible a value
std::size_t dotsPos = line.find_first_of(':');
if(dotsPos != std::string::npos) {
tag = line.substr(0, dotsPos);
value = line.substr(dotsPos+1);
}
// its a node that has a value but no tag
else if(line[0] == '-') {
value = line.substr(1);
}
// its a node that has only a tag
else {
tag = line;
}
// trim the tag and value
boost::trim(tag);
boost::trim(value);
// create the node
Node *node = new Node(this);
node->setLine(m_currentLine);
node->setTag(tag);
// process node value
if(!value.empty()) {
// multiline text scalar
if(value[0] == '|') {
startMultilining(value);
}
// sequence
else if(value[0] == '[') {
if(boost::ends_with(value, "]")) {
value.erase(value.length()-1, 1);
value.erase(0, 1);
boost::trim(value);
boost::tokenizer<boost::escaped_list_separator<char> > tokens(value);
for(boost::tokenizer<boost::escaped_list_separator<char> >::iterator it = tokens.begin(); it != tokens.end(); ++it) {
value = *it;
boost::trim(value);
Node *child = new Node(this);
child->setLine(m_currentLine);
child->setValue(value);
node->addNode(child);
}
} else
setErrorMessage("missing ']' in sequence", m_currentLine);
}
// text scalar
else {
node->setValue(parseTextScalar(value));
}
}
return node;
}
std::string Parser::parseTextScalar(std::string value)
{
if(value[0] == '"' && value[value.length()-1] == '"') {
value = value.substr(1, value.length()-2);
// escape characters
boost::replace_all(value, "\\\\", "\\");
boost::replace_all(value, "\\\"", "\"");
boost::replace_all(value, "\\n", "\n");
}
return value;
}
void Parser::startMultilining(const std::string& param)
{
m_multilineMode = MULTILINE_DONT_FOLD;
m_currentDepth++;
if(param.length() == 2) {
switch(param[1]) {
case '-':
m_multilineMode = MULTILINE_FOLD_BLOCK;
break;
case '+':
m_multilineMode = MULTILINE_FOLD_FLOW;
break;
default:
setErrorMessage("invalid multiline identifier", m_currentLine);
break;
}
}
}
void Parser::stopMultilining()
{
// remove all new lines at the end
if(m_multilineMode == MULTILINE_DONT_FOLD || m_multilineMode == MULTILINE_FOLD_BLOCK) {
while(true) {
int lastPos = m_multilineData.length()-1;
if(m_multilineData[lastPos] != '\n')
break;
m_multilineData.erase(lastPos, 1);
}
}
if(m_multilineMode == MULTILINE_FOLD_BLOCK)
m_multilineData.append("\n");
m_previousNode->setValue(m_multilineData);
m_multilineMode = DONT_MULTILINE;
m_currentDepth--;
m_multilineData.clear();
}
bool Parser::parseMultiline(std::string line)
{
// calculate depth
std::size_t numSpaces = line.find_first_not_of(' ');
// depth above or equal current depth, add the text to the multiline
if(numSpaces != std::string::npos && (int)numSpaces >= m_currentDepth*2) {
m_multilineData += parseTextScalar(line.substr(m_currentDepth*2)) + "\n";
return true;
// depth below the current depth, check if it is a node
} else if(numSpaces == std::string::npos || (int)numSpaces < m_currentDepth*2) {
// if it has contents, its a node, then we must end multiline
if(line.find_first_not_of(' ') != std::string::npos) {
stopMultilining();
// the line still have a node on it
}
// no contents, just an extra line
else {
m_multilineData += "\n";
return true;
}
}
return false;
}
void Parser::setErrorMessage(const std::string& message, int line)
{
std::stringstream ss;
ss << "FML syntax error in ";
if(!m_what.empty())
ss << m_what << " ";
if(line > 0)
ss << "at line " << line;
ss << ": " << message;
m_error = ss.str();
}
} // namespace FML {

@ -0,0 +1,217 @@
#ifndef FML_H
#define FML_H
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
#include <map>
#include <boost/utility.hpp>
#include <boost/foreach.hpp>
namespace FML {
///////////////////////////////////////////////////////////////////////////////
// Utilities
bool fml_convert(const std::string& input, bool& b);
bool fml_convert(const std::string& input, std::string& output);
template <typename V, typename T>
bool fml_convert(const V& in, T& out) {
std::stringstream ss;
ss << in;
ss >> out;
return !!ss;
}
///////////////////////////////////////////////////////////////////////////////
// Node
class Parser;
class Node : boost::noncopyable
{
public:
typedef std::vector<Node*> NodeList;
Node(Parser* parser = 0) : m_parser(parser), m_parent(0), m_line(0) { }
~Node();
bool hasTag() const { return !m_tag.empty(); }
bool hasChildren() const { return size() > 0; }
bool hasValue() const { return !m_value.empty(); }
bool hasNode(const std::string childTag) const { return at(childTag) != 0; }
void setTag(std::string tag) { m_tag = tag; }
void setLine(int line) { m_line = line; }
void setValue(const std::string& value) { m_value = value; }
void setParent(Node* parent) { m_parent = parent; }
std::string tag() const { return m_tag; }
int line() const { return m_line; }
int size() const { return m_children.size(); }
Node* parent() { return m_parent; }
// iterators
typedef NodeList::iterator iterator;
typedef NodeList::const_iterator const_iterator;
iterator begin() { return m_children.begin(); }
iterator end() { return m_children.end(); }
const_iterator begin() const { return m_children.begin(); }
const_iterator end() const { return m_children.end(); }
Node* front() const { return at(0); }
Node* back() const { return at(size()-1); }
// util for generating error message
std::string generateErrorMessage(const std::string& message) const;
// extracting values operator
template <typename T>
friend void operator >> (const Node& node, T& value);
// get nodes
Node* at(const std::string& childTag) const;
Node* at(int pos) const;
// get values
std::string value(const std::string& def = std::string()) const;
std::string valueAt(const std::string childTag, const std::string& def = std::string()) const;
// read values
template <typename T>
T read(T def = T()) const {
T value = def;
*this >> value;
return value;
}
template <typename T>
T readAt(const std::string childTag, T def = T()) const {
T v = def;
for(NodeList::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
if((*it)->tag() == childTag) {
*(*it) >> v;
break;
}
}
return v;
}
void addNode(Node* node);
private:
Parser* m_parser;
Node* m_parent;
int m_line;
NodeList m_children;
std::string m_tag;
std::string m_value;
};
///////////////////////////////////////////////////////////////////////////////
// Node operators
template <typename T>
void operator >> (const Node& node, T& v)
{
fml_convert(node.value(), v);
}
template <typename T>
void operator >> (const Node& node, std::vector<T>& v)
{
v.clear();
v.resize(node.size());
for(unsigned i=0;i<node.size();++i)
*node.at(i) >> v[i];
}
template <typename K, typename T>
void operator >> (const Node& node, std::map<K, T>& m)
{
m.clear();
for(Node::const_iterator it = node.begin(); it != node.end(); ++it) {
Node* child = (*it);
K k;
T v;
if(fml_convert<std::string, K>(child->tag(), k)) {
*child >> v;
m[k] = v;
}
}
}
///////////////////////////////////////////////////////////////////////////////
// Parser
class Parser
{
enum MultilineMode {
DONT_MULTILINE = 0,
MULTILINE_DONT_FOLD,
MULTILINE_FOLD_BLOCK,
MULTILINE_FOLD_FLOW
};
public:
Parser(std::string what = std::string()) : m_rootNode(0), m_what(what) { }
Parser(std::istream& in, std::string what = std::string()) : m_rootNode(0), m_what(what) { load(in); }
~Parser();
bool load(std::istream& in);
Node* getDocument() const { return m_rootNode; }
bool hasError() const { return !m_error.empty(); }
std::string getErrorMessage() { return m_error; }
std::string getWhat() { return m_what; }
protected:
void parseLine(std::string& line);
Node* parseNode(std::string& line);
std::string parseTextScalar(std::string value);
void startMultilining(const std::string& param);
void stopMultilining();
bool parseMultiline(std::string line);
bool isMultilining() { return m_multilineMode != DONT_MULTILINE; }
void setErrorMessage(const std::string& message, int line = 0);
private:
int m_currentDepth;
int m_currentLine;
Node* m_currentParent;
Node* m_previousNode;
Node* m_rootNode;
MultilineMode m_multilineMode;
std::string m_multilineData;
std::string m_error;
std::string m_what;
};
} // namespace FML {
// enable usage with foreach
namespace boost
{
// specialize range_mutable_iterator and range_const_iterator in namespace boost
template<>
struct range_mutable_iterator< FML::Node >
{
typedef FML::Node::iterator type;
};
template<>
struct range_const_iterator< FML::Node >
{
typedef FML::Node::const_iterator type;
};
}
#endif // FML_H

@ -85,4 +85,11 @@ inline std::ostream& operator<<(std::ostream& out, const TPoint<T>& point)
return out; return out;
} }
template <class T>
inline void operator>>(const FML::Node& node, TPoint<T>& point)
{
*node.at(0) >> point.x;
*node.at(1) >> point.y;
}
#endif #endif

@ -309,4 +309,15 @@ inline std::ostream& operator<<(std::ostream& out, const TRect<T>& rect)
return out; return out;
} }
template <class T>
inline void operator>>(const FML::Node& node, TRect<T>& rect)
{
T x, y, width, height;
*node.at(0) >> x;
*node.at(1) >> y;
*node.at(2) >> width;
*node.at(3) >> height;
rect.setRect(x, y, width, height);
}
#endif // RECT_H #endif // RECT_H

@ -112,11 +112,12 @@ typedef TSize<int> Size;
typedef TSize<float> SizeF; typedef TSize<float> SizeF;
template <class T> template <class T>
inline std::ostream& operator<<(std::ostream& out, const TSize<T>& size) inline void operator>>(const FML::Node& node, TSize<T>& size)
{ {
out << "Size(" << size.width() << "," T w, h;
<< size.height() << ")"; *node.at(0) >> w;
return out; *node.at(1) >> h;
size.setSize(w, h);
} }
#endif #endif

@ -1,125 +0,0 @@
/* 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.
*/
#ifndef YAML_H
#define YAML_H
#include <prerequisites.h>
#include <yaml-cpp/yaml.h>
inline void operator>>(const YAML::Node& node, Color& color)
{
int r, g, b, a;
node[0] >> r;
node[1] >> g;
node[2] >> b;
node[3] >> a;
color.setRGBA(r,g,b,a);
}
template <class T>
inline void operator>>(const YAML::Node& node, TPoint<T>& point)
{
node[0] >> point.x;
node[1] >> point.y;
}
template <class T>
inline void operator>>(const YAML::Node& node, TRect<T>& rect)
{
T x, y, width, height;
node[0] >> x;
node[1] >> y;
node[2] >> width;
node[3] >> height;
rect.setRect(x, y, width, height);
}
template <class T>
inline void operator>>(const YAML::Node& node, TSize<T>& size)
{
T w, h;
node[0] >> w;
node[1] >> h;
size.setSize(w, h);
}
template<class T>
inline T yamlRead(const YAML::Node& node)
{
return node.to<T>();
}
template<class T>
inline T yamlRead(const YAML::Node& node, const char *name)
{
T value;
if(node.Type() == YAML::NodeType::Map)
node[name] >> value;
return value;
}
template<class T>
inline T yamlRead(const YAML::Node& node, const char *name, const T& defaultValue)
{
T value = defaultValue;
if(node.Type() == YAML::NodeType::Map && node.FindValue(name))
node[name] >> value;
return value;
}
inline bool yamlHasValue(const YAML::Node& node, const char *name)
{
return (node.Type() == YAML::NodeType::Map && node.FindValue(name) != NULL);
}
inline std::string yamlErrorDesc(const YAML::Node& node, const std::string& error)
{
return YAML::Exception(node.GetMark(), error.c_str()).what();
}
inline void yamlThrowError(const YAML::Node& node, const std::string& error)
{
throw YAML::Exception(node.GetMark(), error.c_str());
}
template<class A, class B>
inline std::map<A,B> yamlReadMap(const YAML::Node& node, const char *name)
{
std::map<A,B> map;
if(node.Type() == YAML::NodeType::Map) {
if(const YAML::Node* mapNode = node.FindValue(name)) {
for(auto it = mapNode->begin(); it != mapNode->end(); ++it) {
A a;
B b;
it.first() >> a;
it.second() >> b;
map[a] = b;
}
}
}
return map;
}
#endif // YAML_H

@ -32,46 +32,40 @@
#include <script/luascript.h> #include <script/luascript.h>
#include <ui/uicontainer.h> #include <ui/uicontainer.h>
/// Catches signals so we can exit nicely
void signal_handler(int sig) void signal_handler(int sig)
{ {
static bool stopping = false;
switch(sig) { switch(sig) {
case SIGTERM: case SIGTERM:
case SIGINT: case SIGINT:
{
static bool stopping = false;
if(!stopping) { if(!stopping) {
stopping = true; stopping = true;
g_engine.onClose(); g_engine.onClose();
} }
break; break;
}
} }
} }
/// Default configurations void loadDefaultConfigs()
void setDefaultConfigs()
{ {
// default size // default size
int defWidth = 550; int defWidth = 550;
int defHeight = 450; int defHeight = 450;
// init on screen center g_configs.set("window x", (Platform::getDisplayWidth() - defWidth)/2);
g_configs.setValue("window x", (Platform::getDisplayWidth() - defWidth)/2); g_configs.set("window y", (Platform::getDisplayHeight() - defHeight)/2);
g_configs.setValue("window y", (Platform::getDisplayHeight() - defHeight)/2); g_configs.set("window width", defWidth);
g_configs.set("window height", defHeight);
g_configs.setValue("window width", defWidth); g_configs.set("window maximized", false);
g_configs.setValue("window height", defHeight);
g_configs.setValue("window maximized", false);
} }
void saveConfigs() void saveConfigs()
{ {
g_configs.setValue("window x", Platform::getWindowX()); g_configs.set("window x", Platform::getWindowX());
g_configs.setValue("window y", Platform::getWindowY()); g_configs.set("window y", Platform::getWindowY());
g_configs.setValue("window width", Platform::getWindowWidth()); g_configs.set("window width", Platform::getWindowWidth());
g_configs.setValue("window height", Platform::getWindowHeight()); g_configs.set("window height", Platform::getWindowHeight());
g_configs.setValue("window maximized", Platform::isWindowMaximized()); g_configs.set("window maximized", Platform::isWindowMaximized());
g_configs.save(); g_configs.save();
} }
@ -89,28 +83,23 @@ int main(int argc, const char *argv[])
args.push_back(argv[i]); args.push_back(argv[i]);
#endif #endif
// install our signal handler logInfo("OTClient 0.2.0");
// install exit signal handler
signal(SIGTERM, signal_handler); signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);
// init platform stuff // init platform stuff
Platform::init("OTClient"); Platform::init("OTClient");
// init random numbers // load resources paths
std::srand(std::time(NULL));
// init resources
g_resources.init(args[0].c_str()); g_resources.init(args[0].c_str());
// before loading configurations set the default ones
setDefaultConfigs();
// load configurations // load configurations
loadDefaultConfigs();
if(!g_configs.load("config.yml")) if(!g_configs.load("config.yml"))
logInfo("Could not read configuration file, default configurations will be used."); logInfo("Could not read configuration file, default configurations will be used.");
logInfo("OTClient 0.2.0");
// create the window // create the window
Platform::createWindow(g_configs.get("window x"), g_configs.get("window y"), Platform::createWindow(g_configs.get("window x"), g_configs.get("window y"),
g_configs.get("window width"), g_configs.get("window height"), g_configs.get("window width"), g_configs.get("window height"),
@ -136,15 +125,9 @@ int main(int argc, const char *argv[])
// terminate stuff // terminate stuff
g_engine.terminate(); g_engine.terminate();
g_uiSkins.terminate(); g_uiSkins.terminate();
// save configurations before exiting
saveConfigs(); saveConfigs();
Platform::terminate(); Platform::terminate();
// unload resources
g_resources.terminate(); g_resources.terminate();
return 0; return 0;
} }

Loading…
Cancel
Save