[Orxonox-commit 2019] r6736 - code/branches/gamestates2/src/libraries/core
rgrieder at orxonox.net
rgrieder at orxonox.net
Fri Apr 16 12:11:58 CEST 2010
Author: rgrieder
Date: 2010-04-16 12:11:57 +0200 (Fri, 16 Apr 2010)
New Revision: 6736
Modified:
code/branches/gamestates2/src/libraries/core/GUIManager.cc
code/branches/gamestates2/src/libraries/core/GUIManager.h
Log:
All CEGUI calls that can execute Lua code should be protected against ScriptException.
Although this is bad design (can leave the game in an undefined state), the game doesn't terminate just because of simple script problem.
When the GUI code is more mature, we should probably remove this again.
Modified: code/branches/gamestates2/src/libraries/core/GUIManager.cc
===================================================================
--- code/branches/gamestates2/src/libraries/core/GUIManager.cc 2010-04-16 10:06:45 UTC (rev 6735)
+++ code/branches/gamestates2/src/libraries/core/GUIManager.cc 2010-04-16 10:11:57 UTC (rev 6736)
@@ -29,6 +29,7 @@
#include "GUIManager.h"
+#include <boost/bind.hpp>
#include <memory>
extern "C" {
#include <lua.h>
@@ -174,7 +175,7 @@
void GUIManager::preUpdate(const Clock& time)
{
assert(guiSystem_);
- guiSystem_->injectTimePulse(time.getDeltaTime());
+ this->protectedCall(boost::bind(&CEGUI::System::injectTimePulse, _1, time.getDeltaTime()));
}
/**
@@ -297,12 +298,13 @@
void GUIManager::keyPressed(const KeyEvent& evt)
{
- guiSystem_->injectKeyDown(evt.getKeyCode());
- guiSystem_->injectChar(evt.getText());
+ this->protectedCall(boost::bind(&CEGUI::System::injectKeyDown, _1, evt.getKeyCode()));
+ this->protectedCall(boost::bind(&CEGUI::System::injectChar, _1, evt.getText()));
}
+
void GUIManager::keyReleased(const KeyEvent& evt)
{
- guiSystem_->injectKeyUp(evt.getKeyCode());
+ this->protectedCall(boost::bind(&CEGUI::System::injectKeyUp, _1, evt.getKeyCode()));
}
/**
@@ -316,15 +318,7 @@
*/
void GUIManager::buttonPressed(MouseButtonCode::ByEnum id)
{
- try
- {
- guiSystem_->injectMouseButtonDown(convertButton(id));
- }
- catch (CEGUI::ScriptException& ex)
- {
- // We simply ignore the exception and proceed
- COUT(1) << ex.getMessage() << std::endl;
- }
+ this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonDown, _1, convertButton(id)));
}
/**
@@ -338,24 +332,17 @@
*/
void GUIManager::buttonReleased(MouseButtonCode::ByEnum id)
{
- try
- {
- guiSystem_->injectMouseButtonUp(convertButton(id));
- }
- catch (CEGUI::ScriptException& ex)
- {
- // We simply ignore the exception and proceed
- COUT(1) << ex.getMessage() << std::endl;
- }
+ this->protectedCall(boost::bind(&CEGUI::System::injectMouseButtonUp, _1, convertButton(id)));
}
void GUIManager::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
{
- guiSystem_->injectMousePosition(static_cast<float>(abs.x), static_cast<float>(abs.y));
+ this->protectedCall(boost::bind(&CEGUI::System::injectMousePosition, _1, (float)abs.x, (float)abs.y));
}
+
void GUIManager::mouseScrolled(int abs, int rel)
{
- guiSystem_->injectMouseWheelChange(static_cast<float>(rel));
+ this->protectedCall(boost::bind(&CEGUI::System::injectMouseWheelChange, _1, (float)rel));
}
/**
@@ -392,6 +379,36 @@
}
}
+ /** Executes a CEGUI function normally, but catches CEGUI::ScriptException.
+ When a ScriptException occurs, the error message will be displayed and
+ the program carries on.
+ @remarks
+ The exception behaviour may pose problems if the code is not written
+ exception-safe (and you can forget about that in Lua). The program might
+ be left in an undefined state. But otherwise one script error would
+ terminate the whole program...
+ @note
+ Your life gets easier if you use boost::bind to create the object/function.
+ @param function
+ Any callable object/function that takes this->guiSystem_ as its only parameter.
+ @return
+ True if input was handled, false otherwise. A caught exception yields true.
+ */
+ template <typename FunctionType>
+ bool GUIManager::protectedCall(FunctionType function)
+ {
+ try
+ {
+ return function(this->guiSystem_);
+ }
+ catch (CEGUI::ScriptException& ex)
+ {
+ // Display the error and proceed. See @remarks why this can be dangerous.
+ COUT(1) << ex.getMessage() << std::endl;
+ return true;
+ }
+ }
+
void GUIManager::subscribeEventHelper(CEGUI::Window* window, const std::string& event, const std::string& function)
{
window->subscribeScriptedEvent(event, function);
Modified: code/branches/gamestates2/src/libraries/core/GUIManager.h
===================================================================
--- code/branches/gamestates2/src/libraries/core/GUIManager.h 2010-04-16 10:06:45 UTC (rev 6735)
+++ code/branches/gamestates2/src/libraries/core/GUIManager.h 2010-04-16 10:11:57 UTC (rev 6736)
@@ -100,6 +100,9 @@
void executeCode(const std::string& str);
+ template <typename FunctionType>
+ bool protectedCall(FunctionType function);
+
// keyHandler functions
void keyPressed (const KeyEvent& evt);
void keyReleased(const KeyEvent& evt);
More information about the Orxonox-commit
mailing list