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