diff --git a/include/osg/Callback b/include/osg/Callback index e81315f48..7b0fd0907 100644 --- a/include/osg/Callback +++ b/include/osg/Callback @@ -17,6 +17,9 @@ #include #include +// 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 diff --git a/include/osgGA/EventHandler b/include/osgGA/EventHandler index 7156a443c..af95fd154 100644 --- a/include/osgGA/EventHandler +++ b/include/osgGA/EventHandler @@ -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(object); - osg::NodeVisitor* nv = dynamic_cast(data); + osg::Node* node = object->asNode(); + osg::NodeVisitor* nv = data->asNodeVisitor(); operator()(node, nv); return true; } diff --git a/include/osgGA/EventVisitor b/include/osgGA/EventVisitor index f6f29839a..2eb15a633 100644 --- a/include/osgGA/EventVisitor +++ b/include/osgGA/EventVisitor @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -83,14 +84,23 @@ class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor osg::Callback* callback = drawable.getEventCallback(); if (callback) { - osg::DrawableEventCallback* drawable_callback = callback->asDrawableEventCallback(); - osg::NodeCallback* node_callback = callback->asNodeCallback(); - osg::CallbackObject* callback_object = callback->asCallbackObject(); + osgGA::EventHandler* eh = callback->asEventHandler(); + if (eh) + { + callback->run(&drawable,this); + } + else + { + osg::DrawableEventCallback* drawable_callback = callback->asDrawableEventCallback(); + osg::NodeCallback* node_callback = callback->asNodeCallback(); + osg::CallbackObject* callback_object = callback->asCallbackObject(); - if (drawable_callback) drawable_callback->event(this,&drawable); - if (node_callback) (*node_callback)(&drawable, this); + 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());