1 // Interface for 2D canvas element
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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.
19 #include "element.hxx"
20 #include <Canvas/MouseEvent.hxx>
21 #include <Canvas/property_helper.hxx>
23 #include <osg/Drawable>
31 const std::string NAME_TRANSFORM = "tf";
32 const std::string NAME_COLOR = "color";
33 const std::string NAME_COLOR_FILL = "color-fill";
35 //----------------------------------------------------------------------------
41 //----------------------------------------------------------------------------
42 void Element::update(double dt)
44 if( !_transform->getNodeMask() )
45 // Don't do anything if element is hidden
48 if( _transform_dirty )
51 for( size_t i = 0; i < _transform_types.size(); ++i )
53 // Skip unused indizes...
54 if( _transform_types[i] == TT_NONE )
57 SGPropertyNode* tf_node = _node->getChild("tf", i, true);
59 // Build up the matrix representation of the current transform node
61 switch( _transform_types[i] )
64 tf = osg::Matrix( tf_node->getDoubleValue("m[0]", 1),
65 tf_node->getDoubleValue("m[1]", 0), 0, 0,
67 tf_node->getDoubleValue("m[2]", 0),
68 tf_node->getDoubleValue("m[3]", 1), 0, 0,
71 tf_node->getDoubleValue("m[4]", 0),
72 tf_node->getDoubleValue("m[5]", 0), 0, 1 );
75 tf.makeTranslate( osg::Vec3f( tf_node->getDoubleValue("t[0]", 0),
76 tf_node->getDoubleValue("t[1]", 0),
80 tf.makeRotate( tf_node->getDoubleValue("rot", 0), 0, 0, 1 );
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 );
94 _transform->setMatrix(m);
95 _transform_dirty = false;
98 if( _attributes_dirty & COLOR )
100 colorChanged( osg::Vec4( _color[0]->getFloatValue(),
101 _color[1]->getFloatValue(),
102 _color[2]->getFloatValue(),
103 _color[3]->getFloatValue() ) );
104 _attributes_dirty &= ~COLOR;
107 if( _attributes_dirty & COLOR_FILL )
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;
116 if( !_bounding_box.empty() )
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());
128 //----------------------------------------------------------------------------
129 bool Element::handleMouseEvent(const canvas::MouseEvent& event)
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);
137 // Drawables have a bounding box...
140 if( !_drawable->getBound().contains(local_event.getPos3()) )
143 // ... for other elements, i.e. groups only a bounding sphere is available
144 else if( !_transform->getBound().contains(local_event.getPos3()) )
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;
150 return handleLocalMouseEvent(local_event);
153 //----------------------------------------------------------------------------
154 osg::ref_ptr<osg::MatrixTransform> Element::getMatrixTransform()
159 //----------------------------------------------------------------------------
160 void Element::childAdded(SGPropertyNode* parent, SGPropertyNode* child)
162 if( parent == _node )
164 if( child->getNameString() == NAME_TRANSFORM )
166 if( child->getIndex() >= static_cast<int>(_transform_types.size()) )
167 _transform_types.resize( child->getIndex() + 1 );
169 _transform_types[ child->getIndex() ] = TT_NONE;
170 _transform_dirty = true;
175 else if( parent->getParent() == _node
176 && parent->getNameString() == NAME_TRANSFORM )
178 assert(parent->getIndex() < static_cast<int>(_transform_types.size()));
180 const std::string& name = child->getNameString();
182 TransformType& type = _transform_types[parent->getIndex()];
186 else if( name == "t" )
188 else if( name == "rot" )
190 else if( name == "s" )
193 _transform_dirty = true;
197 //----------------------------------------------------------------------------
198 void Element::childRemoved(SGPropertyNode* parent, SGPropertyNode* child)
200 if( parent != _node )
203 if( child->getNameString() == NAME_TRANSFORM )
205 assert(child->getIndex() < static_cast<int>(_transform_types.size()));
206 _transform_types[ child->getIndex() ] = TT_NONE;
208 while( !_transform_types.empty() && _transform_types.back() == TT_NONE )
209 _transform_types.pop_back();
211 _transform_dirty = true;
217 //----------------------------------------------------------------------------
218 void Element::valueChanged(SGPropertyNode* child)
220 SGPropertyNode *parent = child->getParent();
221 if( parent->getParent() == _node )
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;
230 else if( parent == _node )
232 if( child->getNameString() == "update" )
234 else if( child->getNameString() == "visible" )
235 // TODO check if we need another nodemask
236 _transform->setNodeMask( child->getBoolValue() ? 0xffffffff : 0 );
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 ),
252 _node->addChangeListener(this);
254 if( _attributes_used & COLOR )
255 linkColorNodes("color", _node, _color, osg::Vec4f(0,0,0,1));
257 if( _attributes_used & COLOR_FILL )
258 linkColorNodes("color-fill", _node, _color_fill, osg::Vec4f(1,1,1,1));
264 "New canvas element " << node->getPath()
268 //----------------------------------------------------------------------------
269 bool Element::handleLocalMouseEvent(const canvas::MouseEvent& event)
271 std::cout << _node->getPath()
272 << " local: pos=(" << event.x << "|" << event.y << ") "
273 << "d=(" << event.dx << "|" << event.dx << ")"
278 //----------------------------------------------------------------------------
279 void Element::setDrawable( osg::Drawable* drawable )
281 _drawable = drawable;
284 osg::ref_ptr<osg::Geode> geode = new osg::Geode;
285 geode->addDrawable(_drawable);
286 _transform->addChild(geode);
288 if( _attributes_used & BOUNDING_BOX )
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);
299 } // namespace canvas