Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10741 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

  • Property svn:eol-style set to native
File size: 33.2 KB
RevLine 
[1638]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
[2896]24 *      Benjamin Knecht
[1638]25 *   Co-authors:
[3196]26 *      ...
[1638]27 *
28 */
29
30#include "GUIManager.h"
31
[8467]32#include <fstream>
[8351]33#include <memory>
[6746]34#include <boost/bind.hpp>
[8351]35#include <OgreRenderQueue.h>
36#include <OgreRenderWindow.h>
[8079]37
[9675]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
[3196]65
[8351]66#ifdef ORXONOX_OLD_CEGUI
[9675]67#   include <CEGUILua.h>
68#   include <ogreceguirenderer/OgreCEGUIRenderer.h>
[8351]69extern "C" {
[9675]70#   include <lauxlib.h>
[8351]71}
[2710]72#else
[9675]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>
[2710]89#endif
90
[8527]91#if defined(ORXONOX_PLATFORM_WINDOWS) && !defined(ORXONOX_COMPILER_MINGW)
[8467]92#  include <windows.h>
93#endif
94
[5929]95#include "util/Clock.h"
[6417]96#include "util/Convert.h"
[8858]97#include "util/Output.h"
[1764]98#include "util/Exception.h"
[8530]99#include "util/Math.h"
[3280]100#include "util/OrxAssert.h"
[8858]101#include "util/output/BaseWriter.h"
[9667]102#include "config/ConfigValueIncludes.h"
[6417]103#include "Core.h"
[7801]104#include "CoreIncludes.h"
[7876]105#include "Game.h"
[6746]106#include "GraphicsManager.h"
[5695]107#include "LuaState.h"
[10624]108#include "ConfigurablePaths.h"
[5695]109#include "Resource.h"
[10624]110#include "command/ConsoleCommandIncludes.h"
[6746]111#include "input/InputManager.h"
112#include "input/InputState.h"
113#include "input/KeyBinderManager.h"
[1638]114
115namespace orxonox
116{
[6417]117    static void key_esc()
118        { GUIManager::getInstance().keyESC(); }
[7284]119    SetConsoleCommand("keyESC", &key_esc);
[6417]120
[3280]121    class CEGUILogger : public CEGUI::DefaultLogger
122    {
123    public:
[5929]124        void logEvent(const CEGUI::String& message, CEGUI::LoggingLevel level = CEGUI::Standard)
[3280]125        {
[8858]126            OutputLevel orxonoxLevel = level::debug_output;
[3280]127            switch (level)
128            {
[8858]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;
[8351]134                default: OrxAssert(false, "CEGUI log level out of range, inspect immediately!");
[3280]135            }
136
[8858]137            orxout(orxonoxLevel, context::cegui) << message << endl;
138
[3280]139            CEGUI::DefaultLogger::logEvent(message, level);
140        }
[8467]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
[8527]149#if defined(ORXONOX_PLATFORM_WINDOWS) && !defined(ORXONOX_COMPILER_MINGW)
[8467]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
[8527]184#if defined(ORXONOX_PLATFORM_WINDOWS) && !defined(ORXONOX_COMPILER_MINGW)
[8467]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
[3280]200    };
201
[8351]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
[8419]228        void renderQueueStarted(Ogre::uint8 id, const Ogre::String& invocation, bool& skipThisQueue)
[8351]229        {
[8419]230            if (id == Ogre::RENDER_QUEUE_OVERLAY && invocation.empty())
[8439]231            {
[9675]232#if CEGUI_VERSION >= 0x000800
233                CEGUI::System::getSingleton().renderAllGUIContexts();
234#else
[8351]235                CEGUI::System::getSingleton().renderGUI();
[9675]236#endif
[8439]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            }
[8351]244        }
245    };
246#endif
247
[3196]248    static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button);
[3339]249
[3366]250    GUIManager* GUIManager::singletonPtr_s = 0;
[9016]251    /*static*/ const std::string GUIManager::defaultScheme_ = "TaharezGreen"; //Alternative: Orxonox (not fully complete yet, see the graphics menu)
[1646]252
[7403]253    SetConsoleCommand("showGUI", &GUIManager::showGUI).defaultValue(1, false).defaultValue(2, false);
[7284]254    SetConsoleCommand("hideGUI", &GUIManager::hideGUI);
[8079]255    SetConsoleCommand("toggleGUI", &GUIManager::toggleGUI).defaultValue(1, false).defaultValue(2, false);
[6417]256
[10624]257    RegisterAbstractClass(GUIManager).inheritsFrom<WindowEventListener>();
258
[2896]259    /**
260    @brief
[3338]261        Constructs the GUIManager by starting up CEGUI
[2896]262
263        Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine.
264        The log is set up and connected to the CEGUILogger.
265        After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code.
266        Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically).
[3338]267    @return true if success, otherwise false
[2896]268    */
[6746]269    GUIManager::GUIManager(const std::pair<int, int>& mousePosition)
[8423]270        : guiRenderer_(NULL)
[8351]271        , resourceProvider_(NULL)
272#ifndef ORXONOX_OLD_CEGUI
[8411]273        , rqListener_(NULL)
[8351]274        , imageCodec_(NULL)
275#endif
276        , luaState_(NULL)
277        , scriptModule_(NULL)
278        , guiSystem_(NULL)
[8411]279        , ceguiLogger_(NULL)
280        , rootWindow_(NULL)
281        , hudRootWindow_(NULL)
282        , menuRootWindow_(NULL)
[5929]283        , camera_(NULL)
[8423]284        , destructionHelper_(this)
[1638]285    {
[9667]286        RegisterObject(GUIManager);
[8858]287
288        orxout(internal_status) << "initializing GUIManager..." << endl;
289
[7801]290        this->setConfigValues();
291
[1638]292        using namespace CEGUI;
293
[8858]294        orxout(internal_info) << "Initialising CEGUI." << endl;
[1638]295
[8706]296        this->oldCEGUI_ = false;
[8858]297
[5695]298        // Note: No SceneManager specified yet
[8351]299#ifdef ORXONOX_OLD_CEGUI
300        guiRenderer_ = new OgreCEGUIRenderer(GraphicsManager::getInstance().getRenderWindow(), Ogre::RENDER_QUEUE_OVERLAY, false, 3000);
[5695]301        resourceProvider_ = guiRenderer_->createResourceProvider();
[8706]302        this->oldCEGUI_ = true;
[8351]303#else
304        guiRenderer_ = &OgreRenderer::create(*GraphicsManager::getInstance().getRenderWindow());
305        // We use our own RenderQueueListener so we can draw UNDER overlays
306        guiRenderer_->setFrameControlExecutionEnabled(false);
307        rqListener_ = new RQListener();
308        resourceProvider_ = &OgreRenderer::createOgreResourceProvider();
309        imageCodec_ = &OgreRenderer::createOgreImageCodec();
310#endif
[7709]311        resourceProvider_->setDefaultResourceGroup("General");
[1776]312
[6749]313        // Setup scripting
[8351]314        luaState_ = new LuaState();
[6417]315        rootFileInfo_ = Resource::getInfo("InitialiseGUI.lua");
316        // This is necessary to ensure that input events also use the right resource info when triggering lua functions
317        luaState_->setDefaultResourceInfo(this->rootFileInfo_);
[8351]318#ifdef ORXONOX_OLD_CEGUI
319        scriptModule_ = new LuaScriptModule(luaState_->getInternalLuaState());
320        // Ugly workaround: older CEGUILua versions don't initialise the member
321        // d_activeErrFuncIndex at all. That leads to "error in error handling"
322        // problems when a Lua error occurs.
323        // We fix this by setting the member manually.
324        reinterpret_cast<LuaScriptModuleWorkaround*>(scriptModule_)->d_activeErrFuncIndex = LUA_NOREF;
325        luaState_->doString("ORXONOX_OLD_CEGUI = true");
326#else
327        scriptModule_ = &LuaScriptModule::create(luaState_->getInternalLuaState());
328#endif
[6763]329        scriptModule_->setDefaultPCallErrorHandler(LuaState::ERROR_HANDLER_NAME);
[1638]330
[5695]331        // Create our own logger to specify the filepath
332        std::auto_ptr<CEGUILogger> ceguiLogger(new CEGUILogger());
[10624]333        ceguiLogger->setLogFilename(ConfigurablePaths::getLogPathString() + "cegui.log");
[8858]334        ceguiLogger->setLoggingLevel(static_cast<CEGUI::LoggingLevel>(this->outputLevelCeguiLog_));
[5695]335        this->ceguiLogger_ = ceguiLogger.release();
[2710]336
[6749]337        // Create the CEGUI system singleton
[8351]338#ifdef ORXONOX_OLD_CEGUI
339        guiSystem_ = new System(guiRenderer_, resourceProvider_, 0, scriptModule_);
340        // Add functions that have been renamed in newer versions
341        luaState_->doString("CEGUI.SchemeManager.create = CEGUI.SchemeManager.loadScheme");
342        luaState_->doString("CEGUI.Window.getUnclippedOuterRect = CEGUI.Window.getUnclippedPixelRect");
[9050]343        luaState_->doString("CEGUI.ImagesetManager.createFromImageFile= CEGUI.ImagesetManager.createImagesetFromImageFile");
[8351]344#else
345        guiSystem_ = &System::create(*guiRenderer_, resourceProvider_, 0, imageCodec_, scriptModule_);
346#endif
[1776]347
[9576]348        CEGUI::String defaultXMLParserName = CEGUI::System::getSingleton().getDefaultXMLParserName();
349        try
350        {
351            // Force Xerces parser (CEGUI 0.7.5+)
352            CEGUI::System::getSingleton().setXMLParser("XercesParser");
353        }
[9759]354        catch (const CEGUI::GenericException&)
[9576]355        {
356            // Fall back to default parser
357            orxout(internal_warning) << "Cannot use XercesParser for CEGUI - using " << defaultXMLParserName << " instead" << endl;
358            CEGUI::System::getSingleton().setXMLParser(defaultXMLParserName);
359        }
[9575]360
[5695]361        // Align CEGUI mouse with OIS mouse
[9675]362#if CEGUI_VERSION >= 0x000800
363        guiSystem_->getDefaultGUIContext().injectMousePosition((float)mousePosition.first, (float)mousePosition.second);
364#else
[6502]365        guiSystem_->injectMousePosition((float)mousePosition.first, (float)mousePosition.second);
[9675]366#endif
[5695]367
[6746]368        // Initialise the Lua framework and load the schemes
[8861]369        orxout(user_info) << "Loading user interface..." << endl;
[6746]370        this->luaState_->doFile("InitialiseGUI.lua");
371
372        // Create the root nodes
373        this->rootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("MenuWidgets/StaticImage", "AbsoluteRootWindow");
374        this->rootWindow_->setProperty("FrameEnabled", "False");
375        this->hudRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "HUDRootWindow");
376        this->menuRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "MenuRootWindow");
377        // And connect them
[9675]378#if CEGUI_VERSION >= 0x000800
379        CEGUI::System::getSingleton().getDefaultGUIContext().setRootWindow(this->rootWindow_);
380        this->rootWindow_->addChild(this->hudRootWindow_);
381        this->rootWindow_->addChild(this->menuRootWindow_);
382#else
[6746]383        CEGUI::System::getSingleton().setGUISheet(this->rootWindow_);
384        this->rootWindow_->addChildWindow(this->hudRootWindow_);
385        this->rootWindow_->addChildWindow(this->menuRootWindow_);
[9675]386#endif
[6746]387
[6749]388        // No background to start with (sets the alpha value to 0)
389        this->setBackgroundImage("");
390
[6746]391        // Set up the sheet manager in the Lua framework
392        this->luaState_->doFile("SheetManager.lua");
[8858]393
394        orxout(internal_status) << "finished initializing GUIManager" << endl;
[3338]395    }
[1776]396
[8423]397    void GUIManager::destroy()
[3338]398    {
[8858]399        orxout(internal_status) << "destroying GUIManager..." << endl;
400
[8351]401        using namespace CEGUI;
402
403#ifdef ORXONOX_OLD_CEGUI
[8423]404        safeObjectDelete(&guiSystem_);
405        safeObjectDelete(&guiRenderer_);
406        safeObjectDelete(&scriptModule_);
[8351]407#else
408        System::destroy();
409        OgreRenderer::destroyOgreResourceProvider(*resourceProvider_);
410        OgreRenderer::destroyOgreImageCodec(*imageCodec_);
411        OgreRenderer::destroy(*guiRenderer_);
412        LuaScriptModule::destroy(*scriptModule_);
[8423]413        safeObjectDelete(&ceguiLogger_);
414        safeObjectDelete(&rqListener_);
[8351]415#endif
[8423]416        safeObjectDelete(&luaState_);
[8858]417
418        orxout(internal_status) << "finished destroying GUIManager" << endl;
[1638]419    }
420
[7801]421    void GUIManager::setConfigValues(void)
422    {
[8858]423        SetConfigValue(guiScheme_, GUIManager::defaultScheme_).description("Changes the current GUI scheme.").callback(this, &GUIManager::changedGUIScheme);
[8530]424        SetConfigValue(numScrollLines_, 1).description("How many lines to scroll in a list if the scroll wheel is used");
[8862]425        SetConfigValue(bPreloadMenuSheets_, false).description("Pre-load menu sheets during startup");
426
[8858]427        SetConfigValueExternal(outputLevelCeguiLog_, BaseWriter::getConfigurableSectionName(), "outputLevelCeguiLog", CEGUI::Standard).description("The log level of the CEGUI log file").callback(this, &GUIManager::changedCeguiOutputLevel);
[7801]428    }
429
430    void GUIManager::changedGUIScheme(void)
431    {
432    }
433
[8858]434    void GUIManager::changedCeguiOutputLevel()
435    {
436        if (this->ceguiLogger_)
437            this->ceguiLogger_->setLoggingLevel(static_cast<CEGUI::LoggingLevel>(this->outputLevelCeguiLog_));
438    }
439
[2896]440    /**
441    @brief
442        used to tick the GUI
443    @param time
444        clock which provides time value for the GUI System
445
446        Ticking the GUI means updating it with a certain regularity.
447        The elapsed time since the last call is given in the time value provided by the clock.
448        This time value is then used to provide a fluent animation of the GUI.
449    */
[6417]450    void GUIManager::preUpdate(const Clock& time)
[1638]451    {
[2896]452        assert(guiSystem_);
[9675]453        this->protectedCeguiSystemCall(boost::bind(&CEGUI::System::injectTimePulse, _1, time.getDeltaTime()));
[2896]454    }
[1638]455
[2896]456    /**
457    @brief
458        Tells the GUIManager which SceneManager to use
459    @param camera
460        The current camera on which the GUI should be displayed on.
461
462        In fact the GUIManager needs the SceneManager and not the Camera to display the GUI.
463        This means the GUI is not bound to a camera but rather to the SceneManager.
[3337]464        Hiding the GUI when needed can therefore not be resolved by just NOT setting the current camera.
[2896]465    */
466    void GUIManager::setCamera(Ogre::Camera* camera)
[1638]467    {
[8351]468#ifdef ORXONOX_OLD_CEGUI
[2927]469        if (camera == NULL)
470            this->guiRenderer_->setTargetSceneManager(0);
471        else
472            this->guiRenderer_->setTargetSceneManager(camera->getSceneManager());
[8351]473#else
474        if (camera_ != NULL && camera_->getSceneManager() != NULL)
475            camera_->getSceneManager()->removeRenderQueueListener(rqListener_);
476        if (camera != NULL && camera->getSceneManager() != NULL)
477            camera->getSceneManager()->addRenderQueueListener(rqListener_);
478#endif
479        this->camera_ = camera;
[2896]480    }
481
482    /**
483    @brief
[3338]484        Executes Lua code
485    @param str
486        reference to string object holding the Lua code which is to be executed
487    */
488    void GUIManager::executeCode(const std::string& str)
489    {
[5695]490        this->luaState_->doString(str, rootFileInfo_);
[3338]491    }
492
[6746]493    /** Loads a GUI sheet by Lua script
494    @param name
495        The name of the GUI (like the script name, but without the extension)
496    */
497    void GUIManager::loadGUI(const std::string& name)
498    {
499        this->executeCode("loadSheet(\"" + name + "\")");
500    }
501
[3338]502    /**
503    @brief
[2896]504        Displays specified GUI on screen
505    @param name
506        The name of the GUI
[7401]507    @param bHidePrevious
508        If true all displayed GUIs on the stack, that are below this GUI are hidden.
[7403]509    @param bNoInput
510        If true the GUI is transparent to input.
[2896]511
512        The function executes the Lua function with the same name in case the GUIManager is ready.
513    */
[7403]514    /*static*/ void GUIManager::showGUI(const std::string& name, bool bHidePrevious, bool bNoInput)
[2896]515    {
[7403]516        GUIManager::getInstance().executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ")");
[1638]517    }
518
[6417]519    /**
520    @brief
521        Hack-ish. Needed for GUIOverlay.
522    */
[7403]523    void GUIManager::showGUIExtra(const std::string& name, const std::string& ptr, bool bHidePrevious, bool bNoInput)
[6417]524    {
[7403]525        this->executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ", " + ptr + ")");
[6417]526    }
527
528    /**
529    @brief
530        Hides specified GUI.
531    @param name
532        The name of the GUI.
533    */
534    /*static*/ void GUIManager::hideGUI(const std::string& name)
535    {
[6746]536        GUIManager::getInstance().executeCode("hideMenuSheet(\"" + name + "\")");
[6417]537    }
538
[8079]539    /**
540    @brief
541        Toggles specified GUI.
542        If the GUI with the input name is already shown and on the top, it is hidden, else it is shown.
543    */
544    /*static*/ void GUIManager::toggleGUI(const std::string& name, bool bHidePrevious, bool bNoInput)
545    {
546        GUIManager::getInstance().executeCode("getGUIFirstActive(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ")");
547    }
548
549    /**
550    @brief
551        Helper method to toggle a specified GUI.
552        Is called by lua.
553    */
554    void GUIManager::toggleGUIHelper(const std::string& name, bool bHidePrevious, bool bNoInput, bool show)
555    {
556        if(show)
557            GUIManager::showGUI(name, bHidePrevious, bNoInput);
558        else
559            GUIManager::hideGUI(name);
560    }
561
[8729]562    const std::string& GUIManager::createInputState(const std::string& name, tribool showCursor, tribool useKeyboard, bool bBlockJoyStick)
[6746]563    {
564        InputState* state = InputManager::getInstance().createInputState(name);
[7811]565        if (!state)
566            return BLANKSTRING;
[6746]567
568        /* Table that maps isFullScreen() and showCursor to mouseExclusive
569        isFullscreen / showCursor | True  | False | Dontcare
570        ----------------------------------------------------
571        true                      | True  | True  | Dontcare
572        ----------------------------------------------------
573        false                     | False | True  | Dontcare
574        */
[8351]575
576#ifdef ORXONOX_PLATFORM_APPLE
577        // There is no non exclusive mode on OS X yet
[8729]578        state->setMouseExclusive(true);
[8351]579#else
[8729]580        if (showCursor == dontcare)
581            state->setMouseExclusive(dontcare);
582        else if (GraphicsManager::getInstance().isFullScreen() || showCursor == false)
583            state->setMouseExclusive(true);
[6746]584        else
[8729]585            state->setMouseExclusive(false);
[8351]586#endif
[6746]587
[8729]588        if (showCursor == true)
[6746]589            state->setMouseHandler(this);
[8729]590        else if (showCursor == false)
[6746]591            state->setMouseHandler(&InputHandler::EMPTY);
592
[8729]593        if (useKeyboard == true)
[6746]594            state->setKeyHandler(this);
[8729]595        else if (useKeyboard == false)
[6746]596            state->setKeyHandler(&InputHandler::EMPTY);
597
598        if (bBlockJoyStick)
599            state->setJoyStickHandler(&InputHandler::EMPTY);
600
601        return state->getName();
602    }
603
[6417]604    void GUIManager::keyESC()
605    {
606        this->executeCode("keyESC()");
607    }
608
[6746]609    void GUIManager::setBackgroundImage(const std::string& imageSet, const std::string imageName)
[6417]610    {
[6746]611        if (imageSet.empty() || imageName.empty())
612            this->setBackgroundImage("");
613        else
614            this->setBackgroundImage("set: " + imageSet + " image: " + imageName);
[6417]615    }
616
[6746]617    void GUIManager::setBackgroundImage(const std::string& image)
618    {
619        if (image.empty())
620            this->rootWindow_->setProperty("Alpha", "0.0");
621        else
622            this->rootWindow_->setProperty("Alpha", "1.0");
623        this->rootWindow_->setProperty("Image", image);
624    }
625
[7163]626    void GUIManager::buttonPressed(const KeyEvent& evt)
[3196]627    {
[9675]628#if CEGUI_VERSION >= 0x000800
629        this->protectedCeguiContextCall(boost::bind(&CEGUI::GUIContext::injectKeyDown, _1, (CEGUI::Key::Scan) evt.getKeyCode())); // TODO: will this cast always work?
630        this->protectedCeguiContextCall(boost::bind(&CEGUI::GUIContext::injectChar, _1, evt.getText()));
631#else
632        this->protectedCeguiSystemCall(boost::bind(&CEGUI::System::injectKeyDown, _1, evt.getKeyCode()));
633        this->protectedCeguiSystemCall(boost::bind(&CEGUI::System::injectChar, _1, evt.getText()));
634#endif
[3196]635    }
[6746]636
[7163]637    void GUIManager::buttonReleased(const KeyEvent& evt)
[3196]638    {
[9675]639#if CEGUI_VERSION >= 0x000800
640        this->protectedCeguiContextCall(boost::bind(&CEGUI::GUIContext::injectKeyUp, _1, (CEGUI::Key::Scan) evt.getKeyCode())); // TODO: will this cast always work?
641#else
642        this->protectedCeguiSystemCall(boost::bind(&CEGUI::System::injectKeyUp, _1, evt.getKeyCode()));
643#endif
[3196]644    }
645
[2896]646    /**
647    @brief
648        Function receiving a mouse button pressed event.
649    @param id
650        ID of the mouse button which got pressed
[1638]651
[2896]652        This function is inherited by MouseHandler and injects the event into CEGUI.
653        It is for CEGUI to process the event.
654    */
[3327]655    void GUIManager::buttonPressed(MouseButtonCode::ByEnum id)
[1638]656    {
[9675]657#if CEGUI_VERSION >= 0x000800
658        this->protectedCeguiContextCall(boost::bind(&CEGUI::GUIContext::injectMouseButtonDown, _1, convertButton(id)));
659#else
660        this->protectedCeguiSystemCall(boost::bind(&CEGUI::System::injectMouseButtonDown, _1, convertButton(id)));
661#endif
[1638]662    }
663
[2896]664    /**
665    @brief
666        Function receiving a mouse button released event.
667    @param id
668        ID of the mouse button which got released
669
670        This function is inherited by MouseHandler and injects the event into CEGUI.
671        It is for CEGUI to process the event.
672    */
[3327]673    void GUIManager::buttonReleased(MouseButtonCode::ByEnum id)
[1638]674    {
[9675]675#if CEGUI_VERSION >= 0x000800
676        this->protectedCeguiContextCall(boost::bind(&CEGUI::GUIContext::injectMouseButtonUp, _1, convertButton(id)));
677#else
678        this->protectedCeguiSystemCall(boost::bind(&CEGUI::System::injectMouseButtonUp, _1, convertButton(id)));
679#endif
[1638]680    }
681
[3196]682    void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
683    {
[9675]684#if CEGUI_VERSION >= 0x000800
685        this->protectedCeguiContextCall(boost::bind(&CEGUI::GUIContext::injectMousePosition, _1, (float)abs.x, (float)abs.y));
686#else
687        this->protectedCeguiSystemCall(boost::bind(&CEGUI::System::injectMousePosition, _1, (float)abs.x, (float)abs.y));
688#endif
[3196]689    }
[6746]690
[3196]691    void GUIManager::mouseScrolled(int abs, int rel)
692    {
[9675]693#if CEGUI_VERSION >= 0x000800
694        this->protectedCeguiContextCall(boost::bind(&CEGUI::GUIContext::injectMouseWheelChange, _1, (float)sgn(rel) * this->numScrollLines_));
695#else
696        this->protectedCeguiSystemCall(boost::bind(&CEGUI::System::injectMouseWheelChange, _1, (float)sgn(rel) * this->numScrollLines_));
697#endif
[3196]698    }
699
[2896]700    /**
[7874]701        @brief Indicates that the mouse left the application's window.
702    */
703    void GUIManager::mouseLeft()
704    {
[9675]705#if CEGUI_VERSION >= 0x000800
706        this->protectedCeguiContextCall(boost::bind(&CEGUI::GUIContext::injectMouseLeaves, _1));
707#else
708        this->protectedCeguiSystemCall(boost::bind(&CEGUI::System::injectMouseLeaves, _1));
709#endif
[7874]710    }
711
712    /**
[2896]713    @brief
714        converts mouse event code to CEGUI event code
715    @param button
716        code of the mouse button as we use it in Orxonox
717    @return
718        code of the mouse button as it is used by CEGUI
[1638]719
[6105]720        Simple conversion from mouse event code in Orxonox to the one used in CEGUI.
[2896]721     */
[3196]722    static inline CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button)
[1638]723    {
724        switch (button)
725        {
[1887]726        case MouseButtonCode::Left:
[1638]727            return CEGUI::LeftButton;
728
[1887]729        case MouseButtonCode::Right:
[1638]730            return CEGUI::RightButton;
731
[1887]732        case MouseButtonCode::Middle:
[1638]733            return CEGUI::MiddleButton;
734
[1887]735        case MouseButtonCode::Button3:
[1638]736            return CEGUI::X1Button;
737
[1887]738        case MouseButtonCode::Button4:
[1638]739            return CEGUI::X2Button;
740
741        default:
742            return CEGUI::NoButton;
743        }
744    }
[6417]745
[6746]746    /** Executes a CEGUI function normally, but catches CEGUI::ScriptException.
747        When a ScriptException occurs, the error message will be displayed and
748        the program carries on.
749    @remarks
750        The exception behaviour may pose problems if the code is not written
751        exception-safe (and you can forget about that in Lua). The program might
752        be left in an undefined state. But otherwise one script error would
753        terminate the whole program...
754    @note
755        Your life gets easier if you use boost::bind to create the object/function.
756    @param function
757        Any callable object/function that takes this->guiSystem_ as its only parameter.
758    @return
759        True if input was handled, false otherwise. A caught exception yields true.
760    */
[9675]761    template <typename FunctionType, typename ObjectType>
762    bool GUIManager::protectedCall(FunctionType function, ObjectType object)
[6746]763    {
764        try
765        {
[9675]766            return function(object);
[6746]767        }
768        catch (CEGUI::ScriptException& ex)
769        {
770            // Display the error and proceed. See @remarks why this can be dangerous.
[8858]771            orxout(internal_error) << ex.getMessage() << endl;
[6746]772            return true;
773        }
774    }
775
[9675]776    template <typename FunctionType>
777    bool GUIManager::protectedCeguiSystemCall(FunctionType function)
778    {
779        return this->protectedCall(function, this->guiSystem_);
780    }
781
782#if CEGUI_VERSION >= 0x000800
783    template <typename FunctionType>
784    bool GUIManager::protectedCeguiContextCall(FunctionType function)
785    {
786        return this->protectedCall(function, this->guiSystem_->getDefaultGUIContext());
787    }
788#endif
789
[7648]790    /**
791    @brief
792        Subscribe the input function to the input event for the input window.
793        This is a helper to be used in lua, because subscribeScriptedEvent() doesn't work in lua.
794    @param window
795        The window for which the event is subscribed.
796    @param event
797        The type of event to which we subscribe.
798    @param function
799        The function that is called when the event occurs.
800    */
[6417]801    void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function)
802    {
803        window->subscribeScriptedEvent(event, function);
804    }
[7648]805
806    /**
807    @brief
808        Set the input tooltip text for the input ListboxItem.
809    @param item
810        The ListboxItem for which the tooltip should be set.
811    @param tooltip
812        The tooltip text that should be set.
813    */
814    void GUIManager::setTooltipTextHelper(CEGUI::ListboxItem* item, const std::string& tooltip)
815    {
816        item->setTooltipText(tooltip);
817    }
818
819    /**
820    @brief
821        Set whether the tooltips for the input Listbox are enabled.
822    @param listbox
823        The Listbox for which to enable (or disable) tooltips.
824    @param enabled
[8351]825        Whether to enable or disable the tooltips.
[7648]826    */
827    void GUIManager::setItemTooltipsEnabledHelper(CEGUI::Listbox* listbox, bool enabled)
828    {
829        listbox->setItemTooltipsEnabled(enabled);
830    }
831
[8729]832    /** Helper method to get the developer's mode without having to export Core.h.
833    @see Core::inDevMode
834    */
835    /*static*/ bool GUIManager::inDevMode()
836    {
[10624]837         return Core::getInstance().getConfig()->inDevMode();
[8729]838    }
839
[7873]840    /**
841        @brief Callback of window event listener, called if the window is resized. Sets the display size of CEGUI.
842    */
843    void GUIManager::windowResized(unsigned int newWidth, unsigned int newHeight)
844    {
[9675]845#if CEGUI_VERSION >= 0x000800
846        this->guiRenderer_->setDisplaySize(CEGUI::Sizef((float)newWidth, (float)newHeight));
847#else
[8079]848        this->guiRenderer_->setDisplaySize(CEGUI::Size((float)newWidth, (float)newHeight));
[9675]849#endif
[10279]850        this->rootWindow_->setSize(CEGUI::UVector2(CEGUI::UDim(1, (float)newWidth), CEGUI::UDim(1, (float)newHeight)));
[7873]851    }
[7874]852
853    /**
[8079]854        @brief Notify CEGUI if the windows loses the focus (stops highlighting of menu items, etc).
[7874]855    */
856    void GUIManager::windowFocusChanged(bool bFocus)
857    {
858        if (!bFocus)
859            this->mouseLeft();
860    }
[7876]861
[8706]862    /**
863    @brief
864        Adds a new freetype font to the CEGUI system.
865    @param name
866        The name of the new font.
867    @param size
868        The font size of the new font in pixels.
869        @param fontName
870        The filename of the font.
871    */
872    /*static*/ void GUIManager::addFontHelper(const std::string& name, int size, const std::string& fontName)
873    {
874#ifdef ORXONOX_OLD_CEGUI
875        if(CEGUI::FontManager::getSingleton().isFontPresent(name)) // If a font with that name already exists.
876            return;
877
878        CEGUI::Font* font = NULL;
879        CEGUI::XMLAttributes xmlAttributes;
880
881        // Attributes specified within CEGUIFont
882        xmlAttributes.add("Name", name);
883        xmlAttributes.add("Filename", fontName);
884        xmlAttributes.add("ResourceGroup", "");
885        xmlAttributes.add("AutoScaled", "true");
886        xmlAttributes.add("NativeHorzRes", "800");
887        xmlAttributes.add("NativeVertRes", "600");
888
889        // Attributes specified within CEGUIXMLAttributes
890        xmlAttributes.add("Size", multi_cast<std::string>(size));
891        xmlAttributes.add("AntiAlias", "true");
892
893        font = CEGUI::FontManager::getSingleton().createFont("FreeType", xmlAttributes);
894        if(font != NULL)
895            font->load();
896#else
897        if(CEGUI::FontManager::getSingleton().isDefined(name)) // If a font with that name already exists.
898            return;
899
[9675]900    #if CEGUI_VERSION >= 0x000800
901        CEGUI::FontManager::getSingleton().createFreeTypeFont(name, (float)size, true, fontName, "", CEGUI::ASM_Both, CEGUI::Sizef(800.0f, 600.0f));
902    #else
[8706]903        CEGUI::FontManager::getSingleton().createFreeTypeFont(name, (float)size, true, fontName, "", true, 800.0f, 600.0f);
[9675]904    #endif
[8706]905#endif
906    }
907
[1638]908}
Note: See TracBrowser for help on using the repository browser.