Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource2/src/core/GUIManager.cc @ 5658

Last change on this file since 5658 was 5658, checked in by rgrieder, 15 years ago

Fixed two bugs:

  • Incomplete exception safety in Core::loadGraphics
  • When shutting down, Game would load the GraphicsManager again (due to the unloadGraphics call). Suppressed this for faster shutdown.

Resolved a little issue:

  • Finally figured out a way to handle exceptions caught with catch (…) generically and implemented this function in Game::getExceptionMessage()
  • Also removes the exception translation in the GUIManager and made Game catch CEGUI::Exception as well.
  • Property svn:eol-style set to native
File size: 10.5 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 *      Benjamin Knecht
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30/**
31@file
32@brief
33    Implementation of the GUIManager class.
34*/
35
36#include "GUIManager.h"
37
38#include <memory>
39extern "C" {
40#include <lua.h>
41}
42#include <CEGUIDefaultLogger.h>
43#include <CEGUIExceptions.h>
44#include <CEGUIInputEvent.h>
45#include <CEGUIResourceProvider.h>
46#include <CEGUISystem.h>
47#include <ogreceguirenderer/OgreCEGUIRenderer.h>
48
49#include "SpecialConfig.h" // Configures the macro below
50#ifdef CEGUILUA_USE_INTERNAL_LIBRARY
51#   include <ceguilua/CEGUILua.h>
52#else
53#   include <CEGUILua.h>
54#endif
55
56#include "util/Debug.h"
57#include "util/Exception.h"
58#include "util/OrxAssert.h"
59#include "Core.h"
60#include "Clock.h"
61#include "LuaState.h"
62
63namespace orxonox
64{
65    class CEGUILogger : public CEGUI::DefaultLogger
66    {
67    public:
68            void logEvent(const CEGUI::String& message, CEGUI::LoggingLevel level = CEGUI::Standard)
69        {
70            int orxonoxLevel = CEGUI::Standard;
71            switch (level)
72            {
73                case CEGUI::Errors:      orxonoxLevel = 1; break;
74                case CEGUI::Warnings:    orxonoxLevel = 2; break;
75                case CEGUI::Standard:    orxonoxLevel = 4; break;
76                case CEGUI::Informative: orxonoxLevel = 5; break;
77                case CEGUI::Insane:      orxonoxLevel = 6; break;
78                default: OrxAssert(false, "CEGUI log level out of range, inpect immediately!");
79            }
80            OutputHandler::getOutStream().setOutputLevel(orxonoxLevel)
81                << "CEGUI: " << message << std::endl;
82
83            CEGUI::DefaultLogger::logEvent(message, level);
84        }
85    };
86
87    static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button);
88
89    GUIManager* GUIManager::singletonPtr_s = 0;
90
91    /**
92    @brief
93        Constructs the GUIManager by starting up CEGUI
94
95        Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine.
96        The log is set up and connected to the CEGUILogger.
97        After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code.
98        Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically).
99    @param renderWindow
100        Ogre's render window. Without this, the GUI cannot be displayed.
101    @return true if success, otherwise false
102    */
103    GUIManager::GUIManager(Ogre::RenderWindow* renderWindow)
104        : renderWindow_(renderWindow)
105        , resourceProvider_(0)
106    {
107        using namespace CEGUI;
108
109        COUT(3) << "Initialising CEGUI." << std::endl;
110
111        // Note: No SceneManager specified yet
112        guiRenderer_.reset(new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, false, 3000));
113        resourceProvider_ = guiRenderer_->createResourceProvider();
114        resourceProvider_->setDefaultResourceGroup("GUI");
115
116        // setup scripting
117        scriptModule_.reset(new LuaScriptModule());
118        luaState_ = scriptModule_->getLuaState();
119
120        // Create our own logger to specify the filepath
121        std::auto_ptr<CEGUILogger> ceguiLogger(new CEGUILogger());
122        ceguiLogger->setLogFilename(Core::getLogPathString() + "cegui.log");
123        // set the log level according to ours (translate by subtracting 1)
124        ceguiLogger->setLoggingLevel(
125            static_cast<LoggingLevel>(Core::getSoftDebugLevel(OutputHandler::LD_Logfile) - 1));
126        this->ceguiLogger_ = ceguiLogger.release();
127
128        // create the CEGUI system singleton
129        guiSystem_.reset(new System(guiRenderer_.get(), resourceProvider_, 0, scriptModule_.get()));
130
131        // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place
132        LuaState::openToluaInterfaces(this->luaState_);
133
134        // initialise the basic lua code
135        this->loadLuaCode();
136    }
137
138    /**
139    @brief
140        Destructor of the GUIManager
141
142        Basically shuts down CEGUI (member smart pointers) but first unloads our Tolua modules.
143    */
144    GUIManager::~GUIManager()
145    {
146        // destroy our own tolua interfaces
147        LuaState::closeToluaInterfaces(this->luaState_);
148    }
149
150    /**
151    @brief
152        Calls main Lua script
153    @todo
154        This function calls the main Lua script for our GUI.
155
156        Additionally we set the datapath variable in Lua. This is needed so Lua can access the data used for the GUI.
157    */
158    void GUIManager::loadLuaCode()
159    {
160        // set datapath for GUI data
161        lua_pushfstring(this->scriptModule_->getLuaState(), Core::getDataPathString().c_str());
162        lua_setglobal(this->scriptModule_->getLuaState(), "datapath");
163        // call main Lua script
164        this->scriptModule_->executeScriptFile("loadGUI_3.lua", "GUI");
165    }
166
167    /**
168    @brief
169        used to tick the GUI
170    @param time
171        clock which provides time value for the GUI System
172
173        Ticking the GUI means updating it with a certain regularity.
174        The elapsed time since the last call is given in the time value provided by the clock.
175        This time value is then used to provide a fluent animation of the GUI.
176    */
177    void GUIManager::update(const Clock& time)
178    {
179        assert(guiSystem_);
180        guiSystem_->injectTimePulse(time.getDeltaTime());
181    }
182
183    /**
184    @brief
185        Tells the GUIManager which SceneManager to use
186    @param camera
187        The current camera on which the GUI should be displayed on.
188
189        In fact the GUIManager needs the SceneManager and not the Camera to display the GUI.
190        This means the GUI is not bound to a camera but rather to the SceneManager.
191        Hiding the GUI when needed can therefore not be resolved by just NOT setting the current camera.
192    */
193    void GUIManager::setCamera(Ogre::Camera* camera)
194    {
195        if (camera == NULL)
196            this->guiRenderer_->setTargetSceneManager(0);
197        else
198            this->guiRenderer_->setTargetSceneManager(camera->getSceneManager());
199    }
200
201    /**
202    @brief
203        Executes Lua code
204    @param str
205        reference to string object holding the Lua code which is to be executed
206
207        This function gives total access to the GUI. You can execute ANY Lua code here.
208    */
209    void GUIManager::executeCode(const std::string& str)
210    {
211        try
212        {
213            this->scriptModule_->executeString(str);
214        }
215        catch (const CEGUI::Exception& ex)
216        {
217            COUT(2) << "CEGUI Error: \"" << ex.getMessage() << "\" while executing code \"" << str << "\"" << std::endl;
218        }
219        catch (...)
220        {
221            COUT(2) << "Couldn't execute GUI related Lua code due to unknown reasons." << std::endl;
222        }
223    }
224
225    /**
226    @brief
227        Displays specified GUI on screen
228    @param name
229        The name of the GUI
230
231        The function executes the Lua function with the same name in case the GUIManager is ready.
232        For more details check out loadGUI_2.lua where the function presides.
233    */
234    void GUIManager::showGUI(const std::string& name)
235    {
236        this->executeCode(std::string("showGUI(\"") + name + "\")");
237    }
238
239    void GUIManager::keyPressed(const KeyEvent& evt)
240    {
241        guiSystem_->injectKeyDown(evt.getKeyCode());
242        guiSystem_->injectChar(evt.getText());
243    }
244    void GUIManager::keyReleased(const KeyEvent& evt)
245    {
246        guiSystem_->injectKeyUp(evt.getKeyCode());
247    }
248
249    /**
250    @brief
251        Function receiving a mouse button pressed event.
252    @param id
253        ID of the mouse button which got pressed
254
255        This function is inherited by MouseHandler and injects the event into CEGUI.
256        It is for CEGUI to process the event.
257    */
258    void GUIManager::buttonPressed(MouseButtonCode::ByEnum id)
259    {
260        try
261        {
262            guiSystem_->injectMouseButtonDown(convertButton(id));
263        }
264        catch (CEGUI::ScriptException& ex)
265        {
266            // We simply ignore the exception and proceed
267            COUT(1) << ex.getMessage() << std::endl;
268        }
269    }
270
271    /**
272    @brief
273        Function receiving a mouse button released event.
274    @param id
275        ID of the mouse button which got released
276
277        This function is inherited by MouseHandler and injects the event into CEGUI.
278        It is for CEGUI to process the event.
279    */
280    void GUIManager::buttonReleased(MouseButtonCode::ByEnum id)
281    {
282        try
283        {
284            guiSystem_->injectMouseButtonUp(convertButton(id));
285        }
286        catch (CEGUI::ScriptException& ex)
287        {
288            // We simply ignore the exception and proceed
289            COUT(1) << ex.getMessage() << std::endl;
290        }
291    }
292
293    void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
294    {
295        guiSystem_->injectMouseMove(static_cast<float>(rel.x), static_cast<float>(rel.y));
296    }
297    void GUIManager::mouseScrolled(int abs, int rel)
298    {
299        guiSystem_->injectMouseWheelChange(static_cast<float>(rel));
300    }
301
302    /**
303    @brief
304        converts mouse event code to CEGUI event code
305    @param button
306        code of the mouse button as we use it in Orxonox
307    @return
308        code of the mouse button as it is used by CEGUI
309
310        Simple convertion from mouse event code in Orxonox to the one used in CEGUI.
311     */
312    static inline CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button)
313    {
314        switch (button)
315        {
316        case MouseButtonCode::Left:
317            return CEGUI::LeftButton;
318
319        case MouseButtonCode::Right:
320            return CEGUI::RightButton;
321
322        case MouseButtonCode::Middle:
323            return CEGUI::MiddleButton;
324
325        case MouseButtonCode::Button3:
326            return CEGUI::X1Button;
327
328        case MouseButtonCode::Button4:
329            return CEGUI::X2Button;
330
331        default:
332            return CEGUI::NoButton;
333        }
334    }
335}
Note: See TracBrowser for help on using the repository browser.