Changeset 2908 for code/branches/questsystem5/src/orxonox/gui/GUIManager.cc
- Timestamp:
- Apr 8, 2009, 12:58:47 AM (16 years ago)
- Location:
- code/branches/questsystem5
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/questsystem5
- Property svn:mergeinfo changed
-
code/branches/questsystem5/src/orxonox/gui/GUIManager.cc
r2907 r2908 22 22 * Author: 23 23 * Reto Grieder 24 * Benjamin Knecht25 24 * Co-authors: 26 * 25 * ... 27 26 * 28 27 */ … … 37 36 #include "GUIManager.h" 38 37 39 #include <boost/filesystem /path.hpp>38 #include <boost/filesystem.hpp> 40 39 #include <OgreRenderWindow.h> 40 #include <OgreRoot.h> 41 41 #include <CEGUI.h> 42 42 #include <CEGUIDefaultLogger.h> … … 50 50 51 51 #include "util/Exception.h" 52 #include "core/input/InputManager.h" 53 #include "core/input/SimpleInputState.h" 52 54 #include "core/ConsoleCommand.h" 53 55 #include "core/Core.h" 54 #include "core/Clock.h"55 56 #include "ToluaBindCore.h" 56 57 #include "ToluaBindOrxonox.h" … … 62 63 namespace orxonox 63 64 { 65 SetConsoleCommandShortcut(GUIManager, showGUI_s).keybindMode(KeybindMode::OnPress); 66 64 67 GUIManager* GUIManager::singletonRef_s = 0; 65 68 66 69 GUIManager::GUIManager() 67 : renderWindow_(0) 70 //: emptySceneManager_(0) 71 : backgroundSceneManager_(0) 72 //, emptyCamera_(0) 73 , backgroundCamera_(0) 74 //, viewport_(0) 75 , renderWindow_(0) 68 76 , guiRenderer_(0) 69 77 , resourceProvider_(0) … … 76 84 } 77 85 78 /**79 @brief80 Deconstructor of the GUIManager81 82 Basically shuts down CEGUI and destroys the Lua engine and afterwards the interface to the Ogre engine.83 */84 86 GUIManager::~GUIManager() 85 87 { 88 if (backgroundCamera_) 89 backgroundSceneManager_->destroyCamera(backgroundCamera_); 90 91 if (backgroundSceneManager_) 92 { 93 // We have to make sure the SceneManager is not anymore referenced. 94 // For the case that the target SceneManager was yet another one, it 95 // wouldn't matter anyway since this is the destructor. 96 guiRenderer_->setTargetSceneManager(0); 97 Ogre::Root::getSingleton().destroySceneManager(backgroundSceneManager_); 98 } 99 100 InputManager::getInstance().requestDestroyState("gui"); 101 86 102 if (guiSystem_) 87 103 delete guiSystem_; … … 94 110 lua_pushnil(luaState_); 95 111 lua_setglobal(luaState_, "Core"); 112 // TODO: deleting the script module fails an assertion. 113 // However there is not much we can do about it since it occurs too when 114 // we don't open Core or Orxonox. Might be a CEGUI issue. 115 // The memory leak is not a problem anyway.. 96 116 delete scriptModule_; 97 117 } … … 103 123 } 104 124 105 /**106 @brief107 Initialises the GUIManager by starting up CEGUI108 @param renderWindow109 Ogre's render window. Without this, the GUI cannot be displayed.110 @return true if success, otherwise false111 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 */119 125 bool GUIManager::initialise(Ogre::RenderWindow* renderWindow) 120 126 { … … 129 135 renderWindow_ = renderWindow; 130 136 137 // Full screen viewport with Z order = 0 (top most). Don't yet feed a camera (so nothing gets rendered) 138 //this->viewport_ = renderWindow_->addViewport(0, 3); 139 //this->viewport_->setOverlaysEnabled(false); 140 //this->viewport_->setShadowsEnabled(false); 141 //this->viewport_->setSkiesEnabled(false); 142 //this->viewport_->setClearEveryFrame(false); 143 131 144 // Note: No SceneManager specified yet 132 this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_ OVERLAY, true, 3000);145 this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_MAIN, true, 3000); 133 146 this->resourceProvider_ = guiRenderer_->createResourceProvider(); 134 147 this->resourceProvider_->setDefaultResourceGroup("GUI"); … … 153 166 tolua_Orxonox_open(this->scriptModule_->getLuaState()); 154 167 155 // initialise the basic lua code 156 loadLuaCode(); 168 // register us as input handler 169 SimpleInputState* state = InputManager::getInstance().createInputState<SimpleInputState>("gui", 30); 170 state->setHandler(this); 171 state->setJoyStickHandler(&InputManager::EMPTY_HANDLER); 172 173 // load the background scene 174 loadScenes(); 175 //CEGUI::KeyEventArgs e; 176 //e.codepoint 157 177 } 158 178 catch (CEGUI::Exception& ex) … … 172 192 } 173 193 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() 186 { 194 void GUIManager::loadScenes() 195 { 196 // first of all, we need to have our own SceneManager for the GUI. The reason 197 // is that we might have multiple viewports when in play mode (e.g. the view of 198 // a camera fixed at the back of the ship). That forces us to create our own 199 // full screen viewport that is on top of all the others, but doesn't clear the 200 // port before rendering, so everything from the GUI gets on top eventually. 201 // But in order to realise that, we also need a SceneManager with an empty scene, 202 // because the SceneManager is responsible for the render queue. 203 //this->emptySceneManager_ = Ogre::Root::getSingleton() 204 // .createSceneManager(Ogre::ST_GENERIC, "GUI/EmptySceneManager"); 205 206 // we also need a camera or we won't see anything at all. 207 // The camera settings don't matter at all for an empty scene since the GUI 208 // gets rendered on top of the screen rather than into the scene. 209 //this->emptyCamera_ = this->emptySceneManager_->createCamera("GUI/EmptyCamera"); 210 211 // Create another SceneManager that enables to display some 3D 212 // scene in the background of the main menu. 213 this->backgroundSceneManager_ = Ogre::Root::getSingleton() 214 .createSceneManager(Ogre::ST_GENERIC, "GUI/BackgroundSceneManager"); 215 this->backgroundCamera_ = backgroundSceneManager_->createCamera("GUI/BackgroundCamera"); 216 217 // TODO: create something 3D 187 218 try 188 219 { 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"); 220 this->scriptModule_->executeScriptFile("loadGUI.lua", "GUI"); 194 221 } 195 222 catch (CEGUI::Exception& ex) … … 204 231 } 205 232 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) 217 { 218 assert(guiSystem_); 219 guiSystem_->injectTimePulse(time.getDeltaTime()); 220 } 221 222 /** 223 @brief 224 Executes Lua code 225 @param str 226 reference to string object holding the Lua code which is to be executed 227 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 { 232 try 233 { 234 this->scriptModule_->executeString(str); 235 } 236 catch (CEGUI::Exception& ex) 237 { 238 COUT(2) << "CEGUI Error: \"" << ex.getMessage() << "\" while executing code \"" << str << "\"" << std::endl; 239 } 240 } 241 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) 253 { 254 this->guiRenderer_->setTargetSceneManager(camera->getSceneManager()); 255 } 256 257 /** 258 @brief 259 Displays specified GUI on screen 260 @param name 261 The name of the GUI 262 263 The function executes the Lua function with the same name in case the GUIManager is ready. 264 For more details check out loadGUI_2.lua where the function presides. 265 */ 266 void GUIManager::showGUI(const std::string& name) 233 void GUIManager::showGUI(const std::string& name, Ogre::SceneManager* sceneManager)// bool showBackground) 267 234 { 268 235 if (state_ != Uninitialised) 269 236 { 270 //COUT(3) << "Loading GUI " << name << std::endl; 237 if (state_ == OnDisplay) 238 hideGUI(); 239 240 COUT(3) << "Loading GUI " << name << std::endl; 271 241 try 272 242 { 273 this->scriptModule_->executeString(std::string("showGUI(\"") + name + "\")"); 243 if (!sceneManager) 244 { 245 // currently, only an image is loaded. We could do 3D, see loadBackground. 246 //this->viewport_->setClearEveryFrame(true); 247 this->guiRenderer_->setTargetSceneManager(this->backgroundSceneManager_); 248 //this->viewport_->setCamera(this->backgroundCamera_); 249 250 lua_pushboolean(this->scriptModule_->getLuaState(), true); 251 lua_setglobal(this->scriptModule_->getLuaState(), "showBackground"); 252 } 253 else 254 { 255 //this->viewport_->setClearEveryFrame(false); 256 this->guiRenderer_->setTargetSceneManager(sceneManager); 257 //this->viewport_->setCamera(this->emptyCamera_); 258 259 lua_pushboolean(this->scriptModule_->getLuaState(), false); 260 lua_setglobal(this->scriptModule_->getLuaState(), "showBackground"); 261 } 262 263 this->scriptModule_->executeScriptGlobal("showMainMenu"); 264 265 InputManager::getInstance().requestEnterState("gui"); 266 267 this->state_ = OnDisplay; 274 268 } 275 269 catch (CEGUI::Exception& ex) … … 288 282 } 289 283 290 /** 291 @brief 292 Function receiving a mouse button pressed event. 293 @param id 294 ID of the mouse button which got pressed 295 296 This function is inherited by MouseHandler and injects the event into CEGUI. 297 It is for CEGUI to process the event. 298 */ 284 void GUIManager::hideGUI() 285 { 286 if (this->state_ != OnDisplay) 287 return; 288 //this->viewport_->setCamera(0); 289 this->guiRenderer_->setTargetSceneManager(0); 290 this->state_ = Ready; 291 InputManager::getInstance().requestLeaveState("gui"); 292 } 293 299 294 void GUIManager::mouseButtonPressed(MouseButtonCode::ByEnum id) 300 295 { … … 310 305 } 311 306 312 /**313 @brief314 Function receiving a mouse button released event.315 @param id316 ID of the mouse button which got released317 318 This function is inherited by MouseHandler and injects the event into CEGUI.319 It is for CEGUI to process the event.320 */321 307 void GUIManager::mouseButtonReleased(MouseButtonCode::ByEnum id) 322 308 { … … 332 318 } 333 319 334 /** 335 @brief 336 converts mouse event code to CEGUI event code 337 @param button 338 code of the mouse button as we use it in Orxonox 339 @return 340 code of the mouse button as it is used by CEGUI 341 342 Simple convertion from mouse event code in Orxonox to the one used in CEGUI. 343 */ 320 344 321 inline CEGUI::MouseButton GUIManager::convertButton(MouseButtonCode::ByEnum button) 345 322 {
Note: See TracChangeset
for help on using the changeset viewer.