Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource2/src/core/GUIManager.cc @ 5661

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

Cleaned out the lua script files for the GUI.
Also replaced "require" function to support resources.
Fixed a problem with the return value of doFile, includeFile and require being discarded because the tolua binding is for a C++ function returning void.

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