]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasGroup.cxx
Clean up Canvas element creation
[simgear.git] / simgear / canvas / elements / CanvasGroup.cxx
1 // A group of 2D Canvas elements
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
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.
9 //
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.
14 //
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
18
19 #include "CanvasGroup.hxx"
20 #include "CanvasImage.hxx"
21 #include "CanvasMap.hxx"
22 #include "CanvasPath.hxx"
23 #include "CanvasText.hxx"
24
25 #include <boost/bind.hpp>
26 #include <boost/foreach.hpp>
27 #include <boost/make_shared.hpp>
28 #include <boost/lambda/core.hpp>
29
30 namespace simgear
31 {
32 namespace canvas
33 {
34   /**
35    * Create an ElementFactory for elements of type T
36    */
37   template<typename T>
38   ElementFactory createElementFactory()
39   {
40     return boost::bind
41     (
42       &boost::make_shared<T, const CanvasWeakPtr&,
43                              const SGPropertyNode_ptr&,
44                              const Style&>,
45       boost::lambda::_1,
46       boost::lambda::_2,
47       boost::lambda::_3
48     );
49   }
50
51   //----------------------------------------------------------------------------
52   Group::Group( const CanvasWeakPtr& canvas,
53                 const SGPropertyNode_ptr& node,
54                 const Style& parent_style ):
55     Element(canvas, node, parent_style)
56   {
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 >();
62   }
63
64   //----------------------------------------------------------------------------
65   Group::~Group()
66   {
67
68   }
69
70   //----------------------------------------------------------------------------
71   void Group::update(double dt)
72   {
73     BOOST_FOREACH( ChildList::value_type child, _children )
74       child.second->update(dt);
75
76     Element::update(dt);
77   }
78
79   //----------------------------------------------------------------------------
80   bool Group::handleLocalMouseEvent(const canvas::MouseEvent& event)
81   {
82     // Iterate in reverse order as last child is displayed on top
83     BOOST_REVERSE_FOREACH( ChildList::value_type child, _children )
84     {
85       if( child.second->handleMouseEvent(event) )
86         return true;
87     }
88     return false;
89   }
90
91   //----------------------------------------------------------------------------
92   void Group::childAdded(SGPropertyNode* child)
93   {
94     if( child->getParent() != _node )
95       return;
96
97     ChildFactories::iterator child_factory =
98       _child_factories.find( child->getNameString() );
99     if( child_factory != _child_factories.end() )
100     {
101       ElementPtr element = child_factory->second(_canvas, child, _style);
102
103       // Add to osg scene graph...
104       _transform->addChild( element->getMatrixTransform() );
105       _children.push_back( ChildList::value_type(child, element) );
106
107       return;
108     }
109
110     _style[ child->getNameString() ] = child;
111   }
112
113   //----------------------------------------------------------------------------
114   struct ChildFinder
115   {
116     public:
117       ChildFinder(SGPropertyNode *node):
118         _node(node)
119       {}
120
121       bool operator()(const Group::ChildList::value_type& el) const
122       {
123         return el.first == _node;
124       }
125
126     private:
127       SGPropertyNode *_node;
128   };
129
130   //----------------------------------------------------------------------------
131   void Group::childRemoved(SGPropertyNode* node)
132   {
133     if( node->getParent() != _node )
134       return;
135
136     if( _child_factories.find(node->getNameString()) != _child_factories.end() )
137     {
138       ChildFinder pred(node);
139       ChildList::iterator child =
140         std::find_if(_children.begin(), _children.end(), pred);
141
142       if( child == _children.end() )
143         SG_LOG
144         (
145           SG_GL,
146           SG_WARN,
147           "can't removed unknown child " << node->getDisplayName()
148         );
149       else
150       {
151         _transform->removeChild( child->second->getMatrixTransform() );
152         _children.erase(child);
153       }
154     }
155     else
156     {
157       Style::iterator style = _style.find(node->getNameString());
158       if( style != _style.end() )
159         _style.erase(style);
160     }
161   }
162
163   //----------------------------------------------------------------------------
164   void Group::childChanged(SGPropertyNode* node)
165   {
166     if(    node->getParent()->getParent() == _node
167         && node->getNameString() == "z-index" )
168       return handleZIndexChanged(node->getParent(), node->getIntValue());
169   }
170
171   //----------------------------------------------------------------------------
172   void Group::handleZIndexChanged(SGPropertyNode* node, int z_index)
173   {
174     ChildFinder pred(node);
175     ChildList::iterator child =
176       std::find_if(_children.begin(), _children.end(), pred);
177
178     if( child == _children.end() )
179       return;
180
181     osg::Node* tf = child->second->getMatrixTransform();
182     int index = _transform->getChildIndex(tf),
183         index_new = index;
184
185     ChildList::iterator next = child;
186     ++next;
187
188     while(    next != _children.end()
189            && next->first->getIntValue("z-index", 0) <= z_index )
190     {
191       ++index_new;
192       ++next;
193     }
194
195     if( index_new != index )
196     {
197       _children.insert(next, *child);
198     }
199     else
200     {
201       ChildList::iterator prev = child;
202       while(    prev != _children.begin()
203              && (--prev)->first->getIntValue("z-index", 0) > z_index)
204       {
205         --index_new;
206       }
207
208       if( index == index_new )
209         return;
210
211       _children.insert(prev, *child);
212     }
213
214     _transform->removeChild(index);
215     _transform->insertChild(index_new, tf);
216
217     _children.erase(child);
218
219     SG_LOG
220     (
221       SG_GENERAL,
222       SG_INFO,
223       "canvas::Group: Moved element " << index << " to position " << index_new
224     );
225   }
226
227 } // namespace canvas
228 } // namespace simgear