Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11117 was 11117, checked in by landauf, 8 years ago

preparing for CEGUI 0.8

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