#include "CanvasEvent.hxx"
#include "CanvasEventVisitor.hxx"
#include <simgear/canvas/elements/CanvasElement.hxx>
-#include <iostream>
namespace simgear
{
//----------------------------------------------------------------------------
EventVisitor::EventVisitor( TraverseMode mode,
const osg::Vec2f& pos,
- const osg::Vec2f& delta ):
- _traverse_mode( mode )
+ const ElementPtr& root ):
+ _traverse_mode( mode ),
+ _root(root)
{
if( mode == TRAVERSE_DOWN )
- {
- EventTarget target = {ElementWeakPtr(), pos, delta};
- _target_path.push_back(target);
- }
+ _target_path.push_back( EventTarget(NULL, pos) );
}
//----------------------------------------------------------------------------
// We only need to check for hits while traversing down
if( _traverse_mode == TRAVERSE_DOWN )
{
- // Transform event to local coordinates
- const osg::Matrix& m = el.getMatrixTransform()->getInverseMatrix();
const osg::Vec2f& pos = _target_path.back().local_pos;
- const osg::Vec2f local_pos
- (
- m(0, 0) * pos[0] + m(1, 0) * pos[1] + m(3, 0),
- m(0, 1) * pos[0] + m(1, 1) * pos[1] + m(3, 1)
- );
+ const osg::Vec2f local_pos = el.posToLocal(pos);
- // Don't check collision with root element (2nd element in _target_path)
- // do event listeners attached to the canvas itself (its root group)
- // always get called even if no element has been hit.
- if( _target_path.size() > 2 && !el.hitBound(pos, local_pos) )
+ // Don't check specified root element for collision, as its purpose is to
+ // catch all events which have no target. This allows for example calling
+ // event listeners attached to the canvas itself (its root group) even if
+ // no element has been hit.
+ if( _root.get() != &el
+ && !el.hitBound(_target_path.front().local_pos, pos, local_pos) )
return false;
- const osg::Vec2f& delta = _target_path.back().local_delta;
- const osg::Vec2f local_delta
- (
- m(0, 0) * delta[0] + m(1, 0) * delta[1],
- m(0, 1) * delta[0] + m(1, 1) * delta[1]
- );
-
- EventTarget target = {el.getWeakPtr(), local_pos, local_delta};
- _target_path.push_back(target);
+ _target_path.push_back( EventTarget(&el, local_pos) );
- if( el.traverse(*this) || _target_path.size() <= 2 )
+ if( el.traverse(*this) || &el == _root.get() )
return true;
_target_path.pop_back();