Index: code/branches/questsystem5/src/orxonox/gui/GUIManager.cc
===================================================================
--- code/branches/questsystem5/src/orxonox/gui/GUIManager.cc	(revision 2907)
+++ code/branches/questsystem5/src/orxonox/gui/GUIManager.cc	(revision 2908)
@@ -22,7 +22,6 @@
  *   Author:
  *      Reto Grieder
- *      Benjamin Knecht
  *   Co-authors:
- *
+ *      ...
  *
  */
@@ -37,6 +36,7 @@
 #include "GUIManager.h"
 
-#include <boost/filesystem/path.hpp>
+#include <boost/filesystem.hpp>
 #include <OgreRenderWindow.h>
+#include <OgreRoot.h>
 #include <CEGUI.h>
 #include <CEGUIDefaultLogger.h>
@@ -50,7 +50,8 @@
 
 #include "util/Exception.h"
+#include "core/input/InputManager.h"
+#include "core/input/SimpleInputState.h"
 #include "core/ConsoleCommand.h"
 #include "core/Core.h"
-#include "core/Clock.h"
 #include "ToluaBindCore.h"
 #include "ToluaBindOrxonox.h"
@@ -62,8 +63,15 @@
 namespace orxonox
 {
+    SetConsoleCommandShortcut(GUIManager, showGUI_s).keybindMode(KeybindMode::OnPress);
+
     GUIManager* GUIManager::singletonRef_s = 0;
 
     GUIManager::GUIManager()
-        : renderWindow_(0)
+        //: emptySceneManager_(0)
+        : backgroundSceneManager_(0)
+        //, emptyCamera_(0)
+        , backgroundCamera_(0)
+        //, viewport_(0)
+        , renderWindow_(0)
         , guiRenderer_(0)
         , resourceProvider_(0)
@@ -76,12 +84,20 @@
     }
 
-    /**
-    @brief
-        Deconstructor of the GUIManager
-
-        Basically shuts down CEGUI and destroys the Lua engine and afterwards the interface to the Ogre engine.
-    */
     GUIManager::~GUIManager()
     {
+        if (backgroundCamera_)
+            backgroundSceneManager_->destroyCamera(backgroundCamera_);
+
+        if (backgroundSceneManager_)
+        {
+            // We have to make sure the SceneManager is not anymore referenced.
+            // For the case that the target SceneManager was yet another one, it
+            // wouldn't matter anyway since this is the destructor.
+            guiRenderer_->setTargetSceneManager(0);
+            Ogre::Root::getSingleton().destroySceneManager(backgroundSceneManager_);
+        }
+
+        InputManager::getInstance().requestDestroyState("gui");
+
         if (guiSystem_)
             delete guiSystem_;
@@ -94,4 +110,8 @@
 	        lua_pushnil(luaState_);
 	        lua_setglobal(luaState_, "Core");
+            // TODO: deleting the script module fails an assertion.
+            // However there is not much we can do about it since it occurs too when
+            // we don't open Core or Orxonox. Might be a CEGUI issue.
+            // The memory leak is not a problem anyway..
             delete scriptModule_;
         }
@@ -103,18 +123,4 @@
     }
 
-    /**
-    @brief
-        Initialises the GUIManager by starting up CEGUI
-    @param renderWindow
-        Ogre's render window. Without this, the GUI cannot be displayed.
-    @return true if success, otherwise false
-
-        Before this call the GUIManager won't do anything, but can be accessed.
-
-        Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine.
-        The log is set up and connected to the CEGUILogger.
-        After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code.
-        Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically).
-    */
     bool GUIManager::initialise(Ogre::RenderWindow* renderWindow)
     {
@@ -129,6 +135,13 @@
                 renderWindow_ = renderWindow;
 
+                // Full screen viewport with Z order = 0 (top most). Don't yet feed a camera (so nothing gets rendered)
+                //this->viewport_ = renderWindow_->addViewport(0, 3);
+                //this->viewport_->setOverlaysEnabled(false);
+                //this->viewport_->setShadowsEnabled(false);
+                //this->viewport_->setSkiesEnabled(false);
+                //this->viewport_->setClearEveryFrame(false);
+
                 // Note: No SceneManager specified yet
-                this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, true, 3000);
+                this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_MAIN, true, 3000);
                 this->resourceProvider_ = guiRenderer_->createResourceProvider();
                 this->resourceProvider_->setDefaultResourceGroup("GUI");
@@ -153,6 +166,13 @@
                 tolua_Orxonox_open(this->scriptModule_->getLuaState());
 
-                // initialise the basic lua code
-                loadLuaCode();
+                // register us as input handler
+                SimpleInputState* state = InputManager::getInstance().createInputState<SimpleInputState>("gui", 30);
+                state->setHandler(this);
+                state->setJoyStickHandler(&InputManager::EMPTY_HANDLER);
+
+                // load the background scene
+                loadScenes();
+                //CEGUI::KeyEventArgs e;
+                //e.codepoint
             }
             catch (CEGUI::Exception& ex)
@@ -172,24 +192,31 @@
     }
 
-    /**
-    @brief
-        Calls main Lua script
-    @todo
-        Replace loadGUI.lua with loadGUI_2.lua after merging this back to trunk.
-        However CEGUI is able to execute a startup script. We could maybe put this call in this startup code.
-
-        This function calls the main Lua script for our GUI.
-
-        Additionally we set the datapath variable in Lua. This is needed so Lua can access the data used for the GUI.
-    */
-    void GUIManager::loadLuaCode()
-    {
+    void GUIManager::loadScenes()
+    {
+        // first of all, we need to have our own SceneManager for the GUI. The reason
+        // is that we might have multiple viewports when in play mode (e.g. the view of
+        // a camera fixed at the back of the ship). That forces us to create our own
+        // full screen viewport that is on top of all the others, but doesn't clear the
+        // port before rendering, so everything from the GUI gets on top eventually.
+        // But in order to realise that, we also need a SceneManager with an empty scene,
+        // because the SceneManager is responsible for the render queue.
+        //this->emptySceneManager_ = Ogre::Root::getSingleton()
+        //    .createSceneManager(Ogre::ST_GENERIC, "GUI/EmptySceneManager");
+
+        // we also need a camera or we won't see anything at all.
+        // The camera settings don't matter at all for an empty scene since the GUI
+        // gets rendered on top of the screen rather than into the scene.
+        //this->emptyCamera_ = this->emptySceneManager_->createCamera("GUI/EmptyCamera");
+
+        // Create another SceneManager that enables to display some 3D
+        // scene in the background of the main menu.
+        this->backgroundSceneManager_ = Ogre::Root::getSingleton()
+            .createSceneManager(Ogre::ST_GENERIC, "GUI/BackgroundSceneManager");
+        this->backgroundCamera_ = backgroundSceneManager_->createCamera("GUI/BackgroundCamera");
+
+        // TODO: create something 3D
         try
         {
-            // call main Lua script
-            this->scriptModule_->executeScriptFile("loadGUI_2.lua", "GUI");
-            // set datapath for GUI data
-            lua_pushfstring(this->scriptModule_->getLuaState(), Core::getMediaPathString().c_str());
-            lua_setglobal(this->scriptModule_->getLuaState(), "datapath");
+            this->scriptModule_->executeScriptFile("loadGUI.lua", "GUI");
         }
         catch (CEGUI::Exception& ex)
@@ -204,72 +231,39 @@
     }
 
-    /**
-    @brief
-        used to tick the GUI
-    @param time
-        clock which provides time value for the GUI System
-
-        Ticking the GUI means updating it with a certain regularity.
-        The elapsed time since the last call is given in the time value provided by the clock.
-        This time value is then used to provide a fluent animation of the GUI.
-    */
-    void GUIManager::update(const Clock& time)
-    {
-        assert(guiSystem_);
-        guiSystem_->injectTimePulse(time.getDeltaTime());
-    }
-
-    /**
-    @brief
-        Executes Lua code
-    @param str
-        reference to string object holding the Lua code which is to be executed
-
-        This function gives total access to the GUI. You can execute ANY Lua code here.
-    */
-    void GUIManager::executeCode(const std::string& str)
-    {
-        try
-        {
-            this->scriptModule_->executeString(str);
-        }
-        catch (CEGUI::Exception& ex)
-        {
-            COUT(2) << "CEGUI Error: \"" << ex.getMessage() << "\" while executing code \"" << str << "\"" << std::endl;
-        }
-    }
-
-    /**
-    @brief
-        Tells the GUIManager which SceneManager to use
-    @param camera
-        The current camera on which the GUI should be displayed on.
-
-        In fact the GUIManager needs the SceneManager and not the Camera to display the GUI.
-        This means the GUI is not bound to a camera but rather to the SceneManager.
-        Hidding the GUI when needed can therefore not be solved by just NOT setting the current camera.
-    */
-    void GUIManager::setCamera(Ogre::Camera* camera)
-    {
-        this->guiRenderer_->setTargetSceneManager(camera->getSceneManager());
-    }
-
-    /**
-    @brief
-        Displays specified GUI on screen
-    @param name
-        The name of the GUI
-
-        The function executes the Lua function with the same name in case the GUIManager is ready.
-        For more details check out loadGUI_2.lua where the function presides.
-    */
-    void GUIManager::showGUI(const std::string& name)
+    void GUIManager::showGUI(const std::string& name, Ogre::SceneManager* sceneManager)// bool showBackground)
     {
         if (state_ != Uninitialised)
         {
-            //COUT(3) << "Loading GUI " << name << std::endl;
+            if (state_ == OnDisplay)
+                hideGUI();
+
+            COUT(3) << "Loading GUI " << name << std::endl;
             try
             {
-                this->scriptModule_->executeString(std::string("showGUI(\"") + name + "\")");
+                if (!sceneManager)
+                {
+                    // currently, only an image is loaded. We could do 3D, see loadBackground.
+                    //this->viewport_->setClearEveryFrame(true);
+                    this->guiRenderer_->setTargetSceneManager(this->backgroundSceneManager_);
+                    //this->viewport_->setCamera(this->backgroundCamera_);
+
+                    lua_pushboolean(this->scriptModule_->getLuaState(), true);
+                    lua_setglobal(this->scriptModule_->getLuaState(), "showBackground");
+                }
+                else
+                {
+                    //this->viewport_->setClearEveryFrame(false);
+                    this->guiRenderer_->setTargetSceneManager(sceneManager);
+                    //this->viewport_->setCamera(this->emptyCamera_);
+
+                    lua_pushboolean(this->scriptModule_->getLuaState(), false);
+                    lua_setglobal(this->scriptModule_->getLuaState(), "showBackground");
+                }
+
+                this->scriptModule_->executeScriptGlobal("showMainMenu");
+
+                InputManager::getInstance().requestEnterState("gui");
+
+                this->state_ = OnDisplay;
             }
             catch (CEGUI::Exception& ex)
@@ -288,13 +282,14 @@
     }
 
-    /**
-    @brief
-        Function receiving a mouse button pressed event.
-    @param id
-        ID of the mouse button which got pressed
-
-        This function is inherited by MouseHandler and injects the event into CEGUI.
-        It is for CEGUI to process the event.
-    */
+    void GUIManager::hideGUI()
+    {
+        if (this->state_ != OnDisplay)
+            return;
+        //this->viewport_->setCamera(0);
+        this->guiRenderer_->setTargetSceneManager(0);
+        this->state_ = Ready;
+        InputManager::getInstance().requestLeaveState("gui");
+    }
+
     void GUIManager::mouseButtonPressed(MouseButtonCode::ByEnum id)
     {
@@ -310,13 +305,4 @@
     }
 
-    /**
-    @brief
-        Function receiving a mouse button released event.
-    @param id
-        ID of the mouse button which got released
-
-        This function is inherited by MouseHandler and injects the event into CEGUI.
-        It is for CEGUI to process the event.
-    */
     void GUIManager::mouseButtonReleased(MouseButtonCode::ByEnum id)
     {
@@ -332,14 +318,5 @@
     }
 
-    /**
-    @brief
-        converts mouse event code to CEGUI event code
-    @param button
-        code of the mouse button as we use it in Orxonox
-    @return
-        code of the mouse button as it is used by CEGUI
-
-        Simple convertion from mouse event code in Orxonox to the one used in CEGUI.
-     */
+
     inline CEGUI::MouseButton GUIManager::convertButton(MouseButtonCode::ByEnum button)
     {
Index: code/branches/questsystem5/src/orxonox/gui/GUIManager.h
===================================================================
--- code/branches/questsystem5/src/orxonox/gui/GUIManager.h	(revision 2907)
+++ code/branches/questsystem5/src/orxonox/gui/GUIManager.h	(revision 2908)
@@ -23,5 +23,5 @@
  *      Reto Grieder
  *   Co-authors:
- *      Benjamin Knecht
+ *      ...
  *
  */
@@ -49,13 +49,6 @@
 {
     /**
-    @class GUIManager
     @brief
-        Provides a simple interface to CEGUI with tolua methods and console commands. It also acts as a key and mouse handler.
-
-        The GUIManager is a singleton and can be called anywhere when access on the GUI is needed.
-        Creation of the GUIManager is therefore not possible and the cunstructor is private.
-
-        Since the GUI needs user input, the GUIManager implements the functions needed to act as a key and/or mouse handler.
-        Those input events are then injected into CEGUI in Lua.
+        Provides a simple interface to CEGUI with tolua methods and console commands
     */
     class _OrxonoxExport GUIManager
@@ -66,13 +59,9 @@
 // tolua_end
     public:
-        /**
-        @enum State
-            The current state of the GUIManager. There should maybe be more (or we can omit this totally).
-        */
         enum State
         {
-            Uninitialised,  //!< Initial state of the GUIManager
-            Ready,          //!< State after initialisation if ready
-            OnDisplay       //!< State if GUI is displayed
+            Uninitialised,
+            Ready,
+            OnDisplay
         };
 
@@ -81,11 +70,18 @@
 
         bool initialise(Ogre::RenderWindow* renderWindow);
+        void tick(float dt)
+        {
+            assert(guiSystem_);
+            guiSystem_->injectTimePulse(dt);
+        }
+        void showGUI(const std::string& name, Ogre::SceneManager* sceneManager);// bool showBackground); // tolua_export
+        void hideGUI(); // tolua_export
 
-        void update(const Clock& time);
+        Ogre::Camera* getCamera() { return this->backgroundCamera_; }
 
-        void showGUI(const std::string& name);
-        void executeCode(const std::string& str);
-
-        void setCamera(Ogre::Camera* camera);
+        static void showGUI_s(const std::string& name, Ogre::SceneManager* sceneManager)//bool showBackground)
+        {
+            getInstance().showGUI(name, sceneManager);
+        }
 
         static GUIManager& getInstance()    { assert(singletonRef_s); return *singletonRef_s; } // tolua_export
@@ -93,41 +89,47 @@
 
     private:
-        GUIManager(const GUIManager& instance);                 //!< private constructor (this is a singleton class)
+        GUIManager(const GUIManager& instance);
 
-        void loadLuaCode();
+        void keyPressed (const KeyEvent& evt)
+        { guiSystem_->injectKeyDown(evt.key); guiSystem_->injectChar(evt.text); }
+        void keyReleased(const KeyEvent& evt)
+        { guiSystem_->injectKeyUp(evt.key); }
+        void keyHeld    (const KeyEvent& evt)
+        { }
 
-        // keyHandler functions
-        void keyPressed (const KeyEvent& evt)
-            { guiSystem_->injectKeyDown(evt.key); guiSystem_->injectChar(evt.text); }
-        void keyReleased(const KeyEvent& evt)
-            { guiSystem_->injectKeyUp(evt.key); }
-        void keyHeld    (const KeyEvent& evt) { }
-
-        // mouseHandler functions
         void mouseButtonPressed (MouseButtonCode::ByEnum id);
         void mouseButtonReleased(MouseButtonCode::ByEnum id);
-        void mouseButtonHeld    (MouseButtonCode::ByEnum id) { }
+        void mouseButtonHeld    (MouseButtonCode::ByEnum id)
+        { }
         void mouseMoved         (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
-            { guiSystem_->injectMouseMove(rel.x, rel.y); }
+        { guiSystem_->injectMouseMove(rel.x, rel.y); }
         void mouseScrolled      (int abs, int rel)
-            { guiSystem_->injectMouseWheelChange(rel);}
+        { guiSystem_->injectMouseWheelChange(rel);}
 
-        void updateInput(float dt)  { }
-        void updateKey  (float dt)  { }
-        void updateMouse(float dt)  { }
+        void tickInput(float dt) { }
+        void tickKey(float dt) { }
+        void tickMouse(float dt) { }
+
+        void loadScenes();
+
+        //Ogre::SceneManager*       emptySceneManager_;
+        Ogre::SceneManager*       backgroundSceneManager_;
+        //Ogre::Camera*             emptyCamera_;
+        Ogre::Camera*             backgroundCamera_;
+        //Ogre::Viewport*           viewport_;
+        Ogre::RenderWindow*       renderWindow_;
+        CEGUI::OgreCEGUIRenderer* guiRenderer_;
+        CEGUI::ResourceProvider*  resourceProvider_;
+        CEGUI::LuaScriptModule*   scriptModule_;
+        CEGUI::DefaultLogger*     ceguiLogger_;
+        CEGUI::System*            guiSystem_;
+        CEGUI::Imageset*          backgroundImage_;
+        lua_State*                luaState_;
+
+        State state_;
 
         static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button);
 
-        Ogre::RenderWindow*         renderWindow_;      //!< Ogre's render window to give CEGUI access to it
-        CEGUI::OgreCEGUIRenderer*   guiRenderer_;       //!< CEGUI's interface to the Ogre Engine
-        CEGUI::ResourceProvider*    resourceProvider_;  //!< CEGUI's resource provider
-        CEGUI::LuaScriptModule*     scriptModule_;      //!< CEGUI's script module to use Lua
-        CEGUI::DefaultLogger*       ceguiLogger_;       //!< CEGUI's logger to be able to log CEGUI errors in our log
-        CEGUI::System*              guiSystem_;         //!< CEGUI's main system
-        lua_State*                  luaState_;          //!< Lua state, access point to the Lua engine
-
-        State                       state_;             //!< reflects state of the GUIManager
-
-        static GUIManager*          singletonRef_s;     //!< Singleton reference to GUIManager
+        static GUIManager*        singletonRef_s;
     }; // tolua_export
 } // tolua_export
