]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/elements/element.cxx
Canvas: Forward mouse events to elements.
[flightgear.git] / src / Canvas / elements / element.cxx
1 // Interface for 2D canvas element
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 #include "element.hxx"
20 #include <Canvas/MouseEvent.hxx>
21 #include <Canvas/property_helper.hxx>
22
23 #include <osg/Drawable>
24 #include <osg/Geode>
25
26 #include <cassert>
27 #include <cstring>
28
29 namespace canvas
30 {
31   const std::string NAME_TRANSFORM = "tf";
32   const std::string NAME_COLOR = "color";
33   const std::string NAME_COLOR_FILL = "color-fill";
34
35   //----------------------------------------------------------------------------
36   Element::~Element()
37   {
38
39   }
40
41   //----------------------------------------------------------------------------
42   void Element::update(double dt)
43   {
44     if( !_transform->getNodeMask() )
45       // Don't do anything if element is hidden
46       return;
47
48     if( _transform_dirty )
49     {
50       osg::Matrix m;
51       for( size_t i = 0; i < _transform_types.size(); ++i )
52       {
53         // Skip unused indizes...
54         if( _transform_types[i] == TT_NONE )
55           continue;
56
57         SGPropertyNode* tf_node = _node->getChild("tf", i, true);
58
59         // Build up the matrix representation of the current transform node
60         osg::Matrix tf;
61         switch( _transform_types[i] )
62         {
63           case TT_MATRIX:
64             tf = osg::Matrix( tf_node->getDoubleValue("m[0]", 1),
65                               tf_node->getDoubleValue("m[1]", 0), 0, 0,
66
67                               tf_node->getDoubleValue("m[2]", 0),
68                               tf_node->getDoubleValue("m[3]", 1), 0, 0,
69
70                               0,    0,    1, 0,
71                               tf_node->getDoubleValue("m[4]", 0),
72                               tf_node->getDoubleValue("m[5]", 0), 0, 1 );
73             break;
74           case TT_TRANSLATE:
75             tf.makeTranslate( osg::Vec3f( tf_node->getDoubleValue("t[0]", 0),
76                                           tf_node->getDoubleValue("t[1]", 0),
77                                           0 ) );
78             break;
79           case TT_ROTATE:
80             tf.makeRotate( tf_node->getDoubleValue("rot", 0), 0, 0, 1 );
81             break;
82           case TT_SCALE:
83           {
84             float sx = tf_node->getDoubleValue("s[0]", 1);
85             // sy defaults to sx...
86             tf.makeScale( sx, tf_node->getDoubleValue("s[1]", sx), 1 );
87             break;
88           }
89           default:
90             break;
91         }
92         m.postMult( tf );
93       }
94       _transform->setMatrix(m);
95       _transform_dirty = false;
96     }
97
98     if( _attributes_dirty & COLOR )
99     {
100       colorChanged( osg::Vec4( _color[0]->getFloatValue(),
101                                _color[1]->getFloatValue(),
102                                _color[2]->getFloatValue(),
103                                _color[3]->getFloatValue() ) );
104       _attributes_dirty &= ~COLOR;
105     }
106
107     if( _attributes_dirty & COLOR_FILL )
108     {
109       colorFillChanged( osg::Vec4( _color_fill[0]->getFloatValue(),
110                                    _color_fill[1]->getFloatValue(),
111                                    _color_fill[2]->getFloatValue(),
112                                    _color_fill[3]->getFloatValue() ) );
113       _attributes_dirty &= ~COLOR_FILL;
114     }
115
116     if( !_bounding_box.empty() )
117     {
118       assert( _drawable );
119
120       const osg::BoundingBox& bb = _drawable->getBound();
121       _bounding_box[0]->setFloatValue(bb._min.x());
122       _bounding_box[1]->setFloatValue(bb._min.y());
123       _bounding_box[2]->setFloatValue(bb._max.x());
124       _bounding_box[3]->setFloatValue(bb._max.y());
125     }
126   }
127
128   //----------------------------------------------------------------------------
129   bool Element::handleMouseEvent(const canvas::MouseEvent& event)
130   {
131     // Transform event to local coordinates
132     const osg::Matrixd& m = _transform->getInverseMatrix();
133     canvas::MouseEvent local_event = event;
134     local_event.x = m(0, 0) * event.x + m(1, 0) * event.y + m(3, 0);
135     local_event.y = m(0, 1) * event.x + m(1, 1) * event.y + m(3, 1);
136
137     // Drawables have a bounding box...
138     if( _drawable )
139     {
140       if( !_drawable->getBound().contains(local_event.getPos3()) )
141         return false;
142     }
143     // ... for other elements, i.e. groups only a bounding sphere is available
144     else if( !_transform->getBound().contains(local_event.getPos3()) )
145       return false;
146
147     local_event.dx = m(0, 0) * event.dx + m(1, 0) * event.dy;
148     local_event.dy = m(0, 1) * event.dx + m(1, 1) * event.dy;
149
150     return handleLocalMouseEvent(local_event);
151   }
152
153   //----------------------------------------------------------------------------
154   osg::ref_ptr<osg::MatrixTransform> Element::getMatrixTransform()
155   {
156     return _transform;
157   }
158
159   //----------------------------------------------------------------------------
160   void Element::childAdded(SGPropertyNode* parent, SGPropertyNode* child)
161   {
162     if( parent == _node )
163     {
164       if( child->getNameString() == NAME_TRANSFORM )
165       {
166         if( child->getIndex() >= static_cast<int>(_transform_types.size()) )
167           _transform_types.resize( child->getIndex() + 1 );
168
169         _transform_types[ child->getIndex() ] = TT_NONE;
170         _transform_dirty = true;
171       }
172       else
173         childAdded(child);
174     }
175     else if(    parent->getParent() == _node
176              && parent->getNameString() == NAME_TRANSFORM )
177     {
178       assert(parent->getIndex() < static_cast<int>(_transform_types.size()));
179
180       const std::string& name = child->getNameString();
181
182       TransformType& type = _transform_types[parent->getIndex()];
183
184       if(      name == "m" )
185         type = TT_MATRIX;
186       else if( name == "t" )
187         type = TT_TRANSLATE;
188       else if( name == "rot" )
189         type = TT_ROTATE;
190       else if( name == "s" )
191         type = TT_SCALE;
192
193       _transform_dirty = true;
194     }
195   }
196
197   //----------------------------------------------------------------------------
198   void Element::childRemoved(SGPropertyNode* parent, SGPropertyNode* child)
199   {
200     if( parent != _node )
201       return;
202
203     if( child->getNameString() == NAME_TRANSFORM )
204     {
205       assert(child->getIndex() < static_cast<int>(_transform_types.size()));
206       _transform_types[ child->getIndex() ] = TT_NONE;
207
208       while( !_transform_types.empty() && _transform_types.back() == TT_NONE )
209         _transform_types.pop_back();
210
211       _transform_dirty = true;
212     }
213     else
214       childRemoved(child);
215   }
216
217   //----------------------------------------------------------------------------
218   void Element::valueChanged(SGPropertyNode* child)
219   {
220     SGPropertyNode *parent = child->getParent();
221     if( parent->getParent() == _node )
222     {
223       if( parent->getNameString() == NAME_TRANSFORM )
224         _transform_dirty = true;
225       else if( !_color.empty() && _color[0]->getParent() == parent )
226         _attributes_dirty |= COLOR;
227       else if( !_color_fill.empty() && _color_fill[0]->getParent() == parent )
228         _attributes_dirty |= COLOR_FILL;
229     }
230     else if( parent == _node )
231     {
232       if( child->getNameString() == "update" )
233         update(0);
234       else if( child->getNameString() == "visible" )
235         // TODO check if we need another nodemask
236         _transform->setNodeMask( child->getBoolValue() ? 0xffffffff : 0 );
237       else
238         childChanged(child);
239     }
240   }
241
242   //----------------------------------------------------------------------------
243   Element::Element(SGPropertyNode_ptr node, uint32_t attributes_used):
244     _attributes_used( attributes_used ),
245     _attributes_dirty( attributes_used ),
246     _transform_dirty( false ),
247     _transform( new osg::MatrixTransform ),
248     _node( node ),
249     _drawable( 0 )
250   {
251     assert( _node );
252     _node->addChangeListener(this);
253
254     if( _attributes_used & COLOR )
255       linkColorNodes("color", _node, _color, osg::Vec4f(0,0,0,1));
256
257     if( _attributes_used & COLOR_FILL )
258       linkColorNodes("color-fill", _node, _color_fill, osg::Vec4f(1,1,1,1));
259
260     SG_LOG
261     (
262       SG_GL,
263       SG_DEBUG,
264       "New canvas element " << node->getPath()
265     );
266   }
267
268   //----------------------------------------------------------------------------
269   bool Element::handleLocalMouseEvent(const canvas::MouseEvent& event)
270   {
271     std::cout << _node->getPath()
272               << " local: pos=(" << event.x << "|" << event.y << ") "
273               <<         "d=(" << event.dx << "|" << event.dx << ")"
274               << std::endl;
275     return true;
276   }
277
278   //----------------------------------------------------------------------------
279   void Element::setDrawable( osg::Drawable* drawable )
280   {
281     _drawable = drawable;
282     assert( _drawable );
283
284     osg::ref_ptr<osg::Geode> geode = new osg::Geode;
285     geode->addDrawable(_drawable);
286     _transform->addChild(geode);
287
288     if( _attributes_used & BOUNDING_BOX )
289     {
290       SGPropertyNode* bb_node = _node->getChild("bounding-box", 0, true);
291       _bounding_box.resize(4);
292       _bounding_box[0] = bb_node->getChild("min-x", 0, true);
293       _bounding_box[1] = bb_node->getChild("min-y", 0, true);
294       _bounding_box[2] = bb_node->getChild("max-x", 0, true);
295       _bounding_box[3] = bb_node->getChild("max-y", 0, true);
296     }
297   }
298
299 } // namespace canvas