[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 | |
---|
[3196] | 38 | extern "C" { |
---|
| 39 | #include <lua.h> |
---|
| 40 | } |
---|
[2710] | 41 | #include <CEGUIDefaultLogger.h> |
---|
[3196] | 42 | #include <CEGUIExceptions.h> |
---|
| 43 | #include <CEGUIInputEvent.h> |
---|
| 44 | #include <CEGUIResourceProvider.h> |
---|
| 45 | #include <CEGUISystem.h> |
---|
[2710] | 46 | #include <ogreceguirenderer/OgreCEGUIRenderer.h> |
---|
[3196] | 47 | |
---|
[2710] | 48 | #include "SpecialConfig.h" // Configures the macro below |
---|
| 49 | #ifdef CEGUILUA_USE_INTERNAL_LIBRARY |
---|
| 50 | # include <ceguilua/CEGUILua.h> |
---|
| 51 | #else |
---|
| 52 | # include <CEGUILua.h> |
---|
| 53 | #endif |
---|
| 54 | |
---|
[1764] | 55 | #include "util/Exception.h" |
---|
[1638] | 56 | #include "core/Core.h" |
---|
[2896] | 57 | #include "core/Clock.h" |
---|
[2710] | 58 | #include "ToluaBindCore.h" |
---|
| 59 | #include "ToluaBindOrxonox.h" |
---|
[3008] | 60 | #include "core/Loader.h" |
---|
[1638] | 61 | |
---|
| 62 | namespace orxonox |
---|
| 63 | { |
---|
[3196] | 64 | static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button); |
---|
[1646] | 65 | GUIManager* GUIManager::singletonRef_s = 0; |
---|
| 66 | |
---|
[1638] | 67 | GUIManager::GUIManager() |
---|
[2896] | 68 | : renderWindow_(0) |
---|
[1638] | 69 | , guiRenderer_(0) |
---|
| 70 | , resourceProvider_(0) |
---|
| 71 | , scriptModule_(0) |
---|
| 72 | , guiSystem_(0) |
---|
| 73 | , state_(Uninitialised) |
---|
| 74 | { |
---|
[1646] | 75 | assert(singletonRef_s == 0); |
---|
| 76 | singletonRef_s = this; |
---|
[1638] | 77 | } |
---|
| 78 | |
---|
[2896] | 79 | /** |
---|
| 80 | @brief |
---|
| 81 | Deconstructor of the GUIManager |
---|
| 82 | |
---|
| 83 | Basically shuts down CEGUI and destroys the Lua engine and afterwards the interface to the Ogre engine. |
---|
| 84 | */ |
---|
[1638] | 85 | GUIManager::~GUIManager() |
---|
| 86 | { |
---|
[1646] | 87 | if (guiSystem_) |
---|
| 88 | delete guiSystem_; |
---|
| 89 | |
---|
| 90 | if (scriptModule_) |
---|
| 91 | { |
---|
| 92 | // destroy our own tolua interfaces |
---|
[3034] | 93 | lua_pushnil(luaState_); |
---|
| 94 | lua_setglobal(luaState_, "Orxonox"); |
---|
| 95 | lua_pushnil(luaState_); |
---|
| 96 | lua_setglobal(luaState_, "Core"); |
---|
[2662] | 97 | delete scriptModule_; |
---|
[1646] | 98 | } |
---|
| 99 | |
---|
| 100 | if (guiRenderer_) |
---|
| 101 | delete guiRenderer_; |
---|
| 102 | |
---|
| 103 | singletonRef_s = 0; |
---|
[1638] | 104 | } |
---|
| 105 | |
---|
[2896] | 106 | /** |
---|
| 107 | @brief |
---|
| 108 | Initialises the GUIManager by starting up CEGUI |
---|
| 109 | @param renderWindow |
---|
| 110 | Ogre's render window. Without this, the GUI cannot be displayed. |
---|
| 111 | @return true if success, otherwise false |
---|
| 112 | |
---|
| 113 | Before this call the GUIManager won't do anything, but can be accessed. |
---|
| 114 | |
---|
| 115 | Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine. |
---|
| 116 | The log is set up and connected to the CEGUILogger. |
---|
| 117 | After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code. |
---|
| 118 | Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically). |
---|
| 119 | */ |
---|
[1686] | 120 | bool GUIManager::initialise(Ogre::RenderWindow* renderWindow) |
---|
[1638] | 121 | { |
---|
| 122 | using namespace CEGUI; |
---|
| 123 | if (state_ == Uninitialised) |
---|
| 124 | { |
---|
[1776] | 125 | COUT(3) << "Initialising CEGUI." << std::endl; |
---|
[1638] | 126 | |
---|
| 127 | try |
---|
| 128 | { |
---|
[1686] | 129 | // save the render window |
---|
| 130 | renderWindow_ = renderWindow; |
---|
[1638] | 131 | |
---|
| 132 | // Note: No SceneManager specified yet |
---|
[2896] | 133 | this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, true, 3000); |
---|
[1638] | 134 | this->resourceProvider_ = guiRenderer_->createResourceProvider(); |
---|
| 135 | this->resourceProvider_->setDefaultResourceGroup("GUI"); |
---|
[1776] | 136 | |
---|
[1638] | 137 | // setup scripting |
---|
| 138 | this->scriptModule_ = new LuaScriptModule(); |
---|
[1646] | 139 | this->luaState_ = this->scriptModule_->getLuaState(); |
---|
[1638] | 140 | |
---|
[2710] | 141 | // Create our own logger to specify the filepath |
---|
| 142 | this->ceguiLogger_ = new DefaultLogger(); |
---|
[3196] | 143 | this->ceguiLogger_->setLogFilename(Core::getLogPathString() + "cegui.log"); |
---|
[2710] | 144 | // set the log level according to ours (translate by subtracting 1) |
---|
| 145 | this->ceguiLogger_->setLoggingLevel( |
---|
| 146 | (LoggingLevel)(Core::getSoftDebugLevel(OutputHandler::LD_Logfile) - 1)); |
---|
| 147 | |
---|
[1638] | 148 | // create the CEGUI system singleton |
---|
| 149 | this->guiSystem_ = new System(this->guiRenderer_, this->resourceProvider_, 0, this->scriptModule_); |
---|
[1776] | 150 | |
---|
[1638] | 151 | // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place |
---|
| 152 | tolua_Core_open(this->scriptModule_->getLuaState()); |
---|
| 153 | tolua_Orxonox_open(this->scriptModule_->getLuaState()); |
---|
| 154 | |
---|
[2896] | 155 | // initialise the basic lua code |
---|
| 156 | loadLuaCode(); |
---|
[1638] | 157 | } |
---|
| 158 | catch (CEGUI::Exception& ex) |
---|
| 159 | { |
---|
[1653] | 160 | #if CEGUI_VERSION_MAJOR == 0 && CEGUI_VERSION_MINOR < 6 |
---|
[1645] | 161 | throw GeneralException(ex.getMessage().c_str()); |
---|
| 162 | #else |
---|
[1638] | 163 | throw GeneralException(ex.getMessage().c_str(), ex.getLine(), |
---|
| 164 | ex.getFileName().c_str(), ex.getName().c_str()); |
---|
[1645] | 165 | #endif |
---|
[1638] | 166 | } |
---|
| 167 | |
---|
| 168 | state_ = Ready; |
---|
| 169 | } |
---|
[1776] | 170 | |
---|
[1638] | 171 | return true; |
---|
| 172 | } |
---|
| 173 | |
---|
[2896] | 174 | /** |
---|
| 175 | @brief |
---|
| 176 | Calls main Lua script |
---|
| 177 | @todo |
---|
| 178 | Replace loadGUI.lua with loadGUI_2.lua after merging this back to trunk. |
---|
| 179 | However CEGUI is able to execute a startup script. We could maybe put this call in this startup code. |
---|
| 180 | |
---|
| 181 | This function calls the main Lua script for our GUI. |
---|
| 182 | |
---|
| 183 | Additionally we set the datapath variable in Lua. This is needed so Lua can access the data used for the GUI. |
---|
| 184 | */ |
---|
| 185 | void GUIManager::loadLuaCode() |
---|
[2790] | 186 | { |
---|
[2896] | 187 | try |
---|
[2790] | 188 | { |
---|
[2896] | 189 | // set datapath for GUI data |
---|
| 190 | lua_pushfstring(this->scriptModule_->getLuaState(), Core::getMediaPathString().c_str()); |
---|
| 191 | lua_setglobal(this->scriptModule_->getLuaState(), "datapath"); |
---|
[2957] | 192 | // call main Lua script |
---|
| 193 | this->scriptModule_->executeScriptFile("loadGUI_3.lua", "GUI"); |
---|
[2896] | 194 | } |
---|
| 195 | catch (CEGUI::Exception& ex) |
---|
| 196 | { |
---|
[2790] | 197 | #if CEGUI_VERSION_MINOR < 6 |
---|
[2896] | 198 | throw GeneralException(ex.getMessage().c_str()); |
---|
[2790] | 199 | #else |
---|
[2896] | 200 | throw GeneralException(ex.getMessage().c_str(), ex.getLine(), |
---|
| 201 | ex.getFileName().c_str(), ex.getName().c_str()); |
---|
[2790] | 202 | #endif |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | |
---|
[2896] | 206 | /** |
---|
| 207 | @brief |
---|
| 208 | used to tick the GUI |
---|
| 209 | @param time |
---|
| 210 | clock which provides time value for the GUI System |
---|
| 211 | |
---|
| 212 | Ticking the GUI means updating it with a certain regularity. |
---|
| 213 | The elapsed time since the last call is given in the time value provided by the clock. |
---|
| 214 | This time value is then used to provide a fluent animation of the GUI. |
---|
| 215 | */ |
---|
| 216 | void GUIManager::update(const Clock& time) |
---|
[1638] | 217 | { |
---|
[2896] | 218 | assert(guiSystem_); |
---|
| 219 | guiSystem_->injectTimePulse(time.getDeltaTime()); |
---|
| 220 | } |
---|
[1638] | 221 | |
---|
[2896] | 222 | /** |
---|
| 223 | @brief |
---|
| 224 | Executes Lua code |
---|
| 225 | @param str |
---|
| 226 | reference to string object holding the Lua code which is to be executed |
---|
[1638] | 227 | |
---|
[2896] | 228 | This function gives total access to the GUI. You can execute ANY Lua code here. |
---|
| 229 | */ |
---|
| 230 | void GUIManager::executeCode(const std::string& str) |
---|
| 231 | { |
---|
[1638] | 232 | try |
---|
| 233 | { |
---|
[2896] | 234 | this->scriptModule_->executeString(str); |
---|
[1638] | 235 | } |
---|
| 236 | catch (CEGUI::Exception& ex) |
---|
| 237 | { |
---|
[2896] | 238 | COUT(2) << "CEGUI Error: \"" << ex.getMessage() << "\" while executing code \"" << str << "\"" << std::endl; |
---|
[1638] | 239 | } |
---|
| 240 | } |
---|
| 241 | |
---|
[2896] | 242 | /** |
---|
[3008] | 243 | |
---|
| 244 | */ |
---|
| 245 | void GUIManager::getLevelList() |
---|
| 246 | { |
---|
| 247 | lua_State* L = this->scriptModule_->getLuaState(); |
---|
| 248 | lua_newtable(L); |
---|
| 249 | |
---|
| 250 | std::vector<std::string> list = Loader::getLevelList(); |
---|
| 251 | |
---|
| 252 | int j = 1; |
---|
| 253 | for (std::vector<std::string>::iterator i = list.begin(); i != list.end(); i++) |
---|
| 254 | { |
---|
| 255 | lua_pushnumber(L,j); |
---|
| 256 | lua_pushstring(L,i->c_str()); |
---|
| 257 | lua_settable(L,-3); |
---|
| 258 | j++; |
---|
| 259 | } |
---|
| 260 | lua_setglobal(L, "levellist"); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | /** |
---|
[2896] | 264 | @brief |
---|
[2963] | 265 | Registers a GUIOverlay with the GUIManager so that the GUIOverlay can be accessed by it's name through the GUIManager. |
---|
| 266 | @param name |
---|
| 267 | The name of the GUI. |
---|
| 268 | @param overlay |
---|
| 269 | A pointer to the GUIOverlay of the GUI. |
---|
| 270 | @return |
---|
| 271 | Returns false if the Overlay was already present. |
---|
| 272 | */ |
---|
[3196] | 273 | bool GUIManager::registerOverlay(const std::string& name, GUIOverlay* overlay) |
---|
[2963] | 274 | { |
---|
| 275 | return (this->guiOverlays_.insert(std::pair<std::string, GUIOverlay*>(name, overlay))).second; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | /** |
---|
| 279 | @brief |
---|
| 280 | Get the GUIOverlay of the GUI with the given name. |
---|
| 281 | @param name |
---|
| 282 | The name of the GUI. |
---|
| 283 | @return |
---|
| 284 | Returns a pointer to the GUIOverlay. |
---|
| 285 | */ |
---|
[3196] | 286 | GUIOverlay* GUIManager::getOverlay(const std::string& name) |
---|
[2963] | 287 | { |
---|
| 288 | return (this->guiOverlays_.find(name))->second; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | /** |
---|
| 292 | @brief |
---|
[2896] | 293 | Tells the GUIManager which SceneManager to use |
---|
| 294 | @param camera |
---|
| 295 | The current camera on which the GUI should be displayed on. |
---|
| 296 | |
---|
| 297 | In fact the GUIManager needs the SceneManager and not the Camera to display the GUI. |
---|
| 298 | This means the GUI is not bound to a camera but rather to the SceneManager. |
---|
| 299 | Hidding the GUI when needed can therefore not be solved by just NOT setting the current camera. |
---|
| 300 | */ |
---|
| 301 | void GUIManager::setCamera(Ogre::Camera* camera) |
---|
[1638] | 302 | { |
---|
[2927] | 303 | if (camera == NULL) |
---|
| 304 | this->guiRenderer_->setTargetSceneManager(0); |
---|
| 305 | else |
---|
| 306 | this->guiRenderer_->setTargetSceneManager(camera->getSceneManager()); |
---|
[2896] | 307 | } |
---|
| 308 | |
---|
| 309 | /** |
---|
| 310 | @brief |
---|
| 311 | Displays specified GUI on screen |
---|
| 312 | @param name |
---|
| 313 | The name of the GUI |
---|
| 314 | |
---|
| 315 | The function executes the Lua function with the same name in case the GUIManager is ready. |
---|
| 316 | For more details check out loadGUI_2.lua where the function presides. |
---|
| 317 | */ |
---|
| 318 | void GUIManager::showGUI(const std::string& name) |
---|
| 319 | { |
---|
[1638] | 320 | if (state_ != Uninitialised) |
---|
| 321 | { |
---|
[2896] | 322 | //COUT(3) << "Loading GUI " << name << std::endl; |
---|
[1638] | 323 | try |
---|
| 324 | { |
---|
[2896] | 325 | this->scriptModule_->executeString(std::string("showGUI(\"") + name + "\")"); |
---|
[1638] | 326 | } |
---|
| 327 | catch (CEGUI::Exception& ex) |
---|
| 328 | { |
---|
| 329 | COUT(2) << "Error while executing lua script. Message:\n" << ex.getMessage() << std::endl; |
---|
| 330 | } |
---|
| 331 | catch (...) |
---|
| 332 | { |
---|
| 333 | COUT(2) << "Could show a menu due to unknown reasons." << std::endl; |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | else |
---|
| 337 | { |
---|
| 338 | COUT(2) << "Warning: GUI Manager not yet initialised, cannot load a GUI" << std::endl; |
---|
| 339 | } |
---|
| 340 | } |
---|
| 341 | |
---|
[3196] | 342 | void GUIManager::keyPressed(const KeyEvent& evt) |
---|
| 343 | { |
---|
| 344 | guiSystem_->injectKeyDown(evt.key); guiSystem_->injectChar(evt.text); |
---|
| 345 | } |
---|
| 346 | void GUIManager::keyReleased(const KeyEvent& evt) |
---|
| 347 | { |
---|
| 348 | guiSystem_->injectKeyUp(evt.key); |
---|
| 349 | } |
---|
| 350 | |
---|
[2896] | 351 | /** |
---|
| 352 | @brief |
---|
| 353 | Function receiving a mouse button pressed event. |
---|
| 354 | @param id |
---|
| 355 | ID of the mouse button which got pressed |
---|
[1638] | 356 | |
---|
[2896] | 357 | This function is inherited by MouseHandler and injects the event into CEGUI. |
---|
| 358 | It is for CEGUI to process the event. |
---|
| 359 | */ |
---|
[1887] | 360 | void GUIManager::mouseButtonPressed(MouseButtonCode::ByEnum id) |
---|
[1638] | 361 | { |
---|
| 362 | try |
---|
| 363 | { |
---|
| 364 | guiSystem_->injectMouseButtonDown(convertButton(id)); |
---|
| 365 | } |
---|
| 366 | catch (CEGUI::ScriptException& ex) |
---|
| 367 | { |
---|
| 368 | // We simply ignore the exception and proceed |
---|
| 369 | COUT(1) << ex.getMessage() << std::endl; |
---|
| 370 | } |
---|
| 371 | } |
---|
| 372 | |
---|
[2896] | 373 | /** |
---|
| 374 | @brief |
---|
| 375 | Function receiving a mouse button released event. |
---|
| 376 | @param id |
---|
| 377 | ID of the mouse button which got released |
---|
| 378 | |
---|
| 379 | This function is inherited by MouseHandler and injects the event into CEGUI. |
---|
| 380 | It is for CEGUI to process the event. |
---|
| 381 | */ |
---|
[1887] | 382 | void GUIManager::mouseButtonReleased(MouseButtonCode::ByEnum id) |
---|
[1638] | 383 | { |
---|
| 384 | try |
---|
| 385 | { |
---|
| 386 | guiSystem_->injectMouseButtonUp(convertButton(id)); |
---|
| 387 | } |
---|
| 388 | catch (CEGUI::ScriptException& ex) |
---|
| 389 | { |
---|
| 390 | // We simply ignore the exception and proceed |
---|
| 391 | COUT(1) << ex.getMessage() << std::endl; |
---|
| 392 | } |
---|
| 393 | } |
---|
| 394 | |
---|
[3196] | 395 | void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
| 396 | { |
---|
| 397 | guiSystem_->injectMouseMove(static_cast<float>(rel.x), static_cast<float>(rel.y)); |
---|
| 398 | } |
---|
| 399 | void GUIManager::mouseScrolled(int abs, int rel) |
---|
| 400 | { |
---|
| 401 | guiSystem_->injectMouseWheelChange(static_cast<float>(rel)); |
---|
| 402 | } |
---|
| 403 | |
---|
[2896] | 404 | /** |
---|
| 405 | @brief |
---|
| 406 | converts mouse event code to CEGUI event code |
---|
| 407 | @param button |
---|
| 408 | code of the mouse button as we use it in Orxonox |
---|
| 409 | @return |
---|
| 410 | code of the mouse button as it is used by CEGUI |
---|
[1638] | 411 | |
---|
[2896] | 412 | Simple convertion from mouse event code in Orxonox to the one used in CEGUI. |
---|
| 413 | */ |
---|
[3196] | 414 | static inline CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button) |
---|
[1638] | 415 | { |
---|
| 416 | switch (button) |
---|
| 417 | { |
---|
[1887] | 418 | case MouseButtonCode::Left: |
---|
[1638] | 419 | return CEGUI::LeftButton; |
---|
| 420 | |
---|
[1887] | 421 | case MouseButtonCode::Right: |
---|
[1638] | 422 | return CEGUI::RightButton; |
---|
| 423 | |
---|
[1887] | 424 | case MouseButtonCode::Middle: |
---|
[1638] | 425 | return CEGUI::MiddleButton; |
---|
| 426 | |
---|
[1887] | 427 | case MouseButtonCode::Button3: |
---|
[1638] | 428 | return CEGUI::X1Button; |
---|
| 429 | |
---|
[1887] | 430 | case MouseButtonCode::Button4: |
---|
[1638] | 431 | return CEGUI::X2Button; |
---|
| 432 | |
---|
| 433 | default: |
---|
| 434 | return CEGUI::NoButton; |
---|
| 435 | } |
---|
| 436 | } |
---|
| 437 | } |
---|