[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 | #include "GUIManager.h" |
---|
| 31 | |
---|
[7941] | 32 | #include <memory> |
---|
[6746] | 33 | #include <boost/bind.hpp> |
---|
[7960] | 34 | #include <OgreRenderQueue.h> |
---|
| 35 | #include <OgreRenderWindow.h> |
---|
[7939] | 36 | |
---|
[2710] | 37 | #include <CEGUIDefaultLogger.h> |
---|
[3196] | 38 | #include <CEGUIExceptions.h> |
---|
| 39 | #include <CEGUIInputEvent.h> |
---|
[5695] | 40 | #include <CEGUIMouseCursor.h> |
---|
[3196] | 41 | #include <CEGUIResourceProvider.h> |
---|
| 42 | #include <CEGUISystem.h> |
---|
[6417] | 43 | #include <CEGUIWindow.h> |
---|
[6746] | 44 | #include <CEGUIWindowManager.h> |
---|
[7648] | 45 | #include <elements/CEGUIListbox.h> |
---|
| 46 | #include <elements/CEGUIListboxItem.h> |
---|
[7941] | 47 | |
---|
[7968] | 48 | #ifdef ORXONOX_OLD_CEGUI |
---|
[7960] | 49 | # include <CEGUILua.h> |
---|
| 50 | # include <ogreceguirenderer/OgreCEGUIRenderer.h> |
---|
[7973] | 51 | extern "C" { |
---|
| 52 | # include <lauxlib.h> |
---|
| 53 | } |
---|
[7960] | 54 | #else |
---|
| 55 | # include <ScriptingModules/LuaScriptModule/CEGUILua.h> |
---|
| 56 | # include <RendererModules/Ogre/CEGUIOgreImageCodec.h> |
---|
| 57 | # include <RendererModules/Ogre/CEGUIOgreRenderer.h> |
---|
| 58 | # include <RendererModules/Ogre/CEGUIOgreResourceProvider.h> |
---|
| 59 | #endif |
---|
[3196] | 60 | |
---|
[5929] | 61 | #include "util/Clock.h" |
---|
[6417] | 62 | #include "util/Convert.h" |
---|
[3280] | 63 | #include "util/Debug.h" |
---|
[1764] | 64 | #include "util/Exception.h" |
---|
[3280] | 65 | #include "util/OrxAssert.h" |
---|
[7801] | 66 | #include "ConfigValueIncludes.h" |
---|
[6417] | 67 | #include "Core.h" |
---|
[7801] | 68 | #include "CoreIncludes.h" |
---|
[7876] | 69 | #include "Game.h" |
---|
[6746] | 70 | #include "GraphicsManager.h" |
---|
[5695] | 71 | #include "LuaState.h" |
---|
[5929] | 72 | #include "PathConfig.h" |
---|
[5695] | 73 | #include "Resource.h" |
---|
[7284] | 74 | #include "command/ConsoleCommand.h" |
---|
[6746] | 75 | #include "input/InputManager.h" |
---|
| 76 | #include "input/InputState.h" |
---|
| 77 | #include "input/KeyBinderManager.h" |
---|
[1638] | 78 | |
---|
| 79 | namespace orxonox |
---|
| 80 | { |
---|
[6417] | 81 | static void key_esc() |
---|
| 82 | { GUIManager::getInstance().keyESC(); } |
---|
[7284] | 83 | SetConsoleCommand("keyESC", &key_esc); |
---|
[6417] | 84 | |
---|
[3280] | 85 | class CEGUILogger : public CEGUI::DefaultLogger |
---|
| 86 | { |
---|
| 87 | public: |
---|
[5929] | 88 | void logEvent(const CEGUI::String& message, CEGUI::LoggingLevel level = CEGUI::Standard) |
---|
[3280] | 89 | { |
---|
[3327] | 90 | int orxonoxLevel = CEGUI::Standard; |
---|
[3280] | 91 | switch (level) |
---|
| 92 | { |
---|
| 93 | case CEGUI::Errors: orxonoxLevel = 1; break; |
---|
| 94 | case CEGUI::Warnings: orxonoxLevel = 2; break; |
---|
| 95 | case CEGUI::Standard: orxonoxLevel = 4; break; |
---|
| 96 | case CEGUI::Informative: orxonoxLevel = 5; break; |
---|
| 97 | case CEGUI::Insane: orxonoxLevel = 6; break; |
---|
[7967] | 98 | default: OrxAssert(false, "CEGUI log level out of range, inspect immediately!"); |
---|
[3280] | 99 | } |
---|
[6105] | 100 | OutputHandler::getOutStream(orxonoxLevel) |
---|
[3280] | 101 | << "CEGUI: " << message << std::endl; |
---|
| 102 | |
---|
| 103 | CEGUI::DefaultLogger::logEvent(message, level); |
---|
| 104 | } |
---|
| 105 | }; |
---|
| 106 | |
---|
[7973] | 107 | #ifdef ORXONOX_OLD_CEGUI |
---|
| 108 | /** Class with the same memory layout as CEGUI::LuaScriptModule. <br> |
---|
| 109 | We need this to fix a problem with an uninitialised member variable |
---|
| 110 | in CEGUI < 0.7 <br> |
---|
| 111 | Notice that "public" modifier for the otherwise private variables. |
---|
| 112 | */ |
---|
| 113 | class CEGUILUA_API LuaScriptModuleWorkaround : public CEGUI::ScriptModule |
---|
| 114 | { |
---|
| 115 | public: |
---|
| 116 | LuaScriptModuleWorkaround(); |
---|
| 117 | ~LuaScriptModuleWorkaround(); |
---|
| 118 | |
---|
| 119 | public: |
---|
| 120 | bool d_ownsState; |
---|
| 121 | lua_State* d_state; |
---|
| 122 | CEGUI::String d_errFuncName; |
---|
| 123 | int d_errFuncIndex; |
---|
| 124 | CEGUI::String d_activeErrFuncName; |
---|
| 125 | int d_activeErrFuncIndex; |
---|
| 126 | }; |
---|
| 127 | #endif |
---|
| 128 | |
---|
[3196] | 129 | static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button); |
---|
[3339] | 130 | |
---|
[3366] | 131 | GUIManager* GUIManager::singletonPtr_s = 0; |
---|
[7801] | 132 | /*static*/ const std::string GUIManager::defaultScheme_ = "TaharezGreen"; |
---|
[1646] | 133 | |
---|
[7403] | 134 | SetConsoleCommand("showGUI", &GUIManager::showGUI).defaultValue(1, false).defaultValue(2, false); |
---|
[7284] | 135 | SetConsoleCommand("hideGUI", &GUIManager::hideGUI); |
---|
[6417] | 136 | |
---|
[2896] | 137 | /** |
---|
| 138 | @brief |
---|
[3338] | 139 | Constructs the GUIManager by starting up CEGUI |
---|
[2896] | 140 | |
---|
| 141 | Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine. |
---|
| 142 | The log is set up and connected to the CEGUILogger. |
---|
| 143 | After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code. |
---|
| 144 | Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically). |
---|
[3338] | 145 | @return true if success, otherwise false |
---|
[2896] | 146 | */ |
---|
[6746] | 147 | GUIManager::GUIManager(const std::pair<int, int>& mousePosition) |
---|
[7957] | 148 | : destroyer_(*this, &GUIManager::cleanup) |
---|
| 149 | , guiRenderer_(NULL) |
---|
| 150 | , resourceProvider_(NULL) |
---|
[7968] | 151 | #ifndef ORXONOX_OLD_CEGUI |
---|
[7960] | 152 | , imageCodec_(NULL) |
---|
| 153 | #endif |
---|
[8067] | 154 | , luaState_(NULL) |
---|
| 155 | , scriptModule_(NULL) |
---|
| 156 | , guiSystem_(NULL) |
---|
[5929] | 157 | , camera_(NULL) |
---|
[1638] | 158 | { |
---|
[7801] | 159 | RegisterRootObject(GUIManager); |
---|
| 160 | this->setConfigValues(); |
---|
| 161 | |
---|
[1638] | 162 | using namespace CEGUI; |
---|
| 163 | |
---|
[3338] | 164 | COUT(3) << "Initialising CEGUI." << std::endl; |
---|
[1638] | 165 | |
---|
[5695] | 166 | // Note: No SceneManager specified yet |
---|
[7968] | 167 | #ifdef ORXONOX_OLD_CEGUI |
---|
[7957] | 168 | guiRenderer_ = new OgreCEGUIRenderer(GraphicsManager::getInstance().getRenderWindow(), Ogre::RENDER_QUEUE_OVERLAY, false, 3000); |
---|
[5695] | 169 | resourceProvider_ = guiRenderer_->createResourceProvider(); |
---|
[7960] | 170 | #else |
---|
| 171 | guiRenderer_ = &OgreRenderer::create(*GraphicsManager::getInstance().getRenderWindow()); |
---|
| 172 | resourceProvider_ = &OgreRenderer::createOgreResourceProvider(); |
---|
| 173 | imageCodec_ = &OgreRenderer::createOgreImageCodec(); |
---|
| 174 | #endif |
---|
[7709] | 175 | resourceProvider_->setDefaultResourceGroup("General"); |
---|
[1776] | 176 | |
---|
[6749] | 177 | // Setup scripting |
---|
[7957] | 178 | luaState_ = new LuaState(); |
---|
[6417] | 179 | rootFileInfo_ = Resource::getInfo("InitialiseGUI.lua"); |
---|
| 180 | // This is necessary to ensure that input events also use the right resource info when triggering lua functions |
---|
| 181 | luaState_->setDefaultResourceInfo(this->rootFileInfo_); |
---|
[7968] | 182 | #ifdef ORXONOX_OLD_CEGUI |
---|
[7957] | 183 | scriptModule_ = new LuaScriptModule(luaState_->getInternalLuaState()); |
---|
[7973] | 184 | // Ugly workaround: older CEGUILua versions don't initialise the member |
---|
| 185 | // d_activeErrFuncIndex at all. That leads to "error in error handling" |
---|
| 186 | // problems when a Lua error occurs. |
---|
| 187 | // We fix this by setting the member manually. |
---|
| 188 | reinterpret_cast<LuaScriptModuleWorkaround*>(scriptModule_)->d_activeErrFuncIndex = LUA_NOREF; |
---|
[7971] | 189 | luaState_->doString("ORXONOX_OLD_CEGUI = true"); |
---|
[7960] | 190 | #else |
---|
| 191 | scriptModule_ = &LuaScriptModule::create(luaState_->getInternalLuaState()); |
---|
| 192 | #endif |
---|
[6763] | 193 | scriptModule_->setDefaultPCallErrorHandler(LuaState::ERROR_HANDLER_NAME); |
---|
[1638] | 194 | |
---|
[5695] | 195 | // Create our own logger to specify the filepath |
---|
| 196 | std::auto_ptr<CEGUILogger> ceguiLogger(new CEGUILogger()); |
---|
[5929] | 197 | ceguiLogger->setLogFilename(PathConfig::getLogPathString() + "cegui.log"); |
---|
[7957] | 198 | // Set the log level according to ours (translate by subtracting 1) |
---|
[5695] | 199 | ceguiLogger->setLoggingLevel( |
---|
[6105] | 200 | static_cast<LoggingLevel>(OutputHandler::getInstance().getSoftDebugLevel("logFile") - 1)); |
---|
[5695] | 201 | this->ceguiLogger_ = ceguiLogger.release(); |
---|
[2710] | 202 | |
---|
[6749] | 203 | // Create the CEGUI system singleton |
---|
[7968] | 204 | #ifdef ORXONOX_OLD_CEGUI |
---|
[7957] | 205 | guiSystem_ = new System(guiRenderer_, resourceProvider_, 0, scriptModule_); |
---|
[7961] | 206 | // Add functions that have been renamed in newer versions |
---|
| 207 | luaState_->doString("CEGUI.SchemeManager.create = CEGUI.SchemeManager.loadScheme"); |
---|
| 208 | luaState_->doString("CEGUI.Window.getUnclippedOuterRect = CEGUI.Window.getUnclippedPixelRect"); |
---|
[7960] | 209 | #else |
---|
| 210 | guiSystem_ = &System::create(*guiRenderer_, resourceProvider_, 0, imageCodec_, scriptModule_); |
---|
| 211 | #endif |
---|
[1776] | 212 | |
---|
[5695] | 213 | // Align CEGUI mouse with OIS mouse |
---|
[6502] | 214 | guiSystem_->injectMousePosition((float)mousePosition.first, (float)mousePosition.second); |
---|
[5695] | 215 | |
---|
[6746] | 216 | // Initialise the Lua framework and load the schemes |
---|
| 217 | this->luaState_->doFile("InitialiseGUI.lua"); |
---|
| 218 | |
---|
| 219 | // Create the root nodes |
---|
| 220 | this->rootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("MenuWidgets/StaticImage", "AbsoluteRootWindow"); |
---|
| 221 | this->rootWindow_->setProperty("FrameEnabled", "False"); |
---|
| 222 | this->hudRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "HUDRootWindow"); |
---|
| 223 | this->menuRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "MenuRootWindow"); |
---|
| 224 | // And connect them |
---|
| 225 | CEGUI::System::getSingleton().setGUISheet(this->rootWindow_); |
---|
| 226 | this->rootWindow_->addChildWindow(this->hudRootWindow_); |
---|
| 227 | this->rootWindow_->addChildWindow(this->menuRootWindow_); |
---|
| 228 | |
---|
[6749] | 229 | // No background to start with (sets the alpha value to 0) |
---|
| 230 | this->setBackgroundImage(""); |
---|
| 231 | |
---|
[6746] | 232 | // Set up the sheet manager in the Lua framework |
---|
| 233 | this->luaState_->doFile("SheetManager.lua"); |
---|
[3338] | 234 | } |
---|
[1776] | 235 | |
---|
[7957] | 236 | void GUIManager::cleanup() |
---|
[3338] | 237 | { |
---|
[7957] | 238 | using namespace CEGUI; |
---|
| 239 | |
---|
[7968] | 240 | #ifdef ORXONOX_OLD_CEGUI |
---|
[7957] | 241 | delete guiSystem_; |
---|
| 242 | delete guiRenderer_; |
---|
| 243 | delete scriptModule_; |
---|
[7960] | 244 | #else |
---|
| 245 | System::destroy(); |
---|
| 246 | OgreRenderer::destroyOgreResourceProvider(*resourceProvider_); |
---|
| 247 | OgreRenderer::destroyOgreImageCodec(*imageCodec_); |
---|
| 248 | OgreRenderer::destroy(*guiRenderer_); |
---|
| 249 | LuaScriptModule::destroy(*scriptModule_); |
---|
| 250 | #endif |
---|
[7957] | 251 | delete luaState_; |
---|
[1638] | 252 | } |
---|
| 253 | |
---|
[7801] | 254 | void GUIManager::setConfigValues(void) |
---|
| 255 | { |
---|
| 256 | SetConfigValue(guiScheme_, GUIManager::defaultScheme_) .description("Changes the current GUI scheme.") .callback(this, &GUIManager::changedGUIScheme); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | void GUIManager::changedGUIScheme(void) |
---|
| 260 | { |
---|
[7873] | 261 | |
---|
[7801] | 262 | } |
---|
| 263 | |
---|
[2896] | 264 | /** |
---|
| 265 | @brief |
---|
| 266 | used to tick the GUI |
---|
| 267 | @param time |
---|
| 268 | clock which provides time value for the GUI System |
---|
| 269 | |
---|
| 270 | Ticking the GUI means updating it with a certain regularity. |
---|
| 271 | The elapsed time since the last call is given in the time value provided by the clock. |
---|
| 272 | This time value is then used to provide a fluent animation of the GUI. |
---|
| 273 | */ |
---|
[6417] | 274 | void GUIManager::preUpdate(const Clock& time) |
---|
[1638] | 275 | { |
---|
[2896] | 276 | assert(guiSystem_); |
---|
[6746] | 277 | this->protectedCall(boost::bind(&CEGUI::System::injectTimePulse, _1, time.getDeltaTime())); |
---|
[2896] | 278 | } |
---|
[1638] | 279 | |
---|
[2896] | 280 | /** |
---|
| 281 | @brief |
---|
| 282 | Tells the GUIManager which SceneManager to use |
---|
| 283 | @param camera |
---|
| 284 | The current camera on which the GUI should be displayed on. |
---|
| 285 | |
---|
| 286 | In fact the GUIManager needs the SceneManager and not the Camera to display the GUI. |
---|
| 287 | This means the GUI is not bound to a camera but rather to the SceneManager. |
---|
[3337] | 288 | Hiding the GUI when needed can therefore not be resolved by just NOT setting the current camera. |
---|
[2896] | 289 | */ |
---|
| 290 | void GUIManager::setCamera(Ogre::Camera* camera) |
---|
[1638] | 291 | { |
---|
[5929] | 292 | this->camera_ = camera; |
---|
[7968] | 293 | #ifdef ORXONOX_OLD_CEGUI |
---|
[2927] | 294 | if (camera == NULL) |
---|
| 295 | this->guiRenderer_->setTargetSceneManager(0); |
---|
| 296 | else |
---|
| 297 | this->guiRenderer_->setTargetSceneManager(camera->getSceneManager()); |
---|
[7960] | 298 | #endif |
---|
[2896] | 299 | } |
---|
| 300 | |
---|
| 301 | /** |
---|
| 302 | @brief |
---|
[3338] | 303 | Executes Lua code |
---|
| 304 | @param str |
---|
| 305 | reference to string object holding the Lua code which is to be executed |
---|
| 306 | */ |
---|
| 307 | void GUIManager::executeCode(const std::string& str) |
---|
| 308 | { |
---|
[5695] | 309 | this->luaState_->doString(str, rootFileInfo_); |
---|
[3338] | 310 | } |
---|
| 311 | |
---|
[6746] | 312 | /** Loads a GUI sheet by Lua script |
---|
| 313 | @param name |
---|
| 314 | The name of the GUI (like the script name, but without the extension) |
---|
| 315 | */ |
---|
| 316 | void GUIManager::loadGUI(const std::string& name) |
---|
| 317 | { |
---|
| 318 | this->executeCode("loadSheet(\"" + name + "\")"); |
---|
| 319 | } |
---|
| 320 | |
---|
[3338] | 321 | /** |
---|
| 322 | @brief |
---|
[2896] | 323 | Displays specified GUI on screen |
---|
| 324 | @param name |
---|
| 325 | The name of the GUI |
---|
[7401] | 326 | @param bHidePrevious |
---|
| 327 | If true all displayed GUIs on the stack, that are below this GUI are hidden. |
---|
[7403] | 328 | @param bNoInput |
---|
| 329 | If true the GUI is transparent to input. |
---|
[2896] | 330 | |
---|
| 331 | The function executes the Lua function with the same name in case the GUIManager is ready. |
---|
| 332 | */ |
---|
[7403] | 333 | /*static*/ void GUIManager::showGUI(const std::string& name, bool bHidePrevious, bool bNoInput) |
---|
[2896] | 334 | { |
---|
[7403] | 335 | GUIManager::getInstance().executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ")"); |
---|
[1638] | 336 | } |
---|
| 337 | |
---|
[6417] | 338 | /** |
---|
| 339 | @brief |
---|
| 340 | Hack-ish. Needed for GUIOverlay. |
---|
| 341 | */ |
---|
[7403] | 342 | void GUIManager::showGUIExtra(const std::string& name, const std::string& ptr, bool bHidePrevious, bool bNoInput) |
---|
[6417] | 343 | { |
---|
[7403] | 344 | this->executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ", " + ptr + ")"); |
---|
[6417] | 345 | } |
---|
| 346 | |
---|
| 347 | /** |
---|
| 348 | @brief |
---|
| 349 | Hides specified GUI. |
---|
| 350 | @param name |
---|
| 351 | The name of the GUI. |
---|
| 352 | */ |
---|
| 353 | /*static*/ void GUIManager::hideGUI(const std::string& name) |
---|
| 354 | { |
---|
[6746] | 355 | GUIManager::getInstance().executeCode("hideMenuSheet(\"" + name + "\")"); |
---|
[6417] | 356 | } |
---|
| 357 | |
---|
[6746] | 358 | const std::string& GUIManager::createInputState(const std::string& name, TriBool::Value showCursor, TriBool::Value useKeyboard, bool bBlockJoyStick) |
---|
| 359 | { |
---|
| 360 | InputState* state = InputManager::getInstance().createInputState(name); |
---|
[7811] | 361 | if (!state) |
---|
| 362 | return BLANKSTRING; |
---|
[6746] | 363 | |
---|
| 364 | /* Table that maps isFullScreen() and showCursor to mouseExclusive |
---|
| 365 | isFullscreen / showCursor | True | False | Dontcare |
---|
| 366 | ---------------------------------------------------- |
---|
| 367 | true | True | True | Dontcare |
---|
| 368 | ---------------------------------------------------- |
---|
| 369 | false | False | True | Dontcare |
---|
| 370 | */ |
---|
[8073] | 371 | |
---|
| 372 | #ifdef ORXONOX_PLATFORM_APPLE |
---|
| 373 | // There is no non exclusive mode on OS X yet |
---|
| 374 | state->setMouseExclusive(TriBool::True); |
---|
| 375 | #else |
---|
[6746] | 376 | if (showCursor == TriBool::Dontcare) |
---|
| 377 | state->setMouseExclusive(TriBool::Dontcare); |
---|
| 378 | else if (GraphicsManager::getInstance().isFullScreen() || showCursor == TriBool::False) |
---|
| 379 | state->setMouseExclusive(TriBool::True); |
---|
| 380 | else |
---|
| 381 | state->setMouseExclusive(TriBool::False); |
---|
[8073] | 382 | #endif |
---|
[6746] | 383 | |
---|
| 384 | if (showCursor == TriBool::True) |
---|
| 385 | state->setMouseHandler(this); |
---|
| 386 | else if (showCursor == TriBool::False) |
---|
| 387 | state->setMouseHandler(&InputHandler::EMPTY); |
---|
| 388 | |
---|
| 389 | if (useKeyboard == TriBool::True) |
---|
| 390 | state->setKeyHandler(this); |
---|
| 391 | else if (useKeyboard == TriBool::False) |
---|
| 392 | state->setKeyHandler(&InputHandler::EMPTY); |
---|
| 393 | |
---|
| 394 | if (bBlockJoyStick) |
---|
| 395 | state->setJoyStickHandler(&InputHandler::EMPTY); |
---|
| 396 | |
---|
| 397 | return state->getName(); |
---|
| 398 | } |
---|
| 399 | |
---|
[6417] | 400 | void GUIManager::keyESC() |
---|
| 401 | { |
---|
| 402 | this->executeCode("keyESC()"); |
---|
| 403 | } |
---|
| 404 | |
---|
[6746] | 405 | void GUIManager::setBackgroundImage(const std::string& imageSet, const std::string imageName) |
---|
[6417] | 406 | { |
---|
[6746] | 407 | if (imageSet.empty() || imageName.empty()) |
---|
| 408 | this->setBackgroundImage(""); |
---|
| 409 | else |
---|
| 410 | this->setBackgroundImage("set: " + imageSet + " image: " + imageName); |
---|
[6417] | 411 | } |
---|
| 412 | |
---|
[6746] | 413 | void GUIManager::setBackgroundImage(const std::string& image) |
---|
| 414 | { |
---|
| 415 | if (image.empty()) |
---|
| 416 | this->rootWindow_->setProperty("Alpha", "0.0"); |
---|
| 417 | else |
---|
| 418 | this->rootWindow_->setProperty("Alpha", "1.0"); |
---|
| 419 | this->rootWindow_->setProperty("Image", image); |
---|
| 420 | } |
---|
| 421 | |
---|
[7163] | 422 | void GUIManager::buttonPressed(const KeyEvent& evt) |
---|
[3196] | 423 | { |
---|
[6746] | 424 | this->protectedCall(boost::bind(&CEGUI::System::injectKeyDown, _1, evt.getKeyCode())); |
---|
| 425 | this->protectedCall(boost::bind(&CEGUI::System::injectChar, _1, evt.getText())); |
---|
[3196] | 426 | } |
---|
[6746] | 427 | |
---|
[7163] | 428 | void GUIManager::buttonReleased(const KeyEvent& evt) |
---|
[3196] | 429 | { |
---|
[6746] | 430 | this->protectedCall(boost::bind(&CEGUI::System::injectKeyUp, _1, evt.getKeyCode())); |
---|
[3196] | 431 | } |
---|
| 432 | |
---|
[2896] | 433 | /** |
---|
| 434 | @brief |
---|
| 435 | Function receiving a mouse button pressed event. |
---|
| 436 | @param id |
---|
| 437 | ID of the mouse button which got pressed |
---|
[1638] | 438 | |
---|
[2896] | 439 | This function is inherited by MouseHandler and injects the event into CEGUI. |
---|
| 440 | It is for CEGUI to process the event. |
---|
| 441 | */ |
---|
[3327] | 442 | void GUIManager::buttonPressed(MouseButtonCode::ByEnum id) |
---|
[1638] | 443 | { |
---|
[6746] | 444 | this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonDown, _1, convertButton(id))); |
---|
[1638] | 445 | } |
---|
| 446 | |
---|
[2896] | 447 | /** |
---|
| 448 | @brief |
---|
| 449 | Function receiving a mouse button released event. |
---|
| 450 | @param id |
---|
| 451 | ID of the mouse button which got released |
---|
| 452 | |
---|
| 453 | This function is inherited by MouseHandler and injects the event into CEGUI. |
---|
| 454 | It is for CEGUI to process the event. |
---|
| 455 | */ |
---|
[3327] | 456 | void GUIManager::buttonReleased(MouseButtonCode::ByEnum id) |
---|
[1638] | 457 | { |
---|
[6746] | 458 | this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonUp, _1, convertButton(id))); |
---|
[1638] | 459 | } |
---|
| 460 | |
---|
[3196] | 461 | void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
| 462 | { |
---|
[6746] | 463 | this->protectedCall(boost::bind(&CEGUI::System::injectMousePosition, _1, (float)abs.x, (float)abs.y)); |
---|
[3196] | 464 | } |
---|
[6746] | 465 | |
---|
[3196] | 466 | void GUIManager::mouseScrolled(int abs, int rel) |
---|
| 467 | { |
---|
[6746] | 468 | this->protectedCall(boost::bind(&CEGUI::System::injectMouseWheelChange, _1, (float)rel)); |
---|
[3196] | 469 | } |
---|
| 470 | |
---|
[2896] | 471 | /** |
---|
[7874] | 472 | @brief Indicates that the mouse left the application's window. |
---|
| 473 | */ |
---|
| 474 | void GUIManager::mouseLeft() |
---|
| 475 | { |
---|
| 476 | this->protectedCall(boost::bind(&CEGUI::System::injectMouseLeaves, _1)); |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | /** |
---|
[2896] | 480 | @brief |
---|
| 481 | converts mouse event code to CEGUI event code |
---|
| 482 | @param button |
---|
| 483 | code of the mouse button as we use it in Orxonox |
---|
| 484 | @return |
---|
| 485 | code of the mouse button as it is used by CEGUI |
---|
[1638] | 486 | |
---|
[6105] | 487 | Simple conversion from mouse event code in Orxonox to the one used in CEGUI. |
---|
[2896] | 488 | */ |
---|
[3196] | 489 | static inline CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button) |
---|
[1638] | 490 | { |
---|
| 491 | switch (button) |
---|
| 492 | { |
---|
[1887] | 493 | case MouseButtonCode::Left: |
---|
[1638] | 494 | return CEGUI::LeftButton; |
---|
| 495 | |
---|
[1887] | 496 | case MouseButtonCode::Right: |
---|
[1638] | 497 | return CEGUI::RightButton; |
---|
| 498 | |
---|
[1887] | 499 | case MouseButtonCode::Middle: |
---|
[1638] | 500 | return CEGUI::MiddleButton; |
---|
| 501 | |
---|
[1887] | 502 | case MouseButtonCode::Button3: |
---|
[1638] | 503 | return CEGUI::X1Button; |
---|
| 504 | |
---|
[1887] | 505 | case MouseButtonCode::Button4: |
---|
[1638] | 506 | return CEGUI::X2Button; |
---|
| 507 | |
---|
| 508 | default: |
---|
| 509 | return CEGUI::NoButton; |
---|
| 510 | } |
---|
| 511 | } |
---|
[6417] | 512 | |
---|
[6746] | 513 | /** Executes a CEGUI function normally, but catches CEGUI::ScriptException. |
---|
| 514 | When a ScriptException occurs, the error message will be displayed and |
---|
| 515 | the program carries on. |
---|
| 516 | @remarks |
---|
| 517 | The exception behaviour may pose problems if the code is not written |
---|
| 518 | exception-safe (and you can forget about that in Lua). The program might |
---|
| 519 | be left in an undefined state. But otherwise one script error would |
---|
| 520 | terminate the whole program... |
---|
| 521 | @note |
---|
| 522 | Your life gets easier if you use boost::bind to create the object/function. |
---|
| 523 | @param function |
---|
| 524 | Any callable object/function that takes this->guiSystem_ as its only parameter. |
---|
| 525 | @return |
---|
| 526 | True if input was handled, false otherwise. A caught exception yields true. |
---|
| 527 | */ |
---|
| 528 | template <typename FunctionType> |
---|
| 529 | bool GUIManager::protectedCall(FunctionType function) |
---|
| 530 | { |
---|
| 531 | try |
---|
| 532 | { |
---|
| 533 | return function(this->guiSystem_); |
---|
| 534 | } |
---|
| 535 | catch (CEGUI::ScriptException& ex) |
---|
| 536 | { |
---|
| 537 | // Display the error and proceed. See @remarks why this can be dangerous. |
---|
| 538 | COUT(1) << ex.getMessage() << std::endl; |
---|
| 539 | return true; |
---|
| 540 | } |
---|
| 541 | } |
---|
| 542 | |
---|
[7648] | 543 | /** |
---|
| 544 | @brief |
---|
| 545 | Subscribe the input function to the input event for the input window. |
---|
| 546 | This is a helper to be used in lua, because subscribeScriptedEvent() doesn't work in lua. |
---|
| 547 | @param window |
---|
| 548 | The window for which the event is subscribed. |
---|
| 549 | @param event |
---|
| 550 | The type of event to which we subscribe. |
---|
| 551 | @param function |
---|
| 552 | The function that is called when the event occurs. |
---|
| 553 | */ |
---|
[6417] | 554 | void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function) |
---|
| 555 | { |
---|
| 556 | window->subscribeScriptedEvent(event, function); |
---|
| 557 | } |
---|
[7648] | 558 | |
---|
| 559 | /** |
---|
| 560 | @brief |
---|
| 561 | Set the input tooltip text for the input ListboxItem. |
---|
| 562 | @param item |
---|
| 563 | The ListboxItem for which the tooltip should be set. |
---|
| 564 | @param tooltip |
---|
| 565 | The tooltip text that should be set. |
---|
| 566 | */ |
---|
| 567 | void GUIManager::setTooltipTextHelper(CEGUI::ListboxItem* item, const std::string& tooltip) |
---|
| 568 | { |
---|
| 569 | item->setTooltipText(tooltip); |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | /** |
---|
| 573 | @brief |
---|
| 574 | Set whether the tooltips for the input Listbox are enabled. |
---|
| 575 | @param listbox |
---|
| 576 | The Listbox for which to enable (or disable) tooltips. |
---|
| 577 | @param enabled |
---|
[7967] | 578 | Whether to enable or disable the tooltips. |
---|
[7648] | 579 | */ |
---|
| 580 | void GUIManager::setItemTooltipsEnabledHelper(CEGUI::Listbox* listbox, bool enabled) |
---|
| 581 | { |
---|
| 582 | listbox->setItemTooltipsEnabled(enabled); |
---|
| 583 | } |
---|
| 584 | |
---|
[7873] | 585 | /** |
---|
| 586 | @brief Callback of window event listener, called if the window is resized. Sets the display size of CEGUI. |
---|
| 587 | */ |
---|
| 588 | void GUIManager::windowResized(unsigned int newWidth, unsigned int newHeight) |
---|
| 589 | { |
---|
[7968] | 590 | #ifdef ORXONOX_OLD_CEGUI |
---|
[7873] | 591 | this->guiRenderer_->setDisplaySize(CEGUI::Size(newWidth, newHeight)); |
---|
[7960] | 592 | #else |
---|
| 593 | this->guiRenderer_->setDisplaySize(CEGUI::Size((float)newWidth, (float)newHeight)); |
---|
| 594 | #endif |
---|
[7873] | 595 | } |
---|
[7874] | 596 | |
---|
| 597 | /** |
---|
[7967] | 598 | @brief Notify CEGUI if the windows loses the focus (stops highlight of menu items, etc). |
---|
[7874] | 599 | */ |
---|
| 600 | void GUIManager::windowFocusChanged(bool bFocus) |
---|
| 601 | { |
---|
| 602 | if (!bFocus) |
---|
| 603 | this->mouseLeft(); |
---|
| 604 | } |
---|
[7876] | 605 | |
---|
[1638] | 606 | } |
---|