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