rework on resources system

This commit is contained in:
Eduardo Bart
2011-05-19 14:11:05 -03:00
parent e1af35f061
commit ab6c52a3ee
24 changed files with 322 additions and 226 deletions

View File

@@ -69,7 +69,7 @@ ImagePtr UIElementSkin::loadImage(const YAML::Node& node)
std::string textureName = yamlRead(cnode, "source", std::string());
if(!textureName.empty())
texture = g_textures.get("skins/" + textureName);
texture = g_textures.get(textureName);
else
texture = g_uiSkins.getDefaultTexture();
@@ -89,7 +89,7 @@ ImagePtr UIElementSkin::loadImage(const YAML::Node& node)
if(!image)
logError(yamlErrorDesc(cnode, "failed to load bordered image"));
} else if(yamlHasValue(node, "image")) {
texture = g_textures.get("skins/" + yamlRead<std::string>(node, "image"));
texture = g_textures.get(yamlRead<std::string>(node, "image"));
if(texture)
image = ImagePtr(new Image(texture));
if(!m_defaultSize.isValid())

View File

@@ -30,6 +30,8 @@
#include <script/luafunctions.h>
#include "uianchorlayout.h"
UILoader g_uiLoader;
UIElementPtr UILoader::createElementFromId(const std::string& id)
{
UIElementPtr element;
@@ -65,20 +67,15 @@ UIElementPtr UILoader::createElementFromId(const std::string& id)
return element;
}
UIElementPtr UILoader::loadFile(const std::string& file, const UIContainerPtr& parent)
UIElementPtr UILoader::loadFromYAML(std::string filePath, const UIContainerPtr& parent)
{
// try to find the file
std::string filePath = "modules/" + file;
if(!g_resources.fileExists(filePath))
filePath = "addons/" + file;
if(!g_resources.fileExists(filePath))
filePath = file;
if(!g_resources.fileExists(filePath)) {
flogError("ERROR: Could not load ui file \"%s", file.c_str());
std::stringstream fin;
if(!g_resources.loadFile(filePath, fin)) {
flogError("ERROR: Could not load ui file \"%s", filePath.c_str());
return UIElementPtr();
}
std::istringstream fin(g_resources.loadTextFile(filePath));
m_currentFile = filePath;
try {
YAML::Parser parser(fin);
@@ -110,7 +107,7 @@ UIElementPtr UILoader::loadFile(const std::string& file, const UIContainerPtr& p
element->onLoad();
return element;
} catch (YAML::Exception& e) {
flogError("ERROR: Failed to load ui file \"%s\":\n %s", file.c_str() % e.what());
flogError("ERROR: Failed to load ui file \"%s\":\n %s", filePath.c_str() % e.what());
}
return UIElementPtr();
@@ -208,7 +205,7 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
// load events
if(yamlHasValue(node, "onLoad")) {
const YAML::Node& cnode = node["onLoad"];
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), element->getId() + ":onLoad"))
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), getElementSourceDesc(element, "onLoad")))
g_lua.setScriptableField(element, "onLoad");
else
logError(yamlErrorDesc(cnode, "failed to parse inline lua script"));
@@ -216,7 +213,7 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
if(yamlHasValue(node, "onDestroy")) {
const YAML::Node& cnode = node["onDestroy"];
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), element->getId() + ":onDestroy"))
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), getElementSourceDesc(element, "onDestroy")))
g_lua.setScriptableField(element, "onDestroy");
else
logError(yamlErrorDesc(cnode, "failed to parse inline lua script"));
@@ -273,9 +270,19 @@ void UILoader::loadButton(const UIButtonPtr& button, const YAML::Node& node)
// set on click event
if(yamlHasValue(node, "onClick")) {
const YAML::Node& cnode = node["onClick"];
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), button->getId() + ":onClick"))
if(g_lua.loadBufferAsFunction(yamlRead<std::string>(cnode), getElementSourceDesc(button, "onClick")))
g_lua.setScriptableField(button, "onClick");
else
logError(yamlErrorDesc(cnode, "failed to parse inline lua script"));
}
}
std::string UILoader::getElementSourceDesc(const UIElementPtr& element, const std::string& key)
{
std::string desc;
desc += g_resources.resolvePath(m_currentFile) + ":" + element->getId();
if(key.length() > 0)
desc += "[" + key + "]";
return desc;
}

View File

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

View File

@@ -35,13 +35,13 @@
UISkins g_uiSkins;
void UISkins::load(const std::string& skinsFile)
void UISkins::load(const std::string& skinName)
{
std::string fileContents = g_resources.loadTextFile(skinsFile);
if(!fileContents.size())
flogFatal("FATAL ERROR: Could not load skin file \"%s", skinsFile.c_str());
g_resources.pushCurrentPath("skins");
std::istringstream fin(fileContents);
std::stringstream fin;
if(!g_resources.loadFile(skinName + ".yml", fin))
flogFatal("FATAL ERROR: Could not load skin \"%s", skinName.c_str());
try {
YAML::Parser parser(fin);
@@ -57,7 +57,7 @@ void UISkins::load(const std::string& skinsFile)
std::string defaultTextureName = yamlRead(doc, "default texture", std::string());
if(!defaultTextureName.empty())
m_defaultTexture = g_textures.get("skins/" + defaultTextureName);
m_defaultTexture = g_textures.get(defaultTextureName);
{
const YAML::Node& node = doc["buttons"];
@@ -133,8 +133,10 @@ void UISkins::load(const std::string& skinsFile)
}
}
} catch (YAML::Exception& e) {
flogFatal("FATAL ERROR: Malformed skin file \"%s\":\n %s", skinsFile.c_str() % e.what());
flogFatal("FATAL ERROR: Malformed skin file \"%s\":\n %s", skinName.c_str() % e.what());
}
g_resources.popCurrentPath();
}
void UISkins::terminate()

View File

@@ -35,7 +35,7 @@ class UISkins
public:
UISkins() { }
void load(const std::string& skinsFile);
void load(const std::string& skinName);
void terminate();
UIElementSkinPtr getElementSkin(UI::ElementType elementType, const std::string& name = "default");