]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasEventVisitor.cxx
canvas: BoxLayout shared pointer typedef.
[simgear.git] / simgear / canvas / CanvasEventVisitor.cxx
1 // Visitor for traversing a canvas element hierarchy similar to the traversal
2 // of the DOM Level 3 Event Model
3 //
4 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
5 //
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.
10 //
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.
15 //
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
19
20 #include "CanvasEvent.hxx"
21 #include "CanvasEventVisitor.hxx"
22 #include <simgear/canvas/elements/CanvasElement.hxx>
23
24 namespace simgear
25 {
26 namespace canvas
27 {
28
29   //----------------------------------------------------------------------------
30   EventVisitor::EventVisitor( TraverseMode mode,
31                               const osg::Vec2f& pos,
32                               const ElementPtr& root ):
33     _traverse_mode( mode ),
34     _root(root)
35   {
36     if( mode == TRAVERSE_DOWN )
37       _target_path.push_back( EventTarget(NULL, pos) );
38   }
39
40   //----------------------------------------------------------------------------
41   EventVisitor::~EventVisitor()
42   {
43
44   }
45
46   //----------------------------------------------------------------------------
47   bool EventVisitor::traverse(Element& el)
48   {
49     if( _traverse_mode == TRAVERSE_UP )
50       return el.ascend(*this);
51     else
52       return el.traverse(*this);
53   }
54
55   //----------------------------------------------------------------------------
56   bool EventVisitor::apply(Element& el)
57   {
58     // We only need to check for hits while traversing down
59     if( _traverse_mode == TRAVERSE_DOWN )
60     {
61       const osg::Vec2f& pos = _target_path.back().local_pos;
62       const osg::Vec2f local_pos = el.posToLocal(pos);
63
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) )
70         return false;
71
72       _target_path.push_back( EventTarget(&el, local_pos) );
73
74       if( el.traverse(*this) || &el == _root.get() )
75         return true;
76
77       _target_path.pop_back();
78       return false;
79     }
80     else
81       return el.ascend(*this);
82   }
83
84   //----------------------------------------------------------------------------
85   const EventPropagationPath& EventVisitor::getPropagationPath() const
86   {
87     return _target_path;
88   }
89
90 } // namespace canvas
91 } // namespace simgear