Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

reload feature tested and works under windows, but only with one joy stick. Will have to test it with multiple ones when that is properly supported.

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