Hi Chris,

As both the Node subclass and the Callback are coupled tightly you could
refactor it so that the event handling is done within the Node subclass
rather than being deferred to the Callback that you are attaching to it.
Simply override the traverse method and checks whether the NodeVisitor is
an EventVisitor.

The osgGA/Widget.cpp implementation in svn/trunk uses the following code:

void Widget::traverseImplementation(osg::NodeVisitor& nv)
{
    if (!_graphicsInitialized &&
nv.getVisitorType()!=osg::NodeVisitor::CULL_VISITOR) createGraphics();


    osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(&nv);
    if (ev)
    {
        updateFocus(nv);

        if (getHasEventFocus())
        {
            // signify that event has been taken by widget with focus
            ev->setEventHandled(true);

            osgGA::EventQueue::Events& events = ev->getEvents();
            for(osgGA::EventQueue::Events::iterator itr = events.begin();
                itr != events.end();
                ++itr)
            {
                if (handle(ev, itr->get()))
                {
                    (*itr)->setHandled(true);
                }
            }
        }
    }
    else
    {
        osg::Group::traverse(nv);
    }
}

You won't need all like code like the graphics initialization line, but the
fallback to do Group::traverse() will be required.  You'll also need to
tell the node that you need an event traversals. So in the constructor
you'd do what the osgGA/Widget.cpp class does:


idget::Widget():
    _focusBehaviour(FOCUS_FOLLOWS_POINTER),
    _hasEventFocus(false),
    _graphicsInitialized(false)

{
    setNumChildrenRequiringEventTraversal(1);
}

Widget::Widget(const Widget& widget, const osg::CopyOp& copyop):
    osg::Group(),
    _focusBehaviour(widget._focusBehaviour),
    _hasEventFocus(false),
    _graphicsInitialized(false)
{
    setNumChildrenRequiringEventTraversal(1);
}

Node the setNumChildrenRequiringEventTraversal(1); line, this is the bit
you'll need to add.

Robert.
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to