Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentationHS15/src/libraries/core/GUIManager.cc @ 11046

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

added argument completion function for active gui sheets.
TODO

a) there should be a better way to read back values from lua (i.e. by using LuaState instead of plain lua.h functions)
b) it shouldn't be necessary to call lua anyway to get the active gui sheets. the GUIManager should always know this.

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