]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/elements/group.cxx
Fix a Clang warning in Shiva.
[flightgear.git] / src / Canvas / elements / group.cxx
1 // A group of 2D canvas elements
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
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.
9 //
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.
14 //
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.
18
19 #include "group.hxx"
20 #include "map.hxx"
21 #include "path.hxx"
22 #include "text.hxx"
23 #include "CanvasImage.hxx"
24
25 #include <boost/foreach.hpp>
26
27 namespace canvas
28 {
29
30   //----------------------------------------------------------------------------
31   Group::Group(SGPropertyNode_ptr node, const Style& parent_style):
32     Element(node, parent_style)
33   {
34
35   }
36
37   //----------------------------------------------------------------------------
38   Group::~Group()
39   {
40
41   }
42
43   //----------------------------------------------------------------------------
44   void Group::update(double dt)
45   {
46     BOOST_FOREACH( ChildList::value_type child, _children )
47       child.second->update(dt);
48
49     Element::update(dt);
50   }
51
52   //----------------------------------------------------------------------------
53   bool Group::handleLocalMouseEvent(const canvas::MouseEvent& event)
54   {
55     // Iterate in reverse order as last child is displayed on top
56     BOOST_REVERSE_FOREACH( ChildList::value_type child, _children )
57     {
58       if( child.second->handleMouseEvent(event) )
59         return true;
60     }
61     return false;
62   }
63
64   //----------------------------------------------------------------------------
65   void Group::childAdded(SGPropertyNode* child)
66   {
67     if( child->getParent() != _node )
68       return;
69
70     boost::shared_ptr<Element> element;
71
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) );
84
85     if( element )
86     {
87       // Add to osg scene graph...
88       _transform->addChild( element->getMatrixTransform() );
89       _children.push_back( ChildList::value_type(child, element) );
90       return;
91     }
92
93     _style[ child->getNameString() ] = child;
94   }
95
96   //----------------------------------------------------------------------------
97   struct ChildFinder
98   {
99     public:
100       ChildFinder(SGPropertyNode *node):
101         _node(node)
102       {}
103
104       bool operator()(const Group::ChildList::value_type& el) const
105       {
106         return el.first == _node;
107       }
108
109     private:
110       SGPropertyNode *_node;
111   };
112
113   //----------------------------------------------------------------------------
114   void Group::childRemoved(SGPropertyNode* node)
115   {
116     if( node->getParent() != _node )
117       return;
118
119     if(    node->getNameString() == "text"
120         || node->getNameString() == "group"
121         || node->getNameString() == "map"
122         || node->getNameString() == "path"
123         || node->getNameString() == "image" )
124     {
125       ChildFinder pred(node);
126       ChildList::iterator child =
127         std::find_if(_children.begin(), _children.end(), pred);
128
129       if( child == _children.end() )
130         SG_LOG
131         (
132           SG_GL,
133           SG_WARN,
134           "can't removed unknown child " << node->getDisplayName()
135         );
136       else
137       {
138         _transform->removeChild( child->second->getMatrixTransform() );
139         _children.erase(child);
140       }
141     }
142     else
143     {
144       Style::iterator style = _style.find(node->getNameString());
145       if( style != _style.end() )
146         _style.erase(style);
147     }
148   }
149
150 } // namespace canvas