Index: code/trunk/data/gui/scripts/InitialiseGUI.lua
===================================================================
--- code/trunk/data/gui/scripts/InitialiseGUI.lua	(revision 5781)
+++ code/trunk/data/gui/scripts/InitialiseGUI.lua	(revision 6417)
@@ -14,4 +14,13 @@
 
 loadedGUIs = {}
+cursorVisibility = {}
+activeSheets = {}
+nrOfActiveSheets = 0
+root = nil
+bShowsCursor = false
+bHidePrevious = {}
+
+-- Require all tools
+require("GUITools")
 
 -- loads the GUI with the specified filename
@@ -22,4 +31,7 @@
     if loadedGui == nil then
         loadedGuiNS = require(filename)
+        if loadedGuiNS == nil then
+            return
+        end
         loadedGui = loadedGuiNS:load()
         loadedGUIs[filename] = loadedGui
@@ -27,5 +39,4 @@
         if table.getn(loadedGUIs) == 1 then
             current = loadedGUIs[1]
-            showing = false
         end
         -- hide new GUI as we do not want to show it accidentially
@@ -35,6 +46,6 @@
 end
 
-function showGUI(filename, ptr)
-    gui = showGUI(filename)
+function showGUI(filename, hidePrevious, bCursorVisible, ptr)
+    gui = showGUI(filename, hidePrevious, bCursorVisible)
     gui.overlay = ptr
 end
@@ -42,43 +53,175 @@
 -- 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)
-    if current == nil or current.filename ~= filename then
-        current = loadedGUIs[filename]
-        if current == nil then
-            current = loadGUI(filename)
-        end
-        system:setGUISheet(current.window)
-    end
-    current:show()
-    showing = true
-    return current
-end
-
-function toggleGUI()
-    if showing == true then
-        current:hide()
+function showGUI(filename, hidePrevious, bCursorVisible)
+    if bCursorVisible == nil then
+        if nrOfActiveSheets > 0 then
+            bCursorVisible = cursorVisibility[activeSheets[nrOfActiveSheets]]
+        else
+            bCursorVisible = true
+        end
+    end
+
+    if root == nil then
+        setBackground("")
+    end
+
+    local currentGUI = loadedGUIs[filename]
+    if(currentGUI == nil) then
+        currentGUI = loadGUI(filename)
+    end
+
+    if(root:isChild(currentGUI.window)) then
+        root:removeChildWindow(currentGUI.window)
+    end
+    root:addChildWindow(currentGUI.window)
+
+    if bCursorVisible then
+        showCursor()
+    else
+        hideCursor()
+    end
+
+    if find( activeSheets, filename ) ~= nil then
+        table.remove( activeSheets, find( activeSheets, filename ) )
+        nrOfActiveSheets = nrOfActiveSheets - 1
+    else
+        if nrOfActiveSheets == 0 then
+            orxonox.InputManager:getInstance():enterState("guiMouseOnly")
+            orxonox.HumanController:pauseControl()
+        end
+    end
+    nrOfActiveSheets = nrOfActiveSheets + 1
+    table.insert(activeSheets, filename)
+    activeSheets[nrOfActiveSheets] = filename
+    bHidePrevious[filename]=hidePrevious
+    cursorVisibility[filename] = bCursorVisible
+
+    if hidePrevious == true then
+        for i=1,nrOfActiveSheets-1 do
+            loadedGUIs[ activeSheets[i] ]:hide()
+        end
+    end
+    currentGUI:show()
+    return currentGUI
+end
+
+function hideCursor()
+    if bShowsCursor==true then
+        bShowsCursor=false
         cursor:hide()
-        showing = false
-    else
-        current:show()
+    end
+end
+
+function showCursor()
+    if bShowsCursor==false then
+        bShowsCursor=true
         cursor:show()
-        showing = true
-    end
-    return showing
-end
-
-function hideCursor()
-    cursor:hide()
-end
-
-function showCursor()
-    cursor:show()
+    end
 end
 
 function hideGUI(filename)
-    current = loadedGUIs[filename]
-    if current ~= nil then
-        current:hide()
-        showing = false
-    end
-end
+    local currentGUI = loadedGUIs[filename]
+    if currentGUI == nil then
+        return
+    end
+    currentGUI:hide()
+    if bHidePrevious[filename] == true then
+        local i = nrOfActiveSheets-1
+        while i>0 do
+            loadedGUIs[ activeSheets[i] ]:show()
+            if bHidePrevious[filename]==true then
+                break
+            else
+                i=i-1
+            end
+        end
+    end
+    root:removeChildWindow(currentGUI.window)
+    local i=1
+    while activeSheets[i] do
+        if activeSheets[i+1] == nil then
+            if activeSheets[i-1] ~= nil then
+                if cursorVisibility[ activeSheets[i-1] ] == true then
+                    showCursor()
+                else
+                    hideCursor()
+                end
+            else
+                hideCursor()
+            end
+        end
+        if activeSheets[i] == filename then
+            table.remove( activeSheets, i )
+            nrOfActiveSheets = nrOfActiveSheets-1
+        else
+            i = i+1
+        end
+    end
+    cursorVisibility[filename] = nil -- remove the cursor visibility of the current gui from the table
+    bHidePrevious[filename] = nil
+    if nrOfActiveSheets == 0 then
+        orxonox.InputManager:getInstance():leaveState("guiMouseOnly")
+        orxonox.HumanController:resumeControl()
+        hideCursor()
+    end
+end
+
+function hideAllGUIs()
+    while nrOfActiveSheets ~= 0 do
+        hideGUI(activeSheets[nrOfActiveSheets])
+    end
+end
+
+function keyESC()
+    if nrOfActiveSheets == 1 and activeSheets[1] == "MainMenu" then
+        orxonox.execute("exit")
+    elseif nrOfActiveSheets > 0 then
+        orxonox.execute("hideGUI "..activeSheets[nrOfActiveSheets])
+    else
+        showGUI("InGameMenu")
+    end
+end
+
+function setBackground(filename)
+    local newroot
+    if root ~= nil then
+        root:rename("oldRootWindow")
+    end
+    if filename ~= "" then
+        newroot = winMgr:loadWindowLayout(filename .. ".layout")
+        newroot:rename("AbsoluteRootWindow")
+        system:setGUISheet(newroot)
+    else
+        newroot = winMgr:createWindow("DefaultWindow", "AbsoluteRootWindow")
+        newroot:setProperty("Alpha", "0.0")
+        newroot:setSize(CEGUI.UVector2(CEGUI.UDim(1.0,0),CEGUI.UDim(1.0,0)))
+        system:setGUISheet(newroot)
+    end
+    if root ~= nil then
+        local child
+        while root:getChildCount()~=0 do
+            child = root:getChildAtIdx(0)
+            root:removeChildWindow(child)
+            newroot:addChildWindow(child)
+        end
+        winMgr:destroyWindow(root)
+    end
+    newroot:show()
+    root = newroot
+end
+
+function find(table, value)
+    local i=0
+    while table[i] ~= nil do
+        if table[i]==value then
+            return i
+        else
+            i=i+1
+        end
+    end
+    return nil
+end
+
+function test(e)
+    debug(0, "Blubb")
+end
