[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 |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file |
---|
[1653] | 31 | @brief |
---|
| 32 | Implementation of the GUIManager class. |
---|
[1638] | 33 | */ |
---|
| 34 | |
---|
| 35 | #include "OrxonoxStableHeaders.h" |
---|
| 36 | #include "GUIManager.h" |
---|
| 37 | |
---|
| 38 | #include <OgreRenderWindow.h> |
---|
| 39 | #include <OgreRoot.h> |
---|
| 40 | #include <CEGUI.h> |
---|
[2664] | 41 | #include <ogreceguirenderer/OgreCEGUIRenderer.h> |
---|
| 42 | #ifdef CEGUILUA_USE_INTERNAL_LIBRARY |
---|
| 43 | # include <ceguilua/CEGUILua.h> |
---|
| 44 | #else |
---|
| 45 | # include <CEGUILua.h> |
---|
| 46 | #endif |
---|
| 47 | |
---|
[1764] | 48 | #include "util/Exception.h" |
---|
[1638] | 49 | #include "core/input/InputManager.h" |
---|
| 50 | #include "core/input/SimpleInputState.h" |
---|
| 51 | #include "core/ConsoleCommand.h" |
---|
| 52 | #include "core/Core.h" |
---|
[2664] | 53 | #include "ToluaBindCore.h" |
---|
| 54 | #include "ToluaBindOrxonox.h" |
---|
[1638] | 55 | |
---|
[2664] | 56 | extern "C" { |
---|
| 57 | #include <lua.h> |
---|
| 58 | } |
---|
[1638] | 59 | |
---|
| 60 | namespace orxonox |
---|
| 61 | { |
---|
[1755] | 62 | SetConsoleCommandShortcut(GUIManager, showGUI_s).keybindMode(KeybindMode::OnPress); |
---|
[1638] | 63 | |
---|
[1646] | 64 | GUIManager* GUIManager::singletonRef_s = 0; |
---|
| 65 | |
---|
[1638] | 66 | GUIManager::GUIManager() |
---|
[1640] | 67 | //: emptySceneManager_(0) |
---|
| 68 | : backgroundSceneManager_(0) |
---|
| 69 | //, emptyCamera_(0) |
---|
[1638] | 70 | , backgroundCamera_(0) |
---|
[1640] | 71 | //, viewport_(0) |
---|
[1638] | 72 | , renderWindow_(0) |
---|
| 73 | , guiRenderer_(0) |
---|
| 74 | , resourceProvider_(0) |
---|
| 75 | , scriptModule_(0) |
---|
| 76 | , guiSystem_(0) |
---|
| 77 | , state_(Uninitialised) |
---|
| 78 | { |
---|
[1646] | 79 | assert(singletonRef_s == 0); |
---|
| 80 | singletonRef_s = this; |
---|
[1638] | 81 | } |
---|
| 82 | |
---|
| 83 | GUIManager::~GUIManager() |
---|
| 84 | { |
---|
[1646] | 85 | if (backgroundCamera_) |
---|
| 86 | backgroundSceneManager_->destroyCamera(backgroundCamera_); |
---|
| 87 | |
---|
| 88 | if (backgroundSceneManager_) |
---|
[1661] | 89 | { |
---|
| 90 | // We have to make sure the SceneManager is not anymore referenced. |
---|
| 91 | // For the case that the target SceneManager was yet another one, it |
---|
| 92 | // wouldn't matter anyway since this is the destructor. |
---|
| 93 | guiRenderer_->setTargetSceneManager(0); |
---|
[1646] | 94 | Ogre::Root::getSingleton().destroySceneManager(backgroundSceneManager_); |
---|
[1661] | 95 | } |
---|
[1646] | 96 | |
---|
[1670] | 97 | InputManager::getInstance().requestDestroyState("gui"); |
---|
[1646] | 98 | |
---|
| 99 | if (guiSystem_) |
---|
| 100 | delete guiSystem_; |
---|
| 101 | |
---|
| 102 | if (scriptModule_) |
---|
| 103 | { |
---|
| 104 | // destroy our own tolua interfaces |
---|
[2662] | 105 | lua_pushnil(luaState_); |
---|
| 106 | lua_setglobal(luaState_, "Orxonox"); |
---|
| 107 | lua_pushnil(luaState_); |
---|
| 108 | lua_setglobal(luaState_, "Core"); |
---|
[2664] | 109 | // TODO: deleting the script module fails an assertion. |
---|
[1646] | 110 | // However there is not much we can do about it since it occurs too when |
---|
| 111 | // we don't open Core or Orxonox. Might be a CEGUI issue. |
---|
| 112 | // The memory leak is not a problem anyway.. |
---|
[2662] | 113 | delete scriptModule_; |
---|
[1646] | 114 | } |
---|
| 115 | |
---|
| 116 | if (guiRenderer_) |
---|
| 117 | delete guiRenderer_; |
---|
| 118 | |
---|
| 119 | singletonRef_s = 0; |
---|
[1638] | 120 | } |
---|
| 121 | |
---|
[1686] | 122 | bool GUIManager::initialise(Ogre::RenderWindow* renderWindow) |
---|
[1638] | 123 | { |
---|
| 124 | using namespace CEGUI; |
---|
| 125 | if (state_ == Uninitialised) |
---|
| 126 | { |
---|
[1776] | 127 | COUT(3) << "Initialising CEGUI." << std::endl; |
---|
[1638] | 128 | |
---|
| 129 | try |
---|
| 130 | { |
---|
[1686] | 131 | // save the render window |
---|
| 132 | renderWindow_ = renderWindow; |
---|
[1638] | 133 | |
---|
| 134 | // Full screen viewport with Z order = 0 (top most). Don't yet feed a camera (so nothing gets rendered) |
---|
[1640] | 135 | //this->viewport_ = renderWindow_->addViewport(0, 3); |
---|
| 136 | //this->viewport_->setOverlaysEnabled(false); |
---|
| 137 | //this->viewport_->setShadowsEnabled(false); |
---|
| 138 | //this->viewport_->setSkiesEnabled(false); |
---|
| 139 | //this->viewport_->setClearEveryFrame(false); |
---|
[1638] | 140 | |
---|
| 141 | // Note: No SceneManager specified yet |
---|
| 142 | this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_MAIN, true, 3000); |
---|
| 143 | this->resourceProvider_ = guiRenderer_->createResourceProvider(); |
---|
| 144 | this->resourceProvider_->setDefaultResourceGroup("GUI"); |
---|
[1776] | 145 | |
---|
[1638] | 146 | // setup scripting |
---|
| 147 | this->scriptModule_ = new LuaScriptModule(); |
---|
[1646] | 148 | this->luaState_ = this->scriptModule_->getLuaState(); |
---|
[1638] | 149 | |
---|
| 150 | // create the CEGUI system singleton |
---|
| 151 | this->guiSystem_ = new System(this->guiRenderer_, this->resourceProvider_, 0, this->scriptModule_); |
---|
[1776] | 152 | |
---|
[1638] | 153 | // set the log level according to ours (translate by subtracting 1) |
---|
| 154 | Logger::getSingleton().setLoggingLevel( |
---|
| 155 | (LoggingLevel)(Core::getSoftDebugLevel(OutputHandler::LD_Logfile) - 1)); |
---|
[1776] | 156 | |
---|
[1638] | 157 | // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place |
---|
| 158 | tolua_Core_open(this->scriptModule_->getLuaState()); |
---|
| 159 | tolua_Orxonox_open(this->scriptModule_->getLuaState()); |
---|
| 160 | |
---|
| 161 | // register us as input handler |
---|
[1653] | 162 | SimpleInputState* state = InputManager::getInstance().createInputState<SimpleInputState>("gui", 30); |
---|
[1638] | 163 | state->setHandler(this); |
---|
[1878] | 164 | state->setJoyStickHandler(&InputManager::EMPTY_HANDLER); |
---|
[1638] | 165 | |
---|
| 166 | // load the background scene |
---|
| 167 | loadScenes(); |
---|
[1662] | 168 | //CEGUI::KeyEventArgs e; |
---|
| 169 | //e.codepoint |
---|
[1638] | 170 | } |
---|
| 171 | catch (CEGUI::Exception& ex) |
---|
| 172 | { |
---|
[1653] | 173 | #if CEGUI_VERSION_MAJOR == 0 && CEGUI_VERSION_MINOR < 6 |
---|
[1645] | 174 | throw GeneralException(ex.getMessage().c_str()); |
---|
| 175 | #else |
---|
[1638] | 176 | throw GeneralException(ex.getMessage().c_str(), ex.getLine(), |
---|
| 177 | ex.getFileName().c_str(), ex.getName().c_str()); |
---|
[1645] | 178 | #endif |
---|
[1638] | 179 | } |
---|
| 180 | |
---|
| 181 | state_ = Ready; |
---|
| 182 | } |
---|
[1776] | 183 | |
---|
[1638] | 184 | return true; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | void GUIManager::loadScenes() |
---|
| 188 | { |
---|
| 189 | // first of all, we need to have our own SceneManager for the GUI. The reason |
---|
| 190 | // is that we might have multiple viewports when in play mode (e.g. the view of |
---|
| 191 | // a camera fixed at the back of the ship). That forces us to create our own |
---|
| 192 | // full screen viewport that is on top of all the others, but doesn't clear the |
---|
| 193 | // port before rendering, so everything from the GUI gets on top eventually. |
---|
| 194 | // But in order to realise that, we also need a SceneManager with an empty scene, |
---|
| 195 | // because the SceneManager is responsible for the render queue. |
---|
[1640] | 196 | //this->emptySceneManager_ = Ogre::Root::getSingleton() |
---|
| 197 | // .createSceneManager(Ogre::ST_GENERIC, "GUI/EmptySceneManager"); |
---|
[1638] | 198 | |
---|
| 199 | // we also need a camera or we won't see anything at all. |
---|
| 200 | // The camera settings don't matter at all for an empty scene since the GUI |
---|
| 201 | // gets rendered on top of the screen rather than into the scene. |
---|
[1640] | 202 | //this->emptyCamera_ = this->emptySceneManager_->createCamera("GUI/EmptyCamera"); |
---|
[1638] | 203 | |
---|
| 204 | // Create another SceneManager that enables to display some 3D |
---|
| 205 | // scene in the background of the main menu. |
---|
| 206 | this->backgroundSceneManager_ = Ogre::Root::getSingleton() |
---|
| 207 | .createSceneManager(Ogre::ST_GENERIC, "GUI/BackgroundSceneManager"); |
---|
| 208 | this->backgroundCamera_ = backgroundSceneManager_->createCamera("GUI/BackgroundCamera"); |
---|
| 209 | |
---|
| 210 | // TODO: create something 3D |
---|
| 211 | try |
---|
| 212 | { |
---|
| 213 | this->scriptModule_->executeScriptFile("loadGUI.lua", "GUI"); |
---|
| 214 | } |
---|
| 215 | catch (CEGUI::Exception& ex) |
---|
| 216 | { |
---|
[1645] | 217 | #if CEGUI_VERSION_MINOR < 6 |
---|
| 218 | throw GeneralException(ex.getMessage().c_str()); |
---|
| 219 | #else |
---|
[1638] | 220 | throw GeneralException(ex.getMessage().c_str(), ex.getLine(), |
---|
| 221 | ex.getFileName().c_str(), ex.getName().c_str()); |
---|
[1645] | 222 | #endif |
---|
[1638] | 223 | } |
---|
| 224 | } |
---|
| 225 | |
---|
[1640] | 226 | void GUIManager::showGUI(const std::string& name, Ogre::SceneManager* sceneManager)// bool showBackground) |
---|
[1638] | 227 | { |
---|
| 228 | if (state_ != Uninitialised) |
---|
| 229 | { |
---|
| 230 | if (state_ == OnDisplay) |
---|
[1661] | 231 | hideGUI(); |
---|
[1638] | 232 | |
---|
| 233 | COUT(3) << "Loading GUI " << name << std::endl; |
---|
| 234 | try |
---|
| 235 | { |
---|
[1640] | 236 | if (!sceneManager) |
---|
[1638] | 237 | { |
---|
| 238 | // currently, only an image is loaded. We could do 3D, see loadBackground. |
---|
[1640] | 239 | //this->viewport_->setClearEveryFrame(true); |
---|
[1638] | 240 | this->guiRenderer_->setTargetSceneManager(this->backgroundSceneManager_); |
---|
[1640] | 241 | //this->viewport_->setCamera(this->backgroundCamera_); |
---|
[1638] | 242 | |
---|
| 243 | lua_pushboolean(this->scriptModule_->getLuaState(), true); |
---|
| 244 | lua_setglobal(this->scriptModule_->getLuaState(), "showBackground"); |
---|
| 245 | } |
---|
| 246 | else |
---|
| 247 | { |
---|
[1640] | 248 | //this->viewport_->setClearEveryFrame(false); |
---|
| 249 | this->guiRenderer_->setTargetSceneManager(sceneManager); |
---|
| 250 | //this->viewport_->setCamera(this->emptyCamera_); |
---|
[1638] | 251 | |
---|
| 252 | lua_pushboolean(this->scriptModule_->getLuaState(), false); |
---|
| 253 | lua_setglobal(this->scriptModule_->getLuaState(), "showBackground"); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | this->scriptModule_->executeScriptGlobal("showMainMenu"); |
---|
| 257 | |
---|
[1642] | 258 | InputManager::getInstance().requestEnterState("gui"); |
---|
[1638] | 259 | |
---|
| 260 | this->state_ = OnDisplay; |
---|
| 261 | } |
---|
| 262 | catch (CEGUI::Exception& ex) |
---|
| 263 | { |
---|
| 264 | COUT(2) << "Error while executing lua script. Message:\n" << ex.getMessage() << std::endl; |
---|
| 265 | } |
---|
| 266 | catch (...) |
---|
| 267 | { |
---|
| 268 | COUT(2) << "Could show a menu due to unknown reasons." << std::endl; |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | else |
---|
| 272 | { |
---|
| 273 | COUT(2) << "Warning: GUI Manager not yet initialised, cannot load a GUI" << std::endl; |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
[1661] | 277 | void GUIManager::hideGUI() |
---|
[1638] | 278 | { |
---|
| 279 | if (this->state_ != OnDisplay) |
---|
| 280 | return; |
---|
[1640] | 281 | //this->viewport_->setCamera(0); |
---|
| 282 | this->guiRenderer_->setTargetSceneManager(0); |
---|
[1638] | 283 | this->state_ = Ready; |
---|
[1642] | 284 | InputManager::getInstance().requestLeaveState("gui"); |
---|
[1638] | 285 | } |
---|
| 286 | |
---|
[1887] | 287 | void GUIManager::mouseButtonPressed(MouseButtonCode::ByEnum id) |
---|
[1638] | 288 | { |
---|
| 289 | try |
---|
| 290 | { |
---|
| 291 | guiSystem_->injectMouseButtonDown(convertButton(id)); |
---|
| 292 | } |
---|
| 293 | catch (CEGUI::ScriptException& ex) |
---|
| 294 | { |
---|
| 295 | // We simply ignore the exception and proceed |
---|
| 296 | COUT(1) << ex.getMessage() << std::endl; |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | |
---|
[1887] | 300 | void GUIManager::mouseButtonReleased(MouseButtonCode::ByEnum id) |
---|
[1638] | 301 | { |
---|
| 302 | try |
---|
| 303 | { |
---|
| 304 | guiSystem_->injectMouseButtonUp(convertButton(id)); |
---|
| 305 | } |
---|
| 306 | catch (CEGUI::ScriptException& ex) |
---|
| 307 | { |
---|
| 308 | // We simply ignore the exception and proceed |
---|
| 309 | COUT(1) << ex.getMessage() << std::endl; |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | |
---|
[1887] | 314 | inline CEGUI::MouseButton GUIManager::convertButton(MouseButtonCode::ByEnum button) |
---|
[1638] | 315 | { |
---|
| 316 | switch (button) |
---|
| 317 | { |
---|
[1887] | 318 | case MouseButtonCode::Left: |
---|
[1638] | 319 | return CEGUI::LeftButton; |
---|
| 320 | |
---|
[1887] | 321 | case MouseButtonCode::Right: |
---|
[1638] | 322 | return CEGUI::RightButton; |
---|
| 323 | |
---|
[1887] | 324 | case MouseButtonCode::Middle: |
---|
[1638] | 325 | return CEGUI::MiddleButton; |
---|
| 326 | |
---|
[1887] | 327 | case MouseButtonCode::Button3: |
---|
[1638] | 328 | return CEGUI::X1Button; |
---|
| 329 | |
---|
[1887] | 330 | case MouseButtonCode::Button4: |
---|
[1638] | 331 | return CEGUI::X2Button; |
---|
| 332 | |
---|
| 333 | default: |
---|
| 334 | return CEGUI::NoButton; |
---|
| 335 | } |
---|
| 336 | } |
---|
| 337 | } |
---|