Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2853 was 2853, checked in by bknecht, 15 years ago

Fixed some bugs. Please do not break the code, when working with other people on one branch. Also check if the game runs and not just compiles.

  • Property svn:eol-style set to native
File size: 7.7 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 <boost/filesystem/path.hpp>
39#include <OgreRenderWindow.h>
40#include <CEGUI.h>
41#include <CEGUIDefaultLogger.h>
42#include <ogreceguirenderer/OgreCEGUIRenderer.h>
43#include "SpecialConfig.h" // Configures the macro below
44#ifdef CEGUILUA_USE_INTERNAL_LIBRARY
45#   include <ceguilua/CEGUILua.h>
46#else
47#   include <CEGUILua.h>
48#endif
49
50#include "util/Exception.h"
51#include "core/ConsoleCommand.h"
52#include "core/Core.h"
53#include "core/Clock.h"
54#include "ToluaBindCore.h"
55#include "ToluaBindOrxonox.h"
56
57extern "C" {
58#include <lua.h>
59}
60
61namespace orxonox
62{
63    GUIManager* GUIManager::singletonRef_s = 0;
64
65    GUIManager::GUIManager()
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 (guiSystem_)
80            delete guiSystem_;
81
82        if (scriptModule_)
83        {
84            // destroy our own tolua interfaces
85                lua_pushnil(luaState_);
86                lua_setglobal(luaState_, "Orxonox");
87                lua_pushnil(luaState_);
88                lua_setglobal(luaState_, "Core");
89            delete scriptModule_;
90        }
91
92        if (guiRenderer_)
93            delete guiRenderer_;
94
95        singletonRef_s = 0;
96    }
97
98    bool GUIManager::initialise(Ogre::RenderWindow* renderWindow)
99    {
100        using namespace CEGUI;
101        if (state_ == Uninitialised)
102        {
103            COUT(3) << "Initialising CEGUI." << std::endl;
104
105            try
106            {
107                // save the render window
108                renderWindow_ = renderWindow;
109
110                // Note: No SceneManager specified yet
111                this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, true, 3000);
112                this->resourceProvider_ = guiRenderer_->createResourceProvider();
113                this->resourceProvider_->setDefaultResourceGroup("GUI");
114
115                // setup scripting
116                this->scriptModule_ = new LuaScriptModule();
117                this->luaState_ = this->scriptModule_->getLuaState();
118
119                // Create our own logger to specify the filepath
120                boost::filesystem::path ceguiLogFilepath(Core::getLogPath() / "cegui.log");
121                this->ceguiLogger_ = new DefaultLogger();
122                this->ceguiLogger_->setLogFilename(ceguiLogFilepath.string());
123                // set the log level according to ours (translate by subtracting 1)
124                this->ceguiLogger_->setLoggingLevel(
125                    (LoggingLevel)(Core::getSoftDebugLevel(OutputHandler::LD_Logfile) - 1));
126
127                // create the CEGUI system singleton
128                this->guiSystem_ = new System(this->guiRenderer_, this->resourceProvider_, 0, this->scriptModule_);
129
130                // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place
131                tolua_Core_open(this->scriptModule_->getLuaState());
132                tolua_Orxonox_open(this->scriptModule_->getLuaState());
133
134                // initialise the basic lua code
135                loadLuaCode();
136            }
137            catch (CEGUI::Exception& ex)
138            {
139#if CEGUI_VERSION_MAJOR == 0 && CEGUI_VERSION_MINOR < 6
140                throw GeneralException(ex.getMessage().c_str());
141#else
142                throw GeneralException(ex.getMessage().c_str(), ex.getLine(),
143                    ex.getFileName().c_str(), ex.getName().c_str());
144#endif
145            }
146
147            state_ = Ready;
148        }
149
150        return true;
151    }
152
153    void GUIManager::loadLuaCode()
154    {
155        try
156        {
157            this->scriptModule_->executeScriptFile("loadGUI_2.lua", "GUI");
158            lua_pushfstring(this->scriptModule_->getLuaState(), Core::getMediaPathString().c_str());
159            lua_setglobal(this->scriptModule_->getLuaState(), "datapath");
160        }
161        catch (CEGUI::Exception& ex)
162        {
163#if CEGUI_VERSION_MINOR < 6
164            throw GeneralException(ex.getMessage().c_str());
165#else
166            throw GeneralException(ex.getMessage().c_str(), ex.getLine(),
167                ex.getFileName().c_str(), ex.getName().c_str());
168#endif
169        }
170    }
171
172    void GUIManager::update(const Clock& time)
173    {
174        assert(guiSystem_);
175        guiSystem_->injectTimePulse(time.getDeltaTime());
176    }
177
178    void GUIManager::executeCode(const std::string& str)
179    {
180        this->scriptModule_->executeString(str);
181    }
182
183    void GUIManager::setCamera(Ogre::Camera* camera)
184    {
185        this->guiRenderer_->setTargetSceneManager(camera->getSceneManager());
186    }
187
188    void GUIManager::testOutput(std::string& str)
189    {
190        COUT(0) << "*** CEGUI: " << str << std::endl;
191    }
192
193    void GUIManager::showGUI(const std::string& name)
194    {
195        if (state_ != Uninitialised)
196        {
197            COUT(3) << "Loading GUI " << name << std::endl;
198            try
199            {
200                this->scriptModule_->executeString(std::string("showGUI(\"") + name + "\")");
201            }
202            catch (CEGUI::Exception& ex)
203            {
204                COUT(2) << "Error while executing lua script. Message:\n" << ex.getMessage() << std::endl;
205            }
206            catch (...)
207            {
208                COUT(2) << "Could show a menu due to unknown reasons." << std::endl;
209            }
210        }
211        else
212        {
213            COUT(2) << "Warning: GUI Manager not yet initialised, cannot load a GUI" << std::endl;
214        }
215    }
216
217    void GUIManager::mouseButtonPressed(MouseButtonCode::ByEnum id)
218    {
219        try
220        {
221            guiSystem_->injectMouseButtonDown(convertButton(id));
222        }
223        catch (CEGUI::ScriptException& ex)
224        {
225            // We simply ignore the exception and proceed
226            COUT(1) << ex.getMessage() << std::endl;
227        }
228    }
229
230    void GUIManager::mouseButtonReleased(MouseButtonCode::ByEnum id)
231    {
232        try
233        {
234            guiSystem_->injectMouseButtonUp(convertButton(id));
235        }
236        catch (CEGUI::ScriptException& ex)
237        {
238            // We simply ignore the exception and proceed
239            COUT(1) << ex.getMessage() << std::endl;
240        }
241    }
242
243
244    inline CEGUI::MouseButton GUIManager::convertButton(MouseButtonCode::ByEnum button)
245    {
246        switch (button)
247        {
248        case MouseButtonCode::Left:
249            return CEGUI::LeftButton;
250
251        case MouseButtonCode::Right:
252            return CEGUI::RightButton;
253
254        case MouseButtonCode::Middle:
255            return CEGUI::MiddleButton;
256
257        case MouseButtonCode::Button3:
258            return CEGUI::X1Button;
259
260        case MouseButtonCode::Button4:
261            return CEGUI::X2Button;
262
263        default:
264            return CEGUI::NoButton;
265        }
266    }
267}
Note: See TracBrowser for help on using the repository browser.