]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasEventVisitor.cxx
9fbae2c84a73a64a784bcdd2c2d5ccd6d68e68c2
[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 #include <iostream>
24
25 namespace simgear
26 {
27 namespace canvas
28 {
29
30   //----------------------------------------------------------------------------
31   EventVisitor::EventVisitor( TraverseMode mode,
32                               const osg::Vec2f& pos,
33                               const ElementPtr& root ):
34     _traverse_mode( mode ),
35     _root(root)
36   {
37     if( mode == TRAVERSE_DOWN )
38     {
39       EventTarget target = {ElementWeakPtr(), pos};
40       _target_path.push_back(target);
41     }
42   }
43
44   //----------------------------------------------------------------------------
45   EventVisitor::~EventVisitor()
46   {
47
48   }
49
50   //----------------------------------------------------------------------------
51   bool EventVisitor::traverse(Element& el)
52   {
53     if( _traverse_mode == TRAVERSE_UP )
54       return el.ascend(*this);
55     else
56       return el.traverse(*this);
57   }
58
59   //----------------------------------------------------------------------------
60   bool EventVisitor::apply(Element& el)
61   {
62     // We only need to check for hits while traversing down
63     if( _traverse_mode == TRAVERSE_DOWN )
64     {
65       const osg::Vec2f& pos = _target_path.back().local_pos;
66       const osg::Vec2f local_pos = el.posToLocal(pos);
67
68       // Don't check specified root element for collision, as its purpose is to
69       // catch all events which have no target. This allows for example calling
70       // event listeners attached to the canvas itself (its root group) even if
71       // no element has been hit.
72       if( _root.get() != &el && !el.hitBound(pos, local_pos) )
73         return false;
74
75       EventTarget target = {el.getWeakPtr(), local_pos};
76       _target_path.push_back(target);
77
78       if( el.traverse(*this) || &el == _root.get() )
79         return true;
80
81       _target_path.pop_back();
82       return false;
83     }
84     else
85       return el.ascend(*this);
86   }
87
88   //----------------------------------------------------------------------------
89   const EventPropagationPath& EventVisitor::getPropagationPath() const
90   {
91     return _target_path;
92   }
93
94 } // namespace canvas
95 } // namespace simgear