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 ElementWeakPtr Element::getWeakPtr() const
60 return boost::static_pointer_cast<Element>(_self.lock());
63 //----------------------------------------------------------------------------
64 void Element::update(double dt)
66 if( !_transform->getNodeMask() )
67 // Don't do anything if element is hidden
70 if( _transform_dirty )
73 for( size_t i = 0; i < _transform_types.size(); ++i )
75 // Skip unused indizes...
76 if( _transform_types[i] == TT_NONE )
79 SGPropertyNode* tf_node = _node->getChild("tf", i, true);
81 // Build up the matrix representation of the current transform node
83 switch( _transform_types[i] )
86 tf = osg::Matrix( tf_node->getDoubleValue("m[0]", 1),
87 tf_node->getDoubleValue("m[1]", 0), 0, 0,
89 tf_node->getDoubleValue("m[2]", 0),
90 tf_node->getDoubleValue("m[3]", 1), 0, 0,
93 tf_node->getDoubleValue("m[4]", 0),
94 tf_node->getDoubleValue("m[5]", 0), 0, 1 );
97 tf.makeTranslate( osg::Vec3f( tf_node->getDoubleValue("t[0]", 0),
98 tf_node->getDoubleValue("t[1]", 0),
102 tf.makeRotate( tf_node->getDoubleValue("rot", 0), 0, 0, 1 );
106 float sx = tf_node->getDoubleValue("s[0]", 1);
107 // sy defaults to sx...
108 tf.makeScale( sx, tf_node->getDoubleValue("s[1]", sx), 1 );
116 _transform->setMatrix(m);
117 _transform_dirty = false;
121 //----------------------------------------------------------------------------
122 naRef Element::addEventListener(const nasal::CallContext& ctx)
124 const std::string type_str = ctx.requireArg<std::string>(0);
125 naRef code = ctx.requireArg<naRef>(1);
131 "addEventListener(" << _node->getPath() << ", " << type_str << ")"
134 Event::Type type = Event::strToType(type_str);
135 if( type == Event::UNKNOWN )
136 naRuntimeError( ctx.c,
137 "addEventListener: Unknown event type %s",
140 _listener[ type ].push_back
142 boost::make_shared<EventListener>( code,
143 _canvas.lock()->getSystemAdapter() )
149 //----------------------------------------------------------------------------
150 bool Element::accept(EventVisitor& visitor)
152 return visitor.apply(*this);
155 //----------------------------------------------------------------------------
156 bool Element::ascend(EventVisitor& visitor)
159 return _parent->accept(visitor);
163 //----------------------------------------------------------------------------
164 bool Element::traverse(EventVisitor& visitor)
169 //----------------------------------------------------------------------------
170 void Element::callListeners(const canvas::EventPtr& event)
172 ListenerMap::iterator listeners = _listener.find(event->getType());
173 if( listeners == _listener.end() )
176 BOOST_FOREACH(EventListenerPtr listener, listeners->second)
177 listener->call(event);
180 //----------------------------------------------------------------------------
181 bool Element::hitBound(const osg::Vec2f& pos) const
183 const osg::Vec3f pos3(pos, 0);
185 // Drawables have a bounding box...
188 if( !_drawable->getBound().contains(pos3) )
191 // ... for other elements, i.e. groups only a bounding sphere is available
192 else if( !_transform->getBound().contains(pos3) )
198 //----------------------------------------------------------------------------
199 osg::ref_ptr<osg::MatrixTransform> Element::getMatrixTransform()
204 //----------------------------------------------------------------------------
205 void Element::childAdded(SGPropertyNode* parent, SGPropertyNode* child)
208 && child->getNameString() == NAME_TRANSFORM )
210 if( child->getIndex() >= static_cast<int>(_transform_types.size()) )
211 _transform_types.resize( child->getIndex() + 1 );
213 _transform_types[ child->getIndex() ] = TT_NONE;
214 _transform_dirty = true;
217 else if( parent->getParent() == _node
218 && parent->getNameString() == NAME_TRANSFORM )
220 assert(parent->getIndex() < static_cast<int>(_transform_types.size()));
222 const std::string& name = child->getNameString();
224 TransformType& type = _transform_types[parent->getIndex()];
228 else if( name == "t" )
230 else if( name == "rot" )
232 else if( name == "s" )
235 _transform_dirty = true;
242 //----------------------------------------------------------------------------
243 void Element::childRemoved(SGPropertyNode* parent, SGPropertyNode* child)
245 if( parent == _node && child->getNameString() == NAME_TRANSFORM )
247 if( child->getIndex() >= static_cast<int>(_transform_types.size()) )
253 "Element::childRemoved: unknown transform: " << child->getPath()
258 _transform_types[ child->getIndex() ] = TT_NONE;
260 while( !_transform_types.empty() && _transform_types.back() == TT_NONE )
261 _transform_types.pop_back();
263 _transform_dirty = true;
270 //----------------------------------------------------------------------------
271 void Element::valueChanged(SGPropertyNode* child)
273 SGPropertyNode *parent = child->getParent();
274 if( parent == _node )
276 if( setStyle(child) )
278 else if( child->getNameString() == "update" )
280 else if( child->getNameString() == "visible" )
281 // TODO check if we need another nodemask
282 return _transform->setNodeMask( child->getBoolValue() ? 0xffffffff : 0 );
284 else if( parent->getParent() == _node
285 && parent->getNameString() == NAME_TRANSFORM )
287 _transform_dirty = true;
294 //----------------------------------------------------------------------------
295 bool Element::setStyle(const SGPropertyNode* child)
297 StyleSetters::const_iterator setter =
298 _style_setters.find(child->getNameString());
299 if( setter == _style_setters.end() )
302 setter->second(child);
306 //----------------------------------------------------------------------------
307 void Element::setBoundingBox(const osg::BoundingBox& bb)
309 if( _bounding_box.empty() )
311 SGPropertyNode* bb_node = _node->getChild("bounding-box", 0, true);
312 _bounding_box.resize(4);
313 _bounding_box[0] = bb_node->getChild("min-x", 0, true);
314 _bounding_box[1] = bb_node->getChild("min-y", 0, true);
315 _bounding_box[2] = bb_node->getChild("max-x", 0, true);
316 _bounding_box[3] = bb_node->getChild("max-y", 0, true);
319 _bounding_box[0]->setFloatValue(bb._min.x());
320 _bounding_box[1]->setFloatValue(bb._min.y());
321 _bounding_box[2]->setFloatValue(bb._max.x());
322 _bounding_box[3]->setFloatValue(bb._max.y());
325 //----------------------------------------------------------------------------
326 osg::BoundingBox Element::getTransformedBounds(const osg::Matrix& m) const
328 return osg::BoundingBox();
331 //----------------------------------------------------------------------------
332 Element::Element( const CanvasWeakPtr& canvas,
333 const SGPropertyNode_ptr& node,
334 const Style& parent_style,
336 PropertyBasedElement(node),
339 _transform_dirty( false ),
340 _transform( new osg::MatrixTransform ),
341 _style( parent_style ),
348 "New canvas element " << node->getPath()
352 //----------------------------------------------------------------------------
353 void Element::setDrawable( osg::Drawable* drawable )
355 _drawable = drawable;
358 osg::ref_ptr<osg::Geode> geode = new osg::Geode;
359 geode->addDrawable(_drawable);
360 _transform->addChild(geode);
363 //----------------------------------------------------------------------------
364 void Element::setupStyle()
366 BOOST_FOREACH( Style::value_type style, _style )
367 setStyle(style.second);
370 } // namespace canvas
371 } // namespace simgear