[Orxonox-commit 265] r2084 - media/gui/scripts

bknecht at orxonox.net bknecht at orxonox.net
Mon Apr 13 17:02:51 CEST 2009


Author: bknecht
Date: 2009-04-13 17:02:50 +0200 (Mon, 13 Apr 2009)
New Revision: 2084

Modified:
   media/gui/scripts/loadGUI_2.lua
Log:
fixed problem with GUI in Lua. You should now be able to easily load, show and hide GUIs you created by yourself

Modified: media/gui/scripts/loadGUI_2.lua
===================================================================
--- media/gui/scripts/loadGUI_2.lua	2009-04-11 22:36:09 UTC (rev 2083)
+++ media/gui/scripts/loadGUI_2.lua	2009-04-13 15:02:50 UTC (rev 2084)
@@ -13,64 +13,63 @@
 
 local current = nil
 local loadedGUIs = {}
-local showing
+-- a bit more complex: GUI is now a class
+GUI = {}
+GUI.__index = GUI
 
-datapath = "" -- points to media-folder (set after loading the script)
+-- hide function for the GUI
+GUI.hide = function (self)
+    self.window:hide()
+end
 
--- function to add a reference to list of reference of loaded GUIs
-loadedGUIs.addGUI = function (gui)
-    loadedGUIs[#loadedGUIs+1] = gui
+-- show function for the GUI
+GUI.show = function (self)
+    self.window:show()
 end
 
--- function which returns a GUI by name
-loadedGUIs.getGUIbyName = function (str)
-    print("number of GUIs loaded: " .. #loadedGUIs)
-    for i = 1, #loadedGUIs, 1 do
-        print("Checking GUI " .. loadedGUIs[i].filename)
-        if str == loadedGUIs[i].filename then
-            return loadedGUIs[i]
-        end
-    end
-    return nil
+-- constructor of the GUI
+GUI.new = function (gui, fname)
+    local newElement = { window = gui, filename = fname }
+    setmetatable(newElement, GUI) -- connects new element with class
+    return newElement
 end
 
+datapath = "" -- points to media-folder (set after loading the script)
+
 -- loads the GUI with the specified filename
 -- be sure to set the global variable "filename" before calling this function
 function loadGUI(filename)
     -- check if it already exists
-    print("about to load " .. filename)
-    print("search for GUI " .. filename)
-    newlyLoaded = loadedGUIs:getGUIbyName(filename)
-    if newlyLoaded == nil then
+    --gui = loadedGUIs:getGUIbyName(filename)
+    gui = loadedGUIs[filename]
+    if gui == nil then
         dofile(datapath .. "gui/scripts/" .. filename .. ".lua")
-        newlyLoaded = winMgr:loadWindowLayout(layoutPath)
-        newlyLoaded.filename = filename
-        loadedGUIs:addGUI(newlyLoaded)
+        win = winMgr:loadWindowLayout(layoutPath)
+        gui = GUI.new(win, filename)
+        loadedGUIs[filename] = gui
         -- if there has no GUI been loaded yet, set new GUI as current
         if #loadedGUIs == 1 then
             current = loadedGUIs[1]
             showing = false
         end
         -- hide new GUI as we do not want to show it accidentially
-        newlyLoaded:hide()
+        gui:hide()
     end
-    return newlyLoaded
+    return gui
 end
 
 -- shows the specified and loads it if not loaded already
 -- be sure to set the global variable "filename" before calling this function
 function showGUI(filename)
-    print("about to show " .. filename)
     if current == nil or current.filename ~= filename then
-        print("current not set")
-        print("search for GUI " .. filename)
-        current = loadedGUIs.getGUIbyName(filename)
+        if current ~= nil then
+        end
+        current = loadedGUIs[filename]
         if current == nil then
             current = loadGUI(filename)
         end
-        system:setGUISheet(current)
+        system:setGUISheet(current.window)
     end
-    print("Showing " .. filename)
     current:show()
     showing = true
 end
@@ -97,12 +96,8 @@
 end
 
 function hideGUI(filename)
-    print("about to hide " .. filename)
-    print("search for GUI " .. filename)
-    current = loadedGUIs.getGUIbyName(filename)
-    print("current is: " .. current)
+    current = loadedGUIs[filename]
     if current ~= nil then
-        print("Hiding " .. filename)
         current:hide()
         showing = false
     end




More information about the Orxonox-commit mailing list