Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10495 was 10279, checked in by landauf, 10 years ago

fixed msvc warnings

  • Property svn:eol-style set to native
File size: 33.1 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"
[5929]108#include "PathConfig.h"
[5695]109#include "Resource.h"
[7284]110#include "command/ConsoleCommand.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
[2896]257    /**
258    @brief
[3338]259        Constructs the GUIManager by starting up CEGUI
[2896]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).
[3338]265    @return true if success, otherwise false
[2896]266    */
[6746]267    GUIManager::GUIManager(const std::pair<int, int>& mousePosition)
[8423]268        : guiRenderer_(NULL)
[8351]269        , resourceProvider_(NULL)
270#ifndef ORXONOX_OLD_CEGUI
[8411]271        , rqListener_(NULL)
[8351]272        , imageCodec_(NULL)
273#endif
274        , luaState_(NULL)
275        , scriptModule_(NULL)
276        , guiSystem_(NULL)
[8411]277        , ceguiLogger_(NULL)
278        , rootWindow_(NULL)
279        , hudRootWindow_(NULL)
280        , menuRootWindow_(NULL)
[5929]281        , camera_(NULL)
[8423]282        , destructionHelper_(this)
[1638]283    {
[9667]284        RegisterObject(GUIManager);
[8858]285
286        orxout(internal_status) << "initializing GUIManager..." << endl;
287
[7801]288        this->setConfigValues();
289
[1638]290        using namespace CEGUI;
291
[8858]292        orxout(internal_info) << "Initialising CEGUI." << endl;
[1638]293
[8706]294        this->oldCEGUI_ = false;
[8858]295
[5695]296        // Note: No SceneManager specified yet
[8351]297#ifdef ORXONOX_OLD_CEGUI
298        guiRenderer_ = new OgreCEGUIRenderer(GraphicsManager::getInstance().getRenderWindow(), Ogre::RENDER_QUEUE_OVERLAY, false, 3000);
[5695]299        resourceProvider_ = guiRenderer_->createResourceProvider();
[8706]300        this->oldCEGUI_ = true;
[8351]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
[7709]309        resourceProvider_->setDefaultResourceGroup("General");
[1776]310
[6749]311        // Setup scripting
[8351]312        luaState_ = new LuaState();
[6417]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_);
[8351]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
[6763]327        scriptModule_->setDefaultPCallErrorHandler(LuaState::ERROR_HANDLER_NAME);
[1638]328
[5695]329        // Create our own logger to specify the filepath
330        std::auto_ptr<CEGUILogger> ceguiLogger(new CEGUILogger());
[5929]331        ceguiLogger->setLogFilename(PathConfig::getLogPathString() + "cegui.log");
[8858]332        ceguiLogger->setLoggingLevel(static_cast<CEGUI::LoggingLevel>(this->outputLevelCeguiLog_));
[5695]333        this->ceguiLogger_ = ceguiLogger.release();
[2710]334
[6749]335        // Create the CEGUI system singleton
[8351]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");
[9050]341        luaState_->doString("CEGUI.ImagesetManager.createFromImageFile= CEGUI.ImagesetManager.createImagesetFromImageFile");
[8351]342#else
343        guiSystem_ = &System::create(*guiRenderer_, resourceProvider_, 0, imageCodec_, scriptModule_);
344#endif
[1776]345
[9576]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        }
[9759]352        catch (const CEGUI::GenericException&)
[9576]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        }
[9575]358
[5695]359        // Align CEGUI mouse with OIS mouse
[9675]360#if CEGUI_VERSION >= 0x000800
361        guiSystem_->getDefaultGUIContext().injectMousePosition((float)mousePosition.first, (float)mousePosition.second);
362#else
[6502]363        guiSystem_->injectMousePosition((float)mousePosition.first, (float)mousePosition.second);
[9675]364#endif
[5695]365
[6746]366        // Initialise the Lua framework and load the schemes
[8861]367        orxout(user_info) << "Loading user interface..." << endl;
[6746]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
[9675]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
[6746]381        CEGUI::System::getSingleton().setGUISheet(this->rootWindow_);
382        this->rootWindow_->addChildWindow(this->hudRootWindow_);
383        this->rootWindow_->addChildWindow(this->menuRootWindow_);
[9675]384#endif
[6746]385
[6749]386        // No background to start with (sets the alpha value to 0)
387        this->setBackgroundImage("");
388
[6746]389        // Set up the sheet manager in the Lua framework
390        this->luaState_->doFile("SheetManager.lua");
[8858]391
392        orxout(internal_status) << "finished initializing GUIManager" << endl;
[3338]393    }
[1776]394
[8423]395    void GUIManager::destroy()
[3338]396    {
[8858]397        orxout(internal_status) << "destroying GUIManager..." << endl;
398
[8351]399        using namespace CEGUI;
400
401#ifdef ORXONOX_OLD_CEGUI
[8423]402        safeObjectDelete(&guiSystem_);
403        safeObjectDelete(&guiRenderer_);
404        safeObjectDelete(&scriptModule_);
[8351]405#else
406        System::destroy();
407        OgreRenderer::destroyOgreResourceProvider(*resourceProvider_);
408        OgreRenderer::destroyOgreImageCodec(*imageCodec_);
409        OgreRenderer::destroy(*guiRenderer_);
410        LuaScriptModule::destroy(*scriptModule_);
[8423]411        safeObjectDelete(&ceguiLogger_);
412        safeObjectDelete(&rqListener_);
[8351]413#endif
[8423]414        safeObjectDelete(&luaState_);
[8858]415
416        orxout(internal_status) << "finished destroying GUIManager" << endl;
[1638]417    }
418
[7801]419    void GUIManager::setConfigValues(void)
420    {
[8858]421        SetConfigValue(guiScheme_, GUIManager::defaultScheme_).description("Changes the current GUI scheme.").callback(this, &GUIManager::changedGUIScheme);
[8530]422        SetConfigValue(numScrollLines_, 1).description("How many lines to scroll in a list if the scroll wheel is used");
[8862]423        SetConfigValue(bPreloadMenuSheets_, false).description("Pre-load menu sheets during startup");
424
[8858]425        SetConfigValueExternal(outputLevelCeguiLog_, BaseWriter::getConfigurableSectionName(), "outputLevelCeguiLog", CEGUI::Standard).description("The log level of the CEGUI log file").callback(this, &GUIManager::changedCeguiOutputLevel);
[7801]426    }
427
428    void GUIManager::changedGUIScheme(void)
429    {
430    }
431
[8858]432    void GUIManager::changedCeguiOutputLevel()
433    {
434        if (this->ceguiLogger_)
435            this->ceguiLogger_->setLoggingLevel(static_cast<CEGUI::LoggingLevel>(this->outputLevelCeguiLog_));
436    }
437
[2896]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    */
[6417]448    void GUIManager::preUpdate(const Clock& time)
[1638]449    {
[2896]450        assert(guiSystem_);
[9675]451        this->protectedCeguiSystemCall(boost::bind(&CEGUI::System::injectTimePulse, _1, time.getDeltaTime()));
[2896]452    }
[1638]453
[2896]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.
[3337]462        Hiding the GUI when needed can therefore not be resolved by just NOT setting the current camera.
[2896]463    */
464    void GUIManager::setCamera(Ogre::Camera* camera)
[1638]465    {
[8351]466#ifdef ORXONOX_OLD_CEGUI
[2927]467        if (camera == NULL)
468            this->guiRenderer_->setTargetSceneManager(0);
469        else
470            this->guiRenderer_->setTargetSceneManager(camera->getSceneManager());
[8351]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;
[2896]478    }
479
480    /**
481    @brief
[3338]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    {
[5695]488        this->luaState_->doString(str, rootFileInfo_);
[3338]489    }
490
[6746]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
[3338]500    /**
501    @brief
[2896]502        Displays specified GUI on screen
503    @param name
504        The name of the GUI
[7401]505    @param bHidePrevious
506        If true all displayed GUIs on the stack, that are below this GUI are hidden.
[7403]507    @param bNoInput
508        If true the GUI is transparent to input.
[2896]509
510        The function executes the Lua function with the same name in case the GUIManager is ready.
511    */
[7403]512    /*static*/ void GUIManager::showGUI(const std::string& name, bool bHidePrevious, bool bNoInput)
[2896]513    {
[7403]514        GUIManager::getInstance().executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ")");
[1638]515    }
516
[6417]517    /**
518    @brief
519        Hack-ish. Needed for GUIOverlay.
520    */
[7403]521    void GUIManager::showGUIExtra(const std::string& name, const std::string& ptr, bool bHidePrevious, bool bNoInput)
[6417]522    {
[7403]523        this->executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ", " + ptr + ")");
[6417]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    {
[6746]534        GUIManager::getInstance().executeCode("hideMenuSheet(\"" + name + "\")");
[6417]535    }
536
[8079]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
[8729]560    const std::string& GUIManager::createInputState(const std::string& name, tribool showCursor, tribool useKeyboard, bool bBlockJoyStick)
[6746]561    {
562        InputState* state = InputManager::getInstance().createInputState(name);
[7811]563        if (!state)
564            return BLANKSTRING;
[6746]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        */
[8351]573
574#ifdef ORXONOX_PLATFORM_APPLE
575        // There is no non exclusive mode on OS X yet
[8729]576        state->setMouseExclusive(true);
[8351]577#else
[8729]578        if (showCursor == dontcare)
579            state->setMouseExclusive(dontcare);
580        else if (GraphicsManager::getInstance().isFullScreen() || showCursor == false)
581            state->setMouseExclusive(true);
[6746]582        else
[8729]583            state->setMouseExclusive(false);
[8351]584#endif
[6746]585
[8729]586        if (showCursor == true)
[6746]587            state->setMouseHandler(this);
[8729]588        else if (showCursor == false)
[6746]589            state->setMouseHandler(&InputHandler::EMPTY);
590
[8729]591        if (useKeyboard == true)
[6746]592            state->setKeyHandler(this);
[8729]593        else if (useKeyboard == false)
[6746]594            state->setKeyHandler(&InputHandler::EMPTY);
595
596        if (bBlockJoyStick)
597            state->setJoyStickHandler(&InputHandler::EMPTY);
598
599        return state->getName();
600    }
601
[6417]602    void GUIManager::keyESC()
603    {
604        this->executeCode("keyESC()");
605    }
606
[6746]607    void GUIManager::setBackgroundImage(const std::string& imageSet, const std::string imageName)
[6417]608    {
[6746]609        if (imageSet.empty() || imageName.empty())
610            this->setBackgroundImage("");
611        else
612            this->setBackgroundImage("set: " + imageSet + " image: " + imageName);
[6417]613    }
614
[6746]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
[7163]624    void GUIManager::buttonPressed(const KeyEvent& evt)
[3196]625    {
[9675]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
[3196]633    }
[6746]634
[7163]635    void GUIManager::buttonReleased(const KeyEvent& evt)
[3196]636    {
[9675]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
[3196]642    }
643
[2896]644    /**
645    @brief
646        Function receiving a mouse button pressed event.
647    @param id
648        ID of the mouse button which got pressed
[1638]649
[2896]650        This function is inherited by MouseHandler and injects the event into CEGUI.
651        It is for CEGUI to process the event.
652    */
[3327]653    void GUIManager::buttonPressed(MouseButtonCode::ByEnum id)
[1638]654    {
[9675]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
[1638]660    }
661
[2896]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    */
[3327]671    void GUIManager::buttonReleased(MouseButtonCode::ByEnum id)
[1638]672    {
[9675]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
[1638]678    }
679
[3196]680    void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
681    {
[9675]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
[3196]687    }
[6746]688
[3196]689    void GUIManager::mouseScrolled(int abs, int rel)
690    {
[9675]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
[3196]696    }
697
[2896]698    /**
[7874]699        @brief Indicates that the mouse left the application's window.
700    */
701    void GUIManager::mouseLeft()
702    {
[9675]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
[7874]708    }
709
710    /**
[2896]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
[1638]717
[6105]718        Simple conversion from mouse event code in Orxonox to the one used in CEGUI.
[2896]719     */
[3196]720    static inline CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button)
[1638]721    {
722        switch (button)
723        {
[1887]724        case MouseButtonCode::Left:
[1638]725            return CEGUI::LeftButton;
726
[1887]727        case MouseButtonCode::Right:
[1638]728            return CEGUI::RightButton;
729
[1887]730        case MouseButtonCode::Middle:
[1638]731            return CEGUI::MiddleButton;
732
[1887]733        case MouseButtonCode::Button3:
[1638]734            return CEGUI::X1Button;
735
[1887]736        case MouseButtonCode::Button4:
[1638]737            return CEGUI::X2Button;
738
739        default:
740            return CEGUI::NoButton;
741        }
742    }
[6417]743
[6746]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    */
[9675]759    template <typename FunctionType, typename ObjectType>
760    bool GUIManager::protectedCall(FunctionType function, ObjectType object)
[6746]761    {
762        try
763        {
[9675]764            return function(object);
[6746]765        }
766        catch (CEGUI::ScriptException& ex)
767        {
768            // Display the error and proceed. See @remarks why this can be dangerous.
[8858]769            orxout(internal_error) << ex.getMessage() << endl;
[6746]770            return true;
771        }
772    }
773
[9675]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
[7648]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    */
[6417]799    void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function)
800    {
801        window->subscribeScriptedEvent(event, function);
802    }
[7648]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
[8351]823        Whether to enable or disable the tooltips.
[7648]824    */
825    void GUIManager::setItemTooltipsEnabledHelper(CEGUI::Listbox* listbox, bool enabled)
826    {
827        listbox->setItemTooltipsEnabled(enabled);
828    }
829
[8729]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
[7873]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    {
[9675]843#if CEGUI_VERSION >= 0x000800
844        this->guiRenderer_->setDisplaySize(CEGUI::Sizef((float)newWidth, (float)newHeight));
845#else
[8079]846        this->guiRenderer_->setDisplaySize(CEGUI::Size((float)newWidth, (float)newHeight));
[9675]847#endif
[10279]848        this->rootWindow_->setSize(CEGUI::UVector2(CEGUI::UDim(1, (float)newWidth), CEGUI::UDim(1, (float)newHeight)));
[7873]849    }
[7874]850
851    /**
[8079]852        @brief Notify CEGUI if the windows loses the focus (stops highlighting of menu items, etc).
[7874]853    */
854    void GUIManager::windowFocusChanged(bool bFocus)
855    {
856        if (!bFocus)
857            this->mouseLeft();
858    }
[7876]859
[8706]860    /**
861    @brief
862        Adds a new freetype font to the CEGUI system.
863    @param name
864        The name of the new font.
865    @param size
866        The font size of the new font in pixels.
867        @param fontName
868        The filename of the font.
869    */
870    /*static*/ void GUIManager::addFontHelper(const std::string& name, int size, const std::string& fontName)
871    {
872#ifdef ORXONOX_OLD_CEGUI
873        if(CEGUI::FontManager::getSingleton().isFontPresent(name)) // If a font with that name already exists.
874            return;
875
876        CEGUI::Font* font = NULL;
877        CEGUI::XMLAttributes xmlAttributes;
878
879        // Attributes specified within CEGUIFont
880        xmlAttributes.add("Name", name);
881        xmlAttributes.add("Filename", fontName);
882        xmlAttributes.add("ResourceGroup", "");
883        xmlAttributes.add("AutoScaled", "true");
884        xmlAttributes.add("NativeHorzRes", "800");
885        xmlAttributes.add("NativeVertRes", "600");
886
887        // Attributes specified within CEGUIXMLAttributes
888        xmlAttributes.add("Size", multi_cast<std::string>(size));
889        xmlAttributes.add("AntiAlias", "true");
890
891        font = CEGUI::FontManager::getSingleton().createFont("FreeType", xmlAttributes);
892        if(font != NULL)
893            font->load();
894#else
895        if(CEGUI::FontManager::getSingleton().isDefined(name)) // If a font with that name already exists.
896            return;
897
[9675]898    #if CEGUI_VERSION >= 0x000800
899        CEGUI::FontManager::getSingleton().createFreeTypeFont(name, (float)size, true, fontName, "", CEGUI::ASM_Both, CEGUI::Sizef(800.0f, 600.0f));
900    #else
[8706]901        CEGUI::FontManager::getSingleton().createFreeTypeFont(name, (float)size, true, fontName, "", true, 800.0f, 600.0f);
[9675]902    #endif
[8706]903#endif
904    }
905
[1638]906}
Note: See TracBrowser for help on using the repository browser.