[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: |
---|
| 26 | * |
---|
[2896] | 27 | * |
---|
[1638] | 28 | */ |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | @file |
---|
[1653] | 32 | @brief |
---|
| 33 | Implementation of the GUIManager class. |
---|
[1638] | 34 | */ |
---|
| 35 | |
---|
| 36 | #include "OrxonoxStableHeaders.h" |
---|
| 37 | #include "GUIManager.h" |
---|
| 38 | |
---|
[2896] | 39 | #include <boost/filesystem/path.hpp> |
---|
[1638] | 40 | #include <OgreRenderWindow.h> |
---|
| 41 | #include <CEGUI.h> |
---|
[2710] | 42 | #include <CEGUIDefaultLogger.h> |
---|
| 43 | #include <ogreceguirenderer/OgreCEGUIRenderer.h> |
---|
| 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 | |
---|
[1764] | 51 | #include "util/Exception.h" |
---|
[1638] | 52 | #include "core/ConsoleCommand.h" |
---|
| 53 | #include "core/Core.h" |
---|
[2896] | 54 | #include "core/Clock.h" |
---|
[2710] | 55 | #include "ToluaBindCore.h" |
---|
| 56 | #include "ToluaBindOrxonox.h" |
---|
[1638] | 57 | |
---|
[2710] | 58 | extern "C" { |
---|
| 59 | #include <lua.h> |
---|
| 60 | } |
---|
[1638] | 61 | |
---|
| 62 | namespace orxonox |
---|
| 63 | { |
---|
[1646] | 64 | GUIManager* GUIManager::singletonRef_s = 0; |
---|
| 65 | |
---|
[1638] | 66 | GUIManager::GUIManager() |
---|
[2896] | 67 | : renderWindow_(0) |
---|
[1638] | 68 | , guiRenderer_(0) |
---|
| 69 | , resourceProvider_(0) |
---|
| 70 | , scriptModule_(0) |
---|
| 71 | , guiSystem_(0) |
---|
| 72 | , state_(Uninitialised) |
---|
| 73 | { |
---|
[1646] | 74 | assert(singletonRef_s == 0); |
---|
| 75 | singletonRef_s = this; |
---|
[1638] | 76 | } |
---|
| 77 | |
---|
[2896] | 78 | /** |
---|
| 79 | @brief |
---|
| 80 | Deconstructor of the GUIManager |
---|
| 81 | |
---|
| 82 | Basically shuts down CEGUI and destroys the Lua engine and afterwards the interface to the Ogre engine. |
---|
| 83 | */ |
---|
[1638] | 84 | GUIManager::~GUIManager() |
---|
| 85 | { |
---|
[1646] | 86 | if (guiSystem_) |
---|
| 87 | delete guiSystem_; |
---|
| 88 | |
---|
| 89 | if (scriptModule_) |
---|
| 90 | { |
---|
| 91 | // destroy our own tolua interfaces |
---|
[2662] | 92 | lua_pushnil(luaState_); |
---|
| 93 | lua_setglobal(luaState_, "Orxonox"); |
---|
| 94 | lua_pushnil(luaState_); |
---|
| 95 | lua_setglobal(luaState_, "Core"); |
---|
| 96 | delete scriptModule_; |
---|
[1646] | 97 | } |
---|
| 98 | |
---|
| 99 | if (guiRenderer_) |
---|
| 100 | delete guiRenderer_; |
---|
| 101 | |
---|
| 102 | singletonRef_s = 0; |
---|
[1638] | 103 | } |
---|
| 104 | |
---|
[2896] | 105 | /** |
---|
| 106 | @brief |
---|
| 107 | Initialises the GUIManager by starting up CEGUI |
---|
| 108 | @param renderWindow |
---|
| 109 | Ogre's render window. Without this, the GUI cannot be displayed. |
---|
| 110 | @return true if success, otherwise false |
---|
| 111 | |
---|
| 112 | Before this call the GUIManager won't do anything, but can be accessed. |
---|
| 113 | |
---|
| 114 | Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine. |
---|
| 115 | The log is set up and connected to the CEGUILogger. |
---|
| 116 | After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code. |
---|
| 117 | Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically). |
---|
| 118 | */ |
---|
[1686] | 119 | bool GUIManager::initialise(Ogre::RenderWindow* renderWindow) |
---|
[1638] | 120 | { |
---|
| 121 | using namespace CEGUI; |
---|
| 122 | if (state_ == Uninitialised) |
---|
| 123 | { |
---|
[1776] | 124 | COUT(3) << "Initialising CEGUI." << std::endl; |
---|
[1638] | 125 | |
---|
| 126 | try |
---|
| 127 | { |
---|
[1686] | 128 | // save the render window |
---|
| 129 | renderWindow_ = renderWindow; |
---|
[1638] | 130 | |
---|
| 131 | // Note: No SceneManager specified yet |
---|
[2896] | 132 | this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, true, 3000); |
---|
[1638] | 133 | this->resourceProvider_ = guiRenderer_->createResourceProvider(); |
---|
| 134 | this->resourceProvider_->setDefaultResourceGroup("GUI"); |
---|
[1776] | 135 | |
---|
[1638] | 136 | // setup scripting |
---|
| 137 | this->scriptModule_ = new LuaScriptModule(); |
---|
[1646] | 138 | this->luaState_ = this->scriptModule_->getLuaState(); |
---|
[1638] | 139 | |
---|
[2710] | 140 | // Create our own logger to specify the filepath |
---|
| 141 | boost::filesystem::path ceguiLogFilepath(Core::getLogPath() / "cegui.log"); |
---|
| 142 | this->ceguiLogger_ = new DefaultLogger(); |
---|
[2759] | 143 | this->ceguiLogger_->setLogFilename(ceguiLogFilepath.string()); |
---|
[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 | // call main Lua script |
---|
| 190 | this->scriptModule_->executeScriptFile("loadGUI_2.lua", "GUI"); |
---|
| 191 | // set datapath for GUI data |
---|
| 192 | lua_pushfstring(this->scriptModule_->getLuaState(), Core::getMediaPathString().c_str()); |
---|
| 193 | lua_setglobal(this->scriptModule_->getLuaState(), "datapath"); |
---|
| 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 | /** |
---|
| 243 | @brief |
---|
| 244 | Tells the GUIManager which SceneManager to use |
---|
| 245 | @param camera |
---|
| 246 | The current camera on which the GUI should be displayed on. |
---|
| 247 | |
---|
| 248 | In fact the GUIManager needs the SceneManager and not the Camera to display the GUI. |
---|
| 249 | This means the GUI is not bound to a camera but rather to the SceneManager. |
---|
| 250 | Hidding the GUI when needed can therefore not be solved by just NOT setting the current camera. |
---|
| 251 | */ |
---|
| 252 | void GUIManager::setCamera(Ogre::Camera* camera) |
---|
[1638] | 253 | { |
---|
[2927] | 254 | if (camera == NULL) |
---|
| 255 | this->guiRenderer_->setTargetSceneManager(0); |
---|
| 256 | else |
---|
| 257 | this->guiRenderer_->setTargetSceneManager(camera->getSceneManager()); |
---|
[2896] | 258 | } |
---|
| 259 | |
---|
| 260 | /** |
---|
| 261 | @brief |
---|
| 262 | Displays specified GUI on screen |
---|
| 263 | @param name |
---|
| 264 | The name of the GUI |
---|
| 265 | |
---|
| 266 | The function executes the Lua function with the same name in case the GUIManager is ready. |
---|
| 267 | For more details check out loadGUI_2.lua where the function presides. |
---|
| 268 | */ |
---|
| 269 | void GUIManager::showGUI(const std::string& name) |
---|
| 270 | { |
---|
[1638] | 271 | if (state_ != Uninitialised) |
---|
| 272 | { |
---|
[2896] | 273 | //COUT(3) << "Loading GUI " << name << std::endl; |
---|
[1638] | 274 | try |
---|
| 275 | { |
---|
[2896] | 276 | this->scriptModule_->executeString(std::string("showGUI(\"") + name + "\")"); |
---|
[1638] | 277 | } |
---|
| 278 | catch (CEGUI::Exception& ex) |
---|
| 279 | { |
---|
| 280 | COUT(2) << "Error while executing lua script. Message:\n" << ex.getMessage() << std::endl; |
---|
| 281 | } |
---|
| 282 | catch (...) |
---|
| 283 | { |
---|
| 284 | COUT(2) << "Could show a menu due to unknown reasons." << std::endl; |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | else |
---|
| 288 | { |
---|
| 289 | COUT(2) << "Warning: GUI Manager not yet initialised, cannot load a GUI" << std::endl; |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
[2896] | 293 | /** |
---|
| 294 | @brief |
---|
| 295 | Function receiving a mouse button pressed event. |
---|
| 296 | @param id |
---|
| 297 | ID of the mouse button which got pressed |
---|
[1638] | 298 | |
---|
[2896] | 299 | This function is inherited by MouseHandler and injects the event into CEGUI. |
---|
| 300 | It is for CEGUI to process the event. |
---|
| 301 | */ |
---|
[1887] | 302 | void GUIManager::mouseButtonPressed(MouseButtonCode::ByEnum id) |
---|
[1638] | 303 | { |
---|
| 304 | try |
---|
| 305 | { |
---|
| 306 | guiSystem_->injectMouseButtonDown(convertButton(id)); |
---|
| 307 | } |
---|
| 308 | catch (CEGUI::ScriptException& ex) |
---|
| 309 | { |
---|
| 310 | // We simply ignore the exception and proceed |
---|
| 311 | COUT(1) << ex.getMessage() << std::endl; |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | |
---|
[2896] | 315 | /** |
---|
| 316 | @brief |
---|
| 317 | Function receiving a mouse button released event. |
---|
| 318 | @param id |
---|
| 319 | ID of the mouse button which got released |
---|
| 320 | |
---|
| 321 | This function is inherited by MouseHandler and injects the event into CEGUI. |
---|
| 322 | It is for CEGUI to process the event. |
---|
| 323 | */ |
---|
[1887] | 324 | void GUIManager::mouseButtonReleased(MouseButtonCode::ByEnum id) |
---|
[1638] | 325 | { |
---|
| 326 | try |
---|
| 327 | { |
---|
| 328 | guiSystem_->injectMouseButtonUp(convertButton(id)); |
---|
| 329 | } |
---|
| 330 | catch (CEGUI::ScriptException& ex) |
---|
| 331 | { |
---|
| 332 | // We simply ignore the exception and proceed |
---|
| 333 | COUT(1) << ex.getMessage() << std::endl; |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | |
---|
[2896] | 337 | /** |
---|
| 338 | @brief |
---|
| 339 | converts mouse event code to CEGUI event code |
---|
| 340 | @param button |
---|
| 341 | code of the mouse button as we use it in Orxonox |
---|
| 342 | @return |
---|
| 343 | code of the mouse button as it is used by CEGUI |
---|
[1638] | 344 | |
---|
[2896] | 345 | Simple convertion from mouse event code in Orxonox to the one used in CEGUI. |
---|
| 346 | */ |
---|
[1887] | 347 | inline CEGUI::MouseButton GUIManager::convertButton(MouseButtonCode::ByEnum button) |
---|
[1638] | 348 | { |
---|
| 349 | switch (button) |
---|
| 350 | { |
---|
[1887] | 351 | case MouseButtonCode::Left: |
---|
[1638] | 352 | return CEGUI::LeftButton; |
---|
| 353 | |
---|
[1887] | 354 | case MouseButtonCode::Right: |
---|
[1638] | 355 | return CEGUI::RightButton; |
---|
| 356 | |
---|
[1887] | 357 | case MouseButtonCode::Middle: |
---|
[1638] | 358 | return CEGUI::MiddleButton; |
---|
| 359 | |
---|
[1887] | 360 | case MouseButtonCode::Button3: |
---|
[1638] | 361 | return CEGUI::X1Button; |
---|
| 362 | |
---|
[1887] | 363 | case MouseButtonCode::Button4: |
---|
[1638] | 364 | return CEGUI::X2Button; |
---|
| 365 | |
---|
| 366 | default: |
---|
| 367 | return CEGUI::NoButton; |
---|
| 368 | } |
---|
| 369 | } |
---|
| 370 | } |
---|