[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 |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | @file |
---|
[1653] | 31 | @brief |
---|
| 32 | Implementation of the GUIManager class. |
---|
[1638] | 33 | */ |
---|
| 34 | |
---|
| 35 | #include "OrxonoxStableHeaders.h" |
---|
| 36 | #include "GUIManager.h" |
---|
| 37 | |
---|
[2850] | 38 | #include <boost/filesystem/path.hpp> |
---|
[1638] | 39 | #include <OgreRenderWindow.h> |
---|
| 40 | #include <CEGUI.h> |
---|
[2710] | 41 | #include <CEGUIDefaultLogger.h> |
---|
| 42 | #include <ogreceguirenderer/OgreCEGUIRenderer.h> |
---|
| 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 | |
---|
[1764] | 50 | #include "util/Exception.h" |
---|
[1638] | 51 | #include "core/ConsoleCommand.h" |
---|
| 52 | #include "core/Core.h" |
---|
[2850] | 53 | #include "core/Clock.h" |
---|
[2710] | 54 | #include "ToluaBindCore.h" |
---|
| 55 | #include "ToluaBindOrxonox.h" |
---|
[1638] | 56 | |
---|
[2710] | 57 | extern "C" { |
---|
| 58 | #include <lua.h> |
---|
| 59 | } |
---|
[1638] | 60 | |
---|
| 61 | namespace orxonox |
---|
| 62 | { |
---|
[1646] | 63 | GUIManager* GUIManager::singletonRef_s = 0; |
---|
| 64 | |
---|
[1638] | 65 | GUIManager::GUIManager() |
---|
[2850] | 66 | : renderWindow_(0) |
---|
[1638] | 67 | , guiRenderer_(0) |
---|
| 68 | , resourceProvider_(0) |
---|
| 69 | , scriptModule_(0) |
---|
| 70 | , guiSystem_(0) |
---|
| 71 | , state_(Uninitialised) |
---|
| 72 | { |
---|
[1646] | 73 | assert(singletonRef_s == 0); |
---|
| 74 | singletonRef_s = this; |
---|
[1638] | 75 | } |
---|
| 76 | |
---|
| 77 | GUIManager::~GUIManager() |
---|
| 78 | { |
---|
[1646] | 79 | if (guiSystem_) |
---|
| 80 | delete guiSystem_; |
---|
| 81 | |
---|
| 82 | if (scriptModule_) |
---|
| 83 | { |
---|
| 84 | // destroy our own tolua interfaces |
---|
[2662] | 85 | lua_pushnil(luaState_); |
---|
| 86 | lua_setglobal(luaState_, "Orxonox"); |
---|
| 87 | lua_pushnil(luaState_); |
---|
| 88 | lua_setglobal(luaState_, "Core"); |
---|
| 89 | delete scriptModule_; |
---|
[1646] | 90 | } |
---|
| 91 | |
---|
| 92 | if (guiRenderer_) |
---|
| 93 | delete guiRenderer_; |
---|
| 94 | |
---|
| 95 | singletonRef_s = 0; |
---|
[1638] | 96 | } |
---|
| 97 | |
---|
[1686] | 98 | bool GUIManager::initialise(Ogre::RenderWindow* renderWindow) |
---|
[1638] | 99 | { |
---|
| 100 | using namespace CEGUI; |
---|
| 101 | if (state_ == Uninitialised) |
---|
| 102 | { |
---|
[1776] | 103 | COUT(3) << "Initialising CEGUI." << std::endl; |
---|
[1638] | 104 | |
---|
| 105 | try |
---|
| 106 | { |
---|
[1686] | 107 | // save the render window |
---|
| 108 | renderWindow_ = renderWindow; |
---|
[1638] | 109 | |
---|
| 110 | // Note: No SceneManager specified yet |
---|
[2808] | 111 | this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, true, 3000); |
---|
[1638] | 112 | this->resourceProvider_ = guiRenderer_->createResourceProvider(); |
---|
| 113 | this->resourceProvider_->setDefaultResourceGroup("GUI"); |
---|
[1776] | 114 | |
---|
[1638] | 115 | // setup scripting |
---|
| 116 | this->scriptModule_ = new LuaScriptModule(); |
---|
[1646] | 117 | this->luaState_ = this->scriptModule_->getLuaState(); |
---|
[1638] | 118 | |
---|
[2710] | 119 | // Create our own logger to specify the filepath |
---|
| 120 | boost::filesystem::path ceguiLogFilepath(Core::getLogPath() / "cegui.log"); |
---|
| 121 | this->ceguiLogger_ = new DefaultLogger(); |
---|
[2759] | 122 | this->ceguiLogger_->setLogFilename(ceguiLogFilepath.string()); |
---|
[2710] | 123 | // set the log level according to ours (translate by subtracting 1) |
---|
| 124 | this->ceguiLogger_->setLoggingLevel( |
---|
| 125 | (LoggingLevel)(Core::getSoftDebugLevel(OutputHandler::LD_Logfile) - 1)); |
---|
| 126 | |
---|
[1638] | 127 | // create the CEGUI system singleton |
---|
| 128 | this->guiSystem_ = new System(this->guiRenderer_, this->resourceProvider_, 0, this->scriptModule_); |
---|
[1776] | 129 | |
---|
[1638] | 130 | // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place |
---|
| 131 | tolua_Core_open(this->scriptModule_->getLuaState()); |
---|
| 132 | tolua_Orxonox_open(this->scriptModule_->getLuaState()); |
---|
| 133 | |
---|
[2850] | 134 | // initialise the basic lua code |
---|
| 135 | loadLuaCode(); |
---|
[1638] | 136 | } |
---|
| 137 | catch (CEGUI::Exception& ex) |
---|
| 138 | { |
---|
[1653] | 139 | #if CEGUI_VERSION_MAJOR == 0 && CEGUI_VERSION_MINOR < 6 |
---|
[1645] | 140 | throw GeneralException(ex.getMessage().c_str()); |
---|
| 141 | #else |
---|
[1638] | 142 | throw GeneralException(ex.getMessage().c_str(), ex.getLine(), |
---|
| 143 | ex.getFileName().c_str(), ex.getName().c_str()); |
---|
[1645] | 144 | #endif |
---|
[1638] | 145 | } |
---|
| 146 | |
---|
| 147 | state_ = Ready; |
---|
| 148 | } |
---|
[1776] | 149 | |
---|
[1638] | 150 | return true; |
---|
| 151 | } |
---|
| 152 | |
---|
[2850] | 153 | void GUIManager::loadLuaCode() |
---|
[2790] | 154 | { |
---|
[1638] | 155 | try |
---|
| 156 | { |
---|
[2841] | 157 | this->scriptModule_->executeScriptFile("loadGUI_2.lua", "GUI"); |
---|
[2840] | 158 | lua_pushfstring(this->scriptModule_->getLuaState(), Core::getMediaPathString().c_str()); |
---|
| 159 | lua_setglobal(this->scriptModule_->getLuaState(), "datapath"); |
---|
[1638] | 160 | } |
---|
| 161 | catch (CEGUI::Exception& ex) |
---|
| 162 | { |
---|
[1645] | 163 | #if CEGUI_VERSION_MINOR < 6 |
---|
| 164 | throw GeneralException(ex.getMessage().c_str()); |
---|
| 165 | #else |
---|
[1638] | 166 | throw GeneralException(ex.getMessage().c_str(), ex.getLine(), |
---|
| 167 | ex.getFileName().c_str(), ex.getName().c_str()); |
---|
[1645] | 168 | #endif |
---|
[1638] | 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
[2850] | 172 | void GUIManager::update(const Clock& time) |
---|
[2808] | 173 | { |
---|
[2850] | 174 | assert(guiSystem_); |
---|
| 175 | guiSystem_->injectTimePulse(time.getDeltaTime()); |
---|
[2808] | 176 | } |
---|
| 177 | |
---|
[2850] | 178 | void GUIManager::executeCode(const std::string& str) |
---|
| 179 | { |
---|
| 180 | this->scriptModule_->executeString(str); |
---|
| 181 | } |
---|
| 182 | |
---|
[2834] | 183 | void GUIManager::setCamera(Ogre::Camera* camera) |
---|
| 184 | { |
---|
[2840] | 185 | this->guiRenderer_->setTargetSceneManager(camera->getSceneManager()); |
---|
[2834] | 186 | } |
---|
| 187 | |
---|
[2853] | 188 | void GUIManager::testOutput(std::string& str) |
---|
| 189 | { |
---|
| 190 | COUT(0) << "*** CEGUI: " << str << std::endl; |
---|
| 191 | } |
---|
| 192 | |
---|
[2850] | 193 | void GUIManager::showGUI(const std::string& name) |
---|
[1638] | 194 | { |
---|
| 195 | if (state_ != Uninitialised) |
---|
| 196 | { |
---|
| 197 | COUT(3) << "Loading GUI " << name << std::endl; |
---|
| 198 | try |
---|
| 199 | { |
---|
[2850] | 200 | this->scriptModule_->executeString(std::string("showGUI(\"") + name + "\")"); |
---|
[1638] | 201 | } |
---|
| 202 | catch (CEGUI::Exception& ex) |
---|
| 203 | { |
---|
| 204 | COUT(2) << "Error while executing lua script. Message:\n" << ex.getMessage() << std::endl; |
---|
| 205 | } |
---|
| 206 | catch (...) |
---|
| 207 | { |
---|
| 208 | COUT(2) << "Could show a menu due to unknown reasons." << std::endl; |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | else |
---|
| 212 | { |
---|
| 213 | COUT(2) << "Warning: GUI Manager not yet initialised, cannot load a GUI" << std::endl; |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | |
---|
[1887] | 217 | void GUIManager::mouseButtonPressed(MouseButtonCode::ByEnum id) |
---|
[1638] | 218 | { |
---|
| 219 | try |
---|
| 220 | { |
---|
| 221 | guiSystem_->injectMouseButtonDown(convertButton(id)); |
---|
| 222 | } |
---|
| 223 | catch (CEGUI::ScriptException& ex) |
---|
| 224 | { |
---|
| 225 | // We simply ignore the exception and proceed |
---|
| 226 | COUT(1) << ex.getMessage() << std::endl; |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
[1887] | 230 | void GUIManager::mouseButtonReleased(MouseButtonCode::ByEnum id) |
---|
[1638] | 231 | { |
---|
| 232 | try |
---|
| 233 | { |
---|
| 234 | guiSystem_->injectMouseButtonUp(convertButton(id)); |
---|
| 235 | } |
---|
| 236 | catch (CEGUI::ScriptException& ex) |
---|
| 237 | { |
---|
| 238 | // We simply ignore the exception and proceed |
---|
| 239 | COUT(1) << ex.getMessage() << std::endl; |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | |
---|
[1887] | 244 | inline CEGUI::MouseButton GUIManager::convertButton(MouseButtonCode::ByEnum button) |
---|
[1638] | 245 | { |
---|
| 246 | switch (button) |
---|
| 247 | { |
---|
[1887] | 248 | case MouseButtonCode::Left: |
---|
[1638] | 249 | return CEGUI::LeftButton; |
---|
| 250 | |
---|
[1887] | 251 | case MouseButtonCode::Right: |
---|
[1638] | 252 | return CEGUI::RightButton; |
---|
| 253 | |
---|
[1887] | 254 | case MouseButtonCode::Middle: |
---|
[1638] | 255 | return CEGUI::MiddleButton; |
---|
| 256 | |
---|
[1887] | 257 | case MouseButtonCode::Button3: |
---|
[1638] | 258 | return CEGUI::X1Button; |
---|
| 259 | |
---|
[1887] | 260 | case MouseButtonCode::Button4: |
---|
[1638] | 261 | return CEGUI::X2Button; |
---|
| 262 | |
---|
| 263 | default: |
---|
| 264 | return CEGUI::NoButton; |
---|
| 265 | } |
---|
| 266 | } |
---|
| 267 | } |
---|