* 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.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
margin.top: 16 margin.top: 16
onClick: UI.load("modules/mainmenu/entergamewindow.yml") onClick: UI.load("mainmenu/entergamewindow.yml")
button#accessAccountButton: button#accessAccountButton:
text: Access Account text: Access Account
@ -35,14 +35,14 @@ panel#background:
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("modules/mainmenu/optionswindow.yml") onClick: UI.load("mainmenu/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("modules/mainmenu/infowindow.yml") onClick: UI.load("mainmenu/infowindow.yml")
button#exitGameButton: button#exitGameButton:
text: Exit text: Exit

View File

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

View File

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

View File

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

View File

@ -46,15 +46,31 @@ void Scriptable::releaseLuaTableRef()
void Scriptable::callLuaTableField(const std::string& field) void Scriptable::callLuaTableField(const std::string& field)
{ {
// set self
g_lua.pushClassInstance(shared_from_this()); g_lua.pushClassInstance(shared_from_this());
g_lua.setGlobal("self"); g_lua.setGlobal("self");
// push field
g_lua.getScriptableField(shared_from_this(), field); g_lua.getScriptableField(shared_from_this(), field);
// 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()) if(g_lua.isFunction())
g_lua.callFunction(); g_lua.callFunction();
else
g_lua.pop(); 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.pushNil();
g_lua.setGlobal("self"); 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) UIElementPtr UILoader::loadFile(const std::string& file, const UIContainerPtr& parent)
{ {
std::string fileContents = g_resources.loadTextFile(file); // try to find the file
if(!fileContents.size()) { 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()); flogError("ERROR: Could not load ui file \"%s", file.c_str());
return UIElementPtr(); return UIElementPtr();
} }
std::istringstream fin(fileContents); std::istringstream fin(g_resources.loadTextFile(filePath));
try { try {
YAML::Parser parser(fin); 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) 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) { for(auto it = node.begin(); it != node.end(); ++it) {
std::string id; std::string id;
it.first() >> id; it.first() >> id;
// check if it's and element id // check if it's an element id
if(id.find("#") != std::string::npos) { if(id.find("#") != std::string::npos)
orderedNodes[it.first().GetMark().pos] = id;
}
// populate ordered elements
foreach(auto pair, orderedNodes) {
std::string id = pair.second;
const YAML::Node& cnode = node[id];
UIElementPtr element = createElementFromId(id); UIElementPtr element = createElementFromId(id);
if(!element) { if(!element) {
logError(YAML::Exception(it.first().GetMark(), "invalid element type").what()); logError(YAML::Exception(cnode.GetMark(), "invalid element type").what());
continue; continue;
} }
parent->addChild(element); parent->addChild(element);
// also populate this element if it's a parent // also populate this element if it's a parent
if(element->asUIContainer()) if(element->asUIContainer())
populateContainer(element->asUIContainer(), it.second()); populateContainer(element->asUIContainer(), cnode);
}
} }
} }
@ -136,17 +150,14 @@ void UILoader::loadElements(const UIElementPtr& parent, const YAML::Node& node)
{ {
loadElement(parent, node); loadElement(parent, node);
if(parent->asUIContainer()) { if(UIContainerPtr container = parent->asUIContainer()) {
UIContainerPtr container = parent->asUIContainer(); foreach(const UIElementPtr& element, container->getChildren()) {
for(auto it = node.begin(); it != node.end(); ++it) { for(auto it = node.begin(); it != node.end(); ++it) {
std::string id; // node found, load it
it.first() >> id; if(boost::ends_with(it.first().Read<std::string>(), "#" + element->getId())) {
loadElements(element, it.second());
// check if it's and element id break;
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());
} }
} }
} }