* load ui elements in order

* shorter ui paths
This commit is contained in:
Eduardo Bart 2011-05-03 10:36:08 -03:00
parent c052723477
commit 6d871b305f
6 changed files with 66 additions and 33 deletions

View File

@ -21,7 +21,7 @@ panel#background:
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
margin.top: 16
onClick: UI.load("modules/mainmenu/entergamewindow.yml")
onClick: UI.load("mainmenu/entergamewindow.yml")
button#accessAccountButton:
text: Access Account
@ -35,14 +35,14 @@ panel#background:
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
margin.top: 76
onClick: UI.load("modules/mainmenu/optionswindow.yml")
onClick: UI.load("mainmenu/optionswindow.yml")
button#infoButton:
text: Info
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
margin.top: 106
onClick: UI.load("modules/mainmenu/infowindow.yml")
onClick: UI.load("mainmenu/infowindow.yml")
button#exitGameButton:
text: Exit

View File

@ -1,6 +1,6 @@
-- menu state
function onEnterMenuState()
mainMenu = UI.load("modules/mainmenu/mainmenu.yml")
mainMenu = UI.load("mainmenu/mainmenu.yml")
end
function onLeaveMenuState()

View File

@ -151,6 +151,11 @@ void LuaScript::remove(int index)
lua_remove(L, index);
}
bool LuaScript::next(int index)
{
return lua_next(L, index);
}
void LuaScript::releaseRef(int ref)
{
luaL_unref(L, LUA_REGISTRYINDEX, ref);

View File

@ -51,6 +51,7 @@ public:
int getStackSize();
void insert(int index);
void remove(int index);
bool next(int index = -2);
void releaseRef(int ref);
void newTable();

View File

@ -46,15 +46,31 @@ void Scriptable::releaseLuaTableRef()
void Scriptable::callLuaTableField(const std::string& field)
{
// set self
g_lua.pushClassInstance(shared_from_this());
g_lua.setGlobal("self");
// push field
g_lua.getScriptableField(shared_from_this(), field);
if(g_lua.isFunction())
g_lua.callFunction();
else
g_lua.pop();
// call it if its a function
if(g_lua.isFunction()) {
g_lua.callFunction();
// if its an array call each element
} else if(g_lua.isTable()) {
g_lua.pushNil();
while(g_lua.next()) {
// call it if its a function
if(g_lua.isFunction())
g_lua.callFunction();
g_lua.pop();
}
} else if(!g_lua.isNil()) {
g_lua.reportError(f("field '%s' for '%s' is not a valid function or array of functions", field % getScriptableName()));
}
// release self
g_lua.pushNil();
g_lua.setGlobal("self");
}

View File

@ -66,13 +66,18 @@ UIElementPtr UILoader::createElementFromId(const std::string& id)
UIElementPtr UILoader::loadFile(const std::string& file, const UIContainerPtr& parent)
{
std::string fileContents = g_resources.loadTextFile(file);
if(!fileContents.size()) {
// 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());
return UIElementPtr();
}
std::istringstream fin(fileContents);
std::istringstream fin(g_resources.loadTextFile(filePath));
try {
YAML::Parser parser(fin);
@ -112,23 +117,32 @@ UIElementPtr UILoader::loadFile(const std::string& file, const UIContainerPtr& p
void UILoader::populateContainer(const UIContainerPtr& parent, const YAML::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 and element id
if(id.find("#") != std::string::npos) {
UIElementPtr element = createElementFromId(id);
if(!element) {
logError(YAML::Exception(it.first().GetMark(), "invalid element type").what());
continue;
}
parent->addChild(element);
// check if it's an element id
if(id.find("#") != std::string::npos)
orderedNodes[it.first().GetMark().pos] = id;
}
// also populate this element if it's a parent
if(element->asUIContainer())
populateContainer(element->asUIContainer(), it.second());
// populate ordered elements
foreach(auto pair, orderedNodes) {
std::string id = pair.second;
const YAML::Node& cnode = node[id];
UIElementPtr element = createElementFromId(id);
if(!element) {
logError(YAML::Exception(cnode.GetMark(), "invalid element type").what());
continue;
}
parent->addChild(element);
// also populate this element if it's a parent
if(element->asUIContainer())
populateContainer(element->asUIContainer(), cnode);
}
}
@ -136,17 +150,14 @@ void UILoader::loadElements(const UIElementPtr& parent, const YAML::Node& node)
{
loadElement(parent, node);
if(parent->asUIContainer()) {
UIContainerPtr container = parent->asUIContainer();
for(auto it = node.begin(); it != node.end(); ++it) {
std::string id;
it.first() >> id;
// check if it's and element id
if(id.find("#") != std::string::npos) {
std::vector<std::string> split;
boost::split(split, id, boost::is_any_of(std::string("#")));
loadElements(container->getChildById(split[1]), it.second());
if(UIContainerPtr container = parent->asUIContainer()) {
foreach(const UIElementPtr& element, container->getChildren()) {
for(auto it = node.begin(); it != node.end(); ++it) {
// node found, load it
if(boost::ends_with(it.first().Read<std::string>(), "#" + element->getId())) {
loadElements(element, it.second());
break;
}
}
}
}