Fixed crash and double call bug in handle event callbacks attached to Drawable

This commit is contained in:
Robert Osfield 2016-10-06 18:27:23 +01:00
parent 96b180ddad
commit 8de4c76582
3 changed files with 27 additions and 8 deletions

View File

@ -17,6 +17,9 @@
#include <osg/Object>
#include <osg/UserDataContainer>
// forward declare
namespace osgGA { class EventHandler; }
namespace osg {
// forward declare
@ -64,6 +67,9 @@ class OSG_EXPORT Callback : public virtual Object {
virtual DrawableCullCallback* asDrawableCullCallback() { return 0; }
virtual const DrawableCullCallback* asDrawableCullCallback() const { return 0; }
virtual osgGA::EventHandler* asEventHandler() { return 0; }
virtual const osgGA::EventHandler* asEventHandler() const { return 0; }
/** Invoke the callback, first parameter is the Object that the callback is attached to,
* the second parameter, the data, is typically the NodeVisitor that is invoking the callback.
* The run(..) method may be overriden by users directly, or if the user is using one of the old

View File

@ -49,10 +49,13 @@ public:
virtual DrawableEventCallback* asDrawableEventCallback() { return osg::DrawableEventCallback::asDrawableEventCallback(); }
virtual const DrawableEventCallback* asDrawableEventCallback() const { return osg::DrawableEventCallback::asDrawableEventCallback(); }
virtual EventHandler* asEventHandler() { return this; }
virtual const EventHandler* asEventHandler() const { return this; }
virtual bool run(osg::Object* object, osg::Object* data)
{
osg::Node* node = dynamic_cast<osg::Node*>(object);
osg::NodeVisitor* nv = dynamic_cast<osg::NodeVisitor*>(data);
osg::Node* node = object->asNode();
osg::NodeVisitor* nv = data->asNodeVisitor();
operator()(node, nv);
return true;
}

View File

@ -27,6 +27,7 @@
#include <osg/ScriptEngine>
#include <osgGA/GUIEventAdapter>
#include <osgGA/GUIEventHandler>
#include <osgGA/GUIActionAdapter>
#include <osgGA/EventQueue>
@ -82,6 +83,13 @@ class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor
{
osg::Callback* callback = drawable.getEventCallback();
if (callback)
{
osgGA::EventHandler* eh = callback->asEventHandler();
if (eh)
{
callback->run(&drawable,this);
}
else
{
osg::DrawableEventCallback* drawable_callback = callback->asDrawableEventCallback();
osg::NodeCallback* node_callback = callback->asNodeCallback();
@ -89,8 +97,10 @@ class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor
if (drawable_callback) drawable_callback->event(this,&drawable);
if (node_callback) (*node_callback)(&drawable, this);
if (callback_object) callback_object->run(&drawable, this);
if ((!drawable_callback && !node_callback) || callback_object) callback_object->run(&drawable, this);
if (!drawable_callback && !node_callback && !callback_object) callback->run(&drawable, this);
}
}
handle_callbacks(drawable.getStateSet());