]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasEventVisitor.cxx
Canvas: clear event listeners on destroy
[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 osg::Vec2f& delta ):
34     _traverse_mode( mode )
35   {
36     if( mode == TRAVERSE_DOWN )
37     {
38       EventTarget target = {ElementWeakPtr(), pos, delta};
39       _target_path.push_back(target);
40     }
41   }
42
43   //----------------------------------------------------------------------------
44   EventVisitor::~EventVisitor()
45   {
46
47   }
48
49   //----------------------------------------------------------------------------
50   bool EventVisitor::traverse(Element& el)
51   {
52     if( _traverse_mode == TRAVERSE_UP )
53       return el.ascend(*this);
54     else
55       return el.traverse(*this);
56   }
57
58   //----------------------------------------------------------------------------
59   bool EventVisitor::apply(Element& el)
60   {
61     // We only need to check for hits while traversing down
62     if( _traverse_mode == TRAVERSE_DOWN )
63     {
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
68       (
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)
71       );
72
73       // Don't check collision with root element (2nd element in _target_path)
74       // do event listeners attached to the canvas itself (its root group)
75       // always get called even if no element has been hit.
76       if( _target_path.size() > 1 && !el.hitBound(pos, local_pos) )
77         return false;
78
79       const osg::Vec2f& delta = _target_path.back().local_delta;
80       const osg::Vec2f local_delta
81       (
82         m(0, 0) * delta[0] + m(1, 0) * delta[1],
83         m(0, 1) * delta[0] + m(1, 1) * delta[1]
84       );
85
86       EventTarget target = {el.getWeakPtr(), local_pos, local_delta};
87       _target_path.push_back(target);
88
89       if( el.traverse(*this) || _target_path.size() <= 2 )
90         return true;
91
92       _target_path.pop_back();
93       return false;
94     }
95     else
96       return el.ascend(*this);
97   }
98
99   //----------------------------------------------------------------------------
100   const EventPropagationPath& EventVisitor::getPropagationPath() const
101   {
102     return _target_path;
103   }
104
105 } // namespace canvas
106 } // namespace simgear