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