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