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
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
[11046]38extern "C" {
39#include <lua.h>
40}
41
[9675]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
[3196]69
[8351]70#ifdef ORXONOX_OLD_CEGUI
[9675]71#   include <CEGUILua.h>
72#   include <ogreceguirenderer/OgreCEGUIRenderer.h>
[8351]73extern "C" {
[9675]74#   include <lauxlib.h>
[8351]75}
[2710]76#else
[9675]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>
[2710]93#endif
94
[8527]95#if defined(ORXONOX_PLATFORM_WINDOWS) && !defined(ORXONOX_COMPILER_MINGW)
[8467]96#  include <windows.h>
97#endif
98
[5929]99#include "util/Clock.h"
[6417]100#include "util/Convert.h"
[8858]101#include "util/Output.h"
[1764]102#include "util/Exception.h"
[8530]103#include "util/Math.h"
[3280]104#include "util/OrxAssert.h"
[8858]105#include "util/output/BaseWriter.h"
[9667]106#include "config/ConfigValueIncludes.h"
[6417]107#include "Core.h"
[7801]108#include "CoreIncludes.h"
[7876]109#include "Game.h"
[6746]110#include "GraphicsManager.h"
[5695]111#include "LuaState.h"
[10624]112#include "ConfigurablePaths.h"
[5695]113#include "Resource.h"
[10624]114#include "command/ConsoleCommandIncludes.h"
[6746]115#include "input/InputManager.h"
116#include "input/InputState.h"
117#include "input/KeyBinderManager.h"
[1638]118
119namespace orxonox
120{
[6417]121    static void key_esc()
122        { GUIManager::getInstance().keyESC(); }
[7284]123    SetConsoleCommand("keyESC", &key_esc);
[6417]124
[3280]125    class CEGUILogger : public CEGUI::DefaultLogger
126    {
127    public:
[5929]128        void logEvent(const CEGUI::String& message, CEGUI::LoggingLevel level = CEGUI::Standard)
[3280]129        {
[8858]130            OutputLevel orxonoxLevel = level::debug_output;
[3280]131            switch (level)
132            {
[8858]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;
[8351]138                default: OrxAssert(false, "CEGUI log level out of range, inspect immediately!");
[3280]139            }
140
[8858]141            orxout(orxonoxLevel, context::cegui) << message << endl;
142
[3280]143            CEGUI::DefaultLogger::logEvent(message, level);
144        }
[8467]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
[8527]153#if defined(ORXONOX_PLATFORM_WINDOWS) && !defined(ORXONOX_COMPILER_MINGW)
[8467]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
[8527]188#if defined(ORXONOX_PLATFORM_WINDOWS) && !defined(ORXONOX_COMPILER_MINGW)
[8467]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
[3280]204    };
205
[8351]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
[8419]232        void renderQueueStarted(Ogre::uint8 id, const Ogre::String& invocation, bool& skipThisQueue)
[8351]233        {
[8419]234            if (id == Ogre::RENDER_QUEUE_OVERLAY && invocation.empty())
[8439]235            {
[9675]236#if CEGUI_VERSION >= 0x000800
237                CEGUI::System::getSingleton().renderAllGUIContexts();
238#else
[8351]239                CEGUI::System::getSingleton().renderGUI();
[9675]240#endif
[8439]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            }
[8351]248        }
249    };
250#endif
251
[3196]252    static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button);
[3339]253
[3366]254    GUIManager* GUIManager::singletonPtr_s = 0;
[9016]255    /*static*/ const std::string GUIManager::defaultScheme_ = "TaharezGreen"; //Alternative: Orxonox (not fully complete yet, see the graphics menu)
[1646]256
[11046]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    }
[6417]272
[11046]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
[10624]280    RegisterAbstractClass(GUIManager).inheritsFrom<WindowEventListener>();
281
[2896]282    /**
283    @brief
[3338]284        Constructs the GUIManager by starting up CEGUI
[2896]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).
[3338]290    @return true if success, otherwise false
[2896]291    */
[6746]292    GUIManager::GUIManager(const std::pair<int, int>& mousePosition)
[8423]293        : guiRenderer_(NULL)
[8351]294        , resourceProvider_(NULL)
295#ifndef ORXONOX_OLD_CEGUI
[8411]296        , rqListener_(NULL)
[8351]297        , imageCodec_(NULL)
298#endif
299        , luaState_(NULL)
300        , scriptModule_(NULL)
301        , guiSystem_(NULL)
[8411]302        , ceguiLogger_(NULL)
303        , rootWindow_(NULL)
304        , hudRootWindow_(NULL)
305        , menuRootWindow_(NULL)
[5929]306        , camera_(NULL)
[8423]307        , destructionHelper_(this)
[1638]308    {
[9667]309        RegisterObject(GUIManager);
[8858]310
311        orxout(internal_status) << "initializing GUIManager..." << endl;
312
[7801]313        this->setConfigValues();
314
[1638]315        using namespace CEGUI;
316
[8858]317        orxout(internal_info) << "Initialising CEGUI." << endl;
[1638]318
[8706]319        this->oldCEGUI_ = false;
[8858]320
[5695]321        // Note: No SceneManager specified yet
[8351]322#ifdef ORXONOX_OLD_CEGUI
323        guiRenderer_ = new OgreCEGUIRenderer(GraphicsManager::getInstance().getRenderWindow(), Ogre::RENDER_QUEUE_OVERLAY, false, 3000);
[5695]324        resourceProvider_ = guiRenderer_->createResourceProvider();
[8706]325        this->oldCEGUI_ = true;
[8351]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
[7709]334        resourceProvider_->setDefaultResourceGroup("General");
[1776]335
[6749]336        // Setup scripting
[8351]337        luaState_ = new LuaState();
[6417]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_);
[8351]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
[6763]352        scriptModule_->setDefaultPCallErrorHandler(LuaState::ERROR_HANDLER_NAME);
[1638]353
[5695]354        // Create our own logger to specify the filepath
355        std::auto_ptr<CEGUILogger> ceguiLogger(new CEGUILogger());
[10624]356        ceguiLogger->setLogFilename(ConfigurablePaths::getLogPathString() + "cegui.log");
[8858]357        ceguiLogger->setLoggingLevel(static_cast<CEGUI::LoggingLevel>(this->outputLevelCeguiLog_));
[5695]358        this->ceguiLogger_ = ceguiLogger.release();
[2710]359
[6749]360        // Create the CEGUI system singleton
[8351]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");
[9050]366        luaState_->doString("CEGUI.ImagesetManager.createFromImageFile= CEGUI.ImagesetManager.createImagesetFromImageFile");
[8351]367#else
368        guiSystem_ = &System::create(*guiRenderer_, resourceProvider_, 0, imageCodec_, scriptModule_);
369#endif
[1776]370
[9576]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        }
[9759]377        catch (const CEGUI::GenericException&)
[9576]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        }
[9575]383
[5695]384        // Align CEGUI mouse with OIS mouse
[9675]385#if CEGUI_VERSION >= 0x000800
386        guiSystem_->getDefaultGUIContext().injectMousePosition((float)mousePosition.first, (float)mousePosition.second);
387#else
[6502]388        guiSystem_->injectMousePosition((float)mousePosition.first, (float)mousePosition.second);
[9675]389#endif
[5695]390
[6746]391        // Initialise the Lua framework and load the schemes
[8861]392        orxout(user_info) << "Loading user interface..." << endl;
[6746]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
[9675]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
[6746]406        CEGUI::System::getSingleton().setGUISheet(this->rootWindow_);
407        this->rootWindow_->addChildWindow(this->hudRootWindow_);
408        this->rootWindow_->addChildWindow(this->menuRootWindow_);
[9675]409#endif
[6746]410
[6749]411        // No background to start with (sets the alpha value to 0)
412        this->setBackgroundImage("");
413
[6746]414        // Set up the sheet manager in the Lua framework
415        this->luaState_->doFile("SheetManager.lua");
[8858]416
417        orxout(internal_status) << "finished initializing GUIManager" << endl;
[3338]418    }
[1776]419
[8423]420    void GUIManager::destroy()
[3338]421    {
[8858]422        orxout(internal_status) << "destroying GUIManager..." << endl;
423
[8351]424        using namespace CEGUI;
425
426#ifdef ORXONOX_OLD_CEGUI
[8423]427        safeObjectDelete(&guiSystem_);
428        safeObjectDelete(&guiRenderer_);
429        safeObjectDelete(&scriptModule_);
[8351]430#else
431        System::destroy();
432        OgreRenderer::destroyOgreResourceProvider(*resourceProvider_);
433        OgreRenderer::destroyOgreImageCodec(*imageCodec_);
434        OgreRenderer::destroy(*guiRenderer_);
435        LuaScriptModule::destroy(*scriptModule_);
[8423]436        safeObjectDelete(&ceguiLogger_);
437        safeObjectDelete(&rqListener_);
[8351]438#endif
[8423]439        safeObjectDelete(&luaState_);
[8858]440
441        orxout(internal_status) << "finished destroying GUIManager" << endl;
[1638]442    }
443
[7801]444    void GUIManager::setConfigValues(void)
445    {
[8858]446        SetConfigValue(guiScheme_, GUIManager::defaultScheme_).description("Changes the current GUI scheme.").callback(this, &GUIManager::changedGUIScheme);
[8530]447        SetConfigValue(numScrollLines_, 1).description("How many lines to scroll in a list if the scroll wheel is used");
[8862]448        SetConfigValue(bPreloadMenuSheets_, false).description("Pre-load menu sheets during startup");
449
[8858]450        SetConfigValueExternal(outputLevelCeguiLog_, BaseWriter::getConfigurableSectionName(), "outputLevelCeguiLog", CEGUI::Standard).description("The log level of the CEGUI log file").callback(this, &GUIManager::changedCeguiOutputLevel);
[7801]451    }
452
453    void GUIManager::changedGUIScheme(void)
454    {
455    }
456
[8858]457    void GUIManager::changedCeguiOutputLevel()
458    {
459        if (this->ceguiLogger_)
460            this->ceguiLogger_->setLoggingLevel(static_cast<CEGUI::LoggingLevel>(this->outputLevelCeguiLog_));
461    }
462
[2896]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    */
[6417]473    void GUIManager::preUpdate(const Clock& time)
[1638]474    {
[2896]475        assert(guiSystem_);
[9675]476        this->protectedCeguiSystemCall(boost::bind(&CEGUI::System::injectTimePulse, _1, time.getDeltaTime()));
[2896]477    }
[1638]478
[2896]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.
[3337]487        Hiding the GUI when needed can therefore not be resolved by just NOT setting the current camera.
[2896]488    */
489    void GUIManager::setCamera(Ogre::Camera* camera)
[1638]490    {
[8351]491#ifdef ORXONOX_OLD_CEGUI
[2927]492        if (camera == NULL)
493            this->guiRenderer_->setTargetSceneManager(0);
494        else
495            this->guiRenderer_->setTargetSceneManager(camera->getSceneManager());
[8351]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;
[2896]503    }
504
505    /**
506    @brief
[3338]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    {
[5695]513        this->luaState_->doString(str, rootFileInfo_);
[3338]514    }
515
[11046]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
[6746]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
[3338]547    /**
548    @brief
[2896]549        Displays specified GUI on screen
550    @param name
551        The name of the GUI
[7401]552    @param bHidePrevious
553        If true all displayed GUIs on the stack, that are below this GUI are hidden.
[7403]554    @param bNoInput
555        If true the GUI is transparent to input.
[2896]556
557        The function executes the Lua function with the same name in case the GUIManager is ready.
558    */
[7403]559    /*static*/ void GUIManager::showGUI(const std::string& name, bool bHidePrevious, bool bNoInput)
[2896]560    {
[7403]561        GUIManager::getInstance().executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ")");
[1638]562    }
563
[6417]564    /**
565    @brief
566        Hack-ish. Needed for GUIOverlay.
567    */
[7403]568    void GUIManager::showGUIExtra(const std::string& name, const std::string& ptr, bool bHidePrevious, bool bNoInput)
[6417]569    {
[7403]570        this->executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ", " + ptr + ")");
[6417]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    {
[6746]581        GUIManager::getInstance().executeCode("hideMenuSheet(\"" + name + "\")");
[6417]582    }
583
[8079]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
[8729]607    const std::string& GUIManager::createInputState(const std::string& name, tribool showCursor, tribool useKeyboard, bool bBlockJoyStick)
[6746]608    {
609        InputState* state = InputManager::getInstance().createInputState(name);
[7811]610        if (!state)
611            return BLANKSTRING;
[6746]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        */
[8351]620
621#ifdef ORXONOX_PLATFORM_APPLE
622        // There is no non exclusive mode on OS X yet
[8729]623        state->setMouseExclusive(true);
[8351]624#else
[8729]625        if (showCursor == dontcare)
626            state->setMouseExclusive(dontcare);
627        else if (GraphicsManager::getInstance().isFullScreen() || showCursor == false)
628            state->setMouseExclusive(true);
[6746]629        else
[8729]630            state->setMouseExclusive(false);
[8351]631#endif
[6746]632
[8729]633        if (showCursor == true)
[6746]634            state->setMouseHandler(this);
[8729]635        else if (showCursor == false)
[6746]636            state->setMouseHandler(&InputHandler::EMPTY);
637
[8729]638        if (useKeyboard == true)
[6746]639            state->setKeyHandler(this);
[8729]640        else if (useKeyboard == false)
[6746]641            state->setKeyHandler(&InputHandler::EMPTY);
642
643        if (bBlockJoyStick)
644            state->setJoyStickHandler(&InputHandler::EMPTY);
645
646        return state->getName();
647    }
648
[6417]649    void GUIManager::keyESC()
650    {
651        this->executeCode("keyESC()");
652    }
653
[6746]654    void GUIManager::setBackgroundImage(const std::string& imageSet, const std::string imageName)
[6417]655    {
[6746]656        if (imageSet.empty() || imageName.empty())
657            this->setBackgroundImage("");
658        else
659            this->setBackgroundImage("set: " + imageSet + " image: " + imageName);
[6417]660    }
661
[6746]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
[7163]671    void GUIManager::buttonPressed(const KeyEvent& evt)
[3196]672    {
[9675]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
[3196]680    }
[6746]681
[7163]682    void GUIManager::buttonReleased(const KeyEvent& evt)
[3196]683    {
[9675]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
[3196]689    }
690
[2896]691    /**
692    @brief
693        Function receiving a mouse button pressed event.
694    @param id
695        ID of the mouse button which got pressed
[1638]696
[2896]697        This function is inherited by MouseHandler and injects the event into CEGUI.
698        It is for CEGUI to process the event.
699    */
[3327]700    void GUIManager::buttonPressed(MouseButtonCode::ByEnum id)
[1638]701    {
[9675]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
[1638]707    }
708
[2896]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    */
[3327]718    void GUIManager::buttonReleased(MouseButtonCode::ByEnum id)
[1638]719    {
[9675]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
[1638]725    }
726
[3196]727    void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
728    {
[9675]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
[3196]734    }
[6746]735
[3196]736    void GUIManager::mouseScrolled(int abs, int rel)
737    {
[9675]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
[3196]743    }
744
[2896]745    /**
[7874]746        @brief Indicates that the mouse left the application's window.
747    */
748    void GUIManager::mouseLeft()
749    {
[9675]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
[7874]755    }
756
757    /**
[2896]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
[1638]764
[6105]765        Simple conversion from mouse event code in Orxonox to the one used in CEGUI.
[2896]766     */
[3196]767    static inline CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button)
[1638]768    {
769        switch (button)
770        {
[1887]771        case MouseButtonCode::Left:
[1638]772            return CEGUI::LeftButton;
773
[1887]774        case MouseButtonCode::Right:
[1638]775            return CEGUI::RightButton;
776
[1887]777        case MouseButtonCode::Middle:
[1638]778            return CEGUI::MiddleButton;
779
[1887]780        case MouseButtonCode::Button3:
[1638]781            return CEGUI::X1Button;
782
[1887]783        case MouseButtonCode::Button4:
[1638]784            return CEGUI::X2Button;
785
786        default:
787            return CEGUI::NoButton;
788        }
789    }
[6417]790
[6746]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    */
[9675]806    template <typename FunctionType, typename ObjectType>
807    bool GUIManager::protectedCall(FunctionType function, ObjectType object)
[6746]808    {
809        try
810        {
[9675]811            return function(object);
[6746]812        }
813        catch (CEGUI::ScriptException& ex)
814        {
815            // Display the error and proceed. See @remarks why this can be dangerous.
[8858]816            orxout(internal_error) << ex.getMessage() << endl;
[6746]817            return true;
818        }
819    }
820
[9675]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
[7648]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    */
[6417]846    void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function)
847    {
848        window->subscribeScriptedEvent(event, function);
849    }
[7648]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
[8351]870        Whether to enable or disable the tooltips.
[7648]871    */
872    void GUIManager::setItemTooltipsEnabledHelper(CEGUI::Listbox* listbox, bool enabled)
873    {
874        listbox->setItemTooltipsEnabled(enabled);
875    }
876
[8729]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    {
[10624]882         return Core::getInstance().getConfig()->inDevMode();
[8729]883    }
884
[7873]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    {
[9675]890#if CEGUI_VERSION >= 0x000800
891        this->guiRenderer_->setDisplaySize(CEGUI::Sizef((float)newWidth, (float)newHeight));
892#else
[8079]893        this->guiRenderer_->setDisplaySize(CEGUI::Size((float)newWidth, (float)newHeight));
[9675]894#endif
[10279]895        this->rootWindow_->setSize(CEGUI::UVector2(CEGUI::UDim(1, (float)newWidth), CEGUI::UDim(1, (float)newHeight)));
[7873]896    }
[7874]897
898    /**
[8079]899        @brief Notify CEGUI if the windows loses the focus (stops highlighting of menu items, etc).
[7874]900    */
901    void GUIManager::windowFocusChanged(bool bFocus)
902    {
903        if (!bFocus)
904            this->mouseLeft();
905    }
[7876]906
[8706]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
[9675]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
[8706]948        CEGUI::FontManager::getSingleton().createFreeTypeFont(name, (float)size, true, fontName, "", true, 800.0f, 600.0f);
[9675]949    #endif
[8706]950#endif
951    }
952
[1638]953}
Note: See TracBrowser for help on using the repository browser.