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