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 return ElementPtr( new T(canvas, node, style, parent) );
47 //----------------------------------------------------------------------------
48 Group::Group( const CanvasWeakPtr& canvas,
49 const SGPropertyNode_ptr& node,
50 const Style& parent_style,
52 Element(canvas, node, parent_style, parent)
54 _child_factories["group"] = &createElement<Group>;
55 _child_factories["image"] = &createElement<Image>;
56 _child_factories["map" ] = &createElement<Map >;
57 _child_factories["path" ] = &createElement<Path >;
58 _child_factories["text" ] = &createElement<Text >;
61 //----------------------------------------------------------------------------
67 //----------------------------------------------------------------------------
68 ElementPtr Group::createChild( const std::string& type,
69 const std::string& id )
71 SGPropertyNode* node = _node->addChild(type, 0, false);
73 node->setStringValue("id", id);
75 return getChild(node);
78 //----------------------------------------------------------------------------
79 ElementPtr Group::getChild(const SGPropertyNode* node)
81 ChildList::iterator child = findChild(node);
82 if( child == _children.end() )
88 //----------------------------------------------------------------------------
89 ElementPtr Group::getElementById(const std::string& id)
91 std::vector<GroupPtr> groups;
93 BOOST_FOREACH( ChildList::value_type child, _children )
95 const ElementPtr& el = child.second;
96 if( el->getProps()->getStringValue("id") == id )
99 GroupPtr group = boost::dynamic_pointer_cast<Group>(el);
101 groups.push_back(group);
104 BOOST_FOREACH( GroupPtr group, groups )
106 ElementPtr el = group->getElementById(id);
114 //----------------------------------------------------------------------------
115 void Group::update(double dt)
117 BOOST_FOREACH( ChildList::value_type child, _children )
118 child.second->update(dt);
123 //----------------------------------------------------------------------------
124 bool Group::traverse(EventVisitor& visitor)
126 // Iterate in reverse order as last child is displayed on top
127 BOOST_REVERSE_FOREACH( ChildList::value_type child, _children )
129 if( child.second->accept(visitor) )
135 //----------------------------------------------------------------------------
136 osg::BoundingBox Group::getTransformedBounds(const osg::Matrix& m) const
140 BOOST_FOREACH( ChildList::value_type child, _children )
142 if( !child.second->getMatrixTransform()->getNodeMask() )
147 child.second->getTransformedBounds
149 child.second->getMatrixTransform()->getMatrix() * m
157 //----------------------------------------------------------------------------
158 void Group::childAdded(SGPropertyNode* child)
160 if( child->getParent() != _node )
163 ChildFactories::iterator child_factory =
164 _child_factories.find( child->getNameString() );
165 if( child_factory != _child_factories.end() )
167 ElementPtr element = child_factory->second(_canvas, child, _style, this);
169 // Add to osg scene graph...
170 _transform->addChild( element->getMatrixTransform() );
171 _children.push_back( ChildList::value_type(child, element) );
176 _style[ child->getNameString() ] = child;
179 //----------------------------------------------------------------------------
180 void Group::childRemoved(SGPropertyNode* node)
182 if( node->getParent() != _node )
185 if( _child_factories.find(node->getNameString()) != _child_factories.end() )
187 ChildList::iterator child = findChild(node);
188 if( child == _children.end() )
193 "can't removed unknown child " << node->getDisplayName()
197 _transform->removeChild( child->second->getMatrixTransform() );
198 _children.erase(child);
203 Style::iterator style = _style.find(node->getNameString());
204 if( style != _style.end() )
209 //----------------------------------------------------------------------------
210 void Group::childChanged(SGPropertyNode* node)
212 if( node->getParent()->getParent() == _node
213 && node->getNameString() == "z-index" )
214 return handleZIndexChanged(node->getParent(), node->getIntValue());
217 //----------------------------------------------------------------------------
218 void Group::handleZIndexChanged(SGPropertyNode* node, int z_index)
220 ChildList::iterator child = findChild(node);
221 if( child == _children.end() )
224 osg::Node* tf = child->second->getMatrixTransform();
225 int index = _transform->getChildIndex(tf),
228 ChildList::iterator next = child;
231 while( next != _children.end()
232 && next->first->getIntValue("z-index", 0) <= z_index )
238 if( index_new != index )
240 _children.insert(next, *child);
244 ChildList::iterator prev = child;
245 while( prev != _children.begin()
246 && (--prev)->first->getIntValue("z-index", 0) > z_index)
251 if( index == index_new )
254 _children.insert(prev, *child);
257 _transform->removeChild(index);
258 _transform->insertChild(index_new, tf);
260 _children.erase(child);
266 "canvas::Group: Moved element " << index << " to position " << index_new
270 //----------------------------------------------------------------------------
271 Group::ChildList::iterator Group::findChild(const SGPropertyNode* node)
277 boost::bind(&ChildList::value_type::first, _1) == node
281 } // namespace canvas
282 } // namespace simgear