1 // A group of 2D canvas elements
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "CanvasImage.hxx"
25 #include <boost/foreach.hpp>
30 //----------------------------------------------------------------------------
31 Group::Group(SGPropertyNode_ptr node, const Style& parent_style):
32 Element(node, parent_style)
37 //----------------------------------------------------------------------------
43 //----------------------------------------------------------------------------
44 void Group::update(double dt)
46 BOOST_FOREACH( ChildList::value_type child, _children )
47 child.second->update(dt);
52 //----------------------------------------------------------------------------
53 bool Group::handleLocalMouseEvent(const canvas::MouseEvent& event)
55 // Iterate in reverse order as last child is displayed on top
56 BOOST_REVERSE_FOREACH( ChildList::value_type child, _children )
58 if( child.second->handleMouseEvent(event) )
64 //----------------------------------------------------------------------------
65 void Group::childAdded(SGPropertyNode* child)
67 if( child->getParent() != _node )
70 boost::shared_ptr<Element> element;
72 // TODO create map of child factories and use also to check for element
73 // on deletion in ::childRemoved
74 if( child->getNameString() == "text" )
75 element.reset( new Text(child, _style) );
76 else if( child->getNameString() == "group" )
77 element.reset( new Group(child, _style) );
78 else if( child->getNameString() == "map" )
79 element.reset( new Map(child, _style) );
80 else if( child->getNameString() == "path" )
81 element.reset( new Path(child, _style) );
82 else if( child->getNameString() == "image" )
83 element.reset( new Image(child, _style) );
87 // Add to osg scene graph...
88 _transform->addChild( element->getMatrixTransform() );
89 _children.push_back( ChildList::value_type(child, element) );
93 _style[ child->getNameString() ] = child;
96 //----------------------------------------------------------------------------
100 ChildFinder(SGPropertyNode *node):
104 bool operator()(const Group::ChildList::value_type& el) const
106 return el.first == _node;
110 SGPropertyNode *_node;
113 //----------------------------------------------------------------------------
114 void Group::childRemoved(SGPropertyNode* node)
116 if( node->getParent() != _node )
119 if( node->getNameString() == "text"
120 || node->getNameString() == "group"
121 || node->getNameString() == "map"
122 || node->getNameString() == "path"
123 || node->getNameString() == "image" )
125 ChildFinder pred(node);
126 ChildList::iterator child =
127 std::find_if(_children.begin(), _children.end(), pred);
129 if( child == _children.end() )
134 "can't removed unknown child " << node->getDisplayName()
138 _transform->removeChild( child->second->getMatrixTransform() );
139 _children.erase(child);
144 Style::iterator style = _style.find(node->getNameString());
145 if( style != _style.end() )
150 } // namespace canvas