Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/output/src/libraries/core/GUIManager.cc @ 8807

Last change on this file since 8807 was 8806, checked in by landauf, 13 years ago

Replaced COUT with orxout in core. Tried to set levels and contexts in a more or less useful way, but not really optimized.

  • Property svn:eol-style set to native
File size: 28.2 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 <fstream>
33#include <memory>
34#include <boost/bind.hpp>
35#include <OgreRenderQueue.h>
36#include <OgreRenderWindow.h>
37
38#include <CEGUIDefaultLogger.h>
39#include <CEGUIExceptions.h>
40#include <CEGUIFontManager.h>
41#include <CEGUIInputEvent.h>
42#include <CEGUIMouseCursor.h>
43#include <CEGUIResourceProvider.h>
44#include <CEGUISystem.h>
45#include <CEGUIWindow.h>
46#include <CEGUIWindowManager.h>
47#include <CEGUIXMLAttributes.h>
48#include <elements/CEGUIListbox.h>
49#include <elements/CEGUIListboxItem.h>
50
51#ifdef ORXONOX_OLD_CEGUI
52#  include <CEGUILua.h>
53#  include <ogreceguirenderer/OgreCEGUIRenderer.h>
54extern "C" {
55#  include <lauxlib.h>
56}
57#else
58#  include <ScriptingModules/LuaScriptModule/CEGUILua.h>
59#  include <RendererModules/Ogre/CEGUIOgreImageCodec.h>
60#  include <RendererModules/Ogre/CEGUIOgreRenderer.h>
61#  include <RendererModules/Ogre/CEGUIOgreResourceProvider.h>
62#  include <OgreCamera.h>
63#  include <OgreRenderQueueListener.h>
64#  include <OgreRenderSystem.h>
65#  include <OgreRoot.h>
66#  include <OgreSceneManager.h>
67#endif
68
69#if defined(ORXONOX_PLATFORM_WINDOWS) && !defined(ORXONOX_COMPILER_MINGW)
70#  include <windows.h>
71#endif
72
73#include "util/Clock.h"
74#include "util/Convert.h"
75#include "util/Output.h"
76#include "util/Exception.h"
77#include "util/Math.h"
78#include "util/OrxAssert.h"
79#include "util/output/BaseWriter.h"
80#include "ConfigValueIncludes.h"
81#include "Core.h"
82#include "CoreIncludes.h"
83#include "Game.h"
84#include "GraphicsManager.h"
85#include "LuaState.h"
86#include "PathConfig.h"
87#include "Resource.h"
88#include "command/ConsoleCommand.h"
89#include "input/InputManager.h"
90#include "input/InputState.h"
91#include "input/KeyBinderManager.h"
92
93namespace orxonox
94{
95    static void key_esc()
96        { GUIManager::getInstance().keyESC(); }
97    SetConsoleCommand("keyESC", &key_esc);
98
99    class CEGUILogger : public CEGUI::DefaultLogger
100    {
101    public:
102        void logEvent(const CEGUI::String& message, CEGUI::LoggingLevel level = CEGUI::Standard)
103        {
104            OutputLevel orxonoxLevel = level::debug_output;
105            switch (level)
106            {
107                case CEGUI::Errors:      orxonoxLevel = level::internal_error; break;
108                case CEGUI::Warnings:    orxonoxLevel = level::internal_warning; break;
109                case CEGUI::Standard:    orxonoxLevel = level::verbose; break;
110                case CEGUI::Informative: orxonoxLevel = level::verbose_more; break;
111                case CEGUI::Insane:      orxonoxLevel = level::verbose_ultra; break;
112                default: OrxAssert(false, "CEGUI log level out of range, inspect immediately!");
113            }
114
115            orxout(orxonoxLevel, context::cegui) << message << endl;
116
117            CEGUI::DefaultLogger::logEvent(message, level);
118        }
119
120        /// Carbon copy from CEGUIDefaultLogger.cpp with a bugfix for Windows
121        void setLogFilename(const CEGUI::String& filename, bool append = false)
122        {
123            // Close current log file (if any)
124            if (d_ostream.is_open())
125                d_ostream.close();
126
127#if defined(ORXONOX_PLATFORM_WINDOWS) && !defined(ORXONOX_COMPILER_MINGW)
128            // filename.c_str() is UTF-8 encoded, but Windows expects characters
129            // according to the current codepage or UTF-16 (wchar)
130            d_ostream.open(utf8ToUtf16(filename.c_str()).c_str(), std::ios_base::out | (append ? std::ios_base::app : std::ios_base::trunc));
131#else
132            d_ostream.open(filename.c_str(), std::ios_base::out | (append ? std::ios_base::app : std::ios_base::trunc));
133#endif
134            if (!d_ostream)
135                ThrowException(General, "Setting the CEGUI log filename failed");
136
137            // Initialise width for date & time alignment.
138            d_ostream.width(2);
139
140            // Write out cached log strings.
141            if (d_caching)
142            {
143                d_caching = false;
144
145                std::vector<std::pair<CEGUI::String, CEGUI::LoggingLevel> >::iterator it = d_cache.begin();
146
147                while (it != d_cache.end())
148                {
149                    if (d_level >= it->second)
150                    {
151                        d_ostream << it->first;
152                        // Ensure new event is written to the file, rather than just being buffered.
153                        d_ostream.flush();
154                    }
155                    ++it;
156                }
157
158                d_cache.clear();
159            }
160        }
161
162#if defined(ORXONOX_PLATFORM_WINDOWS) && !defined(ORXONOX_COMPILER_MINGW)
163        /// Converts a UTF-8 character sequence to Windows UTF-16
164        static std::wstring utf8ToUtf16(const std::string& utf8text)
165        {
166            const int textLen = MultiByteToWideChar(CP_UTF8, 0, utf8text.c_str(),
167                utf8text.size() + 1, 0, 0);
168
169            if (textLen == 0)
170                ThrowException(General, "Utf8ToUtf16 - MultiByteToWideChar failed");
171
172            std::wstring wideStr(textLen, 0);
173            MultiByteToWideChar(CP_UTF8, 0, utf8text.c_str(), utf8text.size() + 1,
174                &wideStr[0], wideStr.size());
175            return wideStr;
176        }
177#endif
178    };
179
180#ifdef ORXONOX_OLD_CEGUI
181    /** Class with the same memory layout as CEGUI::LuaScriptModule. <br>
182        We need this to fix a problem with an uninitialised member variable
183        in CEGUI < 0.7 <br>
184        Notice the "public" modifier for the otherwise private variables.
185    */
186    class LuaScriptModuleWorkaround : public CEGUI::ScriptModule
187    {
188    public:
189        LuaScriptModuleWorkaround();
190        ~LuaScriptModuleWorkaround();
191
192    public:
193        bool d_ownsState;
194        lua_State* d_state;
195        CEGUI::String d_errFuncName;
196        int d_errFuncIndex;
197        CEGUI::String d_activeErrFuncName;
198        int d_activeErrFuncIndex;
199    };
200#else
201    /// RenderQueueListener based class used to hook into the ogre rendering system
202    class RQListener : public Ogre::RenderQueueListener
203    {
204    public:
205        /// Callback from Ogre invoked before other stuff in our target queue is rendered
206        void renderQueueStarted(Ogre::uint8 id, const Ogre::String& invocation, bool& skipThisQueue)
207        {
208            if (id == Ogre::RENDER_QUEUE_OVERLAY && invocation.empty())
209            {
210                CEGUI::System::getSingleton().renderGUI();
211
212                // Important workaround! (at least required by CEGUI 0.7.5)
213                // If we don't reset the scissor test, OGRE will only render overlays
214                // in the area where CEGUI last drew, which is usually nothing
215                // or a little box where the focused element is.
216                Ogre::Root::getSingleton().getRenderSystem()->setScissorTest(false);
217            }
218        }
219    };
220#endif
221
222    static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button);
223
224    GUIManager* GUIManager::singletonPtr_s = 0;
225    /*static*/ const std::string GUIManager::defaultScheme_ = "TaharezGreen";
226
227    SetConsoleCommand("showGUI", &GUIManager::showGUI).defaultValue(1, false).defaultValue(2, false);
228    SetConsoleCommand("hideGUI", &GUIManager::hideGUI);
229    SetConsoleCommand("toggleGUI", &GUIManager::toggleGUI).defaultValue(1, false).defaultValue(2, false);
230
231    /**
232    @brief
233        Constructs the GUIManager by starting up CEGUI
234
235        Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine.
236        The log is set up and connected to the CEGUILogger.
237        After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code.
238        Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically).
239    @return true if success, otherwise false
240    */
241    GUIManager::GUIManager(const std::pair<int, int>& mousePosition)
242        : guiRenderer_(NULL)
243        , resourceProvider_(NULL)
244#ifndef ORXONOX_OLD_CEGUI
245        , rqListener_(NULL)
246        , imageCodec_(NULL)
247#endif
248        , luaState_(NULL)
249        , scriptModule_(NULL)
250        , guiSystem_(NULL)
251        , ceguiLogger_(NULL)
252        , rootWindow_(NULL)
253        , hudRootWindow_(NULL)
254        , menuRootWindow_(NULL)
255        , camera_(NULL)
256        , destructionHelper_(this)
257    {
258        RegisterRootObject(GUIManager);
259        this->setConfigValues();
260
261        using namespace CEGUI;
262
263        orxout(internal_info) << "Initialising CEGUI." << endl;
264
265        this->oldCEGUI_ = false;
266
267        // Note: No SceneManager specified yet
268#ifdef ORXONOX_OLD_CEGUI
269        guiRenderer_ = new OgreCEGUIRenderer(GraphicsManager::getInstance().getRenderWindow(), Ogre::RENDER_QUEUE_OVERLAY, false, 3000);
270        resourceProvider_ = guiRenderer_->createResourceProvider();
271        this->oldCEGUI_ = true;
272#else
273        guiRenderer_ = &OgreRenderer::create(*GraphicsManager::getInstance().getRenderWindow());
274        // We use our own RenderQueueListener so we can draw UNDER overlays
275        guiRenderer_->setFrameControlExecutionEnabled(false);
276        rqListener_ = new RQListener();
277        resourceProvider_ = &OgreRenderer::createOgreResourceProvider();
278        imageCodec_ = &OgreRenderer::createOgreImageCodec();
279#endif
280        resourceProvider_->setDefaultResourceGroup("General");
281
282        // Setup scripting
283        luaState_ = new LuaState();
284        rootFileInfo_ = Resource::getInfo("InitialiseGUI.lua");
285        // This is necessary to ensure that input events also use the right resource info when triggering lua functions
286        luaState_->setDefaultResourceInfo(this->rootFileInfo_);
287#ifdef ORXONOX_OLD_CEGUI
288        scriptModule_ = new LuaScriptModule(luaState_->getInternalLuaState());
289        // Ugly workaround: older CEGUILua versions don't initialise the member
290        // d_activeErrFuncIndex at all. That leads to "error in error handling"
291        // problems when a Lua error occurs.
292        // We fix this by setting the member manually.
293        reinterpret_cast<LuaScriptModuleWorkaround*>(scriptModule_)->d_activeErrFuncIndex = LUA_NOREF;
294        luaState_->doString("ORXONOX_OLD_CEGUI = true");
295#else
296        scriptModule_ = &LuaScriptModule::create(luaState_->getInternalLuaState());
297#endif
298        scriptModule_->setDefaultPCallErrorHandler(LuaState::ERROR_HANDLER_NAME);
299
300        // Create our own logger to specify the filepath
301        std::auto_ptr<CEGUILogger> ceguiLogger(new CEGUILogger());
302        ceguiLogger->setLogFilename(PathConfig::getLogPathString() + "cegui.log");
303        ceguiLogger->setLoggingLevel(static_cast<CEGUI::LoggingLevel>(this->outputLevelCeguiLog_));
304        this->ceguiLogger_ = ceguiLogger.release();
305
306        // Create the CEGUI system singleton
307#ifdef ORXONOX_OLD_CEGUI
308        guiSystem_ = new System(guiRenderer_, resourceProvider_, 0, scriptModule_);
309        // Add functions that have been renamed in newer versions
310        luaState_->doString("CEGUI.SchemeManager.create = CEGUI.SchemeManager.loadScheme");
311        luaState_->doString("CEGUI.Window.getUnclippedOuterRect = CEGUI.Window.getUnclippedPixelRect");
312#else
313        guiSystem_ = &System::create(*guiRenderer_, resourceProvider_, 0, imageCodec_, scriptModule_);
314#endif
315
316        // Align CEGUI mouse with OIS mouse
317        guiSystem_->injectMousePosition((float)mousePosition.first, (float)mousePosition.second);
318
319        // Initialise the Lua framework and load the schemes
320        this->luaState_->doFile("InitialiseGUI.lua");
321
322        // Create the root nodes
323        this->rootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("MenuWidgets/StaticImage", "AbsoluteRootWindow");
324        this->rootWindow_->setProperty("FrameEnabled", "False");
325        this->hudRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "HUDRootWindow");
326        this->menuRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "MenuRootWindow");
327        // And connect them
328        CEGUI::System::getSingleton().setGUISheet(this->rootWindow_);
329        this->rootWindow_->addChildWindow(this->hudRootWindow_);
330        this->rootWindow_->addChildWindow(this->menuRootWindow_);
331
332        // No background to start with (sets the alpha value to 0)
333        this->setBackgroundImage("");
334
335        // Set up the sheet manager in the Lua framework
336        this->luaState_->doFile("SheetManager.lua");
337    }
338
339    void GUIManager::destroy()
340    {
341        using namespace CEGUI;
342
343#ifdef ORXONOX_OLD_CEGUI
344        safeObjectDelete(&guiSystem_);
345        safeObjectDelete(&guiRenderer_);
346        safeObjectDelete(&scriptModule_);
347#else
348        System::destroy();
349        OgreRenderer::destroyOgreResourceProvider(*resourceProvider_);
350        OgreRenderer::destroyOgreImageCodec(*imageCodec_);
351        OgreRenderer::destroy(*guiRenderer_);
352        LuaScriptModule::destroy(*scriptModule_);
353        safeObjectDelete(&ceguiLogger_);
354        safeObjectDelete(&rqListener_);
355#endif
356        safeObjectDelete(&luaState_);
357    }
358
359    void GUIManager::setConfigValues(void)
360    {
361        SetConfigValue(guiScheme_, GUIManager::defaultScheme_).description("Changes the current GUI scheme.").callback(this, &GUIManager::changedGUIScheme);
362        SetConfigValue(numScrollLines_, 1).description("How many lines to scroll in a list if the scroll wheel is used");
363        SetConfigValueExternal(outputLevelCeguiLog_, BaseWriter::getConfigurableSectionName(), "outputLevelCeguiLog", CEGUI::Standard).description("The log level of the CEGUI log file").callback(this, &GUIManager::changedCeguiOutputLevel);
364    }
365
366    void GUIManager::changedGUIScheme(void)
367    {
368    }
369
370    void GUIManager::changedCeguiOutputLevel()
371    {
372        if (this->ceguiLogger_)
373            this->ceguiLogger_->setLoggingLevel(static_cast<CEGUI::LoggingLevel>(this->outputLevelCeguiLog_));
374    }
375
376    /**
377    @brief
378        used to tick the GUI
379    @param time
380        clock which provides time value for the GUI System
381
382        Ticking the GUI means updating it with a certain regularity.
383        The elapsed time since the last call is given in the time value provided by the clock.
384        This time value is then used to provide a fluent animation of the GUI.
385    */
386    void GUIManager::preUpdate(const Clock& time)
387    {
388        assert(guiSystem_);
389        this->protectedCall(boost::bind(&CEGUI::System::injectTimePulse, _1, time.getDeltaTime()));
390    }
391
392    /**
393    @brief
394        Tells the GUIManager which SceneManager to use
395    @param camera
396        The current camera on which the GUI should be displayed on.
397
398        In fact the GUIManager needs the SceneManager and not the Camera to display the GUI.
399        This means the GUI is not bound to a camera but rather to the SceneManager.
400        Hiding the GUI when needed can therefore not be resolved by just NOT setting the current camera.
401    */
402    void GUIManager::setCamera(Ogre::Camera* camera)
403    {
404#ifdef ORXONOX_OLD_CEGUI
405        if (camera == NULL)
406            this->guiRenderer_->setTargetSceneManager(0);
407        else
408            this->guiRenderer_->setTargetSceneManager(camera->getSceneManager());
409#else
410        if (camera_ != NULL && camera_->getSceneManager() != NULL)
411            camera_->getSceneManager()->removeRenderQueueListener(rqListener_);
412        if (camera != NULL && camera->getSceneManager() != NULL)
413            camera->getSceneManager()->addRenderQueueListener(rqListener_);
414#endif
415        this->camera_ = camera;
416    }
417
418    /**
419    @brief
420        Executes Lua code
421    @param str
422        reference to string object holding the Lua code which is to be executed
423    */
424    void GUIManager::executeCode(const std::string& str)
425    {
426        this->luaState_->doString(str, rootFileInfo_);
427    }
428
429    /** Loads a GUI sheet by Lua script
430    @param name
431        The name of the GUI (like the script name, but without the extension)
432    */
433    void GUIManager::loadGUI(const std::string& name)
434    {
435        this->executeCode("loadSheet(\"" + name + "\")");
436    }
437
438    /**
439    @brief
440        Displays specified GUI on screen
441    @param name
442        The name of the GUI
443    @param bHidePrevious
444        If true all displayed GUIs on the stack, that are below this GUI are hidden.
445    @param bNoInput
446        If true the GUI is transparent to input.
447
448        The function executes the Lua function with the same name in case the GUIManager is ready.
449    */
450    /*static*/ void GUIManager::showGUI(const std::string& name, bool bHidePrevious, bool bNoInput)
451    {
452        GUIManager::getInstance().executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ")");
453    }
454
455    /**
456    @brief
457        Hack-ish. Needed for GUIOverlay.
458    */
459    void GUIManager::showGUIExtra(const std::string& name, const std::string& ptr, bool bHidePrevious, bool bNoInput)
460    {
461        this->executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ", " + ptr + ")");
462    }
463
464    /**
465    @brief
466        Hides specified GUI.
467    @param name
468        The name of the GUI.
469    */
470    /*static*/ void GUIManager::hideGUI(const std::string& name)
471    {
472        GUIManager::getInstance().executeCode("hideMenuSheet(\"" + name + "\")");
473    }
474
475    /**
476    @brief
477        Toggles specified GUI.
478        If the GUI with the input name is already shown and on the top, it is hidden, else it is shown.
479    */
480    /*static*/ void GUIManager::toggleGUI(const std::string& name, bool bHidePrevious, bool bNoInput)
481    {
482        GUIManager::getInstance().executeCode("getGUIFirstActive(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ")");
483    }
484
485    /**
486    @brief
487        Helper method to toggle a specified GUI.
488        Is called by lua.
489    */
490    void GUIManager::toggleGUIHelper(const std::string& name, bool bHidePrevious, bool bNoInput, bool show)
491    {
492        if(show)
493            GUIManager::showGUI(name, bHidePrevious, bNoInput);
494        else
495            GUIManager::hideGUI(name);
496    }
497
498    const std::string& GUIManager::createInputState(const std::string& name, tribool showCursor, tribool useKeyboard, bool bBlockJoyStick)
499    {
500        InputState* state = InputManager::getInstance().createInputState(name);
501        if (!state)
502            return BLANKSTRING;
503
504        /* Table that maps isFullScreen() and showCursor to mouseExclusive
505        isFullscreen / showCursor | True  | False | Dontcare
506        ----------------------------------------------------
507        true                      | True  | True  | Dontcare
508        ----------------------------------------------------
509        false                     | False | True  | Dontcare
510        */
511
512#ifdef ORXONOX_PLATFORM_APPLE
513        // There is no non exclusive mode on OS X yet
514        state->setMouseExclusive(true);
515#else
516        if (showCursor == dontcare)
517            state->setMouseExclusive(dontcare);
518        else if (GraphicsManager::getInstance().isFullScreen() || showCursor == false)
519            state->setMouseExclusive(true);
520        else
521            state->setMouseExclusive(false);
522#endif
523
524        if (showCursor == true)
525            state->setMouseHandler(this);
526        else if (showCursor == false)
527            state->setMouseHandler(&InputHandler::EMPTY);
528
529        if (useKeyboard == true)
530            state->setKeyHandler(this);
531        else if (useKeyboard == false)
532            state->setKeyHandler(&InputHandler::EMPTY);
533
534        if (bBlockJoyStick)
535            state->setJoyStickHandler(&InputHandler::EMPTY);
536
537        return state->getName();
538    }
539
540    void GUIManager::keyESC()
541    {
542        this->executeCode("keyESC()");
543    }
544
545    void GUIManager::setBackgroundImage(const std::string& imageSet, const std::string imageName)
546    {
547        if (imageSet.empty() || imageName.empty())
548            this->setBackgroundImage("");
549        else
550            this->setBackgroundImage("set: " + imageSet + " image: " + imageName);
551    }
552
553    void GUIManager::setBackgroundImage(const std::string& image)
554    {
555        if (image.empty())
556            this->rootWindow_->setProperty("Alpha", "0.0");
557        else
558            this->rootWindow_->setProperty("Alpha", "1.0");
559        this->rootWindow_->setProperty("Image", image);
560    }
561
562    void GUIManager::buttonPressed(const KeyEvent& evt)
563    {
564        this->protectedCall(boost::bind(&CEGUI::System::injectKeyDown, _1, evt.getKeyCode()));
565        this->protectedCall(boost::bind(&CEGUI::System::injectChar, _1, evt.getText()));
566    }
567
568    void GUIManager::buttonReleased(const KeyEvent& evt)
569    {
570        this->protectedCall(boost::bind(&CEGUI::System::injectKeyUp, _1, evt.getKeyCode()));
571    }
572
573    /**
574    @brief
575        Function receiving a mouse button pressed event.
576    @param id
577        ID of the mouse button which got pressed
578
579        This function is inherited by MouseHandler and injects the event into CEGUI.
580        It is for CEGUI to process the event.
581    */
582    void GUIManager::buttonPressed(MouseButtonCode::ByEnum id)
583    {
584        this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonDown, _1, convertButton(id)));
585    }
586
587    /**
588    @brief
589        Function receiving a mouse button released event.
590    @param id
591        ID of the mouse button which got released
592
593        This function is inherited by MouseHandler and injects the event into CEGUI.
594        It is for CEGUI to process the event.
595    */
596    void GUIManager::buttonReleased(MouseButtonCode::ByEnum id)
597    {
598        this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonUp, _1, convertButton(id)));
599    }
600
601    void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
602    {
603        this->protectedCall(boost::bind(&CEGUI::System::injectMousePosition, _1, (float)abs.x, (float)abs.y));
604    }
605
606    void GUIManager::mouseScrolled(int abs, int rel)
607    {
608        this->protectedCall(boost::bind(&CEGUI::System::injectMouseWheelChange, _1, (float)sgn(rel) * this->numScrollLines_));
609    }
610
611    /**
612        @brief Indicates that the mouse left the application's window.
613    */
614    void GUIManager::mouseLeft()
615    {
616        this->protectedCall(boost::bind(&CEGUI::System::injectMouseLeaves, _1));
617    }
618
619    /**
620    @brief
621        converts mouse event code to CEGUI event code
622    @param button
623        code of the mouse button as we use it in Orxonox
624    @return
625        code of the mouse button as it is used by CEGUI
626
627        Simple conversion from mouse event code in Orxonox to the one used in CEGUI.
628     */
629    static inline CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button)
630    {
631        switch (button)
632        {
633        case MouseButtonCode::Left:
634            return CEGUI::LeftButton;
635
636        case MouseButtonCode::Right:
637            return CEGUI::RightButton;
638
639        case MouseButtonCode::Middle:
640            return CEGUI::MiddleButton;
641
642        case MouseButtonCode::Button3:
643            return CEGUI::X1Button;
644
645        case MouseButtonCode::Button4:
646            return CEGUI::X2Button;
647
648        default:
649            return CEGUI::NoButton;
650        }
651    }
652
653    /** Executes a CEGUI function normally, but catches CEGUI::ScriptException.
654        When a ScriptException occurs, the error message will be displayed and
655        the program carries on.
656    @remarks
657        The exception behaviour may pose problems if the code is not written
658        exception-safe (and you can forget about that in Lua). The program might
659        be left in an undefined state. But otherwise one script error would
660        terminate the whole program...
661    @note
662        Your life gets easier if you use boost::bind to create the object/function.
663    @param function
664        Any callable object/function that takes this->guiSystem_ as its only parameter.
665    @return
666        True if input was handled, false otherwise. A caught exception yields true.
667    */
668    template <typename FunctionType>
669    bool GUIManager::protectedCall(FunctionType function)
670    {
671        try
672        {
673            return function(this->guiSystem_);
674        }
675        catch (CEGUI::ScriptException& ex)
676        {
677            // Display the error and proceed. See @remarks why this can be dangerous.
678            orxout(internal_error) << ex.getMessage() << endl;
679            return true;
680        }
681    }
682
683    /**
684    @brief
685        Subscribe the input function to the input event for the input window.
686        This is a helper to be used in lua, because subscribeScriptedEvent() doesn't work in lua.
687    @param window
688        The window for which the event is subscribed.
689    @param event
690        The type of event to which we subscribe.
691    @param function
692        The function that is called when the event occurs.
693    */
694    void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function)
695    {
696        window->subscribeScriptedEvent(event, function);
697    }
698
699    /**
700    @brief
701        Set the input tooltip text for the input ListboxItem.
702    @param item
703        The ListboxItem for which the tooltip should be set.
704    @param tooltip
705        The tooltip text that should be set.
706    */
707    void GUIManager::setTooltipTextHelper(CEGUI::ListboxItem* item, const std::string& tooltip)
708    {
709        item->setTooltipText(tooltip);
710    }
711
712    /**
713    @brief
714        Set whether the tooltips for the input Listbox are enabled.
715    @param listbox
716        The Listbox for which to enable (or disable) tooltips.
717    @param enabled
718        Whether to enable or disable the tooltips.
719    */
720    void GUIManager::setItemTooltipsEnabledHelper(CEGUI::Listbox* listbox, bool enabled)
721    {
722        listbox->setItemTooltipsEnabled(enabled);
723    }
724
725    /** Helper method to get the developer's mode without having to export Core.h.
726    @see Core::inDevMode
727    */
728    /*static*/ bool GUIManager::inDevMode()
729    {
730         return Core::getInstance().inDevMode();
731    }
732
733    /**
734        @brief Callback of window event listener, called if the window is resized. Sets the display size of CEGUI.
735    */
736    void GUIManager::windowResized(unsigned int newWidth, unsigned int newHeight)
737    {
738        this->guiRenderer_->setDisplaySize(CEGUI::Size((float)newWidth, (float)newHeight));
739    }
740
741    /**
742        @brief Notify CEGUI if the windows loses the focus (stops highlighting of menu items, etc).
743    */
744    void GUIManager::windowFocusChanged(bool bFocus)
745    {
746        if (!bFocus)
747            this->mouseLeft();
748    }
749
750    /**
751    @brief
752        Adds a new freetype font to the CEGUI system.
753    @param name
754        The name of the new font.
755    @param size
756        The font size of the new font in pixels.
757        @param fontName
758        The filename of the font.
759    */
760    /*static*/ void GUIManager::addFontHelper(const std::string& name, int size, const std::string& fontName)
761    {
762#ifdef ORXONOX_OLD_CEGUI
763        if(CEGUI::FontManager::getSingleton().isFontPresent(name)) // If a font with that name already exists.
764            return;
765
766        CEGUI::Font* font = NULL;
767        CEGUI::XMLAttributes xmlAttributes;
768
769        // Attributes specified within CEGUIFont
770        xmlAttributes.add("Name", name);
771        xmlAttributes.add("Filename", fontName);
772        xmlAttributes.add("ResourceGroup", "");
773        xmlAttributes.add("AutoScaled", "true");
774        xmlAttributes.add("NativeHorzRes", "800");
775        xmlAttributes.add("NativeVertRes", "600");
776
777        // Attributes specified within CEGUIXMLAttributes
778        xmlAttributes.add("Size", multi_cast<std::string>(size));
779        xmlAttributes.add("AntiAlias", "true");
780
781        font = CEGUI::FontManager::getSingleton().createFont("FreeType", xmlAttributes);
782        if(font != NULL)
783            font->load();
784#else
785        if(CEGUI::FontManager::getSingleton().isDefined(name)) // If a font with that name already exists.
786            return;
787
788        CEGUI::FontManager::getSingleton().createFreeTypeFont(name, (float)size, true, fontName, "", true, 800.0f, 600.0f);
789#endif
790    }
791
792}
Note: See TracBrowser for help on using the repository browser.