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 Group::Group( const CanvasWeakPtr& canvas,
51 const SGPropertyNode_ptr& node,
52 const Style& parent_style,
54 Element(canvas, node, parent_style, parent)
56 _child_factories["group"] = &createElement<Group>;
57 _child_factories["image"] = &createElement<Image>;
58 _child_factories["map" ] = &createElement<Map >;
59 _child_factories["path" ] = &createElement<Path >;
60 _child_factories["text" ] = &createElement<Text >;
63 //----------------------------------------------------------------------------
69 //----------------------------------------------------------------------------
70 ElementPtr Group::createChild( const std::string& type,
71 const std::string& id )
73 SGPropertyNode* node = _node->addChild(type, 0, false);
75 node->setStringValue("id", id);
77 return getChild(node);
80 //----------------------------------------------------------------------------
81 ElementPtr Group::getChild(const SGPropertyNode* node)
83 ChildList::iterator child = findChild(node);
84 if( child == _children.end() )
90 //----------------------------------------------------------------------------
91 ElementPtr Group::getElementById(const std::string& id)
93 std::vector<GroupPtr> groups;
95 BOOST_FOREACH( ChildList::value_type child, _children )
97 const ElementPtr& el = child.second;
98 if( el->getProps()->getStringValue("id") == id )
101 GroupPtr group = boost::dynamic_pointer_cast<Group>(el);
103 groups.push_back(group);
106 BOOST_FOREACH( GroupPtr group, groups )
108 ElementPtr el = group->getElementById(id);
116 //----------------------------------------------------------------------------
117 void Group::update(double dt)
119 BOOST_FOREACH( ChildList::value_type child, _children )
120 child.second->update(dt);
125 //----------------------------------------------------------------------------
126 bool Group::traverse(EventVisitor& visitor)
128 // Iterate in reverse order as last child is displayed on top
129 BOOST_REVERSE_FOREACH( ChildList::value_type child, _children )
131 if( child.second->accept(visitor) )
137 //----------------------------------------------------------------------------
138 bool Group::setStyle(const SGPropertyNode* style)
140 // Don't propagate styles directly applicable to this group
141 if( Element::setStyle(style) )
144 if( style->getParent() != _node
145 && _style.find(style->getNameString()) != _style.end() )
148 bool handled = false;
149 BOOST_FOREACH( ChildList::value_type child, _children )
151 if( child.second->setStyle(style) )
158 //----------------------------------------------------------------------------
159 osg::BoundingBox Group::getTransformedBounds(const osg::Matrix& m) const
163 BOOST_FOREACH( ChildList::value_type child, _children )
165 if( !child.second->getMatrixTransform()->getNodeMask() )
170 child.second->getTransformedBounds
172 child.second->getMatrixTransform()->getMatrix() * m
180 //----------------------------------------------------------------------------
181 void Group::childAdded(SGPropertyNode* child)
183 if( child->getParent() != _node )
186 ChildFactories::iterator child_factory =
187 _child_factories.find( child->getNameString() );
188 if( child_factory != _child_factories.end() )
190 ElementPtr element = child_factory->second(_canvas, child, _style, this);
192 // Add to osg scene graph...
193 _transform->addChild( element->getMatrixTransform() );
194 _children.push_back( ChildList::value_type(child, element) );
199 if( !Element::setStyle(child) )
201 // Only add style if not applicable to group itself
202 _style[ child->getNameString() ] = child;
207 //----------------------------------------------------------------------------
208 void Group::childRemoved(SGPropertyNode* node)
210 if( node->getParent() != _node )
213 if( _child_factories.find(node->getNameString()) != _child_factories.end() )
215 ChildList::iterator child = findChild(node);
216 if( child == _children.end() )
221 "can't removed unknown child " << node->getDisplayName()
225 _transform->removeChild( child->second->getMatrixTransform() );
226 _children.erase(child);
231 Style::iterator style = _style.find(node->getNameString());
232 if( style != _style.end() )
237 //----------------------------------------------------------------------------
238 void Group::childChanged(SGPropertyNode* node)
240 if( node->getParent()->getParent() == _node
241 && node->getNameString() == "z-index" )
242 return handleZIndexChanged(node->getParent(), node->getIntValue());
245 //----------------------------------------------------------------------------
246 void Group::handleZIndexChanged(SGPropertyNode* node, int z_index)
248 ChildList::iterator child = findChild(node);
249 if( child == _children.end() )
252 osg::Node* tf = child->second->getMatrixTransform();
253 int index = _transform->getChildIndex(tf),
256 ChildList::iterator next = child;
259 while( next != _children.end()
260 && next->first->getIntValue("z-index", 0) <= z_index )
266 if( index_new != index )
268 _children.insert(next, *child);
272 ChildList::iterator prev = child;
273 while( prev != _children.begin()
274 && (--prev)->first->getIntValue("z-index", 0) > z_index)
279 if( index == index_new )
282 _children.insert(prev, *child);
285 _transform->removeChild(index);
286 _transform->insertChild(index_new, tf);
288 _children.erase(child);
294 "canvas::Group: Moved element " << index << " to position " << index_new
298 //----------------------------------------------------------------------------
299 Group::ChildList::iterator Group::findChild(const SGPropertyNode* node)
305 boost::bind(&ChildList::value_type::first, _1) == node
309 } // namespace canvas
310 } // namespace simgear