1 // Interface for 2D Canvas element
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "CanvasElement.hxx"
20 #include <simgear/canvas/CanvasEventVisitor.hxx>
21 #include <simgear/canvas/MouseEvent.hxx>
23 #include <osg/Drawable>
26 #include <boost/foreach.hpp>
35 const std::string NAME_TRANSFORM = "tf";
37 //----------------------------------------------------------------------------
38 void Element::removeListener()
40 _node->removeChangeListener(this);
43 //----------------------------------------------------------------------------
48 BOOST_FOREACH(osg::Group* parent, _transform->getParents())
50 parent->removeChild(_transform);
54 //----------------------------------------------------------------------------
55 void Element::update(double dt)
57 if( !_transform->getNodeMask() )
58 // Don't do anything if element is hidden
61 if( _transform_dirty )
64 for( size_t i = 0; i < _transform_types.size(); ++i )
66 // Skip unused indizes...
67 if( _transform_types[i] == TT_NONE )
70 SGPropertyNode* tf_node = _node->getChild("tf", i, true);
72 // Build up the matrix representation of the current transform node
74 switch( _transform_types[i] )
77 tf = osg::Matrix( tf_node->getDoubleValue("m[0]", 1),
78 tf_node->getDoubleValue("m[1]", 0), 0, 0,
80 tf_node->getDoubleValue("m[2]", 0),
81 tf_node->getDoubleValue("m[3]", 1), 0, 0,
84 tf_node->getDoubleValue("m[4]", 0),
85 tf_node->getDoubleValue("m[5]", 0), 0, 1 );
88 tf.makeTranslate( osg::Vec3f( tf_node->getDoubleValue("t[0]", 0),
89 tf_node->getDoubleValue("t[1]", 0),
93 tf.makeRotate( tf_node->getDoubleValue("rot", 0), 0, 0, 1 );
97 float sx = tf_node->getDoubleValue("s[0]", 1);
98 // sy defaults to sx...
99 tf.makeScale( sx, tf_node->getDoubleValue("s[1]", sx), 1 );
107 _transform->setMatrix(m);
108 _transform_dirty = false;
112 //----------------------------------------------------------------------------
113 naRef Element::addEventListener(const nasal::CallContext& ctx)
115 std::cout << "addEventListener " << _node->getPath() << std::endl;
119 //----------------------------------------------------------------------------
120 SGConstPropertyNode_ptr Element::getProps() const
125 //----------------------------------------------------------------------------
126 SGPropertyNode_ptr Element::getProps()
131 //----------------------------------------------------------------------------
132 bool Element::accept(EventVisitor& visitor)
134 return visitor.apply(*this);
137 //----------------------------------------------------------------------------
138 bool Element::ascend(EventVisitor& visitor)
141 return _parent->accept(visitor);
145 //----------------------------------------------------------------------------
146 bool Element::traverse(EventVisitor& visitor)
151 //----------------------------------------------------------------------------
152 bool Element::hitBound(const osg::Vec2f& pos) const
154 const osg::Vec3f pos3(pos, 0);
156 // Drawables have a bounding box...
159 if( !_drawable->getBound().contains(pos3) )
162 // ... for other elements, i.e. groups only a bounding sphere is available
163 else if( !_transform->getBound().contains(pos3) )
169 //----------------------------------------------------------------------------
170 osg::ref_ptr<osg::MatrixTransform> Element::getMatrixTransform()
175 //----------------------------------------------------------------------------
176 void Element::childAdded(SGPropertyNode* parent, SGPropertyNode* child)
179 && child->getNameString() == NAME_TRANSFORM )
181 if( child->getIndex() >= static_cast<int>(_transform_types.size()) )
182 _transform_types.resize( child->getIndex() + 1 );
184 _transform_types[ child->getIndex() ] = TT_NONE;
185 _transform_dirty = true;
188 else if( parent->getParent() == _node
189 && parent->getNameString() == NAME_TRANSFORM )
191 assert(parent->getIndex() < static_cast<int>(_transform_types.size()));
193 const std::string& name = child->getNameString();
195 TransformType& type = _transform_types[parent->getIndex()];
199 else if( name == "t" )
201 else if( name == "rot" )
203 else if( name == "s" )
206 _transform_dirty = true;
213 //----------------------------------------------------------------------------
214 void Element::childRemoved(SGPropertyNode* parent, SGPropertyNode* child)
216 if( parent == _node && child->getNameString() == NAME_TRANSFORM )
218 assert(child->getIndex() < static_cast<int>(_transform_types.size()));
219 _transform_types[ child->getIndex() ] = TT_NONE;
221 while( !_transform_types.empty() && _transform_types.back() == TT_NONE )
222 _transform_types.pop_back();
224 _transform_dirty = true;
231 //----------------------------------------------------------------------------
232 void Element::valueChanged(SGPropertyNode* child)
234 SGPropertyNode *parent = child->getParent();
235 if( parent == _node )
237 if( setStyle(child) )
239 else if( child->getNameString() == "update" )
241 else if( child->getNameString() == "visible" )
242 // TODO check if we need another nodemask
243 return _transform->setNodeMask( child->getBoolValue() ? 0xffffffff : 0 );
245 else if( parent->getParent() == _node
246 && parent->getNameString() == NAME_TRANSFORM )
248 _transform_dirty = true;
255 //----------------------------------------------------------------------------
256 void Element::setBoundingBox(const osg::BoundingBox& bb)
258 if( _bounding_box.empty() )
260 SGPropertyNode* bb_node = _node->getChild("bounding-box", 0, true);
261 _bounding_box.resize(4);
262 _bounding_box[0] = bb_node->getChild("min-x", 0, true);
263 _bounding_box[1] = bb_node->getChild("min-y", 0, true);
264 _bounding_box[2] = bb_node->getChild("max-x", 0, true);
265 _bounding_box[3] = bb_node->getChild("max-y", 0, true);
268 _bounding_box[0]->setFloatValue(bb._min.x());
269 _bounding_box[1]->setFloatValue(bb._min.y());
270 _bounding_box[2]->setFloatValue(bb._max.x());
271 _bounding_box[3]->setFloatValue(bb._max.y());
274 //----------------------------------------------------------------------------
275 Element::Element( const CanvasWeakPtr& canvas,
276 const SGPropertyNode_ptr& node,
277 const Style& parent_style,
281 _transform_dirty( false ),
282 _transform( new osg::MatrixTransform ),
284 _style( parent_style ),
288 _node->addChangeListener(this);
294 "New canvas element " << node->getPath()
298 //----------------------------------------------------------------------------
299 void Element::callListeners(canvas::Event& event)
301 ListenerMap::iterator listeners = _listener.find(event.getType());
302 if( listeners == _listener.end() )
306 //----------------------------------------------------------------------------
307 void Element::setDrawable( osg::Drawable* drawable )
309 _drawable = drawable;
312 osg::ref_ptr<osg::Geode> geode = new osg::Geode;
313 geode->addDrawable(_drawable);
314 _transform->addChild(geode);
317 //----------------------------------------------------------------------------
318 void Element::setupStyle()
320 BOOST_FOREACH( Style::value_type style, _style )
321 setStyle(style.second);
324 //----------------------------------------------------------------------------
325 bool Element::setStyle(const SGPropertyNode* child)
327 StyleSetters::const_iterator setter =
328 _style_setters.find(child->getNameString());
329 if( setter == _style_setters.end() )
332 setter->second(child);
336 } // namespace canvas
337 } // namespace simgear