Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ppspickups3/src/libraries/core/GUIManager.cc @ 6756

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

Background wasn't transparent upon start up —> black screen when starting with "—standalone"

  • Property svn:eol-style set to native
File size: 15.4 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 <boost/bind.hpp>
33#include <memory>
34extern "C" {
35#include <lua.h>
36}
37#include <CEGUIDefaultLogger.h>
38#include <CEGUIExceptions.h>
39#include <CEGUIInputEvent.h>
40#include <CEGUIMouseCursor.h>
41#include <CEGUIResourceProvider.h>
42#include <CEGUISystem.h>
43#include <CEGUIWindow.h>
44#include <CEGUIWindowManager.h>
45#include <ogreceguirenderer/OgreCEGUIRenderer.h>
46
47#include "SpecialConfig.h" // Configures the macro below
48#ifdef CEGUILUA_USE_INTERNAL_LIBRARY
49#   include <ceguilua/CEGUILua.h>
50#else
51#   include <CEGUILua.h>
52#endif
53
54#include "util/Clock.h"
55#include "util/Convert.h"
56#include "util/Debug.h"
57#include "util/Exception.h"
58#include "util/OrxAssert.h"
59#include "ConsoleCommand.h"
60#include "Core.h"
61#include "GraphicsManager.h"
62#include "LuaState.h"
63#include "PathConfig.h"
64#include "Resource.h"
65#include "input/InputManager.h"
66#include "input/InputState.h"
67#include "input/KeyBinderManager.h"
68
69namespace orxonox
70{
71    static void key_esc()
72        { GUIManager::getInstance().keyESC(); }
73    SetConsoleCommandShortcutExternAlias(key_esc, "keyESC");
74
75    class CEGUILogger : public CEGUI::DefaultLogger
76    {
77    public:
78        void logEvent(const CEGUI::String& message, CEGUI::LoggingLevel level = CEGUI::Standard)
79        {
80            int orxonoxLevel = CEGUI::Standard;
81            switch (level)
82            {
83                case CEGUI::Errors:      orxonoxLevel = 1; break;
84                case CEGUI::Warnings:    orxonoxLevel = 2; break;
85                case CEGUI::Standard:    orxonoxLevel = 4; break;
86                case CEGUI::Informative: orxonoxLevel = 5; break;
87                case CEGUI::Insane:      orxonoxLevel = 6; break;
88                default: OrxAssert(false, "CEGUI log level out of range, inpect immediately!");
89            }
90            OutputHandler::getOutStream(orxonoxLevel)
91                << "CEGUI: " << message << std::endl;
92
93            CEGUI::DefaultLogger::logEvent(message, level);
94        }
95    };
96
97    static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button);
98
99    GUIManager* GUIManager::singletonPtr_s = 0;
100
101    SetConsoleCommandShortcut(GUIManager, showGUI).accessLevel(AccessLevel::User).defaultValue(1, false).defaultValue(2, true);
102    SetConsoleCommandShortcut(GUIManager, hideGUI).accessLevel(AccessLevel::User);
103
104    /**
105    @brief
106        Constructs the GUIManager by starting up CEGUI
107
108        Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine.
109        The log is set up and connected to the CEGUILogger.
110        After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code.
111        Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically).
112    @param renderWindow
113        Ogre's render window. Without this, the GUI cannot be displayed.
114    @return true if success, otherwise false
115    */
116    GUIManager::GUIManager(const std::pair<int, int>& mousePosition)
117        : resourceProvider_(NULL)
118        , camera_(NULL)
119    {
120        using namespace CEGUI;
121
122        COUT(3) << "Initialising CEGUI." << std::endl;
123
124        // Note: No SceneManager specified yet
125        guiRenderer_.reset(new OgreCEGUIRenderer(GraphicsManager::getInstance().getRenderWindow(), Ogre::RENDER_QUEUE_OVERLAY, false, 3000));
126        resourceProvider_ = guiRenderer_->createResourceProvider();
127        resourceProvider_->setDefaultResourceGroup("GUI");
128
129        // Setup scripting
130        luaState_.reset(new LuaState());
131        rootFileInfo_ = Resource::getInfo("InitialiseGUI.lua");
132        // This is necessary to ensure that input events also use the right resource info when triggering lua functions
133        luaState_->setDefaultResourceInfo(this->rootFileInfo_);
134        scriptModule_.reset(new LuaScriptModule(luaState_->getInternalLuaState()));
135
136        // Create our own logger to specify the filepath
137        std::auto_ptr<CEGUILogger> ceguiLogger(new CEGUILogger());
138        ceguiLogger->setLogFilename(PathConfig::getLogPathString() + "cegui.log");
139        // set the log level according to ours (translate by subtracting 1)
140        ceguiLogger->setLoggingLevel(
141            static_cast<LoggingLevel>(OutputHandler::getInstance().getSoftDebugLevel("logFile") - 1));
142        this->ceguiLogger_ = ceguiLogger.release();
143
144        // Create the CEGUI system singleton
145        guiSystem_.reset(new System(guiRenderer_.get(), resourceProvider_, 0, scriptModule_.get()));
146
147        // Align CEGUI mouse with OIS mouse
148        guiSystem_->injectMousePosition((float)mousePosition.first, (float)mousePosition.second);
149
150        // Initialise the Lua framework and load the schemes
151        this->luaState_->doFile("InitialiseGUI.lua");
152
153        // Create the root nodes
154        this->rootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("MenuWidgets/StaticImage", "AbsoluteRootWindow");
155        this->rootWindow_->setProperty("FrameEnabled", "False");
156        this->hudRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "HUDRootWindow");
157        this->menuRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "MenuRootWindow");
158        // And connect them
159        CEGUI::System::getSingleton().setGUISheet(this->rootWindow_);
160        this->rootWindow_->addChildWindow(this->hudRootWindow_);
161        this->rootWindow_->addChildWindow(this->menuRootWindow_);
162
163        // No background to start with (sets the alpha value to 0)
164        this->setBackgroundImage("");
165
166        // Set up the sheet manager in the Lua framework
167        this->luaState_->doFile("SheetManager.lua");
168    }
169
170    /**
171    @brief
172        Basically shuts down CEGUI (member smart pointers) but first unloads our Tolua modules.
173    */
174    GUIManager::~GUIManager()
175    {
176    }
177
178    /**
179    @brief
180        used to tick the GUI
181    @param time
182        clock which provides time value for the GUI System
183
184        Ticking the GUI means updating it with a certain regularity.
185        The elapsed time since the last call is given in the time value provided by the clock.
186        This time value is then used to provide a fluent animation of the GUI.
187    */
188    void GUIManager::preUpdate(const Clock& time)
189    {
190        assert(guiSystem_);
191        this->protectedCall(boost::bind(&CEGUI::System::injectTimePulse, _1, time.getDeltaTime()));
192    }
193
194    /**
195    @brief
196        Tells the GUIManager which SceneManager to use
197    @param camera
198        The current camera on which the GUI should be displayed on.
199
200        In fact the GUIManager needs the SceneManager and not the Camera to display the GUI.
201        This means the GUI is not bound to a camera but rather to the SceneManager.
202        Hiding the GUI when needed can therefore not be resolved by just NOT setting the current camera.
203    */
204    void GUIManager::setCamera(Ogre::Camera* camera)
205    {
206        this->camera_ = camera;
207        if (camera == NULL)
208            this->guiRenderer_->setTargetSceneManager(0);
209        else
210            this->guiRenderer_->setTargetSceneManager(camera->getSceneManager());
211    }
212
213    /**
214    @brief
215        Executes Lua code
216    @param str
217        reference to string object holding the Lua code which is to be executed
218    */
219    void GUIManager::executeCode(const std::string& str)
220    {
221        this->luaState_->doString(str, rootFileInfo_);
222    }
223
224    /** Loads a GUI sheet by Lua script
225    @param name
226        The name of the GUI (like the script name, but without the extension)
227    */
228    void GUIManager::loadGUI(const std::string& name)
229    {
230        this->executeCode("loadSheet(\"" + name + "\")");
231    }
232
233    /**
234    @brief
235        Displays specified GUI on screen
236    @param name
237        The name of the GUI
238
239        The function executes the Lua function with the same name in case the GUIManager is ready.
240    */
241    /*static*/ void GUIManager::showGUI(const std::string& name, bool bHidePrevious)
242    {
243        GUIManager::getInstance().executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ")");
244    }
245
246    /**
247    @brief
248        Hack-ish. Needed for GUIOverlay.
249    */
250    void GUIManager::showGUIExtra(const std::string& name, const std::string& ptr, bool bHidePrevious)
251    {
252        this->executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + ptr + ")");
253    }
254
255    /**
256    @brief
257        Hides specified GUI.
258    @param name
259        The name of the GUI.
260    */
261    /*static*/ void GUIManager::hideGUI(const std::string& name)
262    {
263        GUIManager::getInstance().executeCode("hideMenuSheet(\"" + name + "\")");
264    }
265
266    const std::string& GUIManager::createInputState(const std::string& name, TriBool::Value showCursor, TriBool::Value useKeyboard, bool bBlockJoyStick)
267    {
268        InputState* state = InputManager::getInstance().createInputState(name);
269
270        /* Table that maps isFullScreen() and showCursor to mouseExclusive
271        isFullscreen / showCursor | True  | False | Dontcare
272        ----------------------------------------------------
273        true                      | True  | True  | Dontcare
274        ----------------------------------------------------
275        false                     | False | True  | Dontcare
276        */
277        if (showCursor == TriBool::Dontcare)
278            state->setMouseExclusive(TriBool::Dontcare);
279        else if (GraphicsManager::getInstance().isFullScreen() || showCursor == TriBool::False)
280            state->setMouseExclusive(TriBool::True);
281        else
282            state->setMouseExclusive(TriBool::False);
283
284        if (showCursor == TriBool::True)
285            state->setMouseHandler(this);
286        else if (showCursor == TriBool::False)
287            state->setMouseHandler(&InputHandler::EMPTY);
288
289        if (useKeyboard == TriBool::True)
290            state->setKeyHandler(this);
291        else if (useKeyboard == TriBool::False)
292            state->setKeyHandler(&InputHandler::EMPTY);
293
294        if (bBlockJoyStick)
295            state->setJoyStickHandler(&InputHandler::EMPTY);
296
297        return state->getName();
298    }
299
300    void GUIManager::keyESC()
301    {
302        this->executeCode("keyESC()");
303    }
304
305    void GUIManager::setBackgroundImage(const std::string& imageSet, const std::string imageName)
306    {
307        if (imageSet.empty() || imageName.empty())
308            this->setBackgroundImage("");
309        else
310            this->setBackgroundImage("set: " + imageSet + " image: " + imageName);
311    }
312
313    void GUIManager::setBackgroundImage(const std::string& image)
314    {
315        if (image.empty())
316            this->rootWindow_->setProperty("Alpha", "0.0");
317        else
318            this->rootWindow_->setProperty("Alpha", "1.0");
319        this->rootWindow_->setProperty("Image", image);
320    }
321
322    void GUIManager::keyPressed(const KeyEvent& evt)
323    {
324        this->protectedCall(boost::bind(&CEGUI::System::injectKeyDown, _1, evt.getKeyCode()));
325        this->protectedCall(boost::bind(&CEGUI::System::injectChar, _1, evt.getText()));
326    }
327
328    void GUIManager::keyReleased(const KeyEvent& evt)
329    {
330        this->protectedCall(boost::bind(&CEGUI::System::injectKeyUp, _1, evt.getKeyCode()));
331    }
332
333    /**
334    @brief
335        Function receiving a mouse button pressed event.
336    @param id
337        ID of the mouse button which got pressed
338
339        This function is inherited by MouseHandler and injects the event into CEGUI.
340        It is for CEGUI to process the event.
341    */
342    void GUIManager::buttonPressed(MouseButtonCode::ByEnum id)
343    {
344        this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonDown, _1, convertButton(id)));
345    }
346
347    /**
348    @brief
349        Function receiving a mouse button released event.
350    @param id
351        ID of the mouse button which got released
352
353        This function is inherited by MouseHandler and injects the event into CEGUI.
354        It is for CEGUI to process the event.
355    */
356    void GUIManager::buttonReleased(MouseButtonCode::ByEnum id)
357    {
358        this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonUp, _1, convertButton(id)));
359    }
360
361    void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
362    {
363        this->protectedCall(boost::bind(&CEGUI::System::injectMousePosition, _1, (float)abs.x, (float)abs.y));
364    }
365
366    void GUIManager::mouseScrolled(int abs, int rel)
367    {
368        this->protectedCall(boost::bind(&CEGUI::System::injectMouseWheelChange, _1, (float)rel));
369    }
370
371    /**
372    @brief
373        converts mouse event code to CEGUI event code
374    @param button
375        code of the mouse button as we use it in Orxonox
376    @return
377        code of the mouse button as it is used by CEGUI
378
379        Simple conversion from mouse event code in Orxonox to the one used in CEGUI.
380     */
381    static inline CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button)
382    {
383        switch (button)
384        {
385        case MouseButtonCode::Left:
386            return CEGUI::LeftButton;
387
388        case MouseButtonCode::Right:
389            return CEGUI::RightButton;
390
391        case MouseButtonCode::Middle:
392            return CEGUI::MiddleButton;
393
394        case MouseButtonCode::Button3:
395            return CEGUI::X1Button;
396
397        case MouseButtonCode::Button4:
398            return CEGUI::X2Button;
399
400        default:
401            return CEGUI::NoButton;
402        }
403    }
404
405    /** Executes a CEGUI function normally, but catches CEGUI::ScriptException.
406        When a ScriptException occurs, the error message will be displayed and
407        the program carries on.
408    @remarks
409        The exception behaviour may pose problems if the code is not written
410        exception-safe (and you can forget about that in Lua). The program might
411        be left in an undefined state. But otherwise one script error would
412        terminate the whole program...
413    @note
414        Your life gets easier if you use boost::bind to create the object/function.
415    @param function
416        Any callable object/function that takes this->guiSystem_ as its only parameter.
417    @return
418        True if input was handled, false otherwise. A caught exception yields true.
419    */
420    template <typename FunctionType>
421    bool GUIManager::protectedCall(FunctionType function)
422    {
423        try
424        {
425            return function(this->guiSystem_);
426        }
427        catch (CEGUI::ScriptException& ex)
428        {
429            // Display the error and proceed. See @remarks why this can be dangerous.
430            COUT(1) << ex.getMessage() << std::endl;
431            return true;
432        }
433    }
434
435    void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function)
436    {
437        window->subscribeScriptedEvent(event, function);
438    }
439}
Note: See TracBrowser for help on using the repository browser.