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