Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9873 was 9759, checked in by landauf, 11 years ago

fixed warning (msvc)

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