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 //----------------------------------------------------------------------------
49 Group::Group( const CanvasWeakPtr& canvas,
50 const SGPropertyNode_ptr& node,
51 const Style& parent_style,
53 Element(canvas, node, parent_style, parent)
55 if( !isInit<Group>() )
57 add<Group>(_child_factories);
58 add<Image>(_child_factories);
59 add<Map >(_child_factories);
60 add<Path >(_child_factories);
61 add<Text >(_child_factories);
65 //----------------------------------------------------------------------------
71 //----------------------------------------------------------------------------
72 ElementPtr Group::createChild( const std::string& type,
73 const std::string& id )
75 SGPropertyNode* node = _node->addChild(type, 0, false);
77 node->setStringValue("id", id);
79 return getChild(node);
82 //----------------------------------------------------------------------------
83 ElementPtr Group::getChild(const SGPropertyNode* node)
85 return findChild(node, "");
88 //----------------------------------------------------------------------------
89 ElementPtr Group::getChild(const std::string& id)
91 return findChild(0, id);
94 //----------------------------------------------------------------------------
95 ElementPtr Group::getOrCreateChild( const std::string& type,
96 const std::string& id )
98 ElementPtr child = getChild(id);
101 if( child->getProps()->getNameString() == type )
108 "Group::getOrCreateChild: type missmatch! "
109 "('" << type << "' != '" << child->getProps()->getName() << "', "
110 "id = '" << id << "')"
116 return createChild(type, id);
119 //----------------------------------------------------------------------------
120 ElementPtr Group::getElementById(const std::string& id)
122 std::vector<GroupPtr> groups;
124 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
126 const ElementPtr& el = getChildByIndex(i);
127 if( el->get<std::string>("id") == id )
130 GroupPtr group = boost::dynamic_pointer_cast<Group>(el);
132 groups.push_back(group);
135 BOOST_FOREACH( GroupPtr group, groups )
137 ElementPtr el = group->getElementById(id);
145 //----------------------------------------------------------------------------
146 void Group::clearEventListener()
149 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
150 getChildByIndex(i)->clearEventListener();
152 Element::clearEventListener();
155 //----------------------------------------------------------------------------
156 void Group::update(double dt)
158 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
159 getChildByIndex(i)->update(dt);
164 //----------------------------------------------------------------------------
165 bool Group::traverse(EventVisitor& visitor)
167 // Iterate in reverse order as last child is displayed on top
168 for(size_t i = _transform->getNumChildren(); i --> 0;)
170 if( getChildByIndex(i)->accept(visitor) )
176 //----------------------------------------------------------------------------
177 bool Group::setStyle(const SGPropertyNode* style)
179 // Don't propagate styles directly applicable to this group
180 if( Element::setStyle(style) )
183 if( style->getParent() != _node
184 && _style.find(style->getNameString()) != _style.end() )
187 bool handled = false;
188 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
190 if( getChildByIndex(i)->setStyle(style) )
197 //----------------------------------------------------------------------------
198 osg::BoundingBox Group::getTransformedBounds(const osg::Matrix& m) const
202 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
204 const ElementPtr& child = getChildByIndex(i);
205 if( !child->getMatrixTransform()->getNodeMask() )
210 child->getTransformedBounds
212 child->getMatrixTransform()->getMatrix() * m
220 //----------------------------------------------------------------------------
221 ElementFactory Group::getChildFactory(const std::string& type) const
223 ElementFactories::iterator child_factory = _child_factories.find(type);
224 if( child_factory != _child_factories.end() )
225 return child_factory->second;
227 return ElementFactory();
230 //----------------------------------------------------------------------------
231 void Group::childAdded(SGPropertyNode* child)
233 if( child->getParent() != _node )
236 ElementFactory child_factory = getChildFactory( child->getNameString() );
239 ElementPtr element = child_factory(_canvas, child, _style, this);
241 // Add to osg scene graph...
242 _transform->addChild( element->getMatrixTransform() );
244 // ...and ensure correct ordering
245 handleZIndexChanged(element);
250 if( !Element::setStyle(child) )
252 // Only add style if not applicable to group itself
253 _style[ child->getNameString() ] = child;
258 //----------------------------------------------------------------------------
259 void Group::childRemoved(SGPropertyNode* node)
261 if( node->getParent() != _node )
264 if( getChildFactory(node->getNameString()) )
266 if( !_transform.valid() )
267 // If transform is destroyed also all children are destroyed, so we can
268 // not do anything here.
271 ElementPtr child = getChild(node);
277 "can't removed unknown child " << node->getDisplayName()
281 // Remove child from the scenegraph (this automatically invalidates the
282 // reference on the element hold by the MatrixTransform)
288 Style::iterator style = _style.find(node->getNameString());
289 if( style != _style.end() )
294 //----------------------------------------------------------------------------
295 void Group::childChanged(SGPropertyNode* node)
297 if( node->getParent()->getParent() == _node
298 && node->getNameString() == "z-index" )
299 return handleZIndexChanged( getChild(node->getParent()),
300 node->getIntValue() );
303 //----------------------------------------------------------------------------
304 void Group::handleZIndexChanged(ElementPtr child, int z_index)
309 osg::ref_ptr<osg::MatrixTransform> tf = child->getMatrixTransform();
310 size_t index = _transform->getChildIndex(tf),
315 if( index_new + 1 == _transform->getNumChildren() )
318 // Move to end of block with same index (= move upwards until the next
319 // element has a higher index)
320 if( getChildByIndex(index_new + 1)->get<int>("z-index", 0) > z_index )
324 if( index_new == index )
326 // We were not able to move upwards so now try downwards
332 // Move to end of block with same index (= move downwards until the
333 // previous element has the same or a lower index)
334 if( getChildByIndex(index_new - 1)->get<int>("z-index", 0) <= z_index )
338 if( index == index_new )
342 _transform->removeChild(index);
343 _transform->insertChild(index_new, tf);
349 "canvas::Group: Moved element " << index << " to position " << index_new
353 //----------------------------------------------------------------------------
354 ElementPtr Group::getChildByIndex(size_t index) const
356 assert(_transform.valid());
358 static_cast<OSGUserData*>(_transform->getChild(index)->getUserData());
363 //----------------------------------------------------------------------------
364 ElementPtr Group::findChild( const SGPropertyNode* node,
365 const std::string& id ) const
367 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
369 ElementPtr el = getChildByIndex(i);
373 if( el->getProps() == node )
378 if( el->get<std::string>("id") == id )
386 } // namespace canvas
387 } // namespace simgear