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