Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3012 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
Line 
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
24 *      Benjamin Knecht
25 *   Co-authors:
26 *
27 *
28 */
29
30/**
31    @file
32    @brief
33        Implementation of the GUIManager class.
34*/
35
36#include "OrxonoxStableHeaders.h"
37#include "GUIManager.h"
38
39#include <boost/filesystem/path.hpp>
40#include <OgreRenderWindow.h>
41#include <CEGUI.h>
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
51#include "util/Exception.h"
52#include "core/ConsoleCommand.h"
53#include "core/Core.h"
54#include "core/Clock.h"
55#include "ToluaBindCore.h"
56#include "ToluaBindOrxonox.h"
57#include "core/Loader.h"
58
59extern "C" {
60#include <lua.h>
61}
62
63namespace orxonox
64{
65    GUIManager* GUIManager::singletonRef_s = 0;
66
67    GUIManager::GUIManager()
68        : renderWindow_(0)
69        , guiRenderer_(0)
70        , resourceProvider_(0)
71        , scriptModule_(0)
72        , guiSystem_(0)
73        , state_(Uninitialised)
74    {
75        assert(singletonRef_s == 0);
76        singletonRef_s = this;
77    }
78
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    */
85    GUIManager::~GUIManager()
86    {
87        if (guiSystem_)
88            delete guiSystem_;
89
90        if (scriptModule_)
91        {
92            // destroy our own tolua interfaces
93                lua_pushnil(luaState_);
94                lua_setglobal(luaState_, "Orxonox");
95                lua_pushnil(luaState_);
96                lua_setglobal(luaState_, "Core");
97            delete scriptModule_;
98        }
99
100        if (guiRenderer_)
101            delete guiRenderer_;
102
103        singletonRef_s = 0;
104    }
105
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    */
120    bool GUIManager::initialise(Ogre::RenderWindow* renderWindow)
121    {
122        using namespace CEGUI;
123        if (state_ == Uninitialised)
124        {
125            COUT(3) << "Initialising CEGUI." << std::endl;
126
127            try
128            {
129                // save the render window
130                renderWindow_ = renderWindow;
131
132                // Note: No SceneManager specified yet
133                this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, true, 3000);
134                this->resourceProvider_ = guiRenderer_->createResourceProvider();
135                this->resourceProvider_->setDefaultResourceGroup("GUI");
136
137                // setup scripting
138                this->scriptModule_ = new LuaScriptModule();
139                this->luaState_ = this->scriptModule_->getLuaState();
140
141                // Create our own logger to specify the filepath
142                boost::filesystem::path ceguiLogFilepath(Core::getLogPath() / "cegui.log");
143                this->ceguiLogger_ = new DefaultLogger();
144                this->ceguiLogger_->setLogFilename(ceguiLogFilepath.string());
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
149                // create the CEGUI system singleton
150                this->guiSystem_ = new System(this->guiRenderer_, this->resourceProvider_, 0, this->scriptModule_);
151
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
156                // initialise the basic lua code
157                loadLuaCode();
158            }
159            catch (CEGUI::Exception& ex)
160            {
161#if CEGUI_VERSION_MAJOR == 0 && CEGUI_VERSION_MINOR < 6
162                throw GeneralException(ex.getMessage().c_str());
163#else
164                throw GeneralException(ex.getMessage().c_str(), ex.getLine(),
165                    ex.getFileName().c_str(), ex.getName().c_str());
166#endif
167            }
168
169            state_ = Ready;
170        }
171
172        return true;
173    }
174
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()
187    {
188        try
189        {
190            // set datapath for GUI data
191            lua_pushfstring(this->scriptModule_->getLuaState(), Core::getMediaPathString().c_str());
192            lua_setglobal(this->scriptModule_->getLuaState(), "datapath");
193            // call main Lua script
194            this->scriptModule_->executeScriptFile("loadGUI_3.lua", "GUI");
195        }
196        catch (CEGUI::Exception& ex)
197        {
198#if CEGUI_VERSION_MINOR < 6
199            throw GeneralException(ex.getMessage().c_str());
200#else
201            throw GeneralException(ex.getMessage().c_str(), ex.getLine(),
202                ex.getFileName().c_str(), ex.getName().c_str());
203#endif
204        }
205    }
206
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)
218    {
219        assert(guiSystem_);
220        guiSystem_->injectTimePulse(time.getDeltaTime());
221    }
222
223    /**
224    @brief
225        Executes Lua code
226    @param str
227        reference to string object holding the Lua code which is to be executed
228
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    {
233        try
234        {
235            this->scriptModule_->executeString(str);
236        }
237        catch (CEGUI::Exception& ex)
238        {
239            COUT(2) << "CEGUI Error: \"" << ex.getMessage() << "\" while executing code \"" << str << "\"" << std::endl;
240        }
241    }
242
243    /**
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    /**
265    @brief
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
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)
303    {
304        if (camera == NULL)
305            this->guiRenderer_->setTargetSceneManager(0);
306        else
307            this->guiRenderer_->setTargetSceneManager(camera->getSceneManager());
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    {
321        if (state_ != Uninitialised)
322        {
323            //COUT(3) << "Loading GUI " << name << std::endl;
324            try
325            {
326                this->scriptModule_->executeString(std::string("showGUI(\"") + name + "\")");
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
343    /**
344    @brief
345        Function receiving a mouse button pressed event.
346    @param id
347        ID of the mouse button which got pressed
348
349        This function is inherited by MouseHandler and injects the event into CEGUI.
350        It is for CEGUI to process the event.
351    */
352    void GUIManager::mouseButtonPressed(MouseButtonCode::ByEnum id)
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
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    */
374    void GUIManager::mouseButtonReleased(MouseButtonCode::ByEnum id)
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
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
394
395        Simple convertion from mouse event code in Orxonox to the one used in CEGUI.
396     */
397    inline CEGUI::MouseButton GUIManager::convertButton(MouseButtonCode::ByEnum button)
398    {
399        switch (button)
400        {
401        case MouseButtonCode::Left:
402            return CEGUI::LeftButton;
403
404        case MouseButtonCode::Right:
405            return CEGUI::RightButton;
406
407        case MouseButtonCode::Middle:
408            return CEGUI::MiddleButton;
409
410        case MouseButtonCode::Button3:
411            return CEGUI::X1Button;
412
413        case MouseButtonCode::Button4:
414            return CEGUI::X2Button;
415
416        default:
417            return CEGUI::NoButton;
418        }
419    }
420}
Note: See TracBrowser for help on using the repository browser.