Changeset 6746 for code/trunk/src/libraries/core/GUIManager.cc
- Timestamp:
- Apr 16, 2010, 2:50:16 PM (14 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/core/GUIManager.cc
r6502 r6746 30 30 #include "GUIManager.h" 31 31 32 #include <boost/bind.hpp> 32 33 #include <memory> 33 34 extern "C" { … … 41 42 #include <CEGUISystem.h> 42 43 #include <CEGUIWindow.h> 44 #include <CEGUIWindowManager.h> 43 45 #include <ogreceguirenderer/OgreCEGUIRenderer.h> 44 46 … … 57 59 #include "ConsoleCommand.h" 58 60 #include "Core.h" 61 #include "GraphicsManager.h" 59 62 #include "LuaState.h" 60 63 #include "PathConfig.h" 61 64 #include "Resource.h" 65 #include "input/InputManager.h" 66 #include "input/InputState.h" 67 #include "input/KeyBinderManager.h" 62 68 63 69 namespace orxonox … … 108 114 @return true if success, otherwise false 109 115 */ 110 GUIManager::GUIManager(Ogre::RenderWindow* renderWindow, const std::pair<int, int>& mousePosition, bool bFullScreen) 111 : renderWindow_(renderWindow) 112 , resourceProvider_(0) 116 GUIManager::GUIManager(const std::pair<int, int>& mousePosition) 117 : resourceProvider_(NULL) 113 118 , camera_(NULL) 114 , bShowIngameGUI_(false)115 119 { 116 120 using namespace CEGUI; … … 119 123 120 124 // Note: No SceneManager specified yet 121 guiRenderer_.reset(new OgreCEGUIRenderer( renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, false, 3000));125 guiRenderer_.reset(new OgreCEGUIRenderer(GraphicsManager::getInstance().getRenderWindow(), Ogre::RENDER_QUEUE_OVERLAY, false, 3000)); 122 126 resourceProvider_ = guiRenderer_->createResourceProvider(); 123 127 resourceProvider_->setDefaultResourceGroup("GUI"); … … 141 145 guiSystem_.reset(new System(guiRenderer_.get(), resourceProvider_, 0, scriptModule_.get())); 142 146 143 // Initialise the basic Lua code144 this->luaState_->doFile("InitialiseGUI.lua");145 146 147 // Align CEGUI mouse with OIS mouse 147 148 guiSystem_->injectMousePosition((float)mousePosition.first, (float)mousePosition.second); 148 149 149 // Hide the mouse cursor unless playing in full screen mode 150 if (!bFullScreen) 151 CEGUI::MouseCursor::getSingleton().hide(); 150 // Initialise the Lua framework and load the schemes 151 this->luaState_->doFile("InitialiseGUI.lua"); 152 153 // Create the root nodes 154 this->rootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("MenuWidgets/StaticImage", "AbsoluteRootWindow"); 155 this->rootWindow_->setProperty("FrameEnabled", "False"); 156 this->hudRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "HUDRootWindow"); 157 this->menuRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "MenuRootWindow"); 158 // And connect them 159 CEGUI::System::getSingleton().setGUISheet(this->rootWindow_); 160 this->rootWindow_->addChildWindow(this->hudRootWindow_); 161 this->rootWindow_->addChildWindow(this->menuRootWindow_); 162 163 // Set up the sheet manager in the Lua framework 164 this->luaState_->doFile("SheetManager.lua"); 152 165 } 153 166 … … 173 186 { 174 187 assert(guiSystem_); 175 guiSystem_->injectTimePulse(time.getDeltaTime());188 this->protectedCall(boost::bind(&CEGUI::System::injectTimePulse, _1, time.getDeltaTime())); 176 189 } 177 190 … … 200 213 @param str 201 214 reference to string object holding the Lua code which is to be executed 202 203 This function gives total access to the GUI. You can execute ANY Lua code here.204 215 */ 205 216 void GUIManager::executeCode(const std::string& str) 206 217 { 207 218 this->luaState_->doString(str, rootFileInfo_); 219 } 220 221 /** Loads a GUI sheet by Lua script 222 @param name 223 The name of the GUI (like the script name, but without the extension) 224 */ 225 void GUIManager::loadGUI(const std::string& name) 226 { 227 this->executeCode("loadSheet(\"" + name + "\")"); 208 228 } 209 229 … … 215 235 216 236 The function executes the Lua function with the same name in case the GUIManager is ready. 217 For more details check out loadGUI_2.lua where the function presides. 218 */ 219 /*static*/ void GUIManager::showGUI(const std::string& name, bool hidePrevious, bool showCursor) 220 { 221 GUIManager::getInstance().executeCode("showGUI(\"" + name + "\", " + multi_cast<std::string>(hidePrevious) + ", " + multi_cast<std::string>(showCursor) + ")"); 237 */ 238 /*static*/ void GUIManager::showGUI(const std::string& name, bool bHidePrevious) 239 { 240 GUIManager::getInstance().executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ")"); 222 241 } 223 242 … … 226 245 Hack-ish. Needed for GUIOverlay. 227 246 */ 228 void GUIManager::showGUIExtra(const std::string& name, const std::string& ptr, bool hidePrevious, bool showCursor)229 { 230 this->executeCode("show GUI(\"" + name + "\", " + multi_cast<std::string>(hidePrevious) + ", " + multi_cast<std::string>(showCursor) + ", " + ptr + ")");247 void GUIManager::showGUIExtra(const std::string& name, const std::string& ptr, bool bHidePrevious) 248 { 249 this->executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + ptr + ")"); 231 250 } 232 251 … … 239 258 /*static*/ void GUIManager::hideGUI(const std::string& name) 240 259 { 241 GUIManager::getInstance().executeCode("hideGUI(\"" + name + "\")"); 260 GUIManager::getInstance().executeCode("hideMenuSheet(\"" + name + "\")"); 261 } 262 263 const std::string& GUIManager::createInputState(const std::string& name, TriBool::Value showCursor, TriBool::Value useKeyboard, bool bBlockJoyStick) 264 { 265 InputState* state = InputManager::getInstance().createInputState(name); 266 267 /* Table that maps isFullScreen() and showCursor to mouseExclusive 268 isFullscreen / showCursor | True | False | Dontcare 269 ---------------------------------------------------- 270 true | True | True | Dontcare 271 ---------------------------------------------------- 272 false | False | True | Dontcare 273 */ 274 if (showCursor == TriBool::Dontcare) 275 state->setMouseExclusive(TriBool::Dontcare); 276 else if (GraphicsManager::getInstance().isFullScreen() || showCursor == TriBool::False) 277 state->setMouseExclusive(TriBool::True); 278 else 279 state->setMouseExclusive(TriBool::False); 280 281 if (showCursor == TriBool::True) 282 state->setMouseHandler(this); 283 else if (showCursor == TriBool::False) 284 state->setMouseHandler(&InputHandler::EMPTY); 285 286 if (useKeyboard == TriBool::True) 287 state->setKeyHandler(this); 288 else if (useKeyboard == TriBool::False) 289 state->setKeyHandler(&InputHandler::EMPTY); 290 291 if (bBlockJoyStick) 292 state->setJoyStickHandler(&InputHandler::EMPTY); 293 294 return state->getName(); 242 295 } 243 296 … … 247 300 } 248 301 249 void GUIManager::setBackground(const std::string& name) 250 { 251 this->executeCode("setBackground(\"" + name + "\")"); 302 void GUIManager::setBackgroundImage(const std::string& imageSet, const std::string imageName) 303 { 304 if (imageSet.empty() || imageName.empty()) 305 this->setBackgroundImage(""); 306 else 307 this->setBackgroundImage("set: " + imageSet + " image: " + imageName); 308 } 309 310 void GUIManager::setBackgroundImage(const std::string& image) 311 { 312 if (image.empty()) 313 this->rootWindow_->setProperty("Alpha", "0.0"); 314 else 315 this->rootWindow_->setProperty("Alpha", "1.0"); 316 this->rootWindow_->setProperty("Image", image); 252 317 } 253 318 254 319 void GUIManager::keyPressed(const KeyEvent& evt) 255 320 { 256 guiSystem_->injectKeyDown(evt.getKeyCode()); 257 guiSystem_->injectChar(evt.getText()); 258 } 321 this->protectedCall(boost::bind(&CEGUI::System::injectKeyDown, _1, evt.getKeyCode())); 322 this->protectedCall(boost::bind(&CEGUI::System::injectChar, _1, evt.getText())); 323 } 324 259 325 void GUIManager::keyReleased(const KeyEvent& evt) 260 326 { 261 guiSystem_->injectKeyUp(evt.getKeyCode());327 this->protectedCall(boost::bind(&CEGUI::System::injectKeyUp, _1, evt.getKeyCode())); 262 328 } 263 329 … … 273 339 void GUIManager::buttonPressed(MouseButtonCode::ByEnum id) 274 340 { 275 try 276 { 277 guiSystem_->injectMouseButtonDown(convertButton(id)); 278 } 279 catch (CEGUI::ScriptException& ex) 280 { 281 // We simply ignore the exception and proceed 282 COUT(1) << ex.getMessage() << std::endl; 283 } 341 this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonDown, _1, convertButton(id))); 284 342 } 285 343 … … 295 353 void GUIManager::buttonReleased(MouseButtonCode::ByEnum id) 296 354 { 297 try 298 { 299 guiSystem_->injectMouseButtonUp(convertButton(id)); 300 } 301 catch (CEGUI::ScriptException& ex) 302 { 303 // We simply ignore the exception and proceed 304 COUT(1) << ex.getMessage() << std::endl; 305 } 355 this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonUp, _1, convertButton(id))); 306 356 } 307 357 308 358 void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) 309 359 { 310 guiSystem_->injectMousePosition(static_cast<float>(abs.x), static_cast<float>(abs.y)); 311 } 360 this->protectedCall(boost::bind(&CEGUI::System::injectMousePosition, _1, (float)abs.x, (float)abs.y)); 361 } 362 312 363 void GUIManager::mouseScrolled(int abs, int rel) 313 364 { 314 guiSystem_->injectMouseWheelChange(static_cast<float>(rel));365 this->protectedCall(boost::bind(&CEGUI::System::injectMouseWheelChange, _1, (float)rel)); 315 366 } 316 367 … … 349 400 } 350 401 402 /** Executes a CEGUI function normally, but catches CEGUI::ScriptException. 403 When a ScriptException occurs, the error message will be displayed and 404 the program carries on. 405 @remarks 406 The exception behaviour may pose problems if the code is not written 407 exception-safe (and you can forget about that in Lua). The program might 408 be left in an undefined state. But otherwise one script error would 409 terminate the whole program... 410 @note 411 Your life gets easier if you use boost::bind to create the object/function. 412 @param function 413 Any callable object/function that takes this->guiSystem_ as its only parameter. 414 @return 415 True if input was handled, false otherwise. A caught exception yields true. 416 */ 417 template <typename FunctionType> 418 bool GUIManager::protectedCall(FunctionType function) 419 { 420 try 421 { 422 return function(this->guiSystem_); 423 } 424 catch (CEGUI::ScriptException& ex) 425 { 426 // Display the error and proceed. See @remarks why this can be dangerous. 427 COUT(1) << ex.getMessage() << std::endl; 428 return true; 429 } 430 } 431 351 432 void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function) 352 433 {
Note: See TracChangeset
for help on using the changeset viewer.