Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5865 was 5781, checked in by rgrieder, 15 years ago

Reverted trunk again. We might want to find a way to delete these revisions again (x3n's changes are still available as diff in the commit mails).

  • Property svn:eol-style set to native
File size: 9.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
[3338]32#include <memory>
[3196]33extern "C" {
34#include <lua.h>
35}
[2710]36#include <CEGUIDefaultLogger.h>
[3196]37#include <CEGUIExceptions.h>
38#include <CEGUIInputEvent.h>
[5695]39#include <CEGUIMouseCursor.h>
[3196]40#include <CEGUIResourceProvider.h>
41#include <CEGUISystem.h>
[2710]42#include <ogreceguirenderer/OgreCEGUIRenderer.h>
[3196]43
[2710]44#include "SpecialConfig.h" // Configures the macro below
45#ifdef CEGUILUA_USE_INTERNAL_LIBRARY
46#   include <ceguilua/CEGUILua.h>
47#else
48#   include <CEGUILua.h>
49#endif
50
[3280]51#include "util/Debug.h"
[1764]52#include "util/Exception.h"
[3280]53#include "util/OrxAssert.h"
[3346]54#include "Core.h"
55#include "Clock.h"
[5695]56#include "LuaState.h"
57#include "Resource.h"
[1638]58
59namespace orxonox
60{
[3280]61    class CEGUILogger : public CEGUI::DefaultLogger
62    {
63    public:
64            void logEvent(const CEGUI::String& message, CEGUI::LoggingLevel level = CEGUI::Standard)
65        {
[3327]66            int orxonoxLevel = CEGUI::Standard;
[3280]67            switch (level)
68            {
69                case CEGUI::Errors:      orxonoxLevel = 1; break;
70                case CEGUI::Warnings:    orxonoxLevel = 2; break;
71                case CEGUI::Standard:    orxonoxLevel = 4; break;
72                case CEGUI::Informative: orxonoxLevel = 5; break;
73                case CEGUI::Insane:      orxonoxLevel = 6; break;
74                default: OrxAssert(false, "CEGUI log level out of range, inpect immediately!");
75            }
76            OutputHandler::getOutStream().setOutputLevel(orxonoxLevel)
77                << "CEGUI: " << message << std::endl;
78
79            CEGUI::DefaultLogger::logEvent(message, level);
80        }
81    };
82
[3196]83    static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button);
[3339]84
[3366]85    GUIManager* GUIManager::singletonPtr_s = 0;
[1646]86
[2896]87    /**
88    @brief
[3338]89        Constructs the GUIManager by starting up CEGUI
[2896]90
91        Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine.
92        The log is set up and connected to the CEGUILogger.
93        After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code.
94        Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically).
[3338]95    @param renderWindow
96        Ogre's render window. Without this, the GUI cannot be displayed.
97    @return true if success, otherwise false
[2896]98    */
[5695]99    GUIManager::GUIManager(Ogre::RenderWindow* renderWindow, const std::pair<int, int>& mousePosition, bool bFullScreen)
[3338]100        : renderWindow_(renderWindow)
101        , resourceProvider_(0)
[1638]102    {
103        using namespace CEGUI;
104
[3338]105        COUT(3) << "Initialising CEGUI." << std::endl;
[1638]106
[5695]107        // Note: No SceneManager specified yet
108        guiRenderer_.reset(new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, false, 3000));
109        resourceProvider_ = guiRenderer_->createResourceProvider();
110        resourceProvider_->setDefaultResourceGroup("GUI");
[1776]111
[5695]112        // setup scripting
113        luaState_.reset(new LuaState());
114        scriptModule_.reset(new LuaScriptModule(luaState_->getInternalLuaState()));
[1638]115
[5695]116        // Create our own logger to specify the filepath
117        std::auto_ptr<CEGUILogger> ceguiLogger(new CEGUILogger());
118        ceguiLogger->setLogFilename(Core::getLogPathString() + "cegui.log");
119        // set the log level according to ours (translate by subtracting 1)
120        ceguiLogger->setLoggingLevel(
121            static_cast<LoggingLevel>(Core::getSoftDebugLevel(OutputHandler::LD_Logfile) - 1));
122        this->ceguiLogger_ = ceguiLogger.release();
[2710]123
[5695]124        // create the CEGUI system singleton
125        guiSystem_.reset(new System(guiRenderer_.get(), resourceProvider_, 0, scriptModule_.get()));
[1776]126
[5695]127        // Initialise the basic lua code
128        rootFileInfo_ = Resource::getInfo("InitialiseGUI.lua", "GUI");
129        this->luaState_->doFile("InitialiseGUI.lua", "GUI", false);
[1638]130
[5695]131        // Align CEGUI mouse with OIS mouse
132        guiSystem_->injectMousePosition(mousePosition.first, mousePosition.second);
133
134        // Hide the mouse cursor unless playing in fullscreen mode
135        if (!bFullScreen)
136            CEGUI::MouseCursor::getSingleton().hide();
[3338]137    }
[1776]138
[3338]139    /**
140    @brief
[3339]141        Basically shuts down CEGUI (member smart pointers) but first unloads our Tolua modules.
[3338]142    */
143    GUIManager::~GUIManager()
144    {
[1638]145    }
146
[2896]147    /**
148    @brief
149        used to tick the GUI
150    @param time
151        clock which provides time value for the GUI System
152
153        Ticking the GUI means updating it with a certain regularity.
154        The elapsed time since the last call is given in the time value provided by the clock.
155        This time value is then used to provide a fluent animation of the GUI.
156    */
157    void GUIManager::update(const Clock& time)
[1638]158    {
[2896]159        assert(guiSystem_);
160        guiSystem_->injectTimePulse(time.getDeltaTime());
161    }
[1638]162
[2896]163    /**
164    @brief
165        Tells the GUIManager which SceneManager to use
166    @param camera
167        The current camera on which the GUI should be displayed on.
168
169        In fact the GUIManager needs the SceneManager and not the Camera to display the GUI.
170        This means the GUI is not bound to a camera but rather to the SceneManager.
[3337]171        Hiding the GUI when needed can therefore not be resolved by just NOT setting the current camera.
[2896]172    */
173    void GUIManager::setCamera(Ogre::Camera* camera)
[1638]174    {
[2927]175        if (camera == NULL)
176            this->guiRenderer_->setTargetSceneManager(0);
177        else
178            this->guiRenderer_->setTargetSceneManager(camera->getSceneManager());
[2896]179    }
180
181    /**
182    @brief
[3338]183        Executes Lua code
184    @param str
185        reference to string object holding the Lua code which is to be executed
186
187        This function gives total access to the GUI. You can execute ANY Lua code here.
188    */
189    void GUIManager::executeCode(const std::string& str)
190    {
[5695]191        this->luaState_->doString(str, rootFileInfo_);
[3338]192    }
193
194    /**
195    @brief
[2896]196        Displays specified GUI on screen
197    @param name
198        The name of the GUI
199
200        The function executes the Lua function with the same name in case the GUIManager is ready.
201        For more details check out loadGUI_2.lua where the function presides.
202    */
203    void GUIManager::showGUI(const std::string& name)
204    {
[5695]205        this->luaState_->doString("showGUI(\"" + name + "\")", rootFileInfo_);
[1638]206    }
207
[3196]208    void GUIManager::keyPressed(const KeyEvent& evt)
209    {
[3327]210        guiSystem_->injectKeyDown(evt.getKeyCode());
211        guiSystem_->injectChar(evt.getText());
[3196]212    }
213    void GUIManager::keyReleased(const KeyEvent& evt)
214    {
[3327]215        guiSystem_->injectKeyUp(evt.getKeyCode());
[3196]216    }
217
[2896]218    /**
219    @brief
220        Function receiving a mouse button pressed event.
221    @param id
222        ID of the mouse button which got pressed
[1638]223
[2896]224        This function is inherited by MouseHandler and injects the event into CEGUI.
225        It is for CEGUI to process the event.
226    */
[3327]227    void GUIManager::buttonPressed(MouseButtonCode::ByEnum id)
[1638]228    {
229        try
230        {
231            guiSystem_->injectMouseButtonDown(convertButton(id));
232        }
233        catch (CEGUI::ScriptException& ex)
234        {
235            // We simply ignore the exception and proceed
236            COUT(1) << ex.getMessage() << std::endl;
237        }
238    }
239
[2896]240    /**
241    @brief
242        Function receiving a mouse button released event.
243    @param id
244        ID of the mouse button which got released
245
246        This function is inherited by MouseHandler and injects the event into CEGUI.
247        It is for CEGUI to process the event.
248    */
[3327]249    void GUIManager::buttonReleased(MouseButtonCode::ByEnum id)
[1638]250    {
251        try
252        {
253            guiSystem_->injectMouseButtonUp(convertButton(id));
254        }
255        catch (CEGUI::ScriptException& ex)
256        {
257            // We simply ignore the exception and proceed
258            COUT(1) << ex.getMessage() << std::endl;
259        }
260    }
261
[3196]262    void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
263    {
[5695]264        guiSystem_->injectMousePosition(static_cast<float>(abs.x), static_cast<float>(abs.y));
[3196]265    }
266    void GUIManager::mouseScrolled(int abs, int rel)
267    {
268        guiSystem_->injectMouseWheelChange(static_cast<float>(rel));
269    }
270
[2896]271    /**
272    @brief
273        converts mouse event code to CEGUI event code
274    @param button
275        code of the mouse button as we use it in Orxonox
276    @return
277        code of the mouse button as it is used by CEGUI
[1638]278
[2896]279        Simple convertion from mouse event code in Orxonox to the one used in CEGUI.
280     */
[3196]281    static inline CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button)
[1638]282    {
283        switch (button)
284        {
[1887]285        case MouseButtonCode::Left:
[1638]286            return CEGUI::LeftButton;
287
[1887]288        case MouseButtonCode::Right:
[1638]289            return CEGUI::RightButton;
290
[1887]291        case MouseButtonCode::Middle:
[1638]292            return CEGUI::MiddleButton;
293
[1887]294        case MouseButtonCode::Button3:
[1638]295            return CEGUI::X1Button;
296
[1887]297        case MouseButtonCode::Button4:
[1638]298            return CEGUI::X2Button;
299
300        default:
301            return CEGUI::NoButton;
302        }
303    }
304}
Note: See TracBrowser for help on using the repository browser.