| 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 | 
|---|
| 24 | *      Benjamin Knecht | 
|---|
| 25 | *   Co-authors: | 
|---|
| 26 | *      ... | 
|---|
| 27 | * | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | #include "GUIManager.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include <memory> | 
|---|
| 33 | extern "C" { | 
|---|
| 34 | #include <lua.h> | 
|---|
| 35 | } | 
|---|
| 36 | #include <CEGUIDefaultLogger.h> | 
|---|
| 37 | #include <CEGUIExceptions.h> | 
|---|
| 38 | #include <CEGUIInputEvent.h> | 
|---|
| 39 | #include <CEGUIMouseCursor.h> | 
|---|
| 40 | #include <CEGUIResourceProvider.h> | 
|---|
| 41 | #include <CEGUISystem.h> | 
|---|
| 42 | #include <CEGUIWindow.h> | 
|---|
| 43 | #include <ogreceguirenderer/OgreCEGUIRenderer.h> | 
|---|
| 44 |  | 
|---|
| 45 | #include "SpecialConfig.h" // Configures the macro below | 
|---|
| 46 | #ifdef CEGUILUA_USE_INTERNAL_LIBRARY | 
|---|
| 47 | #   include <ceguilua/CEGUILua.h> | 
|---|
| 48 | #else | 
|---|
| 49 | #   include <CEGUILua.h> | 
|---|
| 50 | #endif | 
|---|
| 51 |  | 
|---|
| 52 | #include "util/Clock.h" | 
|---|
| 53 | #include "util/Convert.h" | 
|---|
| 54 | #include "util/Debug.h" | 
|---|
| 55 | #include "util/Exception.h" | 
|---|
| 56 | #include "util/OrxAssert.h" | 
|---|
| 57 | #include "ConsoleCommand.h" | 
|---|
| 58 | #include "Core.h" | 
|---|
| 59 | #include "LuaState.h" | 
|---|
| 60 | #include "PathConfig.h" | 
|---|
| 61 | #include "Resource.h" | 
|---|
| 62 |  | 
|---|
| 63 | namespace orxonox | 
|---|
| 64 | { | 
|---|
| 65 | static void key_esc() | 
|---|
| 66 | { GUIManager::getInstance().keyESC(); } | 
|---|
| 67 | SetConsoleCommandShortcutExternAlias(key_esc, "keyESC"); | 
|---|
| 68 |  | 
|---|
| 69 | class CEGUILogger : public CEGUI::DefaultLogger | 
|---|
| 70 | { | 
|---|
| 71 | public: | 
|---|
| 72 | void logEvent(const CEGUI::String& message, CEGUI::LoggingLevel level = CEGUI::Standard) | 
|---|
| 73 | { | 
|---|
| 74 | int orxonoxLevel = CEGUI::Standard; | 
|---|
| 75 | switch (level) | 
|---|
| 76 | { | 
|---|
| 77 | case CEGUI::Errors:      orxonoxLevel = 1; break; | 
|---|
| 78 | case CEGUI::Warnings:    orxonoxLevel = 2; break; | 
|---|
| 79 | case CEGUI::Standard:    orxonoxLevel = 4; break; | 
|---|
| 80 | case CEGUI::Informative: orxonoxLevel = 5; break; | 
|---|
| 81 | case CEGUI::Insane:      orxonoxLevel = 6; break; | 
|---|
| 82 | default: OrxAssert(false, "CEGUI log level out of range, inpect immediately!"); | 
|---|
| 83 | } | 
|---|
| 84 | OutputHandler::getOutStream(orxonoxLevel) | 
|---|
| 85 | << "CEGUI: " << message << std::endl; | 
|---|
| 86 |  | 
|---|
| 87 | CEGUI::DefaultLogger::logEvent(message, level); | 
|---|
| 88 | } | 
|---|
| 89 | }; | 
|---|
| 90 |  | 
|---|
| 91 | static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button); | 
|---|
| 92 |  | 
|---|
| 93 | GUIManager* GUIManager::singletonPtr_s = 0; | 
|---|
| 94 |  | 
|---|
| 95 | SetConsoleCommandShortcut(GUIManager, showGUI).accessLevel(AccessLevel::User).defaultValue(1, false).defaultValue(2, true); | 
|---|
| 96 | SetConsoleCommandShortcut(GUIManager, hideGUI).accessLevel(AccessLevel::User); | 
|---|
| 97 |  | 
|---|
| 98 | /** | 
|---|
| 99 | @brief | 
|---|
| 100 | Constructs the GUIManager by starting up CEGUI | 
|---|
| 101 |  | 
|---|
| 102 | Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine. | 
|---|
| 103 | The log is set up and connected to the CEGUILogger. | 
|---|
| 104 | After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code. | 
|---|
| 105 | Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically). | 
|---|
| 106 | @param renderWindow | 
|---|
| 107 | Ogre's render window. Without this, the GUI cannot be displayed. | 
|---|
| 108 | @return true if success, otherwise false | 
|---|
| 109 | */ | 
|---|
| 110 | GUIManager::GUIManager(Ogre::RenderWindow* renderWindow, const std::pair<int, int>& mousePosition, bool bFullScreen) | 
|---|
| 111 | : renderWindow_(renderWindow) | 
|---|
| 112 | , resourceProvider_(0) | 
|---|
| 113 | , camera_(NULL) | 
|---|
| 114 | , bShowIngameGUI_(false) | 
|---|
| 115 | { | 
|---|
| 116 | using namespace CEGUI; | 
|---|
| 117 |  | 
|---|
| 118 | COUT(3) << "Initialising CEGUI." << std::endl; | 
|---|
| 119 |  | 
|---|
| 120 | // Note: No SceneManager specified yet | 
|---|
| 121 | guiRenderer_.reset(new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, false, 3000)); | 
|---|
| 122 | resourceProvider_ = guiRenderer_->createResourceProvider(); | 
|---|
| 123 | resourceProvider_->setDefaultResourceGroup("GUI"); | 
|---|
| 124 |  | 
|---|
| 125 | // setup scripting | 
|---|
| 126 | luaState_.reset(new LuaState()); | 
|---|
| 127 | rootFileInfo_ = Resource::getInfo("InitialiseGUI.lua"); | 
|---|
| 128 | // This is necessary to ensure that input events also use the right resource info when triggering lua functions | 
|---|
| 129 | luaState_->setDefaultResourceInfo(this->rootFileInfo_); | 
|---|
| 130 | scriptModule_.reset(new LuaScriptModule(luaState_->getInternalLuaState())); | 
|---|
| 131 |  | 
|---|
| 132 | // Create our own logger to specify the filepath | 
|---|
| 133 | std::auto_ptr<CEGUILogger> ceguiLogger(new CEGUILogger()); | 
|---|
| 134 | ceguiLogger->setLogFilename(PathConfig::getLogPathString() + "cegui.log"); | 
|---|
| 135 | // set the log level according to ours (translate by subtracting 1) | 
|---|
| 136 | ceguiLogger->setLoggingLevel( | 
|---|
| 137 | static_cast<LoggingLevel>(OutputHandler::getInstance().getSoftDebugLevel("logFile") - 1)); | 
|---|
| 138 | this->ceguiLogger_ = ceguiLogger.release(); | 
|---|
| 139 |  | 
|---|
| 140 | // create the CEGUI system singleton | 
|---|
| 141 | guiSystem_.reset(new System(guiRenderer_.get(), resourceProvider_, 0, scriptModule_.get())); | 
|---|
| 142 |  | 
|---|
| 143 | // Initialise the basic Lua code | 
|---|
| 144 | this->luaState_->doFile("InitialiseGUI.lua"); | 
|---|
| 145 |  | 
|---|
| 146 | // Align CEGUI mouse with OIS mouse | 
|---|
| 147 | guiSystem_->injectMousePosition(mousePosition.first, mousePosition.second); | 
|---|
| 148 |  | 
|---|
| 149 | // Hide the mouse cursor unless playing in full screen mode | 
|---|
| 150 | if (!bFullScreen) | 
|---|
| 151 | CEGUI::MouseCursor::getSingleton().hide(); | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | /** | 
|---|
| 155 | @brief | 
|---|
| 156 | Basically shuts down CEGUI (member smart pointers) but first unloads our Tolua modules. | 
|---|
| 157 | */ | 
|---|
| 158 | GUIManager::~GUIManager() | 
|---|
| 159 | { | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | /** | 
|---|
| 163 | @brief | 
|---|
| 164 | used to tick the GUI | 
|---|
| 165 | @param time | 
|---|
| 166 | clock which provides time value for the GUI System | 
|---|
| 167 |  | 
|---|
| 168 | Ticking the GUI means updating it with a certain regularity. | 
|---|
| 169 | The elapsed time since the last call is given in the time value provided by the clock. | 
|---|
| 170 | This time value is then used to provide a fluent animation of the GUI. | 
|---|
| 171 | */ | 
|---|
| 172 | void GUIManager::preUpdate(const Clock& time) | 
|---|
| 173 | { | 
|---|
| 174 | assert(guiSystem_); | 
|---|
| 175 | guiSystem_->injectTimePulse(time.getDeltaTime()); | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | /** | 
|---|
| 179 | @brief | 
|---|
| 180 | Tells the GUIManager which SceneManager to use | 
|---|
| 181 | @param camera | 
|---|
| 182 | The current camera on which the GUI should be displayed on. | 
|---|
| 183 |  | 
|---|
| 184 | In fact the GUIManager needs the SceneManager and not the Camera to display the GUI. | 
|---|
| 185 | This means the GUI is not bound to a camera but rather to the SceneManager. | 
|---|
| 186 | Hiding the GUI when needed can therefore not be resolved by just NOT setting the current camera. | 
|---|
| 187 | */ | 
|---|
| 188 | void GUIManager::setCamera(Ogre::Camera* camera) | 
|---|
| 189 | { | 
|---|
| 190 | this->camera_ = camera; | 
|---|
| 191 | if (camera == NULL) | 
|---|
| 192 | this->guiRenderer_->setTargetSceneManager(0); | 
|---|
| 193 | else | 
|---|
| 194 | this->guiRenderer_->setTargetSceneManager(camera->getSceneManager()); | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | /** | 
|---|
| 198 | @brief | 
|---|
| 199 | Executes Lua code | 
|---|
| 200 | @param str | 
|---|
| 201 | reference to string object holding the Lua code which is to be executed | 
|---|
| 202 |  | 
|---|
| 203 | This function gives total access to the GUI. You can execute ANY Lua code here. | 
|---|
| 204 | */ | 
|---|
| 205 | void GUIManager::executeCode(const std::string& str) | 
|---|
| 206 | { | 
|---|
| 207 | this->luaState_->doString(str, rootFileInfo_); | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | /** | 
|---|
| 211 | @brief | 
|---|
| 212 | Displays specified GUI on screen | 
|---|
| 213 | @param name | 
|---|
| 214 | The name of the GUI | 
|---|
| 215 |  | 
|---|
| 216 | The function executes the Lua function with the same name in case the GUIManager is ready. | 
|---|
| 217 | For more details check out loadGUI_2.lua where the function presides. | 
|---|
| 218 | */ | 
|---|
| 219 | /*static*/ void GUIManager::showGUI(const std::string& name, bool hidePrevious, bool showCursor) | 
|---|
| 220 | { | 
|---|
| 221 | GUIManager::getInstance().executeCode("showGUI(\"" + name + "\", " + multi_cast<std::string>(hidePrevious) + ", " + multi_cast<std::string>(showCursor) + ")"); | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 | /** | 
|---|
| 225 | @brief | 
|---|
| 226 | Hack-ish. Needed for GUIOverlay. | 
|---|
| 227 | */ | 
|---|
| 228 | void GUIManager::showGUIExtra(const std::string& name, const std::string& ptr, bool hidePrevious, bool showCursor) | 
|---|
| 229 | { | 
|---|
| 230 | this->executeCode("showGUI(\"" + name + "\", " + multi_cast<std::string>(hidePrevious) + ", " + multi_cast<std::string>(showCursor) + ", " + ptr + ")"); | 
|---|
| 231 | } | 
|---|
| 232 |  | 
|---|
| 233 | /** | 
|---|
| 234 | @brief | 
|---|
| 235 | Hides specified GUI. | 
|---|
| 236 | @param name | 
|---|
| 237 | The name of the GUI. | 
|---|
| 238 | */ | 
|---|
| 239 | /*static*/ void GUIManager::hideGUI(const std::string& name) | 
|---|
| 240 | { | 
|---|
| 241 | GUIManager::getInstance().executeCode("hideGUI(\"" + name + "\")"); | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | void GUIManager::keyESC() | 
|---|
| 245 | { | 
|---|
| 246 | this->executeCode("keyESC()"); | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | void GUIManager::setBackground(const std::string& name) | 
|---|
| 250 | { | 
|---|
| 251 | this->executeCode("setBackground(\"" + name + "\")"); | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | void GUIManager::keyPressed(const KeyEvent& evt) | 
|---|
| 255 | { | 
|---|
| 256 | guiSystem_->injectKeyDown(evt.getKeyCode()); | 
|---|
| 257 | guiSystem_->injectChar(evt.getText()); | 
|---|
| 258 | } | 
|---|
| 259 | void GUIManager::keyReleased(const KeyEvent& evt) | 
|---|
| 260 | { | 
|---|
| 261 | guiSystem_->injectKeyUp(evt.getKeyCode()); | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | /** | 
|---|
| 265 | @brief | 
|---|
| 266 | Function receiving a mouse button pressed event. | 
|---|
| 267 | @param id | 
|---|
| 268 | ID of the mouse button which got pressed | 
|---|
| 269 |  | 
|---|
| 270 | This function is inherited by MouseHandler and injects the event into CEGUI. | 
|---|
| 271 | It is for CEGUI to process the event. | 
|---|
| 272 | */ | 
|---|
| 273 | void GUIManager::buttonPressed(MouseButtonCode::ByEnum id) | 
|---|
| 274 | { | 
|---|
| 275 | try | 
|---|
| 276 | { | 
|---|
| 277 | guiSystem_->injectMouseButtonDown(convertButton(id)); | 
|---|
| 278 | } | 
|---|
| 279 | catch (CEGUI::ScriptException& ex) | 
|---|
| 280 | { | 
|---|
| 281 | // We simply ignore the exception and proceed | 
|---|
| 282 | COUT(1) << ex.getMessage() << std::endl; | 
|---|
| 283 | } | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | /** | 
|---|
| 287 | @brief | 
|---|
| 288 | Function receiving a mouse button released event. | 
|---|
| 289 | @param id | 
|---|
| 290 | ID of the mouse button which got released | 
|---|
| 291 |  | 
|---|
| 292 | This function is inherited by MouseHandler and injects the event into CEGUI. | 
|---|
| 293 | It is for CEGUI to process the event. | 
|---|
| 294 | */ | 
|---|
| 295 | void GUIManager::buttonReleased(MouseButtonCode::ByEnum id) | 
|---|
| 296 | { | 
|---|
| 297 | try | 
|---|
| 298 | { | 
|---|
| 299 | guiSystem_->injectMouseButtonUp(convertButton(id)); | 
|---|
| 300 | } | 
|---|
| 301 | catch (CEGUI::ScriptException& ex) | 
|---|
| 302 | { | 
|---|
| 303 | // We simply ignore the exception and proceed | 
|---|
| 304 | COUT(1) << ex.getMessage() << std::endl; | 
|---|
| 305 | } | 
|---|
| 306 | } | 
|---|
| 307 |  | 
|---|
| 308 | void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) | 
|---|
| 309 | { | 
|---|
| 310 | guiSystem_->injectMousePosition(static_cast<float>(abs.x), static_cast<float>(abs.y)); | 
|---|
| 311 | } | 
|---|
| 312 | void GUIManager::mouseScrolled(int abs, int rel) | 
|---|
| 313 | { | 
|---|
| 314 | guiSystem_->injectMouseWheelChange(static_cast<float>(rel)); | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | /** | 
|---|
| 318 | @brief | 
|---|
| 319 | converts mouse event code to CEGUI event code | 
|---|
| 320 | @param button | 
|---|
| 321 | code of the mouse button as we use it in Orxonox | 
|---|
| 322 | @return | 
|---|
| 323 | code of the mouse button as it is used by CEGUI | 
|---|
| 324 |  | 
|---|
| 325 | Simple conversion from mouse event code in Orxonox to the one used in CEGUI. | 
|---|
| 326 | */ | 
|---|
| 327 | static inline CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button) | 
|---|
| 328 | { | 
|---|
| 329 | switch (button) | 
|---|
| 330 | { | 
|---|
| 331 | case MouseButtonCode::Left: | 
|---|
| 332 | return CEGUI::LeftButton; | 
|---|
| 333 |  | 
|---|
| 334 | case MouseButtonCode::Right: | 
|---|
| 335 | return CEGUI::RightButton; | 
|---|
| 336 |  | 
|---|
| 337 | case MouseButtonCode::Middle: | 
|---|
| 338 | return CEGUI::MiddleButton; | 
|---|
| 339 |  | 
|---|
| 340 | case MouseButtonCode::Button3: | 
|---|
| 341 | return CEGUI::X1Button; | 
|---|
| 342 |  | 
|---|
| 343 | case MouseButtonCode::Button4: | 
|---|
| 344 | return CEGUI::X2Button; | 
|---|
| 345 |  | 
|---|
| 346 | default: | 
|---|
| 347 | return CEGUI::NoButton; | 
|---|
| 348 | } | 
|---|
| 349 | } | 
|---|
| 350 |  | 
|---|
| 351 | void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function) | 
|---|
| 352 | { | 
|---|
| 353 | window->subscribeScriptedEvent(event, function); | 
|---|
| 354 | } | 
|---|
| 355 | } | 
|---|