| [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 |  | 
|---|
| [8467] | 32 | #include <fstream> | 
|---|
| [8351] | 33 | #include <memory> | 
|---|
| [11071] | 34 | #include <functional> | 
|---|
| [8351] | 35 | #include <OgreRenderQueue.h> | 
|---|
|  | 36 | #include <OgreRenderWindow.h> | 
|---|
| [8079] | 37 |  | 
|---|
| [11052] | 38 | extern "C" { | 
|---|
|  | 39 | #include <lua.h> | 
|---|
|  | 40 | } | 
|---|
|  | 41 |  | 
|---|
| [9675] | 42 | #if CEGUI_VERSION >= 0x000800 | 
|---|
|  | 43 | #   include <CEGUI/DefaultLogger.h> | 
|---|
|  | 44 | #   include <CEGUI/Exceptions.h> | 
|---|
|  | 45 | #   include <CEGUI/FontManager.h> | 
|---|
|  | 46 | #   include <CEGUI/InputEvent.h> | 
|---|
|  | 47 | #   include <CEGUI/MouseCursor.h> | 
|---|
|  | 48 | #   include <CEGUI/ResourceProvider.h> | 
|---|
|  | 49 | #   include <CEGUI/System.h> | 
|---|
|  | 50 | #   include <CEGUI/Window.h> | 
|---|
|  | 51 | #   include <CEGUI/WindowManager.h> | 
|---|
|  | 52 | #   include <CEGUI/XMLAttributes.h> | 
|---|
|  | 53 | #   include <CEGUI/widgets/Listbox.h> | 
|---|
|  | 54 | #   include <CEGUI/widgets/ListboxItem.h> | 
|---|
|  | 55 | #else | 
|---|
|  | 56 | #   include <CEGUIDefaultLogger.h> | 
|---|
|  | 57 | #   include <CEGUIExceptions.h> | 
|---|
|  | 58 | #   include <CEGUIFontManager.h> | 
|---|
|  | 59 | #   include <CEGUIInputEvent.h> | 
|---|
|  | 60 | #   include <CEGUIMouseCursor.h> | 
|---|
|  | 61 | #   include <CEGUIResourceProvider.h> | 
|---|
|  | 62 | #   include <CEGUISystem.h> | 
|---|
|  | 63 | #   include <CEGUIWindow.h> | 
|---|
|  | 64 | #   include <CEGUIWindowManager.h> | 
|---|
|  | 65 | #   include <CEGUIXMLAttributes.h> | 
|---|
|  | 66 | #   include <elements/CEGUIListbox.h> | 
|---|
|  | 67 | #   include <elements/CEGUIListboxItem.h> | 
|---|
|  | 68 | #endif | 
|---|
| [3196] | 69 |  | 
|---|
| [8351] | 70 | #ifdef ORXONOX_OLD_CEGUI | 
|---|
| [9675] | 71 | #   include <CEGUILua.h> | 
|---|
|  | 72 | #   include <ogreceguirenderer/OgreCEGUIRenderer.h> | 
|---|
| [8351] | 73 | extern "C" { | 
|---|
| [9675] | 74 | #   include <lauxlib.h> | 
|---|
| [8351] | 75 | } | 
|---|
| [2710] | 76 | #else | 
|---|
| [9675] | 77 | #   if CEGUI_VERSION >= 0x000800 | 
|---|
|  | 78 | #       include <CEGUI/ScriptModules/Lua/ScriptModule.h> | 
|---|
|  | 79 | #       include <CEGUI/RendererModules/Ogre/ImageCodec.h> | 
|---|
|  | 80 | #       include <CEGUI/RendererModules/Ogre/Renderer.h> | 
|---|
|  | 81 | #       include <CEGUI/RendererModules/Ogre/ResourceProvider.h> | 
|---|
|  | 82 | #   else | 
|---|
|  | 83 | #       include <ScriptingModules/LuaScriptModule/CEGUILua.h> | 
|---|
|  | 84 | #       include <RendererModules/Ogre/CEGUIOgreImageCodec.h> | 
|---|
|  | 85 | #       include <RendererModules/Ogre/CEGUIOgreRenderer.h> | 
|---|
|  | 86 | #       include <RendererModules/Ogre/CEGUIOgreResourceProvider.h> | 
|---|
|  | 87 | #   endif | 
|---|
|  | 88 | #   include <OgreCamera.h> | 
|---|
|  | 89 | #   include <OgreRenderQueueListener.h> | 
|---|
|  | 90 | #   include <OgreRenderSystem.h> | 
|---|
|  | 91 | #   include <OgreRoot.h> | 
|---|
|  | 92 | #   include <OgreSceneManager.h> | 
|---|
| [2710] | 93 | #endif | 
|---|
|  | 94 |  | 
|---|
| [8527] | 95 | #if defined(ORXONOX_PLATFORM_WINDOWS) && !defined(ORXONOX_COMPILER_MINGW) | 
|---|
| [8467] | 96 | #  include <windows.h> | 
|---|
|  | 97 | #endif | 
|---|
|  | 98 |  | 
|---|
| [5929] | 99 | #include "util/Clock.h" | 
|---|
| [6417] | 100 | #include "util/Convert.h" | 
|---|
| [8858] | 101 | #include "util/Output.h" | 
|---|
| [1764] | 102 | #include "util/Exception.h" | 
|---|
| [8530] | 103 | #include "util/Math.h" | 
|---|
| [3280] | 104 | #include "util/OrxAssert.h" | 
|---|
| [8858] | 105 | #include "util/output/BaseWriter.h" | 
|---|
| [11083] | 106 | #include "util/StringUtils.h" | 
|---|
|  | 107 | #include "util/SubString.h" | 
|---|
| [9667] | 108 | #include "config/ConfigValueIncludes.h" | 
|---|
| [6417] | 109 | #include "Core.h" | 
|---|
| [7801] | 110 | #include "CoreIncludes.h" | 
|---|
| [7876] | 111 | #include "Game.h" | 
|---|
| [6746] | 112 | #include "GraphicsManager.h" | 
|---|
| [5695] | 113 | #include "LuaState.h" | 
|---|
| [10624] | 114 | #include "ConfigurablePaths.h" | 
|---|
| [5695] | 115 | #include "Resource.h" | 
|---|
| [10624] | 116 | #include "command/ConsoleCommandIncludes.h" | 
|---|
| [6746] | 117 | #include "input/InputManager.h" | 
|---|
|  | 118 | #include "input/InputState.h" | 
|---|
|  | 119 | #include "input/KeyBinderManager.h" | 
|---|
| [1638] | 120 |  | 
|---|
|  | 121 | namespace orxonox | 
|---|
|  | 122 | { | 
|---|
| [11071] | 123 | namespace arg = std::placeholders; | 
|---|
|  | 124 |  | 
|---|
| [6417] | 125 | static void key_esc() | 
|---|
|  | 126 | { GUIManager::getInstance().keyESC(); } | 
|---|
| [7284] | 127 | SetConsoleCommand("keyESC", &key_esc); | 
|---|
| [6417] | 128 |  | 
|---|
| [3280] | 129 | class CEGUILogger : public CEGUI::DefaultLogger | 
|---|
|  | 130 | { | 
|---|
|  | 131 | public: | 
|---|
| [11071] | 132 | virtual void logEvent(const CEGUI::String& message, CEGUI::LoggingLevel level = CEGUI::Standard) override | 
|---|
| [3280] | 133 | { | 
|---|
| [8858] | 134 | OutputLevel orxonoxLevel = level::debug_output; | 
|---|
| [3280] | 135 | switch (level) | 
|---|
|  | 136 | { | 
|---|
| [8858] | 137 | case CEGUI::Errors:      orxonoxLevel = level::internal_error; break; | 
|---|
|  | 138 | case CEGUI::Warnings:    orxonoxLevel = level::internal_warning; break; | 
|---|
|  | 139 | case CEGUI::Standard:    orxonoxLevel = level::verbose; break; | 
|---|
|  | 140 | case CEGUI::Informative: orxonoxLevel = level::verbose_more; break; | 
|---|
|  | 141 | case CEGUI::Insane:      orxonoxLevel = level::verbose_ultra; break; | 
|---|
| [8351] | 142 | default: OrxAssert(false, "CEGUI log level out of range, inspect immediately!"); | 
|---|
| [3280] | 143 | } | 
|---|
|  | 144 |  | 
|---|
| [8858] | 145 | orxout(orxonoxLevel, context::cegui) << message << endl; | 
|---|
|  | 146 |  | 
|---|
| [3280] | 147 | CEGUI::DefaultLogger::logEvent(message, level); | 
|---|
|  | 148 | } | 
|---|
| [8467] | 149 |  | 
|---|
|  | 150 | /// Carbon copy from CEGUIDefaultLogger.cpp with a bugfix for Windows | 
|---|
| [11071] | 151 | virtual void setLogFilename(const CEGUI::String& filename, bool append = false) override | 
|---|
| [8467] | 152 | { | 
|---|
|  | 153 | // Close current log file (if any) | 
|---|
|  | 154 | if (d_ostream.is_open()) | 
|---|
|  | 155 | d_ostream.close(); | 
|---|
|  | 156 |  | 
|---|
| [8527] | 157 | #if defined(ORXONOX_PLATFORM_WINDOWS) && !defined(ORXONOX_COMPILER_MINGW) | 
|---|
| [8467] | 158 | // filename.c_str() is UTF-8 encoded, but Windows expects characters | 
|---|
|  | 159 | // according to the current codepage or UTF-16 (wchar) | 
|---|
|  | 160 | d_ostream.open(utf8ToUtf16(filename.c_str()).c_str(), std::ios_base::out | (append ? std::ios_base::app : std::ios_base::trunc)); | 
|---|
|  | 161 | #else | 
|---|
|  | 162 | d_ostream.open(filename.c_str(), std::ios_base::out | (append ? std::ios_base::app : std::ios_base::trunc)); | 
|---|
|  | 163 | #endif | 
|---|
|  | 164 | if (!d_ostream) | 
|---|
|  | 165 | ThrowException(General, "Setting the CEGUI log filename failed"); | 
|---|
|  | 166 |  | 
|---|
|  | 167 | // Initialise width for date & time alignment. | 
|---|
|  | 168 | d_ostream.width(2); | 
|---|
|  | 169 |  | 
|---|
|  | 170 | // Write out cached log strings. | 
|---|
|  | 171 | if (d_caching) | 
|---|
|  | 172 | { | 
|---|
|  | 173 | d_caching = false; | 
|---|
|  | 174 |  | 
|---|
| [11071] | 175 | std::vector<std::pair<CEGUI::String, CEGUI::LoggingLevel>>::iterator it = d_cache.begin(); | 
|---|
| [8467] | 176 |  | 
|---|
|  | 177 | while (it != d_cache.end()) | 
|---|
|  | 178 | { | 
|---|
|  | 179 | if (d_level >= it->second) | 
|---|
|  | 180 | { | 
|---|
|  | 181 | d_ostream << it->first; | 
|---|
|  | 182 | // Ensure new event is written to the file, rather than just being buffered. | 
|---|
|  | 183 | d_ostream.flush(); | 
|---|
|  | 184 | } | 
|---|
|  | 185 | ++it; | 
|---|
|  | 186 | } | 
|---|
|  | 187 |  | 
|---|
|  | 188 | d_cache.clear(); | 
|---|
|  | 189 | } | 
|---|
|  | 190 | } | 
|---|
|  | 191 |  | 
|---|
| [8527] | 192 | #if defined(ORXONOX_PLATFORM_WINDOWS) && !defined(ORXONOX_COMPILER_MINGW) | 
|---|
| [8467] | 193 | /// Converts a UTF-8 character sequence to Windows UTF-16 | 
|---|
|  | 194 | static std::wstring utf8ToUtf16(const std::string& utf8text) | 
|---|
|  | 195 | { | 
|---|
|  | 196 | const int textLen = MultiByteToWideChar(CP_UTF8, 0, utf8text.c_str(), | 
|---|
|  | 197 | utf8text.size() + 1, 0, 0); | 
|---|
|  | 198 |  | 
|---|
|  | 199 | if (textLen == 0) | 
|---|
|  | 200 | ThrowException(General, "Utf8ToUtf16 - MultiByteToWideChar failed"); | 
|---|
|  | 201 |  | 
|---|
|  | 202 | std::wstring wideStr(textLen, 0); | 
|---|
|  | 203 | MultiByteToWideChar(CP_UTF8, 0, utf8text.c_str(), utf8text.size() + 1, | 
|---|
|  | 204 | &wideStr[0], wideStr.size()); | 
|---|
|  | 205 | return wideStr; | 
|---|
|  | 206 | } | 
|---|
|  | 207 | #endif | 
|---|
| [3280] | 208 | }; | 
|---|
|  | 209 |  | 
|---|
| [8351] | 210 | #ifdef ORXONOX_OLD_CEGUI | 
|---|
|  | 211 | /** Class with the same memory layout as CEGUI::LuaScriptModule. <br> | 
|---|
|  | 212 | We need this to fix a problem with an uninitialised member variable | 
|---|
|  | 213 | in CEGUI < 0.7 <br> | 
|---|
|  | 214 | Notice the "public" modifier for the otherwise private variables. | 
|---|
|  | 215 | */ | 
|---|
|  | 216 | class LuaScriptModuleWorkaround : public CEGUI::ScriptModule | 
|---|
|  | 217 | { | 
|---|
|  | 218 | public: | 
|---|
|  | 219 | LuaScriptModuleWorkaround(); | 
|---|
|  | 220 | ~LuaScriptModuleWorkaround(); | 
|---|
|  | 221 |  | 
|---|
|  | 222 | public: | 
|---|
|  | 223 | bool d_ownsState; | 
|---|
|  | 224 | lua_State* d_state; | 
|---|
|  | 225 | CEGUI::String d_errFuncName; | 
|---|
|  | 226 | int d_errFuncIndex; | 
|---|
|  | 227 | CEGUI::String d_activeErrFuncName; | 
|---|
|  | 228 | int d_activeErrFuncIndex; | 
|---|
|  | 229 | }; | 
|---|
|  | 230 | #else | 
|---|
|  | 231 | /// RenderQueueListener based class used to hook into the ogre rendering system | 
|---|
|  | 232 | class RQListener : public Ogre::RenderQueueListener | 
|---|
|  | 233 | { | 
|---|
|  | 234 | public: | 
|---|
|  | 235 | /// Callback from Ogre invoked before other stuff in our target queue is rendered | 
|---|
| [11071] | 236 | virtual void renderQueueStarted(Ogre::uint8 id, const Ogre::String& invocation, bool& skipThisQueue) override | 
|---|
| [8351] | 237 | { | 
|---|
| [8419] | 238 | if (id == Ogre::RENDER_QUEUE_OVERLAY && invocation.empty()) | 
|---|
| [8439] | 239 | { | 
|---|
| [9675] | 240 | #if CEGUI_VERSION >= 0x000800 | 
|---|
|  | 241 | CEGUI::System::getSingleton().renderAllGUIContexts(); | 
|---|
|  | 242 | #else | 
|---|
| [8351] | 243 | CEGUI::System::getSingleton().renderGUI(); | 
|---|
| [9675] | 244 | #endif | 
|---|
| [8439] | 245 |  | 
|---|
|  | 246 | // Important workaround! (at least required by CEGUI 0.7.5) | 
|---|
|  | 247 | // If we don't reset the scissor test, OGRE will only render overlays | 
|---|
|  | 248 | // in the area where CEGUI last drew, which is usually nothing | 
|---|
|  | 249 | // or a little box where the focused element is. | 
|---|
|  | 250 | Ogre::Root::getSingleton().getRenderSystem()->setScissorTest(false); | 
|---|
|  | 251 | } | 
|---|
| [8351] | 252 | } | 
|---|
|  | 253 | }; | 
|---|
|  | 254 | #endif | 
|---|
|  | 255 |  | 
|---|
| [3196] | 256 | static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button); | 
|---|
| [3339] | 257 |  | 
|---|
| [11071] | 258 | GUIManager* GUIManager::singletonPtr_s = nullptr; | 
|---|
| [9016] | 259 | /*static*/ const std::string GUIManager::defaultScheme_ = "TaharezGreen"; //Alternative: Orxonox (not fully complete yet, see the graphics menu) | 
|---|
| [1646] | 260 |  | 
|---|
| [11052] | 261 | namespace autocompletion | 
|---|
|  | 262 | { | 
|---|
|  | 263 | /** | 
|---|
|  | 264 | @brief Returns the names of all currently existing OverlayGroups. | 
|---|
|  | 265 | */ | 
|---|
|  | 266 | ARGUMENT_COMPLETION_FUNCTION_DECLARATION(guinames)(); | 
|---|
|  | 267 | ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION(guinames)() | 
|---|
|  | 268 | { | 
|---|
|  | 269 | ArgumentCompletionList names; | 
|---|
|  | 270 | const std::vector<std::string> guis = GUIManager::getInstance().getLoadedGUIs(); | 
|---|
| [11071] | 271 | for (const std::string gui : guis) | 
|---|
|  | 272 | names.push_back(ArgumentCompletionListElement(gui, getLowercase(gui))); | 
|---|
| [11052] | 273 | return names; | 
|---|
|  | 274 | } | 
|---|
|  | 275 | } | 
|---|
| [6417] | 276 |  | 
|---|
| [11052] | 277 | SetConsoleCommand("showGUI", &GUIManager::showGUI).defaultValue(1, false).defaultValue(2, false) | 
|---|
|  | 278 | .argumentCompleter(0, autocompletion::guinames()); | 
|---|
|  | 279 | SetConsoleCommand("hideGUI", &GUIManager::hideGUI) | 
|---|
|  | 280 | .argumentCompleter(0, autocompletion::guinames()); | 
|---|
|  | 281 | SetConsoleCommand("toggleGUI", &GUIManager::toggleGUI).defaultValue(1, false).defaultValue(2, false) | 
|---|
|  | 282 | .argumentCompleter(0, autocompletion::guinames()); | 
|---|
|  | 283 |  | 
|---|
| [10624] | 284 | RegisterAbstractClass(GUIManager).inheritsFrom<WindowEventListener>(); | 
|---|
|  | 285 |  | 
|---|
| [2896] | 286 | /** | 
|---|
|  | 287 | @brief | 
|---|
| [3338] | 288 | Constructs the GUIManager by starting up CEGUI | 
|---|
| [2896] | 289 |  | 
|---|
|  | 290 | Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine. | 
|---|
|  | 291 | The log is set up and connected to the CEGUILogger. | 
|---|
|  | 292 | After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code. | 
|---|
|  | 293 | Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically). | 
|---|
| [3338] | 294 | @return true if success, otherwise false | 
|---|
| [2896] | 295 | */ | 
|---|
| [6746] | 296 | GUIManager::GUIManager(const std::pair<int, int>& mousePosition) | 
|---|
| [11071] | 297 | : guiRenderer_(nullptr) | 
|---|
|  | 298 | , resourceProvider_(nullptr) | 
|---|
| [8351] | 299 | #ifndef ORXONOX_OLD_CEGUI | 
|---|
| [11071] | 300 | , rqListener_(nullptr) | 
|---|
|  | 301 | , imageCodec_(nullptr) | 
|---|
| [8351] | 302 | #endif | 
|---|
| [11071] | 303 | , luaState_(nullptr) | 
|---|
|  | 304 | , scriptModule_(nullptr) | 
|---|
|  | 305 | , guiSystem_(nullptr) | 
|---|
|  | 306 | , ceguiLogger_(nullptr) | 
|---|
|  | 307 | , rootWindow_(nullptr) | 
|---|
|  | 308 | , hudRootWindow_(nullptr) | 
|---|
|  | 309 | , menuRootWindow_(nullptr) | 
|---|
|  | 310 | , camera_(nullptr) | 
|---|
| [8423] | 311 | , destructionHelper_(this) | 
|---|
| [1638] | 312 | { | 
|---|
| [9667] | 313 | RegisterObject(GUIManager); | 
|---|
| [8858] | 314 |  | 
|---|
|  | 315 | orxout(internal_status) << "initializing GUIManager..." << endl; | 
|---|
|  | 316 |  | 
|---|
| [7801] | 317 | this->setConfigValues(); | 
|---|
|  | 318 |  | 
|---|
| [1638] | 319 | using namespace CEGUI; | 
|---|
|  | 320 |  | 
|---|
| [8858] | 321 | orxout(internal_info) << "Initialising CEGUI." << endl; | 
|---|
| [1638] | 322 |  | 
|---|
| [8706] | 323 | this->oldCEGUI_ = false; | 
|---|
| [8858] | 324 |  | 
|---|
| [5695] | 325 | // Note: No SceneManager specified yet | 
|---|
| [8351] | 326 | #ifdef ORXONOX_OLD_CEGUI | 
|---|
|  | 327 | guiRenderer_ = new OgreCEGUIRenderer(GraphicsManager::getInstance().getRenderWindow(), Ogre::RENDER_QUEUE_OVERLAY, false, 3000); | 
|---|
| [5695] | 328 | resourceProvider_ = guiRenderer_->createResourceProvider(); | 
|---|
| [8706] | 329 | this->oldCEGUI_ = true; | 
|---|
| [8351] | 330 | #else | 
|---|
|  | 331 | guiRenderer_ = &OgreRenderer::create(*GraphicsManager::getInstance().getRenderWindow()); | 
|---|
|  | 332 | // We use our own RenderQueueListener so we can draw UNDER overlays | 
|---|
|  | 333 | guiRenderer_->setFrameControlExecutionEnabled(false); | 
|---|
|  | 334 | rqListener_ = new RQListener(); | 
|---|
|  | 335 | resourceProvider_ = &OgreRenderer::createOgreResourceProvider(); | 
|---|
|  | 336 | imageCodec_ = &OgreRenderer::createOgreImageCodec(); | 
|---|
|  | 337 | #endif | 
|---|
| [7709] | 338 | resourceProvider_->setDefaultResourceGroup("General"); | 
|---|
| [1776] | 339 |  | 
|---|
| [6749] | 340 | // Setup scripting | 
|---|
| [8351] | 341 | luaState_ = new LuaState(); | 
|---|
| [6417] | 342 | rootFileInfo_ = Resource::getInfo("InitialiseGUI.lua"); | 
|---|
|  | 343 | // This is necessary to ensure that input events also use the right resource info when triggering lua functions | 
|---|
|  | 344 | luaState_->setDefaultResourceInfo(this->rootFileInfo_); | 
|---|
| [8351] | 345 | #ifdef ORXONOX_OLD_CEGUI | 
|---|
|  | 346 | scriptModule_ = new LuaScriptModule(luaState_->getInternalLuaState()); | 
|---|
|  | 347 | // Ugly workaround: older CEGUILua versions don't initialise the member | 
|---|
|  | 348 | // d_activeErrFuncIndex at all. That leads to "error in error handling" | 
|---|
|  | 349 | // problems when a Lua error occurs. | 
|---|
|  | 350 | // We fix this by setting the member manually. | 
|---|
|  | 351 | reinterpret_cast<LuaScriptModuleWorkaround*>(scriptModule_)->d_activeErrFuncIndex = LUA_NOREF; | 
|---|
|  | 352 | luaState_->doString("ORXONOX_OLD_CEGUI = true"); | 
|---|
|  | 353 | #else | 
|---|
|  | 354 | scriptModule_ = &LuaScriptModule::create(luaState_->getInternalLuaState()); | 
|---|
|  | 355 | #endif | 
|---|
| [6763] | 356 | scriptModule_->setDefaultPCallErrorHandler(LuaState::ERROR_HANDLER_NAME); | 
|---|
| [1638] | 357 |  | 
|---|
| [5695] | 358 | // Create our own logger to specify the filepath | 
|---|
| [11071] | 359 | std::unique_ptr<CEGUILogger> ceguiLogger(new CEGUILogger()); | 
|---|
| [10624] | 360 | ceguiLogger->setLogFilename(ConfigurablePaths::getLogPathString() + "cegui.log"); | 
|---|
| [8858] | 361 | ceguiLogger->setLoggingLevel(static_cast<CEGUI::LoggingLevel>(this->outputLevelCeguiLog_)); | 
|---|
| [5695] | 362 | this->ceguiLogger_ = ceguiLogger.release(); | 
|---|
| [2710] | 363 |  | 
|---|
| [6749] | 364 | // Create the CEGUI system singleton | 
|---|
| [8351] | 365 | #ifdef ORXONOX_OLD_CEGUI | 
|---|
| [11071] | 366 | guiSystem_ = new System(guiRenderer_, resourceProvider_, nullptr, scriptModule_); | 
|---|
| [8351] | 367 | // Add functions that have been renamed in newer versions | 
|---|
|  | 368 | luaState_->doString("CEGUI.SchemeManager.create = CEGUI.SchemeManager.loadScheme"); | 
|---|
|  | 369 | luaState_->doString("CEGUI.Window.getUnclippedOuterRect = CEGUI.Window.getUnclippedPixelRect"); | 
|---|
| [9050] | 370 | luaState_->doString("CEGUI.ImagesetManager.createFromImageFile= CEGUI.ImagesetManager.createImagesetFromImageFile"); | 
|---|
| [8351] | 371 | #else | 
|---|
| [11071] | 372 | guiSystem_ = &System::create(*guiRenderer_, resourceProvider_, nullptr, imageCodec_, scriptModule_); | 
|---|
| [8351] | 373 | #endif | 
|---|
| [1776] | 374 |  | 
|---|
| [9576] | 375 | CEGUI::String defaultXMLParserName = CEGUI::System::getSingleton().getDefaultXMLParserName(); | 
|---|
|  | 376 | try | 
|---|
|  | 377 | { | 
|---|
|  | 378 | // Force Xerces parser (CEGUI 0.7.5+) | 
|---|
|  | 379 | CEGUI::System::getSingleton().setXMLParser("XercesParser"); | 
|---|
|  | 380 | } | 
|---|
| [9759] | 381 | catch (const CEGUI::GenericException&) | 
|---|
| [9576] | 382 | { | 
|---|
|  | 383 | // Fall back to default parser | 
|---|
|  | 384 | orxout(internal_warning) << "Cannot use XercesParser for CEGUI - using " << defaultXMLParserName << " instead" << endl; | 
|---|
|  | 385 | CEGUI::System::getSingleton().setXMLParser(defaultXMLParserName); | 
|---|
|  | 386 | } | 
|---|
| [9575] | 387 |  | 
|---|
| [5695] | 388 | // Align CEGUI mouse with OIS mouse | 
|---|
| [9675] | 389 | #if CEGUI_VERSION >= 0x000800 | 
|---|
|  | 390 | guiSystem_->getDefaultGUIContext().injectMousePosition((float)mousePosition.first, (float)mousePosition.second); | 
|---|
|  | 391 | #else | 
|---|
| [6502] | 392 | guiSystem_->injectMousePosition((float)mousePosition.first, (float)mousePosition.second); | 
|---|
| [9675] | 393 | #endif | 
|---|
| [5695] | 394 |  | 
|---|
| [6746] | 395 | // Initialise the Lua framework and load the schemes | 
|---|
| [8861] | 396 | orxout(user_info) << "Loading user interface..." << endl; | 
|---|
| [6746] | 397 | this->luaState_->doFile("InitialiseGUI.lua"); | 
|---|
|  | 398 |  | 
|---|
|  | 399 | // Create the root nodes | 
|---|
|  | 400 | this->rootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("MenuWidgets/StaticImage", "AbsoluteRootWindow"); | 
|---|
|  | 401 | this->rootWindow_->setProperty("FrameEnabled", "False"); | 
|---|
|  | 402 | this->hudRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "HUDRootWindow"); | 
|---|
|  | 403 | this->menuRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "MenuRootWindow"); | 
|---|
|  | 404 | // And connect them | 
|---|
| [9675] | 405 | #if CEGUI_VERSION >= 0x000800 | 
|---|
|  | 406 | CEGUI::System::getSingleton().getDefaultGUIContext().setRootWindow(this->rootWindow_); | 
|---|
|  | 407 | this->rootWindow_->addChild(this->hudRootWindow_); | 
|---|
|  | 408 | this->rootWindow_->addChild(this->menuRootWindow_); | 
|---|
|  | 409 | #else | 
|---|
| [6746] | 410 | CEGUI::System::getSingleton().setGUISheet(this->rootWindow_); | 
|---|
|  | 411 | this->rootWindow_->addChildWindow(this->hudRootWindow_); | 
|---|
|  | 412 | this->rootWindow_->addChildWindow(this->menuRootWindow_); | 
|---|
| [9675] | 413 | #endif | 
|---|
| [6746] | 414 |  | 
|---|
| [6749] | 415 | // No background to start with (sets the alpha value to 0) | 
|---|
|  | 416 | this->setBackgroundImage(""); | 
|---|
|  | 417 |  | 
|---|
| [6746] | 418 | // Set up the sheet manager in the Lua framework | 
|---|
|  | 419 | this->luaState_->doFile("SheetManager.lua"); | 
|---|
| [8858] | 420 |  | 
|---|
|  | 421 | orxout(internal_status) << "finished initializing GUIManager" << endl; | 
|---|
| [3338] | 422 | } | 
|---|
| [1776] | 423 |  | 
|---|
| [8423] | 424 | void GUIManager::destroy() | 
|---|
| [3338] | 425 | { | 
|---|
| [8858] | 426 | orxout(internal_status) << "destroying GUIManager..." << endl; | 
|---|
|  | 427 |  | 
|---|
| [8351] | 428 | using namespace CEGUI; | 
|---|
|  | 429 |  | 
|---|
|  | 430 | #ifdef ORXONOX_OLD_CEGUI | 
|---|
| [8423] | 431 | safeObjectDelete(&guiSystem_); | 
|---|
|  | 432 | safeObjectDelete(&guiRenderer_); | 
|---|
|  | 433 | safeObjectDelete(&scriptModule_); | 
|---|
| [8351] | 434 | #else | 
|---|
|  | 435 | System::destroy(); | 
|---|
|  | 436 | OgreRenderer::destroyOgreResourceProvider(*resourceProvider_); | 
|---|
|  | 437 | OgreRenderer::destroyOgreImageCodec(*imageCodec_); | 
|---|
|  | 438 | OgreRenderer::destroy(*guiRenderer_); | 
|---|
|  | 439 | LuaScriptModule::destroy(*scriptModule_); | 
|---|
| [8423] | 440 | safeObjectDelete(&ceguiLogger_); | 
|---|
|  | 441 | safeObjectDelete(&rqListener_); | 
|---|
| [8351] | 442 | #endif | 
|---|
| [8423] | 443 | safeObjectDelete(&luaState_); | 
|---|
| [8858] | 444 |  | 
|---|
|  | 445 | orxout(internal_status) << "finished destroying GUIManager" << endl; | 
|---|
| [1638] | 446 | } | 
|---|
|  | 447 |  | 
|---|
| [7801] | 448 | void GUIManager::setConfigValues(void) | 
|---|
|  | 449 | { | 
|---|
| [8858] | 450 | SetConfigValue(guiScheme_, GUIManager::defaultScheme_).description("Changes the current GUI scheme.").callback(this, &GUIManager::changedGUIScheme); | 
|---|
| [8530] | 451 | SetConfigValue(numScrollLines_, 1).description("How many lines to scroll in a list if the scroll wheel is used"); | 
|---|
| [8862] | 452 | SetConfigValue(bPreloadMenuSheets_, false).description("Pre-load menu sheets during startup"); | 
|---|
|  | 453 |  | 
|---|
| [8858] | 454 | SetConfigValueExternal(outputLevelCeguiLog_, BaseWriter::getConfigurableSectionName(), "outputLevelCeguiLog", CEGUI::Standard).description("The log level of the CEGUI log file").callback(this, &GUIManager::changedCeguiOutputLevel); | 
|---|
| [7801] | 455 | } | 
|---|
|  | 456 |  | 
|---|
|  | 457 | void GUIManager::changedGUIScheme(void) | 
|---|
|  | 458 | { | 
|---|
|  | 459 | } | 
|---|
|  | 460 |  | 
|---|
| [8858] | 461 | void GUIManager::changedCeguiOutputLevel() | 
|---|
|  | 462 | { | 
|---|
|  | 463 | if (this->ceguiLogger_) | 
|---|
|  | 464 | this->ceguiLogger_->setLoggingLevel(static_cast<CEGUI::LoggingLevel>(this->outputLevelCeguiLog_)); | 
|---|
|  | 465 | } | 
|---|
|  | 466 |  | 
|---|
| [2896] | 467 | /** | 
|---|
|  | 468 | @brief | 
|---|
|  | 469 | used to tick the GUI | 
|---|
|  | 470 | @param time | 
|---|
|  | 471 | clock which provides time value for the GUI System | 
|---|
|  | 472 |  | 
|---|
|  | 473 | Ticking the GUI means updating it with a certain regularity. | 
|---|
|  | 474 | The elapsed time since the last call is given in the time value provided by the clock. | 
|---|
|  | 475 | This time value is then used to provide a fluent animation of the GUI. | 
|---|
|  | 476 | */ | 
|---|
| [6417] | 477 | void GUIManager::preUpdate(const Clock& time) | 
|---|
| [1638] | 478 | { | 
|---|
| [2896] | 479 | assert(guiSystem_); | 
|---|
| [11071] | 480 | this->protectedCeguiSystemCall(std::bind(&CEGUI::System::injectTimePulse, arg::_1, time.getDeltaTime())); | 
|---|
| [2896] | 481 | } | 
|---|
| [1638] | 482 |  | 
|---|
| [2896] | 483 | /** | 
|---|
|  | 484 | @brief | 
|---|
|  | 485 | Tells the GUIManager which SceneManager to use | 
|---|
|  | 486 | @param camera | 
|---|
|  | 487 | The current camera on which the GUI should be displayed on. | 
|---|
|  | 488 |  | 
|---|
|  | 489 | In fact the GUIManager needs the SceneManager and not the Camera to display the GUI. | 
|---|
|  | 490 | This means the GUI is not bound to a camera but rather to the SceneManager. | 
|---|
| [3337] | 491 | Hiding the GUI when needed can therefore not be resolved by just NOT setting the current camera. | 
|---|
| [2896] | 492 | */ | 
|---|
|  | 493 | void GUIManager::setCamera(Ogre::Camera* camera) | 
|---|
| [1638] | 494 | { | 
|---|
| [8351] | 495 | #ifdef ORXONOX_OLD_CEGUI | 
|---|
| [11071] | 496 | if (camera == nullptr) | 
|---|
|  | 497 | this->guiRenderer_->setTargetSceneManager(nullptr); | 
|---|
| [2927] | 498 | else | 
|---|
|  | 499 | this->guiRenderer_->setTargetSceneManager(camera->getSceneManager()); | 
|---|
| [8351] | 500 | #else | 
|---|
| [11071] | 501 | if (camera_ != nullptr && camera_->getSceneManager() != nullptr) | 
|---|
| [8351] | 502 | camera_->getSceneManager()->removeRenderQueueListener(rqListener_); | 
|---|
| [11071] | 503 | if (camera != nullptr && camera->getSceneManager() != nullptr) | 
|---|
| [8351] | 504 | camera->getSceneManager()->addRenderQueueListener(rqListener_); | 
|---|
|  | 505 | #endif | 
|---|
|  | 506 | this->camera_ = camera; | 
|---|
| [2896] | 507 | } | 
|---|
|  | 508 |  | 
|---|
|  | 509 | /** | 
|---|
|  | 510 | @brief | 
|---|
| [3338] | 511 | Executes Lua code | 
|---|
|  | 512 | @param str | 
|---|
|  | 513 | reference to string object holding the Lua code which is to be executed | 
|---|
|  | 514 | */ | 
|---|
|  | 515 | void GUIManager::executeCode(const std::string& str) | 
|---|
|  | 516 | { | 
|---|
| [5695] | 517 | this->luaState_->doString(str, rootFileInfo_); | 
|---|
| [3338] | 518 | } | 
|---|
|  | 519 |  | 
|---|
| [11052] | 520 | std::vector<std::string> GUIManager::getLoadedGUIs() | 
|---|
|  | 521 | { | 
|---|
|  | 522 | // TODO: is there a better way to read back a return value from lua? i.e. by using LuaState? | 
|---|
|  | 523 | lua_State* L = this->luaState_->getInternalLuaState(); | 
|---|
|  | 524 |  | 
|---|
|  | 525 | // push function | 
|---|
|  | 526 | lua_getglobal(L, "getLoadedSheets"); | 
|---|
|  | 527 |  | 
|---|
|  | 528 | // do the call (0 arguments, 1 result) | 
|---|
|  | 529 | if (lua_pcall(L, 0, 1, 0) != 0) | 
|---|
|  | 530 | orxout(internal_error) << "error running function: " << lua_tostring(L, -1) << endl; | 
|---|
|  | 531 |  | 
|---|
|  | 532 | // retrieve result | 
|---|
|  | 533 | if (!lua_isstring(L, -1)) | 
|---|
|  | 534 | orxout(internal_error) << "function must return a string" << endl; | 
|---|
|  | 535 | std::string value = lua_tostring(L, -1); | 
|---|
|  | 536 | lua_pop(L, 1); | 
|---|
|  | 537 |  | 
|---|
|  | 538 | SubString tokens(value, ","); | 
|---|
|  | 539 | return tokens.getAllStrings(); | 
|---|
|  | 540 | } | 
|---|
|  | 541 |  | 
|---|
| [6746] | 542 | /** Loads a GUI sheet by Lua script | 
|---|
|  | 543 | @param name | 
|---|
|  | 544 | The name of the GUI (like the script name, but without the extension) | 
|---|
|  | 545 | */ | 
|---|
|  | 546 | void GUIManager::loadGUI(const std::string& name) | 
|---|
|  | 547 | { | 
|---|
|  | 548 | this->executeCode("loadSheet(\"" + name + "\")"); | 
|---|
|  | 549 | } | 
|---|
|  | 550 |  | 
|---|
| [3338] | 551 | /** | 
|---|
|  | 552 | @brief | 
|---|
| [2896] | 553 | Displays specified GUI on screen | 
|---|
|  | 554 | @param name | 
|---|
|  | 555 | The name of the GUI | 
|---|
| [7401] | 556 | @param bHidePrevious | 
|---|
|  | 557 | If true all displayed GUIs on the stack, that are below this GUI are hidden. | 
|---|
| [7403] | 558 | @param bNoInput | 
|---|
|  | 559 | If true the GUI is transparent to input. | 
|---|
| [2896] | 560 |  | 
|---|
|  | 561 | The function executes the Lua function with the same name in case the GUIManager is ready. | 
|---|
|  | 562 | */ | 
|---|
| [7403] | 563 | /*static*/ void GUIManager::showGUI(const std::string& name, bool bHidePrevious, bool bNoInput) | 
|---|
| [2896] | 564 | { | 
|---|
| [7403] | 565 | GUIManager::getInstance().executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ")"); | 
|---|
| [1638] | 566 | } | 
|---|
|  | 567 |  | 
|---|
| [6417] | 568 | /** | 
|---|
|  | 569 | @brief | 
|---|
|  | 570 | Hack-ish. Needed for GUIOverlay. | 
|---|
|  | 571 | */ | 
|---|
| [7403] | 572 | void GUIManager::showGUIExtra(const std::string& name, const std::string& ptr, bool bHidePrevious, bool bNoInput) | 
|---|
| [6417] | 573 | { | 
|---|
| [7403] | 574 | this->executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ", " + ptr + ")"); | 
|---|
| [6417] | 575 | } | 
|---|
|  | 576 |  | 
|---|
|  | 577 | /** | 
|---|
|  | 578 | @brief | 
|---|
|  | 579 | Hides specified GUI. | 
|---|
|  | 580 | @param name | 
|---|
|  | 581 | The name of the GUI. | 
|---|
|  | 582 | */ | 
|---|
|  | 583 | /*static*/ void GUIManager::hideGUI(const std::string& name) | 
|---|
|  | 584 | { | 
|---|
| [6746] | 585 | GUIManager::getInstance().executeCode("hideMenuSheet(\"" + name + "\")"); | 
|---|
| [6417] | 586 | } | 
|---|
|  | 587 |  | 
|---|
| [8079] | 588 | /** | 
|---|
|  | 589 | @brief | 
|---|
|  | 590 | Toggles specified GUI. | 
|---|
|  | 591 | If the GUI with the input name is already shown and on the top, it is hidden, else it is shown. | 
|---|
|  | 592 | */ | 
|---|
|  | 593 | /*static*/ void GUIManager::toggleGUI(const std::string& name, bool bHidePrevious, bool bNoInput) | 
|---|
|  | 594 | { | 
|---|
|  | 595 | GUIManager::getInstance().executeCode("getGUIFirstActive(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ")"); | 
|---|
|  | 596 | } | 
|---|
|  | 597 |  | 
|---|
|  | 598 | /** | 
|---|
|  | 599 | @brief | 
|---|
|  | 600 | Helper method to toggle a specified GUI. | 
|---|
|  | 601 | Is called by lua. | 
|---|
|  | 602 | */ | 
|---|
|  | 603 | void GUIManager::toggleGUIHelper(const std::string& name, bool bHidePrevious, bool bNoInput, bool show) | 
|---|
|  | 604 | { | 
|---|
|  | 605 | if(show) | 
|---|
|  | 606 | GUIManager::showGUI(name, bHidePrevious, bNoInput); | 
|---|
|  | 607 | else | 
|---|
|  | 608 | GUIManager::hideGUI(name); | 
|---|
|  | 609 | } | 
|---|
|  | 610 |  | 
|---|
| [8729] | 611 | const std::string& GUIManager::createInputState(const std::string& name, tribool showCursor, tribool useKeyboard, bool bBlockJoyStick) | 
|---|
| [6746] | 612 | { | 
|---|
|  | 613 | InputState* state = InputManager::getInstance().createInputState(name); | 
|---|
| [7811] | 614 | if (!state) | 
|---|
|  | 615 | return BLANKSTRING; | 
|---|
| [6746] | 616 |  | 
|---|
|  | 617 | /* Table that maps isFullScreen() and showCursor to mouseExclusive | 
|---|
|  | 618 | isFullscreen / showCursor | True  | False | Dontcare | 
|---|
|  | 619 | ---------------------------------------------------- | 
|---|
|  | 620 | true                      | True  | True  | Dontcare | 
|---|
|  | 621 | ---------------------------------------------------- | 
|---|
|  | 622 | false                     | False | True  | Dontcare | 
|---|
|  | 623 | */ | 
|---|
| [8351] | 624 |  | 
|---|
|  | 625 | #ifdef ORXONOX_PLATFORM_APPLE | 
|---|
|  | 626 | // There is no non exclusive mode on OS X yet | 
|---|
| [8729] | 627 | state->setMouseExclusive(true); | 
|---|
| [8351] | 628 | #else | 
|---|
| [8729] | 629 | if (showCursor == dontcare) | 
|---|
|  | 630 | state->setMouseExclusive(dontcare); | 
|---|
|  | 631 | else if (GraphicsManager::getInstance().isFullScreen() || showCursor == false) | 
|---|
|  | 632 | state->setMouseExclusive(true); | 
|---|
| [6746] | 633 | else | 
|---|
| [8729] | 634 | state->setMouseExclusive(false); | 
|---|
| [8351] | 635 | #endif | 
|---|
| [6746] | 636 |  | 
|---|
| [8729] | 637 | if (showCursor == true) | 
|---|
| [6746] | 638 | state->setMouseHandler(this); | 
|---|
| [8729] | 639 | else if (showCursor == false) | 
|---|
| [6746] | 640 | state->setMouseHandler(&InputHandler::EMPTY); | 
|---|
|  | 641 |  | 
|---|
| [8729] | 642 | if (useKeyboard == true) | 
|---|
| [6746] | 643 | state->setKeyHandler(this); | 
|---|
| [8729] | 644 | else if (useKeyboard == false) | 
|---|
| [6746] | 645 | state->setKeyHandler(&InputHandler::EMPTY); | 
|---|
|  | 646 |  | 
|---|
|  | 647 | if (bBlockJoyStick) | 
|---|
|  | 648 | state->setJoyStickHandler(&InputHandler::EMPTY); | 
|---|
|  | 649 |  | 
|---|
|  | 650 | return state->getName(); | 
|---|
|  | 651 | } | 
|---|
|  | 652 |  | 
|---|
| [6417] | 653 | void GUIManager::keyESC() | 
|---|
|  | 654 | { | 
|---|
|  | 655 | this->executeCode("keyESC()"); | 
|---|
|  | 656 | } | 
|---|
|  | 657 |  | 
|---|
| [6746] | 658 | void GUIManager::setBackgroundImage(const std::string& imageSet, const std::string imageName) | 
|---|
| [6417] | 659 | { | 
|---|
| [6746] | 660 | if (imageSet.empty() || imageName.empty()) | 
|---|
|  | 661 | this->setBackgroundImage(""); | 
|---|
|  | 662 | else | 
|---|
|  | 663 | this->setBackgroundImage("set: " + imageSet + " image: " + imageName); | 
|---|
| [6417] | 664 | } | 
|---|
|  | 665 |  | 
|---|
| [6746] | 666 | void GUIManager::setBackgroundImage(const std::string& image) | 
|---|
|  | 667 | { | 
|---|
|  | 668 | if (image.empty()) | 
|---|
|  | 669 | this->rootWindow_->setProperty("Alpha", "0.0"); | 
|---|
|  | 670 | else | 
|---|
|  | 671 | this->rootWindow_->setProperty("Alpha", "1.0"); | 
|---|
|  | 672 | this->rootWindow_->setProperty("Image", image); | 
|---|
|  | 673 | } | 
|---|
|  | 674 |  | 
|---|
| [7163] | 675 | void GUIManager::buttonPressed(const KeyEvent& evt) | 
|---|
| [3196] | 676 | { | 
|---|
| [9675] | 677 | #if CEGUI_VERSION >= 0x000800 | 
|---|
| [11071] | 678 | this->protectedCeguiContextCall(std::bind(&CEGUI::GUIContext::injectKeyDown, arg::_1, (CEGUI::Key::Scan) evt.getKeyCode())); // TODO: will this cast always work? | 
|---|
|  | 679 | this->protectedCeguiContextCall(std::bind(&CEGUI::GUIContext::injectChar, arg::_1, evt.getText())); | 
|---|
| [9675] | 680 | #else | 
|---|
| [11071] | 681 | this->protectedCeguiSystemCall(std::bind(&CEGUI::System::injectKeyDown, arg::_1, evt.getKeyCode())); | 
|---|
|  | 682 | this->protectedCeguiSystemCall(std::bind(&CEGUI::System::injectChar, arg::_1, evt.getText())); | 
|---|
| [9675] | 683 | #endif | 
|---|
| [3196] | 684 | } | 
|---|
| [6746] | 685 |  | 
|---|
| [7163] | 686 | void GUIManager::buttonReleased(const KeyEvent& evt) | 
|---|
| [3196] | 687 | { | 
|---|
| [9675] | 688 | #if CEGUI_VERSION >= 0x000800 | 
|---|
| [11071] | 689 | this->protectedCeguiContextCall(std::bind(&CEGUI::GUIContext::injectKeyUp, arg::_1, (CEGUI::Key::Scan) evt.getKeyCode())); // TODO: will this cast always work? | 
|---|
| [9675] | 690 | #else | 
|---|
| [11071] | 691 | this->protectedCeguiSystemCall(std::bind(&CEGUI::System::injectKeyUp, arg::_1, evt.getKeyCode())); | 
|---|
| [9675] | 692 | #endif | 
|---|
| [3196] | 693 | } | 
|---|
|  | 694 |  | 
|---|
| [2896] | 695 | /** | 
|---|
|  | 696 | @brief | 
|---|
|  | 697 | Function receiving a mouse button pressed event. | 
|---|
|  | 698 | @param id | 
|---|
|  | 699 | ID of the mouse button which got pressed | 
|---|
| [1638] | 700 |  | 
|---|
| [2896] | 701 | This function is inherited by MouseHandler and injects the event into CEGUI. | 
|---|
|  | 702 | It is for CEGUI to process the event. | 
|---|
|  | 703 | */ | 
|---|
| [3327] | 704 | void GUIManager::buttonPressed(MouseButtonCode::ByEnum id) | 
|---|
| [1638] | 705 | { | 
|---|
| [9675] | 706 | #if CEGUI_VERSION >= 0x000800 | 
|---|
| [11071] | 707 | this->protectedCeguiContextCall(std::bind(&CEGUI::GUIContext::injectMouseButtonDown, arg::_1, convertButton(id))); | 
|---|
| [9675] | 708 | #else | 
|---|
| [11071] | 709 | this->protectedCeguiSystemCall(std::bind(&CEGUI::System::injectMouseButtonDown, arg::_1, convertButton(id))); | 
|---|
| [9675] | 710 | #endif | 
|---|
| [1638] | 711 | } | 
|---|
|  | 712 |  | 
|---|
| [2896] | 713 | /** | 
|---|
|  | 714 | @brief | 
|---|
|  | 715 | Function receiving a mouse button released event. | 
|---|
|  | 716 | @param id | 
|---|
|  | 717 | ID of the mouse button which got released | 
|---|
|  | 718 |  | 
|---|
|  | 719 | This function is inherited by MouseHandler and injects the event into CEGUI. | 
|---|
|  | 720 | It is for CEGUI to process the event. | 
|---|
|  | 721 | */ | 
|---|
| [3327] | 722 | void GUIManager::buttonReleased(MouseButtonCode::ByEnum id) | 
|---|
| [1638] | 723 | { | 
|---|
| [9675] | 724 | #if CEGUI_VERSION >= 0x000800 | 
|---|
| [11071] | 725 | this->protectedCeguiContextCall(std::bind(&CEGUI::GUIContext::injectMouseButtonUp, arg::_1, convertButton(id))); | 
|---|
| [9675] | 726 | #else | 
|---|
| [11071] | 727 | this->protectedCeguiSystemCall(std::bind(&CEGUI::System::injectMouseButtonUp, arg::_1, convertButton(id))); | 
|---|
| [9675] | 728 | #endif | 
|---|
| [1638] | 729 | } | 
|---|
|  | 730 |  | 
|---|
| [3196] | 731 | void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) | 
|---|
|  | 732 | { | 
|---|
| [9675] | 733 | #if CEGUI_VERSION >= 0x000800 | 
|---|
| [11071] | 734 | this->protectedCeguiContextCall(std::bind(&CEGUI::GUIContext::injectMousePosition, arg::_1, (float)abs.x, (float)abs.y)); | 
|---|
| [9675] | 735 | #else | 
|---|
| [11071] | 736 | this->protectedCeguiSystemCall(std::bind(&CEGUI::System::injectMousePosition, arg::_1, (float)abs.x, (float)abs.y)); | 
|---|
| [9675] | 737 | #endif | 
|---|
| [3196] | 738 | } | 
|---|
| [6746] | 739 |  | 
|---|
| [3196] | 740 | void GUIManager::mouseScrolled(int abs, int rel) | 
|---|
|  | 741 | { | 
|---|
| [9675] | 742 | #if CEGUI_VERSION >= 0x000800 | 
|---|
| [11071] | 743 | this->protectedCeguiContextCall(std::bind(&CEGUI::GUIContext::injectMouseWheelChange, arg::_1, (float)sgn(rel) * this->numScrollLines_)); | 
|---|
| [9675] | 744 | #else | 
|---|
| [11071] | 745 | this->protectedCeguiSystemCall(std::bind(&CEGUI::System::injectMouseWheelChange, arg::_1, (float)sgn(rel) * this->numScrollLines_)); | 
|---|
| [9675] | 746 | #endif | 
|---|
| [3196] | 747 | } | 
|---|
|  | 748 |  | 
|---|
| [2896] | 749 | /** | 
|---|
| [7874] | 750 | @brief Indicates that the mouse left the application's window. | 
|---|
|  | 751 | */ | 
|---|
|  | 752 | void GUIManager::mouseLeft() | 
|---|
|  | 753 | { | 
|---|
| [9675] | 754 | #if CEGUI_VERSION >= 0x000800 | 
|---|
| [11071] | 755 | this->protectedCeguiContextCall(std::bind(&CEGUI::GUIContext::injectMouseLeaves, arg::_1)); | 
|---|
| [9675] | 756 | #else | 
|---|
| [11071] | 757 | this->protectedCeguiSystemCall(std::bind(&CEGUI::System::injectMouseLeaves, arg::_1)); | 
|---|
| [9675] | 758 | #endif | 
|---|
| [7874] | 759 | } | 
|---|
|  | 760 |  | 
|---|
|  | 761 | /** | 
|---|
| [2896] | 762 | @brief | 
|---|
|  | 763 | converts mouse event code to CEGUI event code | 
|---|
|  | 764 | @param button | 
|---|
|  | 765 | code of the mouse button as we use it in Orxonox | 
|---|
|  | 766 | @return | 
|---|
|  | 767 | code of the mouse button as it is used by CEGUI | 
|---|
| [1638] | 768 |  | 
|---|
| [6105] | 769 | Simple conversion from mouse event code in Orxonox to the one used in CEGUI. | 
|---|
| [2896] | 770 | */ | 
|---|
| [3196] | 771 | static inline CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button) | 
|---|
| [1638] | 772 | { | 
|---|
|  | 773 | switch (button) | 
|---|
|  | 774 | { | 
|---|
| [1887] | 775 | case MouseButtonCode::Left: | 
|---|
| [1638] | 776 | return CEGUI::LeftButton; | 
|---|
|  | 777 |  | 
|---|
| [1887] | 778 | case MouseButtonCode::Right: | 
|---|
| [1638] | 779 | return CEGUI::RightButton; | 
|---|
|  | 780 |  | 
|---|
| [1887] | 781 | case MouseButtonCode::Middle: | 
|---|
| [1638] | 782 | return CEGUI::MiddleButton; | 
|---|
|  | 783 |  | 
|---|
| [1887] | 784 | case MouseButtonCode::Button3: | 
|---|
| [1638] | 785 | return CEGUI::X1Button; | 
|---|
|  | 786 |  | 
|---|
| [1887] | 787 | case MouseButtonCode::Button4: | 
|---|
| [1638] | 788 | return CEGUI::X2Button; | 
|---|
|  | 789 |  | 
|---|
|  | 790 | default: | 
|---|
|  | 791 | return CEGUI::NoButton; | 
|---|
|  | 792 | } | 
|---|
|  | 793 | } | 
|---|
| [6417] | 794 |  | 
|---|
| [6746] | 795 | /** Executes a CEGUI function normally, but catches CEGUI::ScriptException. | 
|---|
|  | 796 | When a ScriptException occurs, the error message will be displayed and | 
|---|
|  | 797 | the program carries on. | 
|---|
|  | 798 | @remarks | 
|---|
|  | 799 | The exception behaviour may pose problems if the code is not written | 
|---|
|  | 800 | exception-safe (and you can forget about that in Lua). The program might | 
|---|
|  | 801 | be left in an undefined state. But otherwise one script error would | 
|---|
|  | 802 | terminate the whole program... | 
|---|
|  | 803 | @note | 
|---|
| [11071] | 804 | Your life gets easier if you use std::bind to create the object/function. | 
|---|
| [6746] | 805 | @param function | 
|---|
|  | 806 | Any callable object/function that takes this->guiSystem_ as its only parameter. | 
|---|
|  | 807 | @return | 
|---|
|  | 808 | True if input was handled, false otherwise. A caught exception yields true. | 
|---|
|  | 809 | */ | 
|---|
| [9675] | 810 | template <typename FunctionType, typename ObjectType> | 
|---|
|  | 811 | bool GUIManager::protectedCall(FunctionType function, ObjectType object) | 
|---|
| [6746] | 812 | { | 
|---|
|  | 813 | try | 
|---|
|  | 814 | { | 
|---|
| [9675] | 815 | return function(object); | 
|---|
| [6746] | 816 | } | 
|---|
|  | 817 | catch (CEGUI::ScriptException& ex) | 
|---|
|  | 818 | { | 
|---|
|  | 819 | // Display the error and proceed. See @remarks why this can be dangerous. | 
|---|
| [8858] | 820 | orxout(internal_error) << ex.getMessage() << endl; | 
|---|
| [6746] | 821 | return true; | 
|---|
|  | 822 | } | 
|---|
|  | 823 | } | 
|---|
|  | 824 |  | 
|---|
| [9675] | 825 | template <typename FunctionType> | 
|---|
|  | 826 | bool GUIManager::protectedCeguiSystemCall(FunctionType function) | 
|---|
|  | 827 | { | 
|---|
|  | 828 | return this->protectedCall(function, this->guiSystem_); | 
|---|
|  | 829 | } | 
|---|
|  | 830 |  | 
|---|
|  | 831 | #if CEGUI_VERSION >= 0x000800 | 
|---|
|  | 832 | template <typename FunctionType> | 
|---|
|  | 833 | bool GUIManager::protectedCeguiContextCall(FunctionType function) | 
|---|
|  | 834 | { | 
|---|
|  | 835 | return this->protectedCall(function, this->guiSystem_->getDefaultGUIContext()); | 
|---|
|  | 836 | } | 
|---|
|  | 837 | #endif | 
|---|
|  | 838 |  | 
|---|
| [7648] | 839 | /** | 
|---|
|  | 840 | @brief | 
|---|
|  | 841 | Subscribe the input function to the input event for the input window. | 
|---|
|  | 842 | This is a helper to be used in lua, because subscribeScriptedEvent() doesn't work in lua. | 
|---|
|  | 843 | @param window | 
|---|
|  | 844 | The window for which the event is subscribed. | 
|---|
|  | 845 | @param event | 
|---|
|  | 846 | The type of event to which we subscribe. | 
|---|
|  | 847 | @param function | 
|---|
|  | 848 | The function that is called when the event occurs. | 
|---|
|  | 849 | */ | 
|---|
| [6417] | 850 | void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function) | 
|---|
|  | 851 | { | 
|---|
|  | 852 | window->subscribeScriptedEvent(event, function); | 
|---|
|  | 853 | } | 
|---|
| [7648] | 854 |  | 
|---|
|  | 855 | /** | 
|---|
|  | 856 | @brief | 
|---|
|  | 857 | Set the input tooltip text for the input ListboxItem. | 
|---|
|  | 858 | @param item | 
|---|
|  | 859 | The ListboxItem for which the tooltip should be set. | 
|---|
|  | 860 | @param tooltip | 
|---|
|  | 861 | The tooltip text that should be set. | 
|---|
|  | 862 | */ | 
|---|
|  | 863 | void GUIManager::setTooltipTextHelper(CEGUI::ListboxItem* item, const std::string& tooltip) | 
|---|
|  | 864 | { | 
|---|
|  | 865 | item->setTooltipText(tooltip); | 
|---|
|  | 866 | } | 
|---|
|  | 867 |  | 
|---|
|  | 868 | /** | 
|---|
|  | 869 | @brief | 
|---|
|  | 870 | Set whether the tooltips for the input Listbox are enabled. | 
|---|
|  | 871 | @param listbox | 
|---|
|  | 872 | The Listbox for which to enable (or disable) tooltips. | 
|---|
|  | 873 | @param enabled | 
|---|
| [8351] | 874 | Whether to enable or disable the tooltips. | 
|---|
| [7648] | 875 | */ | 
|---|
|  | 876 | void GUIManager::setItemTooltipsEnabledHelper(CEGUI::Listbox* listbox, bool enabled) | 
|---|
|  | 877 | { | 
|---|
|  | 878 | listbox->setItemTooltipsEnabled(enabled); | 
|---|
|  | 879 | } | 
|---|
|  | 880 |  | 
|---|
| [8729] | 881 | /** Helper method to get the developer's mode without having to export Core.h. | 
|---|
|  | 882 | @see Core::inDevMode | 
|---|
|  | 883 | */ | 
|---|
|  | 884 | /*static*/ bool GUIManager::inDevMode() | 
|---|
|  | 885 | { | 
|---|
| [10624] | 886 | return Core::getInstance().getConfig()->inDevMode(); | 
|---|
| [8729] | 887 | } | 
|---|
|  | 888 |  | 
|---|
| [7873] | 889 | /** | 
|---|
|  | 890 | @brief Callback of window event listener, called if the window is resized. Sets the display size of CEGUI. | 
|---|
|  | 891 | */ | 
|---|
|  | 892 | void GUIManager::windowResized(unsigned int newWidth, unsigned int newHeight) | 
|---|
|  | 893 | { | 
|---|
| [9675] | 894 | #if CEGUI_VERSION >= 0x000800 | 
|---|
|  | 895 | this->guiRenderer_->setDisplaySize(CEGUI::Sizef((float)newWidth, (float)newHeight)); | 
|---|
|  | 896 | #else | 
|---|
| [8079] | 897 | this->guiRenderer_->setDisplaySize(CEGUI::Size((float)newWidth, (float)newHeight)); | 
|---|
| [9675] | 898 | #endif | 
|---|
| [10279] | 899 | this->rootWindow_->setSize(CEGUI::UVector2(CEGUI::UDim(1, (float)newWidth), CEGUI::UDim(1, (float)newHeight))); | 
|---|
| [7873] | 900 | } | 
|---|
| [7874] | 901 |  | 
|---|
|  | 902 | /** | 
|---|
| [8079] | 903 | @brief Notify CEGUI if the windows loses the focus (stops highlighting of menu items, etc). | 
|---|
| [7874] | 904 | */ | 
|---|
|  | 905 | void GUIManager::windowFocusChanged(bool bFocus) | 
|---|
|  | 906 | { | 
|---|
|  | 907 | if (!bFocus) | 
|---|
|  | 908 | this->mouseLeft(); | 
|---|
|  | 909 | } | 
|---|
| [7876] | 910 |  | 
|---|
| [8706] | 911 | /** | 
|---|
|  | 912 | @brief | 
|---|
|  | 913 | Adds a new freetype font to the CEGUI system. | 
|---|
|  | 914 | @param name | 
|---|
|  | 915 | The name of the new font. | 
|---|
|  | 916 | @param size | 
|---|
|  | 917 | The font size of the new font in pixels. | 
|---|
|  | 918 | @param fontName | 
|---|
|  | 919 | The filename of the font. | 
|---|
|  | 920 | */ | 
|---|
|  | 921 | /*static*/ void GUIManager::addFontHelper(const std::string& name, int size, const std::string& fontName) | 
|---|
|  | 922 | { | 
|---|
|  | 923 | #ifdef ORXONOX_OLD_CEGUI | 
|---|
|  | 924 | if(CEGUI::FontManager::getSingleton().isFontPresent(name)) // If a font with that name already exists. | 
|---|
|  | 925 | return; | 
|---|
|  | 926 |  | 
|---|
| [11071] | 927 | CEGUI::Font* font = nullptr; | 
|---|
| [8706] | 928 | CEGUI::XMLAttributes xmlAttributes; | 
|---|
|  | 929 |  | 
|---|
|  | 930 | // Attributes specified within CEGUIFont | 
|---|
|  | 931 | xmlAttributes.add("Name", name); | 
|---|
|  | 932 | xmlAttributes.add("Filename", fontName); | 
|---|
|  | 933 | xmlAttributes.add("ResourceGroup", ""); | 
|---|
|  | 934 | xmlAttributes.add("AutoScaled", "true"); | 
|---|
|  | 935 | xmlAttributes.add("NativeHorzRes", "800"); | 
|---|
|  | 936 | xmlAttributes.add("NativeVertRes", "600"); | 
|---|
|  | 937 |  | 
|---|
|  | 938 | // Attributes specified within CEGUIXMLAttributes | 
|---|
|  | 939 | xmlAttributes.add("Size", multi_cast<std::string>(size)); | 
|---|
|  | 940 | xmlAttributes.add("AntiAlias", "true"); | 
|---|
|  | 941 |  | 
|---|
|  | 942 | font = CEGUI::FontManager::getSingleton().createFont("FreeType", xmlAttributes); | 
|---|
| [11071] | 943 | if(font != nullptr) | 
|---|
| [8706] | 944 | font->load(); | 
|---|
|  | 945 | #else | 
|---|
|  | 946 | if(CEGUI::FontManager::getSingleton().isDefined(name)) // If a font with that name already exists. | 
|---|
|  | 947 | return; | 
|---|
|  | 948 |  | 
|---|
| [9675] | 949 | #if CEGUI_VERSION >= 0x000800 | 
|---|
|  | 950 | CEGUI::FontManager::getSingleton().createFreeTypeFont(name, (float)size, true, fontName, "", CEGUI::ASM_Both, CEGUI::Sizef(800.0f, 600.0f)); | 
|---|
|  | 951 | #else | 
|---|
| [8706] | 952 | CEGUI::FontManager::getSingleton().createFreeTypeFont(name, (float)size, true, fontName, "", true, 800.0f, 600.0f); | 
|---|
| [9675] | 953 | #endif | 
|---|
| [8706] | 954 | #endif | 
|---|
|  | 955 | } | 
|---|
|  | 956 |  | 
|---|
| [1638] | 957 | } | 
|---|