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