Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 6, 2009, 1:59:00 AM (15 years ago)
Author:
landauf
Message:

Merged gui branch back to trunk.

I did 2 small changes in IngameManager.cc on line 777 and 888 (yes, really), because const_reverse_iterator strangely doesn't work on MinGW.

Location:
code/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/orxonox/gui/GUIManager.cc

    r2790 r2896  
    2222 *   Author:
    2323 *      Reto Grieder
     24 *      Benjamin Knecht
    2425 *   Co-authors:
    25  *      ...
     26 *
    2627 *
    2728 */
     
    3637#include "GUIManager.h"
    3738
    38 #include <boost/filesystem.hpp>
     39#include <boost/filesystem/path.hpp>
    3940#include <OgreRenderWindow.h>
    40 #include <OgreRoot.h>
    4141#include <CEGUI.h>
    4242#include <CEGUIDefaultLogger.h>
     
    5050
    5151#include "util/Exception.h"
    52 #include "core/input/InputManager.h"
    53 #include "core/input/SimpleInputState.h"
    5452#include "core/ConsoleCommand.h"
    5553#include "core/Core.h"
     54#include "core/Clock.h"
    5655#include "ToluaBindCore.h"
    5756#include "ToluaBindOrxonox.h"
     
    6362namespace orxonox
    6463{
    65     SetConsoleCommandShortcut(GUIManager, showGUI_s).keybindMode(KeybindMode::OnPress);
    66 
    6764    GUIManager* GUIManager::singletonRef_s = 0;
    6865
    6966    GUIManager::GUIManager()
    70         //: emptySceneManager_(0)
    71         : backgroundSceneManager_(0)
    72         //, emptyCamera_(0)
    73         , backgroundCamera_(0)
    74         //, viewport_(0)
    75         , renderWindow_(0)
     67        : renderWindow_(0)
    7668        , guiRenderer_(0)
    7769        , resourceProvider_(0)
     
    8476    }
    8577
     78    /**
     79    @brief
     80        Deconstructor of the GUIManager
     81
     82        Basically shuts down CEGUI and destroys the Lua engine and afterwards the interface to the Ogre engine.
     83    */
    8684    GUIManager::~GUIManager()
    8785    {
    88         if (backgroundCamera_)
    89             backgroundSceneManager_->destroyCamera(backgroundCamera_);
    90 
    91         if (backgroundSceneManager_)
    92         {
    93             // We have to make sure the SceneManager is not anymore referenced.
    94             // For the case that the target SceneManager was yet another one, it
    95             // wouldn't matter anyway since this is the destructor.
    96             guiRenderer_->setTargetSceneManager(0);
    97             Ogre::Root::getSingleton().destroySceneManager(backgroundSceneManager_);
    98         }
    99 
    100         InputManager::getInstance().requestDestroyState("gui");
    101 
    10286        if (guiSystem_)
    10387            delete guiSystem_;
     
    11094                lua_pushnil(luaState_);
    11195                lua_setglobal(luaState_, "Core");
    112             // TODO: deleting the script module fails an assertion.
    113             // However there is not much we can do about it since it occurs too when
    114             // we don't open Core or Orxonox. Might be a CEGUI issue.
    115             // The memory leak is not a problem anyway..
    11696            delete scriptModule_;
    11797        }
     
    123103    }
    124104
     105    /**
     106    @brief
     107        Initialises the GUIManager by starting up CEGUI
     108    @param renderWindow
     109        Ogre's render window. Without this, the GUI cannot be displayed.
     110    @return true if success, otherwise false
     111
     112        Before this call the GUIManager won't do anything, but can be accessed.
     113
     114        Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine.
     115        The log is set up and connected to the CEGUILogger.
     116        After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code.
     117        Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically).
     118    */
    125119    bool GUIManager::initialise(Ogre::RenderWindow* renderWindow)
    126120    {
     
    135129                renderWindow_ = renderWindow;
    136130
    137                 // Full screen viewport with Z order = 0 (top most). Don't yet feed a camera (so nothing gets rendered)
    138                 //this->viewport_ = renderWindow_->addViewport(0, 3);
    139                 //this->viewport_->setOverlaysEnabled(false);
    140                 //this->viewport_->setShadowsEnabled(false);
    141                 //this->viewport_->setSkiesEnabled(false);
    142                 //this->viewport_->setClearEveryFrame(false);
    143 
    144131                // Note: No SceneManager specified yet
    145                 this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_MAIN, true, 3000);
     132                this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, true, 3000);
    146133                this->resourceProvider_ = guiRenderer_->createResourceProvider();
    147134                this->resourceProvider_->setDefaultResourceGroup("GUI");
     
    166153                tolua_Orxonox_open(this->scriptModule_->getLuaState());
    167154
    168                 // register us as input handler
    169                 SimpleInputState* state = InputManager::getInstance().createInputState<SimpleInputState>("gui", 30);
    170                 state->setHandler(this);
    171                 state->setJoyStickHandler(&InputManager::EMPTY_HANDLER);
    172 
    173                 // load the background scene
    174                 //loadScenes();
    175                 //CEGUI::KeyEventArgs e;
    176                 //e.codepoint
     155                // initialise the basic lua code
     156                loadLuaCode();
    177157            }
    178158            catch (CEGUI::Exception& ex)
     
    192172    }
    193173
    194     void GUIManager::loadScene(const std::string& name)
    195     {
    196         if (name.compare("IngameMenu") == 0)
    197         {
    198             try
    199             {
    200                 /*this->scriptModule_ = new LuaScriptModule();
    201                 this->luaState_ = this->scriptModule_->getLuaState();
    202                 this->guiSystem_ = new System(this->guiRenderer_, this->resourceProvider_, 0, this->scriptModule_);
    203                 tolua_Core_open(this->scriptModule_->getLuaState());
    204                 tolua_Orxonox_open(this->scriptModule_->getLuaState());
    205                 */
    206                 this->scriptModule_->executeScriptFile("ingameGUI.lua", "GUI");
    207             }
    208             catch (CEGUI::Exception& ex)
    209             {
    210 #if CEGUI_VERSION_MINOR < 6
    211                 throw GeneralException(ex.getMessage().c_str());
    212 #else
    213                 throw GeneralException(ex.getMessage().c_str(), ex.getLine(),
    214                                        ex.getFileName().c_str(), ex.getName().c_str());
    215 #endif
    216             }
    217         }
    218         else
    219         {
    220             loadScenes();
    221         }
    222     }
    223 
    224     void GUIManager::loadScenes()
    225     {
    226         // first of all, we need to have our own SceneManager for the GUI. The reason
    227         // is that we might have multiple viewports when in play mode (e.g. the view of
    228         // a camera fixed at the back of the ship). That forces us to create our own
    229         // full screen viewport that is on top of all the others, but doesn't clear the
    230         // port before rendering, so everything from the GUI gets on top eventually.
    231         // But in order to realise that, we also need a SceneManager with an empty scene,
    232         // because the SceneManager is responsible for the render queue.
    233         //this->emptySceneManager_ = Ogre::Root::getSingleton()
    234         //    .createSceneManager(Ogre::ST_GENERIC, "GUI/EmptySceneManager");
    235 
    236         // we also need a camera or we won't see anything at all.
    237         // The camera settings don't matter at all for an empty scene since the GUI
    238         // gets rendered on top of the screen rather than into the scene.
    239         //this->emptyCamera_ = this->emptySceneManager_->createCamera("GUI/EmptyCamera");
    240 
    241         // Create another SceneManager that enables to display some 3D
    242         // scene in the background of the main menu.
    243         this->backgroundSceneManager_ = Ogre::Root::getSingleton()
    244             .createSceneManager(Ogre::ST_GENERIC, "GUI/BackgroundSceneManager");
    245         this->backgroundCamera_ = backgroundSceneManager_->createCamera("GUI/BackgroundCamera");
    246 
    247         // TODO: create something 3D
     174    /**
     175    @brief
     176        Calls main Lua script
     177    @todo
     178        Replace loadGUI.lua with loadGUI_2.lua after merging this back to trunk.
     179        However CEGUI is able to execute a startup script. We could maybe put this call in this startup code.
     180
     181        This function calls the main Lua script for our GUI.
     182
     183        Additionally we set the datapath variable in Lua. This is needed so Lua can access the data used for the GUI.
     184    */
     185    void GUIManager::loadLuaCode()
     186    {
    248187        try
    249188        {
    250             this->scriptModule_->executeScriptFile("loadGUI.lua", "GUI");
     189            // call main Lua script
     190            this->scriptModule_->executeScriptFile("loadGUI_2.lua", "GUI");
     191            // set datapath for GUI data
     192            lua_pushfstring(this->scriptModule_->getLuaState(), Core::getMediaPathString().c_str());
     193            lua_setglobal(this->scriptModule_->getLuaState(), "datapath");
    251194        }
    252195        catch (CEGUI::Exception& ex)
     
    261204    }
    262205
    263     void GUIManager::showGUI(const std::string& name, Ogre::SceneManager* sceneManager)// bool showBackground)
     206    /**
     207    @brief
     208        used to tick the GUI
     209    @param time
     210        clock which provides time value for the GUI System
     211
     212        Ticking the GUI means updating it with a certain regularity.
     213        The elapsed time since the last call is given in the time value provided by the clock.
     214        This time value is then used to provide a fluent animation of the GUI.
     215    */
     216    void GUIManager::update(const Clock& time)
     217    {
     218        assert(guiSystem_);
     219        guiSystem_->injectTimePulse(time.getDeltaTime());
     220    }
     221
     222    /**
     223    @brief
     224        Executes Lua code
     225    @param str
     226        reference to string object holding the Lua code which is to be executed
     227
     228        This function gives total access to the GUI. You can execute ANY Lua code here.
     229    */
     230    void GUIManager::executeCode(const std::string& str)
     231    {
     232        try
     233        {
     234            this->scriptModule_->executeString(str);
     235        }
     236        catch (CEGUI::Exception& ex)
     237        {
     238            COUT(2) << "CEGUI Error: \"" << ex.getMessage() << "\" while executing code \"" << str << "\"" << std::endl;
     239        }
     240    }
     241
     242    /**
     243    @brief
     244        Tells the GUIManager which SceneManager to use
     245    @param camera
     246        The current camera on which the GUI should be displayed on.
     247
     248        In fact the GUIManager needs the SceneManager and not the Camera to display the GUI.
     249        This means the GUI is not bound to a camera but rather to the SceneManager.
     250        Hidding the GUI when needed can therefore not be solved by just NOT setting the current camera.
     251    */
     252    void GUIManager::setCamera(Ogre::Camera* camera)
     253    {
     254        this->guiRenderer_->setTargetSceneManager(camera->getSceneManager());
     255    }
     256
     257    /**
     258    @brief
     259        Displays specified GUI on screen
     260    @param name
     261        The name of the GUI
     262
     263        The function executes the Lua function with the same name in case the GUIManager is ready.
     264        For more details check out loadGUI_2.lua where the function presides.
     265    */
     266    void GUIManager::showGUI(const std::string& name)
    264267    {
    265268        if (state_ != Uninitialised)
    266269        {
    267             if (state_ == OnDisplay)
    268                 hideGUI();
    269 
    270             COUT(3) << "Loading GUI " << name << std::endl;
     270            //COUT(3) << "Loading GUI " << name << std::endl;
    271271            try
    272272            {
    273                 COUT (0) << "************* sceneManager: " << sceneManager << std::endl;
    274                 if (!sceneManager)
    275                 {
    276                     // currently, only an image is loaded. We could do 3D, see loadBackground.
    277                     //this->viewport_->setClearEveryFrame(true);
    278                     this->guiRenderer_->setTargetSceneManager(this->backgroundSceneManager_);
    279                     //this->viewport_->setCamera(this->backgroundCamera_);
    280 
    281                     lua_pushboolean(this->scriptModule_->getLuaState(), true);
    282                     lua_setglobal(this->scriptModule_->getLuaState(), "showBackground");
    283                 }
    284                 else
    285                 {
    286                     //this->viewport_->setClearEveryFrame(false);
    287                     this->guiRenderer_->setTargetSceneManager(sceneManager);
    288                     //this->viewport_->setCamera(this->emptyCamera_);
    289 
    290                     lua_pushboolean(this->scriptModule_->getLuaState(), false);
    291                     lua_setglobal(this->scriptModule_->getLuaState(), "showBackground");
    292                 }
    293 
    294                 this->scriptModule_->executeScriptGlobal("showMainMenu");
    295 
    296                 InputManager::getInstance().requestEnterState("gui");
    297 
    298                 this->state_ = OnDisplay;
     273                this->scriptModule_->executeString(std::string("showGUI(\"") + name + "\")");
    299274            }
    300275            catch (CEGUI::Exception& ex)
     
    313288    }
    314289
    315     void GUIManager::hideGUI()
    316     {
    317         if (this->state_ != OnDisplay)
    318             return;
    319         //this->viewport_->setCamera(0);
    320         // has no effect since you cannot assign 0 as SceneManager
    321         //this->guiRenderer_->setTargetSceneManager(0);
    322         this->state_ = Ready;
    323         InputManager::getInstance().requestLeaveState("gui");
    324     }
    325 
     290    /**
     291    @brief
     292        Function receiving a mouse button pressed event.
     293    @param id
     294        ID of the mouse button which got pressed
     295
     296        This function is inherited by MouseHandler and injects the event into CEGUI.
     297        It is for CEGUI to process the event.
     298    */
    326299    void GUIManager::mouseButtonPressed(MouseButtonCode::ByEnum id)
    327300    {
     
    337310    }
    338311
     312    /**
     313    @brief
     314        Function receiving a mouse button released event.
     315    @param id
     316        ID of the mouse button which got released
     317
     318        This function is inherited by MouseHandler and injects the event into CEGUI.
     319        It is for CEGUI to process the event.
     320    */
    339321    void GUIManager::mouseButtonReleased(MouseButtonCode::ByEnum id)
    340322    {
     
    350332    }
    351333
    352 
     334    /**
     335    @brief
     336        converts mouse event code to CEGUI event code
     337    @param button
     338        code of the mouse button as we use it in Orxonox
     339    @return
     340        code of the mouse button as it is used by CEGUI
     341
     342        Simple convertion from mouse event code in Orxonox to the one used in CEGUI.
     343     */
    353344    inline CEGUI::MouseButton GUIManager::convertButton(MouseButtonCode::ByEnum button)
    354345    {
  • code/trunk/src/orxonox/gui/GUIManager.h

    r2790 r2896  
    2323 *      Reto Grieder
    2424 *   Co-authors:
    25  *      ...
     25 *      Benjamin Knecht
    2626 *
    2727 */
     
    4949{
    5050    /**
     51    @class GUIManager
    5152    @brief
    52         Provides a simple interface to CEGUI with tolua methods and console commands
     53        Provides a simple interface to CEGUI with tolua methods and console commands. It also acts as a key and mouse handler.
     54
     55        The GUIManager is a singleton and can be called anywhere when access on the GUI is needed.
     56        Creation of the GUIManager is therefore not possible and the cunstructor is private.
     57
     58        Since the GUI needs user input, the GUIManager implements the functions needed to act as a key and/or mouse handler.
     59        Those input events are then injected into CEGUI in Lua.
    5360    */
    5461    class _OrxonoxExport GUIManager
     
    5966// tolua_end
    6067    public:
     68        /**
     69        @enum State
     70            The current state of the GUIManager. There should maybe be more (or we can omit this totally).
     71        */
    6172        enum State
    6273        {
    63             Uninitialised,
    64             Ready,
    65             OnDisplay
     74            Uninitialised,  //!< Initial state of the GUIManager
     75            Ready,          //!< State after initialisation if ready
     76            OnDisplay       //!< State if GUI is displayed
    6677        };
    6778
     
    7081
    7182        bool initialise(Ogre::RenderWindow* renderWindow);
    72         void loadScene(const std::string& name);
    73         void tick(float dt)
    74         {
    75             assert(guiSystem_);
    76             guiSystem_->injectTimePulse(dt);
    77         }
    78         void showGUI(const std::string& name, Ogre::SceneManager* sceneManager);// bool showBackground); // tolua_export
    79         void hideGUI(); // tolua_export
    8083
    81         Ogre::Camera* getCamera() { return this->backgroundCamera_; }
     84        void update(const Clock& time);
    8285
    83         static void showGUI_s(const std::string& name, Ogre::SceneManager* sceneManager)//bool showBackground)
    84         {
    85             getInstance().showGUI(name, sceneManager);
    86         }
     86        void showGUI(const std::string& name);
     87        void executeCode(const std::string& str);
     88
     89        void setCamera(Ogre::Camera* camera);
    8790
    8891        static GUIManager& getInstance()    { assert(singletonRef_s); return *singletonRef_s; } // tolua_export
     
    9093
    9194    private:
    92         GUIManager(const GUIManager& instance);
     95        GUIManager(const GUIManager& instance);                 //!< private constructor (this is a singleton class)
    9396
     97        void loadLuaCode();
     98
     99        // keyHandler functions
    94100        void keyPressed (const KeyEvent& evt)
    95         { guiSystem_->injectKeyDown(evt.key); guiSystem_->injectChar(evt.text); }
     101            { guiSystem_->injectKeyDown(evt.key); guiSystem_->injectChar(evt.text); }
    96102        void keyReleased(const KeyEvent& evt)
    97         { guiSystem_->injectKeyUp(evt.key); }
    98         void keyHeld    (const KeyEvent& evt)
    99         { }
     103            { guiSystem_->injectKeyUp(evt.key); }
     104        void keyHeld    (const KeyEvent& evt) { }
    100105
     106        // mouseHandler functions
    101107        void mouseButtonPressed (MouseButtonCode::ByEnum id);
    102108        void mouseButtonReleased(MouseButtonCode::ByEnum id);
    103         void mouseButtonHeld    (MouseButtonCode::ByEnum id)
    104         { }
     109        void mouseButtonHeld    (MouseButtonCode::ByEnum id) { }
    105110        void mouseMoved         (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
    106         { guiSystem_->injectMouseMove(rel.x, rel.y); }
     111            { guiSystem_->injectMouseMove(rel.x, rel.y); }
    107112        void mouseScrolled      (int abs, int rel)
    108         { guiSystem_->injectMouseWheelChange(rel);}
     113            { guiSystem_->injectMouseWheelChange(rel);}
    109114
    110         void tickInput(float dt) { }
    111         void tickKey(float dt) { }
    112         void tickMouse(float dt) { }
    113 
    114         void loadScenes();
    115 
    116         //Ogre::SceneManager*       emptySceneManager_;
    117         Ogre::SceneManager*       backgroundSceneManager_;
    118         //Ogre::Camera*             emptyCamera_;
    119         Ogre::Camera*             backgroundCamera_;
    120         //Ogre::Viewport*           viewport_;
    121         Ogre::RenderWindow*       renderWindow_;
    122         CEGUI::OgreCEGUIRenderer* guiRenderer_;
    123         CEGUI::ResourceProvider*  resourceProvider_;
    124         CEGUI::LuaScriptModule*   scriptModule_;
    125         CEGUI::DefaultLogger*     ceguiLogger_;
    126         CEGUI::System*            guiSystem_;
    127         CEGUI::Imageset*          backgroundImage_;
    128         lua_State*                luaState_;
    129 
    130         State state_;
     115        void updateInput(float dt)  { }
     116        void updateKey  (float dt)  { }
     117        void updateMouse(float dt)  { }
    131118
    132119        static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button);
    133120
    134         static GUIManager*        singletonRef_s;
     121        Ogre::RenderWindow*         renderWindow_;      //!< Ogre's render window to give CEGUI access to it
     122        CEGUI::OgreCEGUIRenderer*   guiRenderer_;       //!< CEGUI's interface to the Ogre Engine
     123        CEGUI::ResourceProvider*    resourceProvider_;  //!< CEGUI's resource provider
     124        CEGUI::LuaScriptModule*     scriptModule_;      //!< CEGUI's script module to use Lua
     125        CEGUI::DefaultLogger*       ceguiLogger_;       //!< CEGUI's logger to be able to log CEGUI errors in our log
     126        CEGUI::System*              guiSystem_;         //!< CEGUI's main system
     127        lua_State*                  luaState_;          //!< Lua state, access point to the Lua engine
     128
     129        State                       state_;             //!< reflects state of the GUIManager
     130
     131        static GUIManager*          singletonRef_s;     //!< Singleton reference to GUIManager
    135132    }; // tolua_export
    136133} // tolua_export
Note: See TracChangeset for help on using the changeset viewer.