Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gui/GUIManager.cc @ 3008

Last change on this file since 3008 was 3008, checked in by bknecht, 16 years ago

You don't need no —level or -l anymore now. You may choose your favorite level from the main menu ;-)

  • Property svn:eol-style set to native
File size: 12.8 KB
RevLine 
[1638]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
[2896]24 *      Benjamin Knecht
[1638]25 *   Co-authors:
26 *
[2896]27 *
[1638]28 */
29
30/**
31    @file
[1653]32    @brief
33        Implementation of the GUIManager class.
[1638]34*/
35
36#include "OrxonoxStableHeaders.h"
37#include "GUIManager.h"
38
[2896]39#include <boost/filesystem/path.hpp>
[1638]40#include <OgreRenderWindow.h>
41#include <CEGUI.h>
[2710]42#include <CEGUIDefaultLogger.h>
43#include <ogreceguirenderer/OgreCEGUIRenderer.h>
44#include "SpecialConfig.h" // Configures the macro below
45#ifdef CEGUILUA_USE_INTERNAL_LIBRARY
46#   include <ceguilua/CEGUILua.h>
47#else
48#   include <CEGUILua.h>
49#endif
50
[1764]51#include "util/Exception.h"
[1638]52#include "core/ConsoleCommand.h"
53#include "core/Core.h"
[2896]54#include "core/Clock.h"
[2710]55#include "ToluaBindCore.h"
56#include "ToluaBindOrxonox.h"
[3008]57#include "core/Loader.h"
[1638]58
[2710]59extern "C" {
60#include <lua.h>
61}
[1638]62
63namespace orxonox
64{
[1646]65    GUIManager* GUIManager::singletonRef_s = 0;
66
[1638]67    GUIManager::GUIManager()
[2896]68        : renderWindow_(0)
[1638]69        , guiRenderer_(0)
70        , resourceProvider_(0)
71        , scriptModule_(0)
72        , guiSystem_(0)
73        , state_(Uninitialised)
74    {
[1646]75        assert(singletonRef_s == 0);
76        singletonRef_s = this;
[1638]77    }
78
[2896]79    /**
80    @brief
81        Deconstructor of the GUIManager
82
83        Basically shuts down CEGUI and destroys the Lua engine and afterwards the interface to the Ogre engine.
84    */
[1638]85    GUIManager::~GUIManager()
86    {
[1646]87        if (guiSystem_)
88            delete guiSystem_;
89
90        if (scriptModule_)
91        {
92            // destroy our own tolua interfaces
[2662]93                lua_pushnil(luaState_);
94                lua_setglobal(luaState_, "Orxonox");
95                lua_pushnil(luaState_);
96                lua_setglobal(luaState_, "Core");
97            delete scriptModule_;
[1646]98        }
99
100        if (guiRenderer_)
101            delete guiRenderer_;
102
103        singletonRef_s = 0;
[1638]104    }
105
[2896]106    /**
107    @brief
108        Initialises the GUIManager by starting up CEGUI
109    @param renderWindow
110        Ogre's render window. Without this, the GUI cannot be displayed.
111    @return true if success, otherwise false
112
113        Before this call the GUIManager won't do anything, but can be accessed.
114
115        Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine.
116        The log is set up and connected to the CEGUILogger.
117        After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code.
118        Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically).
119    */
[1686]120    bool GUIManager::initialise(Ogre::RenderWindow* renderWindow)
[1638]121    {
122        using namespace CEGUI;
123        if (state_ == Uninitialised)
124        {
[1776]125            COUT(3) << "Initialising CEGUI." << std::endl;
[1638]126
127            try
128            {
[1686]129                // save the render window
130                renderWindow_ = renderWindow;
[1638]131
132                // Note: No SceneManager specified yet
[2896]133                this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, true, 3000);
[1638]134                this->resourceProvider_ = guiRenderer_->createResourceProvider();
135                this->resourceProvider_->setDefaultResourceGroup("GUI");
[1776]136
[1638]137                // setup scripting
138                this->scriptModule_ = new LuaScriptModule();
[1646]139                this->luaState_ = this->scriptModule_->getLuaState();
[1638]140
[2710]141                // Create our own logger to specify the filepath
142                boost::filesystem::path ceguiLogFilepath(Core::getLogPath() / "cegui.log");
143                this->ceguiLogger_ = new DefaultLogger();
[2759]144                this->ceguiLogger_->setLogFilename(ceguiLogFilepath.string());
[2710]145                // set the log level according to ours (translate by subtracting 1)
146                this->ceguiLogger_->setLoggingLevel(
147                    (LoggingLevel)(Core::getSoftDebugLevel(OutputHandler::LD_Logfile) - 1));
148
[1638]149                // create the CEGUI system singleton
150                this->guiSystem_ = new System(this->guiRenderer_, this->resourceProvider_, 0, this->scriptModule_);
[1776]151
[1638]152                // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place
153                tolua_Core_open(this->scriptModule_->getLuaState());
154                tolua_Orxonox_open(this->scriptModule_->getLuaState());
155
[2896]156                // initialise the basic lua code
157                loadLuaCode();
[1638]158            }
159            catch (CEGUI::Exception& ex)
160            {
[1653]161#if CEGUI_VERSION_MAJOR == 0 && CEGUI_VERSION_MINOR < 6
[1645]162                throw GeneralException(ex.getMessage().c_str());
163#else
[1638]164                throw GeneralException(ex.getMessage().c_str(), ex.getLine(),
165                    ex.getFileName().c_str(), ex.getName().c_str());
[1645]166#endif
[1638]167            }
168
169            state_ = Ready;
170        }
[1776]171
[1638]172        return true;
173    }
174
[2896]175    /**
176    @brief
177        Calls main Lua script
178    @todo
179        Replace loadGUI.lua with loadGUI_2.lua after merging this back to trunk.
180        However CEGUI is able to execute a startup script. We could maybe put this call in this startup code.
181
182        This function calls the main Lua script for our GUI.
183
184        Additionally we set the datapath variable in Lua. This is needed so Lua can access the data used for the GUI.
185    */
186    void GUIManager::loadLuaCode()
[2790]187    {
[2896]188        try
[2790]189        {
[2896]190            // set datapath for GUI data
191            lua_pushfstring(this->scriptModule_->getLuaState(), Core::getMediaPathString().c_str());
192            lua_setglobal(this->scriptModule_->getLuaState(), "datapath");
[2957]193            // call main Lua script
194            this->scriptModule_->executeScriptFile("loadGUI_3.lua", "GUI");
[2896]195        }
196        catch (CEGUI::Exception& ex)
197        {
[2790]198#if CEGUI_VERSION_MINOR < 6
[2896]199            throw GeneralException(ex.getMessage().c_str());
[2790]200#else
[2896]201            throw GeneralException(ex.getMessage().c_str(), ex.getLine(),
202                ex.getFileName().c_str(), ex.getName().c_str());
[2790]203#endif
204        }
205    }
206
[2896]207    /**
208    @brief
209        used to tick the GUI
210    @param time
211        clock which provides time value for the GUI System
212
213        Ticking the GUI means updating it with a certain regularity.
214        The elapsed time since the last call is given in the time value provided by the clock.
215        This time value is then used to provide a fluent animation of the GUI.
216    */
217    void GUIManager::update(const Clock& time)
[1638]218    {
[2896]219        assert(guiSystem_);
220        guiSystem_->injectTimePulse(time.getDeltaTime());
221    }
[1638]222
[2896]223    /**
224    @brief
225        Executes Lua code
226    @param str
227        reference to string object holding the Lua code which is to be executed
[1638]228
[2896]229        This function gives total access to the GUI. You can execute ANY Lua code here.
230    */
231    void GUIManager::executeCode(const std::string& str)
232    {
[1638]233        try
234        {
[2896]235            this->scriptModule_->executeString(str);
[1638]236        }
237        catch (CEGUI::Exception& ex)
238        {
[2896]239            COUT(2) << "CEGUI Error: \"" << ex.getMessage() << "\" while executing code \"" << str << "\"" << std::endl;
[1638]240        }
241    }
242
[2896]243    /**
[3008]244
245    */
246    void GUIManager::getLevelList()
247    {
248        lua_State* L = this->scriptModule_->getLuaState();
249        lua_newtable(L);
250
251        std::vector<std::string> list = Loader::getLevelList();
252
253        int j = 1;
254        for (std::vector<std::string>::iterator i = list.begin(); i != list.end(); i++)
255        {
256            lua_pushnumber(L,j);
257            lua_pushstring(L,i->c_str());
258            lua_settable(L,-3);
259            j++;
260        }
261        lua_setglobal(L, "levellist");
262    }
263
264    /**
[2896]265    @brief
[2963]266        Registers a GUIOverlay with the GUIManager so that the GUIOverlay can be accessed by it's name through the GUIManager.
267    @param name
268        The name of the GUI.
269    @param overlay
270        A pointer to the GUIOverlay of the GUI.
271    @return
272        Returns false if the Overlay was already present.
273    */
274    bool GUIManager::registerOverlay(std::string name, GUIOverlay* overlay)
275    {
276        return (this->guiOverlays_.insert(std::pair<std::string, GUIOverlay*>(name, overlay))).second;
277    }
278
279    /**
280    @brief
281        Get the GUIOverlay of the GUI with the given name.
282    @param name
283        The name of the GUI.
284    @return
285        Returns a pointer to the GUIOverlay.
286    */
287    GUIOverlay* GUIManager::getOverlay(std::string name)
288    {
289        return (this->guiOverlays_.find(name))->second;
290    }
291
292    /**
293    @brief
[2896]294        Tells the GUIManager which SceneManager to use
295    @param camera
296        The current camera on which the GUI should be displayed on.
297
298        In fact the GUIManager needs the SceneManager and not the Camera to display the GUI.
299        This means the GUI is not bound to a camera but rather to the SceneManager.
300        Hidding the GUI when needed can therefore not be solved by just NOT setting the current camera.
301    */
302    void GUIManager::setCamera(Ogre::Camera* camera)
[1638]303    {
[2927]304        if (camera == NULL)
305            this->guiRenderer_->setTargetSceneManager(0);
306        else
307            this->guiRenderer_->setTargetSceneManager(camera->getSceneManager());
[2896]308    }
309
310    /**
311    @brief
312        Displays specified GUI on screen
313    @param name
314        The name of the GUI
315
316        The function executes the Lua function with the same name in case the GUIManager is ready.
317        For more details check out loadGUI_2.lua where the function presides.
318    */
319    void GUIManager::showGUI(const std::string& name)
320    {
[1638]321        if (state_ != Uninitialised)
322        {
[2896]323            //COUT(3) << "Loading GUI " << name << std::endl;
[1638]324            try
325            {
[2896]326                this->scriptModule_->executeString(std::string("showGUI(\"") + name + "\")");
[1638]327            }
328            catch (CEGUI::Exception& ex)
329            {
330                COUT(2) << "Error while executing lua script. Message:\n" << ex.getMessage() << std::endl;
331            }
332            catch (...)
333            {
334                COUT(2) << "Could show a menu due to unknown reasons." << std::endl;
335            }
336        }
337        else
338        {
339            COUT(2) << "Warning: GUI Manager not yet initialised, cannot load a GUI" << std::endl;
340        }
341    }
342
[2896]343    /**
344    @brief
345        Function receiving a mouse button pressed event.
346    @param id
347        ID of the mouse button which got pressed
[1638]348
[2896]349        This function is inherited by MouseHandler and injects the event into CEGUI.
350        It is for CEGUI to process the event.
351    */
[1887]352    void GUIManager::mouseButtonPressed(MouseButtonCode::ByEnum id)
[1638]353    {
354        try
355        {
356            guiSystem_->injectMouseButtonDown(convertButton(id));
357        }
358        catch (CEGUI::ScriptException& ex)
359        {
360            // We simply ignore the exception and proceed
361            COUT(1) << ex.getMessage() << std::endl;
362        }
363    }
364
[2896]365    /**
366    @brief
367        Function receiving a mouse button released event.
368    @param id
369        ID of the mouse button which got released
370
371        This function is inherited by MouseHandler and injects the event into CEGUI.
372        It is for CEGUI to process the event.
373    */
[1887]374    void GUIManager::mouseButtonReleased(MouseButtonCode::ByEnum id)
[1638]375    {
376        try
377        {
378            guiSystem_->injectMouseButtonUp(convertButton(id));
379        }
380        catch (CEGUI::ScriptException& ex)
381        {
382            // We simply ignore the exception and proceed
383            COUT(1) << ex.getMessage() << std::endl;
384        }
385    }
386
[2896]387    /**
388    @brief
389        converts mouse event code to CEGUI event code
390    @param button
391        code of the mouse button as we use it in Orxonox
392    @return
393        code of the mouse button as it is used by CEGUI
[1638]394
[2896]395        Simple convertion from mouse event code in Orxonox to the one used in CEGUI.
396     */
[1887]397    inline CEGUI::MouseButton GUIManager::convertButton(MouseButtonCode::ByEnum button)
[1638]398    {
399        switch (button)
400        {
[1887]401        case MouseButtonCode::Left:
[1638]402            return CEGUI::LeftButton;
403
[1887]404        case MouseButtonCode::Right:
[1638]405            return CEGUI::RightButton;
406
[1887]407        case MouseButtonCode::Middle:
[1638]408            return CEGUI::MiddleButton;
409
[1887]410        case MouseButtonCode::Button3:
[1638]411            return CEGUI::X1Button;
412
[1887]413        case MouseButtonCode::Button4:
[1638]414            return CEGUI::X2Button;
415
416        default:
417            return CEGUI::NoButton;
418        }
419    }
420}
Note: See TracBrowser for help on using the repository browser.