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) );
49 //----------------------------------------------------------------------------
50 ElementFactories Group::_child_factories;
52 //----------------------------------------------------------------------------
53 Group::Group( const CanvasWeakPtr& canvas,
54 const SGPropertyNode_ptr& node,
55 const Style& parent_style,
57 Element(canvas, node, parent_style, parent)
59 if( !isInit<Group>() )
61 _child_factories["group"] = &createElement<Group>;
62 _child_factories["image"] = &createElement<Image>;
63 _child_factories["map" ] = &createElement<Map >;
64 _child_factories["path" ] = &createElement<Path >;
65 _child_factories["text" ] = &createElement<Text >;
69 //----------------------------------------------------------------------------
75 //----------------------------------------------------------------------------
76 ElementPtr Group::createChild( const std::string& type,
77 const std::string& id )
79 SGPropertyNode* node = _node->addChild(type, 0, false);
81 node->setStringValue("id", id);
83 return getChild(node);
86 //----------------------------------------------------------------------------
87 ElementPtr Group::getChild(const SGPropertyNode* node)
89 ChildList::iterator child = findChild(node);
90 if( child == _children.end() )
96 //----------------------------------------------------------------------------
97 ElementPtr Group::getElementById(const std::string& id)
99 std::vector<GroupPtr> groups;
101 BOOST_FOREACH( ChildList::value_type child, _children )
103 const ElementPtr& el = child.second;
104 if( el->getProps()->getStringValue("id") == id )
107 GroupPtr group = boost::dynamic_pointer_cast<Group>(el);
109 groups.push_back(group);
112 BOOST_FOREACH( GroupPtr group, groups )
114 ElementPtr el = group->getElementById(id);
122 //----------------------------------------------------------------------------
123 void Group::update(double dt)
125 BOOST_FOREACH( ChildList::value_type child, _children )
126 child.second->update(dt);
131 //----------------------------------------------------------------------------
132 bool Group::traverse(EventVisitor& visitor)
134 // Iterate in reverse order as last child is displayed on top
135 BOOST_REVERSE_FOREACH( ChildList::value_type child, _children )
137 if( child.second->accept(visitor) )
143 //----------------------------------------------------------------------------
144 bool Group::setStyle(const SGPropertyNode* style)
146 // Don't propagate styles directly applicable to this group
147 if( Element::setStyle(style) )
150 if( style->getParent() != _node
151 && _style.find(style->getNameString()) != _style.end() )
154 bool handled = false;
155 BOOST_FOREACH( ChildList::value_type child, _children )
157 if( child.second->setStyle(style) )
164 //----------------------------------------------------------------------------
165 osg::BoundingBox Group::getTransformedBounds(const osg::Matrix& m) const
169 BOOST_FOREACH( ChildList::value_type child, _children )
171 if( !child.second->getMatrixTransform()->getNodeMask() )
176 child.second->getTransformedBounds
178 child.second->getMatrixTransform()->getMatrix() * m
186 //----------------------------------------------------------------------------
187 void Group::childAdded(SGPropertyNode* child)
189 if( child->getParent() != _node )
192 ElementFactories::iterator child_factory =
193 _child_factories.find( child->getNameString() );
194 if( child_factory != _child_factories.end() )
196 ElementPtr element = child_factory->second(_canvas, child, _style, this);
198 // Add to osg scene graph...
199 _transform->addChild( element->getMatrixTransform() );
200 _children.push_back( ChildList::value_type(child, element) );
202 // ...and ensure correct ordering
203 handleZIndexChanged( --_children.end() );
208 if( !Element::setStyle(child) )
210 // Only add style if not applicable to group itself
211 _style[ child->getNameString() ] = child;
216 //----------------------------------------------------------------------------
217 void Group::childRemoved(SGPropertyNode* node)
219 if( node->getParent() != _node )
222 if( _child_factories.find(node->getNameString()) != _child_factories.end() )
224 ChildList::iterator child = findChild(node);
225 if( child == _children.end() )
230 "can't removed unknown child " << node->getDisplayName()
234 _transform->removeChild( child->second->getMatrixTransform() );
235 _children.erase(child);
240 Style::iterator style = _style.find(node->getNameString());
241 if( style != _style.end() )
246 //----------------------------------------------------------------------------
247 void Group::childChanged(SGPropertyNode* node)
249 if( node->getParent()->getParent() == _node
250 && node->getNameString() == "z-index" )
251 return handleZIndexChanged( findChild(node->getParent()),
252 node->getIntValue() );
255 //----------------------------------------------------------------------------
256 void Group::handleZIndexChanged(ChildList::iterator child, int z_index)
258 if( child == _children.end() )
261 osg::Node* tf = child->second->getMatrixTransform();
262 int index = _transform->getChildIndex(tf),
265 ChildList::iterator next = child;
268 while( next != _children.end()
269 && next->first->getIntValue("z-index", 0) <= z_index )
275 if( index_new != index )
277 _children.insert(next, *child);
281 ChildList::iterator prev = child;
282 while( prev != _children.begin()
283 && (--prev)->first->getIntValue("z-index", 0) > z_index)
288 if( index == index_new )
291 _children.insert(prev, *child);
294 _transform->removeChild(index);
295 _transform->insertChild(index_new, tf);
297 _children.erase(child);
303 "canvas::Group: Moved element " << index << " to position " << index_new
307 //----------------------------------------------------------------------------
308 Group::ChildList::iterator Group::findChild(const SGPropertyNode* node)
314 boost::bind(&ChildList::value_type::first, _1) == node
318 } // namespace canvas
319 } // namespace simgear