1 // A group of 2D Canvas elements
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 "CanvasGroup.hxx"
20 #include "CanvasImage.hxx"
21 #include "CanvasMap.hxx"
22 #include "CanvasPath.hxx"
23 #include "CanvasText.hxx"
24 #include <simgear/canvas/CanvasEventVisitor.hxx>
25 #include <simgear/canvas/MouseEvent.hxx>
27 #include <boost/bind.hpp>
28 #include <boost/foreach.hpp>
29 #include <boost/lambda/core.hpp>
36 * Add canvas Element type to factory map
38 template<typename ElementType>
39 void add(ElementFactories& factories)
41 factories[ElementType::TYPE_NAME] = &Element::create<ElementType>;
44 //----------------------------------------------------------------------------
45 ElementFactories Group::_child_factories;
46 const std::string Group::TYPE_NAME = "group";
48 void warnTransformExpired(const char* member_name)
52 "canvas::Group::" << member_name << ": Group has expired." );
55 //----------------------------------------------------------------------------
56 Group::Group( const CanvasWeakPtr& canvas,
57 const SGPropertyNode_ptr& node,
58 const Style& parent_style,
60 Element(canvas, node, parent_style, parent)
62 if( !isInit<Group>() )
64 add<Group>(_child_factories);
65 add<Image>(_child_factories);
66 add<Map >(_child_factories);
67 add<Path >(_child_factories);
68 add<Text >(_child_factories);
72 //----------------------------------------------------------------------------
78 //----------------------------------------------------------------------------
79 ElementPtr Group::createChild( const std::string& type,
80 const std::string& id )
82 SGPropertyNode* node = _node->addChild(type, 0, false);
84 node->setStringValue("id", id);
86 return getChild(node);
89 //----------------------------------------------------------------------------
90 ElementPtr Group::getChild(const SGPropertyNode* node)
92 return findChild(node, "");
95 //----------------------------------------------------------------------------
96 ElementPtr Group::getChild(const std::string& id)
98 return findChild(0, id);
101 //----------------------------------------------------------------------------
102 ElementPtr Group::getOrCreateChild( const std::string& type,
103 const std::string& id )
105 ElementPtr child = getChild(id);
108 if( child->getProps()->getNameString() == type )
115 "Group::getOrCreateChild: type missmatch! "
116 "('" << type << "' != '" << child->getProps()->getName() << "', "
117 "id = '" << id << "')"
123 return createChild(type, id);
126 //----------------------------------------------------------------------------
127 ElementPtr Group::getElementById(const std::string& id)
129 if( !_transform.valid() )
131 warnTransformExpired("getElementById");
135 std::vector<GroupPtr> groups;
136 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
138 const ElementPtr& el = getChildByIndex(i);
139 if( el->get<std::string>("id") == id )
142 GroupPtr group = boost::dynamic_pointer_cast<Group>(el);
144 groups.push_back(group);
147 BOOST_FOREACH( GroupPtr group, groups )
149 ElementPtr el = group->getElementById(id);
157 //----------------------------------------------------------------------------
158 void Group::clearEventListener()
160 if( !_transform.valid() )
161 return warnTransformExpired("clearEventListener");
163 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
164 getChildByIndex(i)->clearEventListener();
166 Element::clearEventListener();
169 //----------------------------------------------------------------------------
170 void Group::update(double dt)
172 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
173 getChildByIndex(i)->update(dt);
178 //----------------------------------------------------------------------------
179 bool Group::traverse(EventVisitor& visitor)
181 // Iterate in reverse order as last child is displayed on top
182 for(size_t i = _transform->getNumChildren(); i --> 0;)
184 if( getChildByIndex(i)->accept(visitor) )
190 //----------------------------------------------------------------------------
191 bool Group::setStyle( const SGPropertyNode* style,
192 const StyleInfo* style_info )
194 if( !canApplyStyle(style) )
197 bool handled = setStyleImpl(style, style_info);
198 if( style_info->inheritable )
200 if( !_transform.valid() )
202 warnTransformExpired("setStyle");
206 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
207 handled |= getChildByIndex(i)->setStyle(style, style_info);
213 //----------------------------------------------------------------------------
214 osg::BoundingBox Group::getTransformedBounds(const osg::Matrix& m) const
217 if( !_transform.valid() )
219 warnTransformExpired("getTransformedBounds");
223 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
225 const ElementPtr& child = getChildByIndex(i);
226 if( !child->getMatrixTransform()->getNodeMask() )
231 child->getTransformedBounds
233 child->getMatrixTransform()->getMatrix() * m
241 //----------------------------------------------------------------------------
242 ElementFactory Group::getChildFactory(const std::string& type) const
244 ElementFactories::iterator child_factory = _child_factories.find(type);
245 if( child_factory != _child_factories.end() )
246 return child_factory->second;
248 return ElementFactory();
251 //----------------------------------------------------------------------------
252 void Group::childAdded(SGPropertyNode* child)
254 if( child->getParent() != _node )
257 ElementFactory child_factory = getChildFactory( child->getNameString() );
260 if( !_transform.valid() )
261 return warnTransformExpired("childAdded");
263 ElementPtr element = child_factory(_canvas, child, _style, this);
265 // Add to osg scene graph...
266 _transform->addChild( element->getMatrixTransform() );
268 // ...and ensure correct ordering
269 handleZIndexChanged(element);
274 StyleInfo const* style = getStyleInfo(child->getNameString());
275 if( style && style->inheritable )
276 _style[ child->getNameString() ] = child;
279 //----------------------------------------------------------------------------
280 void Group::childRemoved(SGPropertyNode* node)
282 if( node->getParent() != _node )
285 if( getChildFactory(node->getNameString()) )
287 if( !_transform.valid() )
288 // If transform is destroyed also all children are destroyed, so we can
289 // not do anything here.
292 ElementPtr child = getChild(node);
298 "can't removed unknown child " << node->getDisplayName()
302 // Remove child from the scenegraph (this automatically invalidates the
303 // reference on the element hold by the MatrixTransform)
309 _style.erase( node->getNameString() );
313 //----------------------------------------------------------------------------
314 void Group::childChanged(SGPropertyNode* node)
316 if( node->getParent()->getParent() == _node
317 && node->getNameString() == "z-index" )
318 return handleZIndexChanged( getChild(node->getParent()),
319 node->getIntValue() );
322 //----------------------------------------------------------------------------
323 void Group::handleZIndexChanged(ElementPtr child, int z_index)
325 if( !child || !_transform.valid() )
328 osg::ref_ptr<osg::MatrixTransform> tf = child->getMatrixTransform();
329 size_t index = _transform->getChildIndex(tf),
334 if( index_new + 1 == _transform->getNumChildren() )
337 // Move to end of block with same index (= move upwards until the next
338 // element has a higher index)
339 if( getChildByIndex(index_new + 1)->get<int>("z-index", 0) > z_index )
343 if( index_new == index )
345 // We were not able to move upwards so now try downwards
351 // Move to end of block with same index (= move downwards until the
352 // previous element has the same or a lower index)
353 if( getChildByIndex(index_new - 1)->get<int>("z-index", 0) <= z_index )
357 if( index == index_new )
361 _transform->removeChild(index);
362 _transform->insertChild(index_new, tf);
368 "canvas::Group: Moved element " << index << " to position " << index_new
372 //----------------------------------------------------------------------------
373 ElementPtr Group::getChildByIndex(size_t index) const
375 assert(_transform.valid());
377 static_cast<OSGUserData*>(_transform->getChild(index)->getUserData());
382 //----------------------------------------------------------------------------
383 ElementPtr Group::findChild( const SGPropertyNode* node,
384 const std::string& id ) const
386 if( !_transform.valid() )
388 warnTransformExpired("findChild");
392 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
394 ElementPtr el = getChildByIndex(i);
398 if( el->getProps() == node )
403 if( el->get<std::string>("id") == id )
411 } // namespace canvas
412 } // namespace simgear