Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/orxonox/gui/GUIManager.cc @ 1661

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

started implementing the GameStates. Not much for now, but most of the Orxonox.cc code has been copied to GSRoot, GSGraphics and GSLevel.
There is no level currently, but the main menu is shown. This is more of an svn save because I would really like to have Member ConsoleCommands.

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