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"
25 #include <boost/foreach.hpp>
32 * Create an canvas Element of type T
35 ElementPtr createElement( const CanvasWeakPtr& canvas,
36 const SGPropertyNode_ptr& node,
39 return ElementPtr( new T(canvas, node, style) );
42 //----------------------------------------------------------------------------
43 Group::Group( const CanvasWeakPtr& canvas,
44 const SGPropertyNode_ptr& node,
45 const Style& parent_style ):
46 Element(canvas, node, parent_style)
48 _child_factories["group"] = &createElement<Group>;
49 _child_factories["image"] = &createElement<Image>;
50 _child_factories["map" ] = &createElement<Map >;
51 _child_factories["path" ] = &createElement<Path >;
52 _child_factories["text" ] = &createElement<Text >;
55 //----------------------------------------------------------------------------
61 //----------------------------------------------------------------------------
62 void Group::update(double dt)
64 BOOST_FOREACH( ChildList::value_type child, _children )
65 child.second->update(dt);
70 //----------------------------------------------------------------------------
71 bool Group::handleLocalMouseEvent(const canvas::MouseEvent& event)
73 // Iterate in reverse order as last child is displayed on top
74 BOOST_REVERSE_FOREACH( ChildList::value_type child, _children )
76 if( child.second->handleMouseEvent(event) )
82 //----------------------------------------------------------------------------
83 void Group::childAdded(SGPropertyNode* child)
85 if( child->getParent() != _node )
88 ChildFactories::iterator child_factory =
89 _child_factories.find( child->getNameString() );
90 if( child_factory != _child_factories.end() )
92 ElementPtr element = child_factory->second(_canvas, child, _style);
94 // Add to osg scene graph...
95 _transform->addChild( element->getMatrixTransform() );
96 _children.push_back( ChildList::value_type(child, element) );
101 _style[ child->getNameString() ] = child;
104 //----------------------------------------------------------------------------
108 ChildFinder(SGPropertyNode *node):
112 bool operator()(const Group::ChildList::value_type& el) const
114 return el.first == _node;
118 SGPropertyNode *_node;
121 //----------------------------------------------------------------------------
122 void Group::childRemoved(SGPropertyNode* node)
124 if( node->getParent() != _node )
127 if( _child_factories.find(node->getNameString()) != _child_factories.end() )
129 ChildFinder pred(node);
130 ChildList::iterator child =
131 std::find_if(_children.begin(), _children.end(), pred);
133 if( child == _children.end() )
138 "can't removed unknown child " << node->getDisplayName()
142 _transform->removeChild( child->second->getMatrixTransform() );
143 _children.erase(child);
148 Style::iterator style = _style.find(node->getNameString());
149 if( style != _style.end() )
154 //----------------------------------------------------------------------------
155 void Group::childChanged(SGPropertyNode* node)
157 if( node->getParent()->getParent() == _node
158 && node->getNameString() == "z-index" )
159 return handleZIndexChanged(node->getParent(), node->getIntValue());
162 //----------------------------------------------------------------------------
163 void Group::handleZIndexChanged(SGPropertyNode* node, int z_index)
165 ChildFinder pred(node);
166 ChildList::iterator child =
167 std::find_if(_children.begin(), _children.end(), pred);
169 if( child == _children.end() )
172 osg::Node* tf = child->second->getMatrixTransform();
173 int index = _transform->getChildIndex(tf),
176 ChildList::iterator next = child;
179 while( next != _children.end()
180 && next->first->getIntValue("z-index", 0) <= z_index )
186 if( index_new != index )
188 _children.insert(next, *child);
192 ChildList::iterator prev = child;
193 while( prev != _children.begin()
194 && (--prev)->first->getIntValue("z-index", 0) > z_index)
199 if( index == index_new )
202 _children.insert(prev, *child);
205 _transform->removeChild(index);
206 _transform->insertChild(index_new, tf);
208 _children.erase(child);
214 "canvas::Group: Moved element " << index << " to position " << index_new
218 } // namespace canvas
219 } // namespace simgear