1 // Visitor for traversing a canvas element hierarchy similar to the traversal
2 // of the DOM Level 3 Event Model
4 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Library General Public License for more details.
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "CanvasEvent.hxx"
21 #include "CanvasEventVisitor.hxx"
22 #include <simgear/canvas/elements/CanvasElement.hxx>
29 //----------------------------------------------------------------------------
30 EventVisitor::EventVisitor( TraverseMode mode,
31 const osg::Vec2f& pos,
32 const ElementPtr& root ):
33 _traverse_mode( mode ),
36 if( mode == TRAVERSE_DOWN )
37 _target_path.push_back( EventTarget(NULL, pos) );
40 //----------------------------------------------------------------------------
41 EventVisitor::~EventVisitor()
46 //----------------------------------------------------------------------------
47 bool EventVisitor::traverse(Element& el)
49 if( _traverse_mode == TRAVERSE_UP )
50 return el.ascend(*this);
52 return el.traverse(*this);
55 //----------------------------------------------------------------------------
56 bool EventVisitor::apply(Element& el)
58 // We only need to check for hits while traversing down
59 if( _traverse_mode == TRAVERSE_DOWN )
61 const osg::Vec2f& pos = _target_path.back().local_pos;
62 const osg::Vec2f local_pos = el.posToLocal(pos);
64 // Don't check specified root element for collision, as its purpose is to
65 // catch all events which have no target. This allows for example calling
66 // event listeners attached to the canvas itself (its root group) even if
67 // no element has been hit.
68 if( _root.get() != &el
69 && !el.hitBound(_target_path.front().local_pos, pos, local_pos) )
72 _target_path.push_back( EventTarget(&el, local_pos) );
74 if( el.traverse(*this) || &el == _root.get() )
77 _target_path.pop_back();
81 return el.ascend(*this);
84 //----------------------------------------------------------------------------
85 const EventPropagationPath& EventVisitor::getPropagationPath() const
91 } // namespace simgear