diff --git a/data/modules/mainmenu/mainmenu.yml b/data/modules/mainmenu/mainmenu.yml index b72d452f..970e1cba 100644 --- a/data/modules/mainmenu/mainmenu.yml +++ b/data/modules/mainmenu/mainmenu.yml @@ -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 diff --git a/data/modules/mainmenu/menustate.lua b/data/modules/mainmenu/menustate.lua index 2fbc9a26..629bd3af 100644 --- a/data/modules/mainmenu/menustate.lua +++ b/data/modules/mainmenu/menustate.lua @@ -1,6 +1,6 @@ -- menu state function onEnterMenuState() - mainMenu = UI.load("modules/mainmenu/mainmenu.yml") + mainMenu = UI.load("mainmenu/mainmenu.yml") end function onLeaveMenuState() diff --git a/src/framework/script/luascript.cpp b/src/framework/script/luascript.cpp index 3b732d52..c95dd853 100644 --- a/src/framework/script/luascript.cpp +++ b/src/framework/script/luascript.cpp @@ -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); diff --git a/src/framework/script/luascript.h b/src/framework/script/luascript.h index 0c379c13..cdbac6c5 100644 --- a/src/framework/script/luascript.h +++ b/src/framework/script/luascript.h @@ -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(); diff --git a/src/framework/script/scriptable.cpp b/src/framework/script/scriptable.cpp index 277d7e22..8ea89284 100644 --- a/src/framework/script/scriptable.cpp +++ b/src/framework/script/scriptable.cpp @@ -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"); } + diff --git a/src/framework/ui/uiloader.cpp b/src/framework/ui/uiloader.cpp index 086c7465..3b2cdd5b 100644 --- a/src/framework/ui/uiloader.cpp +++ b/src/framework/ui/uiloader.cpp @@ -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 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 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(), "#" + element->getId())) { + loadElements(element, it.second()); + break; + } } } }