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/bind.hpp>
26 #include <boost/foreach.hpp>
27 #include <boost/make_shared.hpp>
28 #include <boost/lambda/core.hpp>
35 * Create an ElementFactory for elements of type T
38 ElementFactory createElementFactory()
42 &boost::make_shared<T, const CanvasWeakPtr&,
43 const SGPropertyNode_ptr&,
51 //----------------------------------------------------------------------------
52 Group::Group( const CanvasWeakPtr& canvas,
53 const SGPropertyNode_ptr& node,
54 const Style& parent_style ):
55 Element(canvas, node, parent_style)
57 _child_factories["group"] = createElementFactory<Group>();
58 _child_factories["image"] = createElementFactory<Image>();
59 _child_factories["map" ] = createElementFactory<Map >();
60 _child_factories["path" ] = createElementFactory<Path >();
61 _child_factories["text" ] = createElementFactory<Text >();
64 //----------------------------------------------------------------------------
70 //----------------------------------------------------------------------------
71 void Group::update(double dt)
73 BOOST_FOREACH( ChildList::value_type child, _children )
74 child.second->update(dt);
79 //----------------------------------------------------------------------------
80 bool Group::handleLocalMouseEvent(const canvas::MouseEvent& event)
82 // Iterate in reverse order as last child is displayed on top
83 BOOST_REVERSE_FOREACH( ChildList::value_type child, _children )
85 if( child.second->handleMouseEvent(event) )
91 //----------------------------------------------------------------------------
92 void Group::childAdded(SGPropertyNode* child)
94 if( child->getParent() != _node )
97 ChildFactories::iterator child_factory =
98 _child_factories.find( child->getNameString() );
99 if( child_factory != _child_factories.end() )
101 ElementPtr element = child_factory->second(_canvas, child, _style);
103 // Add to osg scene graph...
104 _transform->addChild( element->getMatrixTransform() );
105 _children.push_back( ChildList::value_type(child, element) );
110 _style[ child->getNameString() ] = child;
113 //----------------------------------------------------------------------------
117 ChildFinder(SGPropertyNode *node):
121 bool operator()(const Group::ChildList::value_type& el) const
123 return el.first == _node;
127 SGPropertyNode *_node;
130 //----------------------------------------------------------------------------
131 void Group::childRemoved(SGPropertyNode* node)
133 if( node->getParent() != _node )
136 if( _child_factories.find(node->getNameString()) != _child_factories.end() )
138 ChildFinder pred(node);
139 ChildList::iterator child =
140 std::find_if(_children.begin(), _children.end(), pred);
142 if( child == _children.end() )
147 "can't removed unknown child " << node->getDisplayName()
151 _transform->removeChild( child->second->getMatrixTransform() );
152 _children.erase(child);
157 Style::iterator style = _style.find(node->getNameString());
158 if( style != _style.end() )
163 //----------------------------------------------------------------------------
164 void Group::childChanged(SGPropertyNode* node)
166 if( node->getParent()->getParent() == _node
167 && node->getNameString() == "z-index" )
168 return handleZIndexChanged(node->getParent(), node->getIntValue());
171 //----------------------------------------------------------------------------
172 void Group::handleZIndexChanged(SGPropertyNode* node, int z_index)
174 ChildFinder pred(node);
175 ChildList::iterator child =
176 std::find_if(_children.begin(), _children.end(), pred);
178 if( child == _children.end() )
181 osg::Node* tf = child->second->getMatrixTransform();
182 int index = _transform->getChildIndex(tf),
185 ChildList::iterator next = child;
188 while( next != _children.end()
189 && next->first->getIntValue("z-index", 0) <= z_index )
195 if( index_new != index )
197 _children.insert(next, *child);
201 ChildList::iterator prev = child;
202 while( prev != _children.begin()
203 && (--prev)->first->getIntValue("z-index", 0) > z_index)
208 if( index == index_new )
211 _children.insert(prev, *child);
214 _transform->removeChild(index);
215 _transform->insertChild(index_new, tf);
217 _children.erase(child);
223 "canvas::Group: Moved element " << index << " to position " << index_new
227 } // namespace canvas
228 } // namespace simgear