1 // Visitor for traversing a canvas element hierarchy similar to the traversal
2 // of the DOM Level 2 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>
30 //----------------------------------------------------------------------------
31 EventVisitor::EventVisitor( TraverseMode mode,
32 const osg::Vec2f& pos,
33 const osg::Vec2f& delta ):
34 _traverse_mode( mode )
36 if( mode == TRAVERSE_DOWN )
38 EventTarget target = {0, pos, delta};
39 _target_path.push_back(target);
43 //----------------------------------------------------------------------------
44 EventVisitor::~EventVisitor()
49 //----------------------------------------------------------------------------
50 bool EventVisitor::traverse(Element& el)
52 if( _traverse_mode == TRAVERSE_UP )
53 return el.ascend(*this);
55 return el.traverse(*this);
58 //----------------------------------------------------------------------------
59 bool EventVisitor::apply(Element& el)
61 // We only need to check for hits while traversing down
62 if( _traverse_mode == TRAVERSE_DOWN )
64 // Transform event to local coordinates
65 const osg::Matrix& m = el.getMatrixTransform()->getInverseMatrix();
66 const osg::Vec2f& pos = _target_path.back().local_pos;
67 const osg::Vec2f local_pos
69 m(0, 0) * pos[0] + m(1, 0) * pos[1] + m(3, 0),
70 m(0, 1) * pos[0] + m(1, 1) * pos[1] + m(3, 1)
73 if( !el.hitBound(local_pos) )
76 const osg::Vec2f& delta = _target_path.back().local_delta;
77 const osg::Vec2f local_delta
79 m(0, 0) * delta[0] + m(1, 0) * delta[1],
80 m(0, 1) * delta[0] + m(1, 1) * delta[1]
83 EventTarget target = {&el, local_pos, local_delta};
84 _target_path.push_back(target);
86 if( el.traverse(*this) )
89 _target_path.pop_back();
93 return el.ascend(*this);
96 //----------------------------------------------------------------------------
97 bool EventVisitor::propagateEvent(const EventPtr& event)
99 // std::cout << "Propagate event " << event->getTypeString() << "\n";
100 // for( EventTargets::iterator it = _target_path.begin();
101 // it != _target_path.end();
105 // std::cout << it->element->getProps()->getPath() << " "
106 // << "(" << it->local_pos.x() << "|" << it->local_pos.y() << ")\n";
112 } // namespace canvas
113 } // namespace simgear