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 * Create an canvas Element of type T
39 ElementPtr createElement( const CanvasWeakPtr& canvas,
40 const SGPropertyNode_ptr& node,
44 ElementPtr el( new T(canvas, node, style, parent) );
50 * Add canvas Element type to factory map
53 void add(ElementFactories& factories)
55 factories[T::TYPE_NAME] = &createElement<T>;
58 //----------------------------------------------------------------------------
59 ElementFactories Group::_child_factories;
60 const std::string Group::TYPE_NAME = "group";
62 //----------------------------------------------------------------------------
63 Group::Group( const CanvasWeakPtr& canvas,
64 const SGPropertyNode_ptr& node,
65 const Style& parent_style,
67 Element(canvas, node, parent_style, parent)
69 if( !isInit<Group>() )
71 add<Group>(_child_factories);
72 add<Image>(_child_factories);
73 add<Map >(_child_factories);
74 add<Path >(_child_factories);
75 add<Text >(_child_factories);
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 ChildList::iterator child = findChild(node);
100 if( child == _children.end() )
103 return child->second;
106 //----------------------------------------------------------------------------
107 ElementPtr Group::getChild(const std::string& id)
109 for( ChildList::iterator child = _children.begin();
110 child != _children.end();
113 if( child->second->get<std::string>("id") == id )
114 return child->second;
120 //----------------------------------------------------------------------------
121 ElementPtr Group::getOrCreateChild( const std::string& type,
122 const std::string& id )
124 ElementPtr child = getChild(id);
127 if( child->getProps()->getNameString() == type )
134 "Group::getOrCreateChild: type missmatch! "
135 "('" << type << "' != '" << child->getProps()->getName() << "', "
136 "id = '" << id << "')"
142 return createChild(type, id);
145 //----------------------------------------------------------------------------
146 ElementPtr Group::getElementById(const std::string& id)
148 std::vector<GroupPtr> groups;
150 BOOST_FOREACH( ChildList::value_type child, _children )
152 const ElementPtr& el = child.second;
153 if( el->getProps()->getStringValue("id") == id )
156 GroupPtr group = boost::dynamic_pointer_cast<Group>(el);
158 groups.push_back(group);
161 BOOST_FOREACH( GroupPtr group, groups )
163 ElementPtr el = group->getElementById(id);
171 //----------------------------------------------------------------------------
172 void Group::clearEventListener()
174 BOOST_FOREACH( ChildList::value_type child, _children )
175 child.second->clearEventListener();
177 Element::clearEventListener();
180 //----------------------------------------------------------------------------
181 void Group::update(double dt)
183 BOOST_FOREACH( ChildList::value_type child, _children )
184 child.second->update(dt);
189 //----------------------------------------------------------------------------
190 bool Group::traverse(EventVisitor& visitor)
192 // Iterate in reverse order as last child is displayed on top
193 BOOST_REVERSE_FOREACH( ChildList::value_type child, _children )
195 if( child.second->accept(visitor) )
201 //----------------------------------------------------------------------------
202 bool Group::setStyle(const SGPropertyNode* style)
204 // Don't propagate styles directly applicable to this group
205 if( Element::setStyle(style) )
208 if( style->getParent() != _node
209 && _style.find(style->getNameString()) != _style.end() )
212 bool handled = false;
213 BOOST_FOREACH( ChildList::value_type child, _children )
215 if( child.second->setStyle(style) )
222 //----------------------------------------------------------------------------
223 osg::BoundingBox Group::getTransformedBounds(const osg::Matrix& m) const
227 BOOST_FOREACH( ChildList::value_type child, _children )
229 if( !child.second->getMatrixTransform()->getNodeMask() )
234 child.second->getTransformedBounds
236 child.second->getMatrixTransform()->getMatrix() * m
244 //----------------------------------------------------------------------------
245 void Group::childAdded(SGPropertyNode* child)
247 if( child->getParent() != _node )
250 ElementFactories::iterator child_factory =
251 _child_factories.find( child->getNameString() );
252 if( child_factory != _child_factories.end() )
254 ElementPtr element = child_factory->second(_canvas, child, _style, this);
256 // Add to osg scene graph...
257 _transform->addChild( element->getMatrixTransform() );
258 _children.push_back( ChildList::value_type(child, element) );
260 // ...and ensure correct ordering
261 handleZIndexChanged( --_children.end() );
266 if( !Element::setStyle(child) )
268 // Only add style if not applicable to group itself
269 _style[ child->getNameString() ] = child;
274 //----------------------------------------------------------------------------
275 void Group::childRemoved(SGPropertyNode* node)
277 if( node->getParent() != _node )
280 if( _child_factories.find(node->getNameString()) != _child_factories.end() )
282 ChildList::iterator child = findChild(node);
283 if( child == _children.end() )
288 "can't removed unknown child " << node->getDisplayName()
292 _transform->removeChild( child->second->getMatrixTransform() );
293 _children.erase(child);
298 Style::iterator style = _style.find(node->getNameString());
299 if( style != _style.end() )
304 //----------------------------------------------------------------------------
305 void Group::childChanged(SGPropertyNode* node)
307 if( node->getParent()->getParent() == _node
308 && node->getNameString() == "z-index" )
309 return handleZIndexChanged( findChild(node->getParent()),
310 node->getIntValue() );
313 //----------------------------------------------------------------------------
314 void Group::handleZIndexChanged(ChildList::iterator child, int z_index)
316 if( child == _children.end() )
319 osg::Node* tf = child->second->getMatrixTransform();
320 int index = _transform->getChildIndex(tf),
323 ChildList::iterator next = child;
326 while( next != _children.end()
327 && next->first->getIntValue("z-index", 0) <= z_index )
333 if( index_new != index )
335 _children.insert(next, *child);
339 ChildList::iterator prev = child,
341 while( check != _children.begin()
342 && (--check)->first->getIntValue("z-index", 0) > z_index )
348 if( index == index_new )
351 _children.insert(prev, *child);
354 _transform->removeChild(index);
355 _transform->insertChild(index_new, tf);
357 _children.erase(child);
363 "canvas::Group: Moved element " << index << " to position " << index_new
367 //----------------------------------------------------------------------------
368 Group::ChildList::iterator Group::findChild(const SGPropertyNode* node)
374 boost::bind(&ChildList::value_type::first, _1) == node
378 } // namespace canvas
379 } // namespace simgear