]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasEventVisitor.cxx
Canvas::Text: add line-height property.
[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                               const ElementPtr& root ):
35     _traverse_mode( mode ),
36     _root(root)
37   {
38     if( mode == TRAVERSE_DOWN )
39     {
40       EventTarget target = {ElementWeakPtr(), pos, delta};
41       _target_path.push_back(target);
42     }
43   }
44
45   //----------------------------------------------------------------------------
46   EventVisitor::~EventVisitor()
47   {
48
49   }
50
51   //----------------------------------------------------------------------------
52   bool EventVisitor::traverse(Element& el)
53   {
54     if( _traverse_mode == TRAVERSE_UP )
55       return el.ascend(*this);
56     else
57       return el.traverse(*this);
58   }
59
60   //----------------------------------------------------------------------------
61   bool EventVisitor::apply(Element& el)
62   {
63     // We only need to check for hits while traversing down
64     if( _traverse_mode == TRAVERSE_DOWN )
65     {
66       // Transform event to local coordinates
67       const osg::Matrix& m = el.getMatrixTransform()->getInverseMatrix();
68       const osg::Vec2f& pos = _target_path.back().local_pos;
69       const osg::Vec2f local_pos
70       (
71         m(0, 0) * pos[0] + m(1, 0) * pos[1] + m(3, 0),
72         m(0, 1) * pos[0] + m(1, 1) * pos[1] + m(3, 1)
73       );
74
75       // Don't check specified root element for collision, as its purpose is to
76       // catch all events which have no target. This allows for example calling
77       // event listeners attached to the canvas itself (its root group) even if
78       // no element has been hit.
79       if( _root.get() != &el && !el.hitBound(pos, local_pos) )
80         return false;
81
82       const osg::Vec2f& delta = _target_path.back().local_delta;
83       const osg::Vec2f local_delta
84       (
85         m(0, 0) * delta[0] + m(1, 0) * delta[1],
86         m(0, 1) * delta[0] + m(1, 1) * delta[1]
87       );
88
89       EventTarget target = {el.getWeakPtr(), local_pos, local_delta};
90       _target_path.push_back(target);
91
92       if( el.traverse(*this) || &el == _root.get() )
93         return true;
94
95       _target_path.pop_back();
96       return false;
97     }
98     else
99       return el.ascend(*this);
100   }
101
102   //----------------------------------------------------------------------------
103   const EventPropagationPath& EventVisitor::getPropagationPath() const
104   {
105     return _target_path;
106   }
107
108 } // namespace canvas
109 } // namespace simgear