Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

When starting with no arguments, the GUI is loaded, which consists merely of 2 buttons: start and exit (quite self explanatory). But it does work on my box (still no cmake support).

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