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