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