Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1849 was 1810, checked in by rgrieder, 16 years ago

merged ceguilua branch back to trunk

  • Property svn:eol-style set to native
File size: 11.4 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 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file
31    @brief
32        Implementation of the GUIManager class.
33*/
34
35#include "OrxonoxStableHeaders.h"
36#include "GUIManager.h"
37
38#include <OgreRenderWindow.h>
39#include <OgreRoot.h>
40#include <CEGUI.h>
41#include "ceguilua/CEGUILua.h"
42#include "util/Exception.h"
43#include "core/input/InputManager.h"
44#include "core/input/SimpleInputState.h"
45#include "core/tolua/tolua_bind.h"
46#include "core/ConsoleCommand.h"
47#include "core/Core.h"
48#include "tolua/tolua_bind.h"
49#include "GraphicsEngine.h"
50#include "OgreCEGUIRenderer.h"
51
52#include "lua/lua.hpp"
53
54namespace orxonox
55{
56    SetConsoleCommandShortcut(GUIManager, showGUI_s).keybindMode(KeybindMode::OnPress);
57
58    GUIManager* GUIManager::singletonRef_s = 0;
59
60    GUIManager::GUIManager()
61        //: emptySceneManager_(0)
62        : backgroundSceneManager_(0)
63        //, emptyCamera_(0)
64        , backgroundCamera_(0)
65        //, viewport_(0)
66        , renderWindow_(0)
67        , guiRenderer_(0)
68        , resourceProvider_(0)
69        , scriptModule_(0)
70        , guiSystem_(0)
71        , state_(Uninitialised)
72    {
73        assert(singletonRef_s == 0);
74        singletonRef_s = this;
75    }
76
77    GUIManager::~GUIManager()
78    {
79        if (backgroundCamera_)
80            backgroundSceneManager_->destroyCamera(backgroundCamera_);
81
82        if (backgroundSceneManager_)
83        {
84            // We have to make sure the SceneManager is not anymore referenced.
85            // For the case that the target SceneManager was yet another one, it
86            // wouldn't matter anyway since this is the destructor.
87            guiRenderer_->setTargetSceneManager(0);
88            Ogre::Root::getSingleton().destroySceneManager(backgroundSceneManager_);
89        }
90
91        InputManager::getInstance().requestDestroyState("gui");
92
93        if (guiSystem_)
94            delete guiSystem_;
95
96        if (scriptModule_)
97        {
98            // destroy our own tolua interfaces
99                //lua_pushnil(luaState_);
100                //lua_setglobal(luaState_, "Orxonox");
101                //lua_pushnil(luaState_);
102                //lua_setglobal(luaState_, "Core");
103            // TODO: deleting the script module fails an assertation.
104            // However there is not much we can do about it since it occurs too when
105            // we don't open Core or Orxonox. Might be a CEGUI issue.
106            // The memory leak is not a problem anyway..
107            //delete scriptModule_;
108        }
109
110        if (guiRenderer_)
111            delete guiRenderer_;
112
113        singletonRef_s = 0;
114    }
115
116    bool GUIManager::initialise(Ogre::RenderWindow* renderWindow)
117    {
118        using namespace CEGUI;
119        if (state_ == Uninitialised)
120        {
121            COUT(3) << "Initialising CEGUI." << std::endl;
122
123            try
124            {
125                // save the render window
126                renderWindow_ = renderWindow;
127
128                // Full screen viewport with Z order = 0 (top most). Don't yet feed a camera (so nothing gets rendered)
129                //this->viewport_ = renderWindow_->addViewport(0, 3);
130                //this->viewport_->setOverlaysEnabled(false);
131                //this->viewport_->setShadowsEnabled(false);
132                //this->viewport_->setSkiesEnabled(false);
133                //this->viewport_->setClearEveryFrame(false);
134
135                // Note: No SceneManager specified yet
136                this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_MAIN, true, 3000);
137                this->resourceProvider_ = guiRenderer_->createResourceProvider();
138                this->resourceProvider_->setDefaultResourceGroup("GUI");
139
140                // setup scripting
141                this->scriptModule_ = new LuaScriptModule();
142                this->luaState_ = this->scriptModule_->getLuaState();
143
144                // create the CEGUI system singleton
145                this->guiSystem_ = new System(this->guiRenderer_, this->resourceProvider_, 0, this->scriptModule_);
146
147                // set the log level according to ours (translate by subtracting 1)
148                Logger::getSingleton().setLoggingLevel(
149                    (LoggingLevel)(Core::getSoftDebugLevel(OutputHandler::LD_Logfile) - 1));
150
151                // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place
152                tolua_Core_open(this->scriptModule_->getLuaState());
153                tolua_Orxonox_open(this->scriptModule_->getLuaState());
154
155                // register us as input handler
156                SimpleInputState* state = InputManager::getInstance().createInputState<SimpleInputState>("gui", 30);
157                state->setHandler(this);
158                state->setJoyStickHandler(new EmptyHandler());
159
160                // load the background scene
161                loadScenes();
162                //CEGUI::KeyEventArgs e;
163                //e.codepoint
164            }
165            catch (CEGUI::Exception& ex)
166            {
167#if CEGUI_VERSION_MAJOR == 0 && CEGUI_VERSION_MINOR < 6
168                throw GeneralException(ex.getMessage().c_str());
169#else
170                throw GeneralException(ex.getMessage().c_str(), ex.getLine(),
171                    ex.getFileName().c_str(), ex.getName().c_str());
172#endif
173            }
174
175            state_ = Ready;
176        }
177
178        return true;
179    }
180
181    void GUIManager::loadScenes()
182    {
183        // first of all, we need to have our own SceneManager for the GUI. The reason
184        // is that we might have multiple viewports when in play mode (e.g. the view of
185        // a camera fixed at the back of the ship). That forces us to create our own
186        // full screen viewport that is on top of all the others, but doesn't clear the
187        // port before rendering, so everything from the GUI gets on top eventually.
188        // But in order to realise that, we also need a SceneManager with an empty scene,
189        // because the SceneManager is responsible for the render queue.
190        //this->emptySceneManager_ = Ogre::Root::getSingleton()
191        //    .createSceneManager(Ogre::ST_GENERIC, "GUI/EmptySceneManager");
192
193        // we also need a camera or we won't see anything at all.
194        // The camera settings don't matter at all for an empty scene since the GUI
195        // gets rendered on top of the screen rather than into the scene.
196        //this->emptyCamera_ = this->emptySceneManager_->createCamera("GUI/EmptyCamera");
197
198        // Create another SceneManager that enables to display some 3D
199        // scene in the background of the main menu.
200        this->backgroundSceneManager_ = Ogre::Root::getSingleton()
201            .createSceneManager(Ogre::ST_GENERIC, "GUI/BackgroundSceneManager");
202        this->backgroundCamera_ = backgroundSceneManager_->createCamera("GUI/BackgroundCamera");
203
204        // TODO: create something 3D
205        try
206        {
207            this->scriptModule_->executeScriptFile("loadGUI.lua", "GUI");
208        }
209        catch (CEGUI::Exception& ex)
210        {
211#if CEGUI_VERSION_MINOR < 6
212            throw GeneralException(ex.getMessage().c_str());
213#else
214            throw GeneralException(ex.getMessage().c_str(), ex.getLine(),
215                ex.getFileName().c_str(), ex.getName().c_str());
216#endif
217        }
218    }
219
220    void GUIManager::showGUI(const std::string& name, Ogre::SceneManager* sceneManager)// bool showBackground)
221    {
222        if (state_ != Uninitialised)
223        {
224            if (state_ == OnDisplay)
225                hideGUI();
226
227            COUT(3) << "Loading GUI " << name << std::endl;
228            try
229            {
230                if (!sceneManager)
231                {
232                    // currently, only an image is loaded. We could do 3D, see loadBackground.
233                    //this->viewport_->setClearEveryFrame(true);
234                    this->guiRenderer_->setTargetSceneManager(this->backgroundSceneManager_);
235                    //this->viewport_->setCamera(this->backgroundCamera_);
236
237                    lua_pushboolean(this->scriptModule_->getLuaState(), true);
238                    lua_setglobal(this->scriptModule_->getLuaState(), "showBackground");
239                }
240                else
241                {
242                    //this->viewport_->setClearEveryFrame(false);
243                    this->guiRenderer_->setTargetSceneManager(sceneManager);
244                    //this->viewport_->setCamera(this->emptyCamera_);
245
246                    lua_pushboolean(this->scriptModule_->getLuaState(), false);
247                    lua_setglobal(this->scriptModule_->getLuaState(), "showBackground");
248                }
249
250                this->scriptModule_->executeScriptGlobal("showMainMenu");
251
252                InputManager::getInstance().requestEnterState("gui");
253
254                this->state_ = OnDisplay;
255            }
256            catch (CEGUI::Exception& ex)
257            {
258                COUT(2) << "Error while executing lua script. Message:\n" << ex.getMessage() << std::endl;
259            }
260            catch (...)
261            {
262                COUT(2) << "Could show a menu due to unknown reasons." << std::endl;
263            }
264        }
265        else
266        {
267            COUT(2) << "Warning: GUI Manager not yet initialised, cannot load a GUI" << std::endl;
268        }
269    }
270
271    void GUIManager::hideGUI()
272    {
273        if (this->state_ != OnDisplay)
274            return;
275        //this->viewport_->setCamera(0);
276        this->guiRenderer_->setTargetSceneManager(0);
277        this->state_ = Ready;
278        InputManager::getInstance().requestLeaveState("gui");
279    }
280
281    void GUIManager::mouseButtonPressed(MouseButton::Enum id)
282    {
283        try
284        {
285            guiSystem_->injectMouseButtonDown(convertButton(id));
286        }
287        catch (CEGUI::ScriptException& ex)
288        {
289            // We simply ignore the exception and proceed
290            COUT(1) << ex.getMessage() << std::endl;
291        }
292    }
293
294    void GUIManager::mouseButtonReleased(MouseButton::Enum id)
295    {
296        try
297        {
298            guiSystem_->injectMouseButtonUp(convertButton(id));
299        }
300        catch (CEGUI::ScriptException& ex)
301        {
302            // We simply ignore the exception and proceed
303            COUT(1) << ex.getMessage() << std::endl;
304        }
305    }
306
307
308    inline CEGUI::MouseButton GUIManager::convertButton(MouseButton::Enum button)
309    {
310        switch (button)
311        {
312        case MouseButton::Left:
313            return CEGUI::LeftButton;
314
315        case MouseButton::Right:
316            return CEGUI::RightButton;
317
318        case MouseButton::Middle:
319            return CEGUI::MiddleButton;
320
321        case MouseButton::Button3:
322            return CEGUI::X1Button;
323
324        case MouseButton::Button4:
325            return CEGUI::X2Button;
326
327        default:
328            return CEGUI::NoButton;
329        }
330    }
331}
Note: See TracBrowser for help on using the repository browser.