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/events/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 ElementType::staticInit();
42 factories[ElementType::TYPE_NAME] = &Element::create<ElementType>;
45 //----------------------------------------------------------------------------
46 ElementFactories Group::_child_factories;
47 const std::string Group::TYPE_NAME = "group";
49 void warnTransformExpired(const char* member_name)
53 "canvas::Group::" << member_name << ": Group has expired." );
56 //----------------------------------------------------------------------------
57 void Group::staticInit()
62 add<Group>(_child_factories);
63 add<Image>(_child_factories);
64 add<Map >(_child_factories);
65 add<Path >(_child_factories);
66 add<Text >(_child_factories);
69 //----------------------------------------------------------------------------
70 Group::Group( const CanvasWeakPtr& canvas,
71 const SGPropertyNode_ptr& node,
72 const Style& parent_style,
74 Element(canvas, node, parent_style, parent)
79 //----------------------------------------------------------------------------
85 //----------------------------------------------------------------------------
86 ElementPtr Group::createChild( const std::string& type,
87 const std::string& id )
89 SGPropertyNode* node = _node->addChild(type, 0, false);
91 node->setStringValue("id", id);
93 return getChild(node);
96 //----------------------------------------------------------------------------
97 ElementPtr Group::getChild(const SGPropertyNode* node)
99 return findChild(node, "");
102 //----------------------------------------------------------------------------
103 ElementPtr Group::getChild(const std::string& id)
105 return findChild(0, id);
108 //----------------------------------------------------------------------------
109 ElementPtr Group::getOrCreateChild( const std::string& type,
110 const std::string& id )
112 ElementPtr child = getChild(id);
115 if( child->getProps()->getNameString() == type )
122 "Group::getOrCreateChild: type missmatch! "
123 "('" << type << "' != '" << child->getProps()->getName() << "', "
124 "id = '" << id << "')"
130 return createChild(type, id);
133 //----------------------------------------------------------------------------
134 ElementPtr Group::getElementById(const std::string& id)
136 if( !_transform.valid() )
138 warnTransformExpired("getElementById");
142 std::vector<GroupPtr> groups;
143 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
145 const ElementPtr& el = getChildByIndex(i);
146 if( el->get<std::string>("id") == id )
149 Group* group = dynamic_cast<Group*>(el.get());
151 groups.push_back(group);
154 BOOST_FOREACH( GroupPtr group, groups )
156 ElementPtr el = group->getElementById(id);
164 //----------------------------------------------------------------------------
165 void Group::clearEventListener()
167 if( !_transform.valid() )
168 return warnTransformExpired("clearEventListener");
170 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
171 getChildByIndex(i)->clearEventListener();
173 Element::clearEventListener();
176 //----------------------------------------------------------------------------
177 void Group::update(double dt)
179 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
180 getChildByIndex(i)->update(dt);
185 //----------------------------------------------------------------------------
186 bool Group::traverse(EventVisitor& visitor)
188 // Iterate in reverse order as last child is displayed on top
189 for(size_t i = _transform->getNumChildren(); i --> 0;)
191 if( getChildByIndex(i)->accept(visitor) )
197 //----------------------------------------------------------------------------
198 bool Group::setStyle( const SGPropertyNode* style,
199 const StyleInfo* style_info )
201 if( !canApplyStyle(style) )
204 bool handled = setStyleImpl(style, style_info);
205 if( style_info->inheritable )
207 if( !_transform.valid() )
209 warnTransformExpired("setStyle");
213 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
214 handled |= getChildByIndex(i)->setStyle(style, style_info);
220 //----------------------------------------------------------------------------
221 osg::BoundingBox Group::getTransformedBounds(const osg::Matrix& m) const
224 if( !_transform.valid() )
226 warnTransformExpired("getTransformedBounds");
230 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
232 const ElementPtr& child = getChildByIndex(i);
233 if( !child->getMatrixTransform()->getNodeMask() )
238 child->getTransformedBounds
240 child->getMatrixTransform()->getMatrix() * m
248 //----------------------------------------------------------------------------
249 ElementFactory Group::getChildFactory(const std::string& type) const
251 ElementFactories::iterator child_factory = _child_factories.find(type);
252 if( child_factory != _child_factories.end() )
253 return child_factory->second;
255 return ElementFactory();
258 //----------------------------------------------------------------------------
259 void Group::childAdded(SGPropertyNode* child)
261 if( child->getParent() != _node )
264 ElementFactory child_factory = getChildFactory( child->getNameString() );
267 if( !_transform.valid() )
268 return warnTransformExpired("childAdded");
270 ElementPtr element = child_factory(_canvas, child, _style, this);
272 // Add to osg scene graph...
273 _transform->addChild( element->getMatrixTransform() );
275 // ...and ensure correct ordering
276 handleZIndexChanged(element);
281 StyleInfo const* style = getStyleInfo(child->getNameString());
282 if( style && style->inheritable )
283 _style[ child->getNameString() ] = child;
286 //----------------------------------------------------------------------------
287 void Group::childRemoved(SGPropertyNode* node)
289 if( node->getParent() != _node )
292 if( getChildFactory(node->getNameString()) )
294 if( !_transform.valid() )
295 // If transform is destroyed also all children are destroyed, so we can
296 // not do anything here.
299 ElementPtr child = getChild(node);
305 "can't removed unknown child " << node->getDisplayName()
309 // Remove child from the scenegraph (this automatically invalidates the
310 // reference on the element hold by the MatrixTransform)
316 _style.erase( node->getNameString() );
320 //----------------------------------------------------------------------------
321 void Group::childChanged(SGPropertyNode* node)
323 SGPropertyNode* parent = node->getParent();
324 SGPropertyNode* grand_parent = parent ? parent->getParent() : NULL;
326 if( grand_parent == _node
327 && node->getNameString() == "z-index" )
328 return handleZIndexChanged(getChild(parent), node->getIntValue());
331 //----------------------------------------------------------------------------
332 void Group::handleZIndexChanged(ElementPtr child, int z_index)
334 if( !child || !_transform.valid() )
337 osg::ref_ptr<osg::MatrixTransform> tf = child->getMatrixTransform();
338 size_t index = _transform->getChildIndex(tf),
343 if( index_new + 1 == _transform->getNumChildren() )
346 // Move to end of block with same index (= move upwards until the next
347 // element has a higher index)
348 if( getChildByIndex(index_new + 1)->get<int>("z-index", 0) > z_index )
352 if( index_new == index )
354 // We were not able to move upwards so now try downwards
360 // Move to end of block with same index (= move downwards until the
361 // previous element has the same or a lower index)
362 if( getChildByIndex(index_new - 1)->get<int>("z-index", 0) <= z_index )
366 if( index == index_new )
370 _transform->removeChild(index);
371 _transform->insertChild(index_new, tf);
377 "canvas::Group: Moved element " << index << " to position " << index_new
381 //----------------------------------------------------------------------------
382 ElementPtr Group::getChildByIndex(size_t index) const
384 assert(_transform.valid());
386 static_cast<OSGUserData*>(_transform->getChild(index)->getUserData());
391 //----------------------------------------------------------------------------
392 ElementPtr Group::findChild( const SGPropertyNode* node,
393 const std::string& id ) const
395 if( !_transform.valid() )
397 warnTransformExpired("findChild");
401 for(size_t i = 0; i < _transform->getNumChildren(); ++i)
403 ElementPtr el = getChildByIndex(i);
407 if( el->getProps() == node )
412 if( el->get<std::string>("id") == id )
420 } // namespace canvas
421 } // namespace simgear