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 | #include <boost/bind.hpp> |
---|
34 | #include <OgreRenderQueue.h> |
---|
35 | #include <OgreRenderWindow.h> |
---|
36 | |
---|
37 | #include <CEGUIDefaultLogger.h> |
---|
38 | #include <CEGUIExceptions.h> |
---|
39 | #include <CEGUIInputEvent.h> |
---|
40 | #include <CEGUIMouseCursor.h> |
---|
41 | #include <CEGUIResourceProvider.h> |
---|
42 | #include <CEGUISystem.h> |
---|
43 | #include <CEGUIWindow.h> |
---|
44 | #include <CEGUIWindowManager.h> |
---|
45 | #include <elements/CEGUIListbox.h> |
---|
46 | #include <elements/CEGUIListboxItem.h> |
---|
47 | |
---|
48 | #ifdef ORXONOX_OLD_CEGUI |
---|
49 | # include <CEGUILua.h> |
---|
50 | # include <ogreceguirenderer/OgreCEGUIRenderer.h> |
---|
51 | extern "C" { |
---|
52 | # include <lauxlib.h> |
---|
53 | } |
---|
54 | #else |
---|
55 | # include <ScriptingModules/LuaScriptModule/CEGUILua.h> |
---|
56 | # include <RendererModules/Ogre/CEGUIOgreImageCodec.h> |
---|
57 | # include <RendererModules/Ogre/CEGUIOgreRenderer.h> |
---|
58 | # include <RendererModules/Ogre/CEGUIOgreResourceProvider.h> |
---|
59 | # include <OgreCamera.h> |
---|
60 | # include <OgreRenderQueueListener.h> |
---|
61 | # include <OgreRenderSystem.h> |
---|
62 | # include <OgreRoot.h> |
---|
63 | # include <OgreSceneManager.h> |
---|
64 | #endif |
---|
65 | |
---|
66 | #include "util/Clock.h" |
---|
67 | #include "util/Convert.h" |
---|
68 | #include "util/Debug.h" |
---|
69 | #include "util/Exception.h" |
---|
70 | #include "util/OrxAssert.h" |
---|
71 | #include "ConfigValueIncludes.h" |
---|
72 | #include "Core.h" |
---|
73 | #include "CoreIncludes.h" |
---|
74 | #include "Game.h" |
---|
75 | #include "GraphicsManager.h" |
---|
76 | #include "LuaState.h" |
---|
77 | #include "PathConfig.h" |
---|
78 | #include "Resource.h" |
---|
79 | #include "command/ConsoleCommand.h" |
---|
80 | #include "input/InputManager.h" |
---|
81 | #include "input/InputState.h" |
---|
82 | #include "input/KeyBinderManager.h" |
---|
83 | |
---|
84 | namespace orxonox |
---|
85 | { |
---|
86 | static void key_esc() |
---|
87 | { GUIManager::getInstance().keyESC(); } |
---|
88 | SetConsoleCommand("keyESC", &key_esc); |
---|
89 | |
---|
90 | class CEGUILogger : public CEGUI::DefaultLogger |
---|
91 | { |
---|
92 | public: |
---|
93 | void logEvent(const CEGUI::String& message, CEGUI::LoggingLevel level = CEGUI::Standard) |
---|
94 | { |
---|
95 | int orxonoxLevel = CEGUI::Standard; |
---|
96 | switch (level) |
---|
97 | { |
---|
98 | case CEGUI::Errors: orxonoxLevel = 1; break; |
---|
99 | case CEGUI::Warnings: orxonoxLevel = 2; break; |
---|
100 | case CEGUI::Standard: orxonoxLevel = 4; break; |
---|
101 | case CEGUI::Informative: orxonoxLevel = 5; break; |
---|
102 | case CEGUI::Insane: orxonoxLevel = 6; break; |
---|
103 | default: OrxAssert(false, "CEGUI log level out of range, inspect immediately!"); |
---|
104 | } |
---|
105 | OutputHandler::getOutStream(orxonoxLevel) |
---|
106 | << "CEGUI: " << message << std::endl; |
---|
107 | |
---|
108 | CEGUI::DefaultLogger::logEvent(message, level); |
---|
109 | } |
---|
110 | }; |
---|
111 | |
---|
112 | #ifdef ORXONOX_OLD_CEGUI |
---|
113 | /** Class with the same memory layout as CEGUI::LuaScriptModule. <br> |
---|
114 | We need this to fix a problem with an uninitialised member variable |
---|
115 | in CEGUI < 0.7 <br> |
---|
116 | Notice the "public" modifier for the otherwise private variables. |
---|
117 | */ |
---|
118 | class LuaScriptModuleWorkaround : public CEGUI::ScriptModule |
---|
119 | { |
---|
120 | public: |
---|
121 | LuaScriptModuleWorkaround(); |
---|
122 | ~LuaScriptModuleWorkaround(); |
---|
123 | |
---|
124 | public: |
---|
125 | bool d_ownsState; |
---|
126 | lua_State* d_state; |
---|
127 | CEGUI::String d_errFuncName; |
---|
128 | int d_errFuncIndex; |
---|
129 | CEGUI::String d_activeErrFuncName; |
---|
130 | int d_activeErrFuncIndex; |
---|
131 | }; |
---|
132 | #else |
---|
133 | /// RenderQueueListener based class used to hook into the ogre rendering system |
---|
134 | class RQListener : public Ogre::RenderQueueListener |
---|
135 | { |
---|
136 | public: |
---|
137 | /// Callback from Ogre invoked before other stuff in our target queue is rendered |
---|
138 | void renderQueueStarted(Ogre::uint8 id, const Ogre::String& invocation, bool& skipThisQueue) |
---|
139 | { |
---|
140 | if (id == Ogre::RENDER_QUEUE_OVERLAY && invocation.empty()) |
---|
141 | { |
---|
142 | CEGUI::System::getSingleton().renderGUI(); |
---|
143 | |
---|
144 | // Important workaround! (at least required by CEGUI 0.7.5) |
---|
145 | // If we don't reset the scissor test, OGRE will only render overlays |
---|
146 | // in the area where CEGUI last drew, which is usually nothing |
---|
147 | // or a little box where the focused element is. |
---|
148 | Ogre::Root::getSingleton().getRenderSystem()->setScissorTest(false); |
---|
149 | } |
---|
150 | } |
---|
151 | }; |
---|
152 | #endif |
---|
153 | |
---|
154 | static CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button); |
---|
155 | |
---|
156 | GUIManager* GUIManager::singletonPtr_s = 0; |
---|
157 | /*static*/ const std::string GUIManager::defaultScheme_ = "TaharezGreen"; |
---|
158 | |
---|
159 | SetConsoleCommand("showGUI", &GUIManager::showGUI).defaultValue(1, false).defaultValue(2, false); |
---|
160 | SetConsoleCommand("hideGUI", &GUIManager::hideGUI); |
---|
161 | SetConsoleCommand("toggleGUI", &GUIManager::toggleGUI).defaultValue(1, false).defaultValue(2, false); |
---|
162 | |
---|
163 | /** |
---|
164 | @brief |
---|
165 | Constructs the GUIManager by starting up CEGUI |
---|
166 | |
---|
167 | Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine. |
---|
168 | The log is set up and connected to the CEGUILogger. |
---|
169 | After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code. |
---|
170 | Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically). |
---|
171 | @return true if success, otherwise false |
---|
172 | */ |
---|
173 | GUIManager::GUIManager(const std::pair<int, int>& mousePosition) |
---|
174 | : guiRenderer_(NULL) |
---|
175 | , resourceProvider_(NULL) |
---|
176 | #ifndef ORXONOX_OLD_CEGUI |
---|
177 | , rqListener_(NULL) |
---|
178 | , imageCodec_(NULL) |
---|
179 | #endif |
---|
180 | , luaState_(NULL) |
---|
181 | , scriptModule_(NULL) |
---|
182 | , guiSystem_(NULL) |
---|
183 | , ceguiLogger_(NULL) |
---|
184 | , rootWindow_(NULL) |
---|
185 | , hudRootWindow_(NULL) |
---|
186 | , menuRootWindow_(NULL) |
---|
187 | , camera_(NULL) |
---|
188 | , destructionHelper_(this) |
---|
189 | { |
---|
190 | RegisterRootObject(GUIManager); |
---|
191 | this->setConfigValues(); |
---|
192 | |
---|
193 | using namespace CEGUI; |
---|
194 | |
---|
195 | COUT(3) << "Initialising CEGUI." << std::endl; |
---|
196 | |
---|
197 | // Note: No SceneManager specified yet |
---|
198 | #ifdef ORXONOX_OLD_CEGUI |
---|
199 | guiRenderer_ = new OgreCEGUIRenderer(GraphicsManager::getInstance().getRenderWindow(), Ogre::RENDER_QUEUE_OVERLAY, false, 3000); |
---|
200 | resourceProvider_ = guiRenderer_->createResourceProvider(); |
---|
201 | #else |
---|
202 | guiRenderer_ = &OgreRenderer::create(*GraphicsManager::getInstance().getRenderWindow()); |
---|
203 | // We use our own RenderQueueListener so we can draw UNDER overlays |
---|
204 | guiRenderer_->setFrameControlExecutionEnabled(false); |
---|
205 | rqListener_ = new RQListener(); |
---|
206 | resourceProvider_ = &OgreRenderer::createOgreResourceProvider(); |
---|
207 | imageCodec_ = &OgreRenderer::createOgreImageCodec(); |
---|
208 | #endif |
---|
209 | resourceProvider_->setDefaultResourceGroup("General"); |
---|
210 | |
---|
211 | // Setup scripting |
---|
212 | luaState_ = new LuaState(); |
---|
213 | rootFileInfo_ = Resource::getInfo("InitialiseGUI.lua"); |
---|
214 | // This is necessary to ensure that input events also use the right resource info when triggering lua functions |
---|
215 | luaState_->setDefaultResourceInfo(this->rootFileInfo_); |
---|
216 | #ifdef ORXONOX_OLD_CEGUI |
---|
217 | scriptModule_ = new LuaScriptModule(luaState_->getInternalLuaState()); |
---|
218 | // Ugly workaround: older CEGUILua versions don't initialise the member |
---|
219 | // d_activeErrFuncIndex at all. That leads to "error in error handling" |
---|
220 | // problems when a Lua error occurs. |
---|
221 | // We fix this by setting the member manually. |
---|
222 | reinterpret_cast<LuaScriptModuleWorkaround*>(scriptModule_)->d_activeErrFuncIndex = LUA_NOREF; |
---|
223 | luaState_->doString("ORXONOX_OLD_CEGUI = true"); |
---|
224 | #else |
---|
225 | scriptModule_ = &LuaScriptModule::create(luaState_->getInternalLuaState()); |
---|
226 | #endif |
---|
227 | scriptModule_->setDefaultPCallErrorHandler(LuaState::ERROR_HANDLER_NAME); |
---|
228 | |
---|
229 | // Create our own logger to specify the filepath |
---|
230 | std::auto_ptr<CEGUILogger> ceguiLogger(new CEGUILogger()); |
---|
231 | ceguiLogger->setLogFilename(PathConfig::getLogPathString() + "cegui.log"); |
---|
232 | // Set the log level according to ours (translate by subtracting 1) |
---|
233 | ceguiLogger->setLoggingLevel( |
---|
234 | static_cast<LoggingLevel>(OutputHandler::getInstance().getSoftDebugLevel("logFile") - 1)); |
---|
235 | this->ceguiLogger_ = ceguiLogger.release(); |
---|
236 | |
---|
237 | // Create the CEGUI system singleton |
---|
238 | #ifdef ORXONOX_OLD_CEGUI |
---|
239 | guiSystem_ = new System(guiRenderer_, resourceProvider_, 0, scriptModule_); |
---|
240 | // Add functions that have been renamed in newer versions |
---|
241 | luaState_->doString("CEGUI.SchemeManager.create = CEGUI.SchemeManager.loadScheme"); |
---|
242 | luaState_->doString("CEGUI.Window.getUnclippedOuterRect = CEGUI.Window.getUnclippedPixelRect"); |
---|
243 | #else |
---|
244 | guiSystem_ = &System::create(*guiRenderer_, resourceProvider_, 0, imageCodec_, scriptModule_); |
---|
245 | #endif |
---|
246 | |
---|
247 | // Align CEGUI mouse with OIS mouse |
---|
248 | guiSystem_->injectMousePosition((float)mousePosition.first, (float)mousePosition.second); |
---|
249 | |
---|
250 | // Initialise the Lua framework and load the schemes |
---|
251 | this->luaState_->doFile("InitialiseGUI.lua"); |
---|
252 | |
---|
253 | // Create the root nodes |
---|
254 | this->rootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("MenuWidgets/StaticImage", "AbsoluteRootWindow"); |
---|
255 | this->rootWindow_->setProperty("FrameEnabled", "False"); |
---|
256 | this->hudRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "HUDRootWindow"); |
---|
257 | this->menuRootWindow_ = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow", "MenuRootWindow"); |
---|
258 | // And connect them |
---|
259 | CEGUI::System::getSingleton().setGUISheet(this->rootWindow_); |
---|
260 | this->rootWindow_->addChildWindow(this->hudRootWindow_); |
---|
261 | this->rootWindow_->addChildWindow(this->menuRootWindow_); |
---|
262 | |
---|
263 | // No background to start with (sets the alpha value to 0) |
---|
264 | this->setBackgroundImage(""); |
---|
265 | |
---|
266 | // Set up the sheet manager in the Lua framework |
---|
267 | this->luaState_->doFile("SheetManager.lua"); |
---|
268 | } |
---|
269 | |
---|
270 | void GUIManager::destroy() |
---|
271 | { |
---|
272 | using namespace CEGUI; |
---|
273 | |
---|
274 | #ifdef ORXONOX_OLD_CEGUI |
---|
275 | safeObjectDelete(&guiSystem_); |
---|
276 | safeObjectDelete(&guiRenderer_); |
---|
277 | safeObjectDelete(&scriptModule_); |
---|
278 | #else |
---|
279 | System::destroy(); |
---|
280 | OgreRenderer::destroyOgreResourceProvider(*resourceProvider_); |
---|
281 | OgreRenderer::destroyOgreImageCodec(*imageCodec_); |
---|
282 | OgreRenderer::destroy(*guiRenderer_); |
---|
283 | LuaScriptModule::destroy(*scriptModule_); |
---|
284 | safeObjectDelete(&ceguiLogger_); |
---|
285 | safeObjectDelete(&rqListener_); |
---|
286 | #endif |
---|
287 | safeObjectDelete(&luaState_); |
---|
288 | } |
---|
289 | |
---|
290 | void GUIManager::setConfigValues(void) |
---|
291 | { |
---|
292 | SetConfigValue(guiScheme_, GUIManager::defaultScheme_) .description("Changes the current GUI scheme.") .callback(this, &GUIManager::changedGUIScheme); |
---|
293 | } |
---|
294 | |
---|
295 | void GUIManager::changedGUIScheme(void) |
---|
296 | { |
---|
297 | } |
---|
298 | |
---|
299 | /** |
---|
300 | @brief |
---|
301 | used to tick the GUI |
---|
302 | @param time |
---|
303 | clock which provides time value for the GUI System |
---|
304 | |
---|
305 | Ticking the GUI means updating it with a certain regularity. |
---|
306 | The elapsed time since the last call is given in the time value provided by the clock. |
---|
307 | This time value is then used to provide a fluent animation of the GUI. |
---|
308 | */ |
---|
309 | void GUIManager::preUpdate(const Clock& time) |
---|
310 | { |
---|
311 | assert(guiSystem_); |
---|
312 | this->protectedCall(boost::bind(&CEGUI::System::injectTimePulse, _1, time.getDeltaTime())); |
---|
313 | } |
---|
314 | |
---|
315 | /** |
---|
316 | @brief |
---|
317 | Tells the GUIManager which SceneManager to use |
---|
318 | @param camera |
---|
319 | The current camera on which the GUI should be displayed on. |
---|
320 | |
---|
321 | In fact the GUIManager needs the SceneManager and not the Camera to display the GUI. |
---|
322 | This means the GUI is not bound to a camera but rather to the SceneManager. |
---|
323 | Hiding the GUI when needed can therefore not be resolved by just NOT setting the current camera. |
---|
324 | */ |
---|
325 | void GUIManager::setCamera(Ogre::Camera* camera) |
---|
326 | { |
---|
327 | #ifdef ORXONOX_OLD_CEGUI |
---|
328 | if (camera == NULL) |
---|
329 | this->guiRenderer_->setTargetSceneManager(0); |
---|
330 | else |
---|
331 | this->guiRenderer_->setTargetSceneManager(camera->getSceneManager()); |
---|
332 | #else |
---|
333 | if (camera_ != NULL && camera_->getSceneManager() != NULL) |
---|
334 | camera_->getSceneManager()->removeRenderQueueListener(rqListener_); |
---|
335 | if (camera != NULL && camera->getSceneManager() != NULL) |
---|
336 | camera->getSceneManager()->addRenderQueueListener(rqListener_); |
---|
337 | #endif |
---|
338 | this->camera_ = camera; |
---|
339 | } |
---|
340 | |
---|
341 | /** |
---|
342 | @brief |
---|
343 | Executes Lua code |
---|
344 | @param str |
---|
345 | reference to string object holding the Lua code which is to be executed |
---|
346 | */ |
---|
347 | void GUIManager::executeCode(const std::string& str) |
---|
348 | { |
---|
349 | this->luaState_->doString(str, rootFileInfo_); |
---|
350 | } |
---|
351 | |
---|
352 | /** Loads a GUI sheet by Lua script |
---|
353 | @param name |
---|
354 | The name of the GUI (like the script name, but without the extension) |
---|
355 | */ |
---|
356 | void GUIManager::loadGUI(const std::string& name) |
---|
357 | { |
---|
358 | this->executeCode("loadSheet(\"" + name + "\")"); |
---|
359 | } |
---|
360 | |
---|
361 | /** |
---|
362 | @brief |
---|
363 | Displays specified GUI on screen |
---|
364 | @param name |
---|
365 | The name of the GUI |
---|
366 | @param bHidePrevious |
---|
367 | If true all displayed GUIs on the stack, that are below this GUI are hidden. |
---|
368 | @param bNoInput |
---|
369 | If true the GUI is transparent to input. |
---|
370 | |
---|
371 | The function executes the Lua function with the same name in case the GUIManager is ready. |
---|
372 | */ |
---|
373 | /*static*/ void GUIManager::showGUI(const std::string& name, bool bHidePrevious, bool bNoInput) |
---|
374 | { |
---|
375 | GUIManager::getInstance().executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ")"); |
---|
376 | } |
---|
377 | |
---|
378 | /** |
---|
379 | @brief |
---|
380 | Hack-ish. Needed for GUIOverlay. |
---|
381 | */ |
---|
382 | void GUIManager::showGUIExtra(const std::string& name, const std::string& ptr, bool bHidePrevious, bool bNoInput) |
---|
383 | { |
---|
384 | this->executeCode("showMenuSheet(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ", " + ptr + ")"); |
---|
385 | } |
---|
386 | |
---|
387 | /** |
---|
388 | @brief |
---|
389 | Hides specified GUI. |
---|
390 | @param name |
---|
391 | The name of the GUI. |
---|
392 | */ |
---|
393 | /*static*/ void GUIManager::hideGUI(const std::string& name) |
---|
394 | { |
---|
395 | GUIManager::getInstance().executeCode("hideMenuSheet(\"" + name + "\")"); |
---|
396 | } |
---|
397 | |
---|
398 | /** |
---|
399 | @brief |
---|
400 | Toggles specified GUI. |
---|
401 | If the GUI with the input name is already shown and on the top, it is hidden, else it is shown. |
---|
402 | */ |
---|
403 | /*static*/ void GUIManager::toggleGUI(const std::string& name, bool bHidePrevious, bool bNoInput) |
---|
404 | { |
---|
405 | GUIManager::getInstance().executeCode("getGUIFirstActive(\"" + name + "\", " + multi_cast<std::string>(bHidePrevious) + ", " + multi_cast<std::string>(bNoInput) + ")"); |
---|
406 | } |
---|
407 | |
---|
408 | /** |
---|
409 | @brief |
---|
410 | Helper method to toggle a specified GUI. |
---|
411 | Is called by lua. |
---|
412 | */ |
---|
413 | void GUIManager::toggleGUIHelper(const std::string& name, bool bHidePrevious, bool bNoInput, bool show) |
---|
414 | { |
---|
415 | if(show) |
---|
416 | GUIManager::showGUI(name, bHidePrevious, bNoInput); |
---|
417 | else |
---|
418 | GUIManager::hideGUI(name); |
---|
419 | } |
---|
420 | |
---|
421 | const std::string& GUIManager::createInputState(const std::string& name, tribool showCursor, tribool useKeyboard, bool bBlockJoyStick) |
---|
422 | { |
---|
423 | InputState* state = InputManager::getInstance().createInputState(name); |
---|
424 | if (!state) |
---|
425 | return BLANKSTRING; |
---|
426 | |
---|
427 | /* Table that maps isFullScreen() and showCursor to mouseExclusive |
---|
428 | isFullscreen / showCursor | True | False | Dontcare |
---|
429 | ---------------------------------------------------- |
---|
430 | true | True | True | Dontcare |
---|
431 | ---------------------------------------------------- |
---|
432 | false | False | True | Dontcare |
---|
433 | */ |
---|
434 | |
---|
435 | #ifdef ORXONOX_PLATFORM_APPLE |
---|
436 | // There is no non exclusive mode on OS X yet |
---|
437 | state->setMouseExclusive(true); |
---|
438 | #else |
---|
439 | if (showCursor == dontcare) |
---|
440 | state->setMouseExclusive(dontcare); |
---|
441 | else if (GraphicsManager::getInstance().isFullScreen() || showCursor == false) |
---|
442 | state->setMouseExclusive(true); |
---|
443 | else |
---|
444 | state->setMouseExclusive(false); |
---|
445 | #endif |
---|
446 | |
---|
447 | if (showCursor == true) |
---|
448 | state->setMouseHandler(this); |
---|
449 | else if (showCursor == false) |
---|
450 | state->setMouseHandler(&InputHandler::EMPTY); |
---|
451 | |
---|
452 | if (useKeyboard == true) |
---|
453 | state->setKeyHandler(this); |
---|
454 | else if (useKeyboard == false) |
---|
455 | state->setKeyHandler(&InputHandler::EMPTY); |
---|
456 | |
---|
457 | if (bBlockJoyStick) |
---|
458 | state->setJoyStickHandler(&InputHandler::EMPTY); |
---|
459 | |
---|
460 | return state->getName(); |
---|
461 | } |
---|
462 | |
---|
463 | void GUIManager::keyESC() |
---|
464 | { |
---|
465 | this->executeCode("keyESC()"); |
---|
466 | } |
---|
467 | |
---|
468 | void GUIManager::setBackgroundImage(const std::string& imageSet, const std::string imageName) |
---|
469 | { |
---|
470 | if (imageSet.empty() || imageName.empty()) |
---|
471 | this->setBackgroundImage(""); |
---|
472 | else |
---|
473 | this->setBackgroundImage("set: " + imageSet + " image: " + imageName); |
---|
474 | } |
---|
475 | |
---|
476 | void GUIManager::setBackgroundImage(const std::string& image) |
---|
477 | { |
---|
478 | if (image.empty()) |
---|
479 | this->rootWindow_->setProperty("Alpha", "0.0"); |
---|
480 | else |
---|
481 | this->rootWindow_->setProperty("Alpha", "1.0"); |
---|
482 | this->rootWindow_->setProperty("Image", image); |
---|
483 | } |
---|
484 | |
---|
485 | void GUIManager::buttonPressed(const KeyEvent& evt) |
---|
486 | { |
---|
487 | this->protectedCall(boost::bind(&CEGUI::System::injectKeyDown, _1, evt.getKeyCode())); |
---|
488 | this->protectedCall(boost::bind(&CEGUI::System::injectChar, _1, evt.getText())); |
---|
489 | } |
---|
490 | |
---|
491 | void GUIManager::buttonReleased(const KeyEvent& evt) |
---|
492 | { |
---|
493 | this->protectedCall(boost::bind(&CEGUI::System::injectKeyUp, _1, evt.getKeyCode())); |
---|
494 | } |
---|
495 | |
---|
496 | /** |
---|
497 | @brief |
---|
498 | Function receiving a mouse button pressed event. |
---|
499 | @param id |
---|
500 | ID of the mouse button which got pressed |
---|
501 | |
---|
502 | This function is inherited by MouseHandler and injects the event into CEGUI. |
---|
503 | It is for CEGUI to process the event. |
---|
504 | */ |
---|
505 | void GUIManager::buttonPressed(MouseButtonCode::ByEnum id) |
---|
506 | { |
---|
507 | this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonDown, _1, convertButton(id))); |
---|
508 | } |
---|
509 | |
---|
510 | /** |
---|
511 | @brief |
---|
512 | Function receiving a mouse button released event. |
---|
513 | @param id |
---|
514 | ID of the mouse button which got released |
---|
515 | |
---|
516 | This function is inherited by MouseHandler and injects the event into CEGUI. |
---|
517 | It is for CEGUI to process the event. |
---|
518 | */ |
---|
519 | void GUIManager::buttonReleased(MouseButtonCode::ByEnum id) |
---|
520 | { |
---|
521 | this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonUp, _1, convertButton(id))); |
---|
522 | } |
---|
523 | |
---|
524 | void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
525 | { |
---|
526 | this->protectedCall(boost::bind(&CEGUI::System::injectMousePosition, _1, (float)abs.x, (float)abs.y)); |
---|
527 | } |
---|
528 | |
---|
529 | void GUIManager::mouseScrolled(int abs, int rel) |
---|
530 | { |
---|
531 | this->protectedCall(boost::bind(&CEGUI::System::injectMouseWheelChange, _1, (float)rel)); |
---|
532 | } |
---|
533 | |
---|
534 | /** |
---|
535 | @brief Indicates that the mouse left the application's window. |
---|
536 | */ |
---|
537 | void GUIManager::mouseLeft() |
---|
538 | { |
---|
539 | this->protectedCall(boost::bind(&CEGUI::System::injectMouseLeaves, _1)); |
---|
540 | } |
---|
541 | |
---|
542 | /** |
---|
543 | @brief |
---|
544 | converts mouse event code to CEGUI event code |
---|
545 | @param button |
---|
546 | code of the mouse button as we use it in Orxonox |
---|
547 | @return |
---|
548 | code of the mouse button as it is used by CEGUI |
---|
549 | |
---|
550 | Simple conversion from mouse event code in Orxonox to the one used in CEGUI. |
---|
551 | */ |
---|
552 | static inline CEGUI::MouseButton convertButton(MouseButtonCode::ByEnum button) |
---|
553 | { |
---|
554 | switch (button) |
---|
555 | { |
---|
556 | case MouseButtonCode::Left: |
---|
557 | return CEGUI::LeftButton; |
---|
558 | |
---|
559 | case MouseButtonCode::Right: |
---|
560 | return CEGUI::RightButton; |
---|
561 | |
---|
562 | case MouseButtonCode::Middle: |
---|
563 | return CEGUI::MiddleButton; |
---|
564 | |
---|
565 | case MouseButtonCode::Button3: |
---|
566 | return CEGUI::X1Button; |
---|
567 | |
---|
568 | case MouseButtonCode::Button4: |
---|
569 | return CEGUI::X2Button; |
---|
570 | |
---|
571 | default: |
---|
572 | return CEGUI::NoButton; |
---|
573 | } |
---|
574 | } |
---|
575 | |
---|
576 | /** Executes a CEGUI function normally, but catches CEGUI::ScriptException. |
---|
577 | When a ScriptException occurs, the error message will be displayed and |
---|
578 | the program carries on. |
---|
579 | @remarks |
---|
580 | The exception behaviour may pose problems if the code is not written |
---|
581 | exception-safe (and you can forget about that in Lua). The program might |
---|
582 | be left in an undefined state. But otherwise one script error would |
---|
583 | terminate the whole program... |
---|
584 | @note |
---|
585 | Your life gets easier if you use boost::bind to create the object/function. |
---|
586 | @param function |
---|
587 | Any callable object/function that takes this->guiSystem_ as its only parameter. |
---|
588 | @return |
---|
589 | True if input was handled, false otherwise. A caught exception yields true. |
---|
590 | */ |
---|
591 | template <typename FunctionType> |
---|
592 | bool GUIManager::protectedCall(FunctionType function) |
---|
593 | { |
---|
594 | try |
---|
595 | { |
---|
596 | return function(this->guiSystem_); |
---|
597 | } |
---|
598 | catch (CEGUI::ScriptException& ex) |
---|
599 | { |
---|
600 | // Display the error and proceed. See @remarks why this can be dangerous. |
---|
601 | COUT(1) << ex.getMessage() << std::endl; |
---|
602 | return true; |
---|
603 | } |
---|
604 | } |
---|
605 | |
---|
606 | /** |
---|
607 | @brief |
---|
608 | Subscribe the input function to the input event for the input window. |
---|
609 | This is a helper to be used in lua, because subscribeScriptedEvent() doesn't work in lua. |
---|
610 | @param window |
---|
611 | The window for which the event is subscribed. |
---|
612 | @param event |
---|
613 | The type of event to which we subscribe. |
---|
614 | @param function |
---|
615 | The function that is called when the event occurs. |
---|
616 | */ |
---|
617 | void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function) |
---|
618 | { |
---|
619 | window->subscribeScriptedEvent(event, function); |
---|
620 | } |
---|
621 | |
---|
622 | /** |
---|
623 | @brief |
---|
624 | Set the input tooltip text for the input ListboxItem. |
---|
625 | @param item |
---|
626 | The ListboxItem for which the tooltip should be set. |
---|
627 | @param tooltip |
---|
628 | The tooltip text that should be set. |
---|
629 | */ |
---|
630 | void GUIManager::setTooltipTextHelper(CEGUI::ListboxItem* item, const std::string& tooltip) |
---|
631 | { |
---|
632 | item->setTooltipText(tooltip); |
---|
633 | } |
---|
634 | |
---|
635 | /** |
---|
636 | @brief |
---|
637 | Set whether the tooltips for the input Listbox are enabled. |
---|
638 | @param listbox |
---|
639 | The Listbox for which to enable (or disable) tooltips. |
---|
640 | @param enabled |
---|
641 | Whether to enable or disable the tooltips. |
---|
642 | */ |
---|
643 | void GUIManager::setItemTooltipsEnabledHelper(CEGUI::Listbox* listbox, bool enabled) |
---|
644 | { |
---|
645 | listbox->setItemTooltipsEnabled(enabled); |
---|
646 | } |
---|
647 | |
---|
648 | /** Helper method to get the developer's mode without having to export Core.h. |
---|
649 | @see Core::inDevMode |
---|
650 | */ |
---|
651 | /*static*/ bool GUIManager::inDevMode() |
---|
652 | { |
---|
653 | return Core::getInstance().inDevMode(); |
---|
654 | } |
---|
655 | |
---|
656 | /** |
---|
657 | @brief Callback of window event listener, called if the window is resized. Sets the display size of CEGUI. |
---|
658 | */ |
---|
659 | void GUIManager::windowResized(unsigned int newWidth, unsigned int newHeight) |
---|
660 | { |
---|
661 | this->guiRenderer_->setDisplaySize(CEGUI::Size((float)newWidth, (float)newHeight)); |
---|
662 | } |
---|
663 | |
---|
664 | /** |
---|
665 | @brief Notify CEGUI if the windows loses the focus (stops highlighting of menu items, etc). |
---|
666 | */ |
---|
667 | void GUIManager::windowFocusChanged(bool bFocus) |
---|
668 | { |
---|
669 | if (!bFocus) |
---|
670 | this->mouseLeft(); |
---|
671 | } |
---|
672 | |
---|
673 | } |
---|