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/Canvas.hxx>
21 #include <simgear/canvas/CanvasEventListener.hxx>
22 #include <simgear/canvas/CanvasEventVisitor.hxx>
23 #include <simgear/canvas/MouseEvent.hxx>
25 #include <osg/Drawable>
28 #include <boost/foreach.hpp>
29 #include <boost/make_shared.hpp>
38 const std::string NAME_TRANSFORM = "tf";
40 //----------------------------------------------------------------------------
41 void Element::removeListener()
43 _node->removeChangeListener(this);
46 //----------------------------------------------------------------------------
51 BOOST_FOREACH(osg::Group* parent, _transform->getParents())
53 parent->removeChild(_transform);
57 //----------------------------------------------------------------------------
58 void Element::update(double dt)
60 if( !_transform->getNodeMask() )
61 // Don't do anything if element is hidden
64 if( _transform_dirty )
67 for( size_t i = 0; i < _transform_types.size(); ++i )
69 // Skip unused indizes...
70 if( _transform_types[i] == TT_NONE )
73 SGPropertyNode* tf_node = _node->getChild("tf", i, true);
75 // Build up the matrix representation of the current transform node
77 switch( _transform_types[i] )
80 tf = osg::Matrix( tf_node->getDoubleValue("m[0]", 1),
81 tf_node->getDoubleValue("m[1]", 0), 0, 0,
83 tf_node->getDoubleValue("m[2]", 0),
84 tf_node->getDoubleValue("m[3]", 1), 0, 0,
87 tf_node->getDoubleValue("m[4]", 0),
88 tf_node->getDoubleValue("m[5]", 0), 0, 1 );
91 tf.makeTranslate( osg::Vec3f( tf_node->getDoubleValue("t[0]", 0),
92 tf_node->getDoubleValue("t[1]", 0),
96 tf.makeRotate( tf_node->getDoubleValue("rot", 0), 0, 0, 1 );
100 float sx = tf_node->getDoubleValue("s[0]", 1);
101 // sy defaults to sx...
102 tf.makeScale( sx, tf_node->getDoubleValue("s[1]", sx), 1 );
110 _transform->setMatrix(m);
111 _transform_dirty = false;
115 //----------------------------------------------------------------------------
116 naRef Element::addEventListener(const nasal::CallContext& ctx)
118 const std::string type_str = ctx.requireArg<std::string>(0);
119 naRef code = ctx.requireArg<naRef>(1);
125 "addEventListener(" << _node->getPath() << ", " << type_str << ")"
128 Event::Type type = Event::strToType(type_str);
129 if( type == Event::UNKNOWN )
130 naRuntimeError( ctx.c,
131 "addEventListener: Unknown event type %s",
134 _listener[ type ].push_back
136 boost::make_shared<EventListener>( code,
137 _canvas.lock()->getSystemAdapter() )
143 //----------------------------------------------------------------------------
144 SGConstPropertyNode_ptr Element::getProps() const
149 //----------------------------------------------------------------------------
150 SGPropertyNode_ptr Element::getProps()
155 //----------------------------------------------------------------------------
156 bool Element::accept(EventVisitor& visitor)
158 return visitor.apply(*this);
161 //----------------------------------------------------------------------------
162 bool Element::ascend(EventVisitor& visitor)
165 return _parent->accept(visitor);
169 //----------------------------------------------------------------------------
170 bool Element::traverse(EventVisitor& visitor)
175 //----------------------------------------------------------------------------
176 void Element::callListeners(const canvas::EventPtr& event)
178 ListenerMap::iterator listeners = _listener.find(event->getType());
179 if( listeners == _listener.end() )
182 BOOST_FOREACH(EventListenerPtr listener, listeners->second)
183 listener->call(event);
186 //----------------------------------------------------------------------------
187 bool Element::hitBound(const osg::Vec2f& pos) const
189 const osg::Vec3f pos3(pos, 0);
191 // Drawables have a bounding box...
194 if( !_drawable->getBound().contains(pos3) )
197 // ... for other elements, i.e. groups only a bounding sphere is available
198 else if( !_transform->getBound().contains(pos3) )
204 //----------------------------------------------------------------------------
205 osg::ref_ptr<osg::MatrixTransform> Element::getMatrixTransform()
210 //----------------------------------------------------------------------------
211 void Element::childAdded(SGPropertyNode* parent, SGPropertyNode* child)
214 && child->getNameString() == NAME_TRANSFORM )
216 if( child->getIndex() >= static_cast<int>(_transform_types.size()) )
217 _transform_types.resize( child->getIndex() + 1 );
219 _transform_types[ child->getIndex() ] = TT_NONE;
220 _transform_dirty = true;
223 else if( parent->getParent() == _node
224 && parent->getNameString() == NAME_TRANSFORM )
226 assert(parent->getIndex() < static_cast<int>(_transform_types.size()));
228 const std::string& name = child->getNameString();
230 TransformType& type = _transform_types[parent->getIndex()];
234 else if( name == "t" )
236 else if( name == "rot" )
238 else if( name == "s" )
241 _transform_dirty = true;
248 //----------------------------------------------------------------------------
249 void Element::childRemoved(SGPropertyNode* parent, SGPropertyNode* child)
251 if( parent == _node && child->getNameString() == NAME_TRANSFORM )
253 assert(child->getIndex() < static_cast<int>(_transform_types.size()));
254 _transform_types[ child->getIndex() ] = TT_NONE;
256 while( !_transform_types.empty() && _transform_types.back() == TT_NONE )
257 _transform_types.pop_back();
259 _transform_dirty = true;
266 //----------------------------------------------------------------------------
267 void Element::valueChanged(SGPropertyNode* child)
269 SGPropertyNode *parent = child->getParent();
270 if( parent == _node )
272 if( setStyle(child) )
274 else if( child->getNameString() == "update" )
276 else if( child->getNameString() == "visible" )
277 // TODO check if we need another nodemask
278 return _transform->setNodeMask( child->getBoolValue() ? 0xffffffff : 0 );
280 else if( parent->getParent() == _node
281 && parent->getNameString() == NAME_TRANSFORM )
283 _transform_dirty = true;
290 //----------------------------------------------------------------------------
291 void Element::setBoundingBox(const osg::BoundingBox& bb)
293 if( _bounding_box.empty() )
295 SGPropertyNode* bb_node = _node->getChild("bounding-box", 0, true);
296 _bounding_box.resize(4);
297 _bounding_box[0] = bb_node->getChild("min-x", 0, true);
298 _bounding_box[1] = bb_node->getChild("min-y", 0, true);
299 _bounding_box[2] = bb_node->getChild("max-x", 0, true);
300 _bounding_box[3] = bb_node->getChild("max-y", 0, true);
303 _bounding_box[0]->setFloatValue(bb._min.x());
304 _bounding_box[1]->setFloatValue(bb._min.y());
305 _bounding_box[2]->setFloatValue(bb._max.x());
306 _bounding_box[3]->setFloatValue(bb._max.y());
309 //----------------------------------------------------------------------------
310 Element::Element( const CanvasWeakPtr& canvas,
311 const SGPropertyNode_ptr& node,
312 const Style& parent_style,
316 _transform_dirty( false ),
317 _transform( new osg::MatrixTransform ),
319 _style( parent_style ),
323 _node->addChangeListener(this);
329 "New canvas element " << node->getPath()
333 //----------------------------------------------------------------------------
334 void Element::setDrawable( osg::Drawable* drawable )
336 _drawable = drawable;
339 osg::ref_ptr<osg::Geode> geode = new osg::Geode;
340 geode->addDrawable(_drawable);
341 _transform->addChild(geode);
344 //----------------------------------------------------------------------------
345 void Element::setupStyle()
347 BOOST_FOREACH( Style::value_type style, _style )
348 setStyle(style.second);
351 //----------------------------------------------------------------------------
352 bool Element::setStyle(const SGPropertyNode* child)
354 StyleSetters::const_iterator setter =
355 _style_setters.find(child->getNameString());
356 if( setter == _style_setters.end() )
359 setter->second(child);
363 } // namespace canvas
364 } // namespace simgear