Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/GUIManager.cc @ 6067

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

Merged core5 branch back to the trunk.
Key features include clean level unloading and an extended XML event system.

Two important notes:
Delete your keybindings.ini files! * or you will still get parser errors when loading the key bindings.
Delete build_dir/lib/modules/libgamestates.module! * or orxonox won't start.
Best thing to do is to delete the build folder ;)

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