]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasGroup.cxx
CanvasGroup: Fix handling z-index while moving child to front.
[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 #include <simgear/canvas/CanvasEventVisitor.hxx>
25 #include <simgear/canvas/MouseEvent.hxx>
26
27 #include <boost/bind.hpp>
28 #include <boost/foreach.hpp>
29 #include <boost/lambda/core.hpp>
30
31 namespace simgear
32 {
33 namespace canvas
34 {
35   /**
36    * Create an canvas Element of type T
37    */
38   template<typename T>
39   ElementPtr createElement( const CanvasWeakPtr& canvas,
40                             const SGPropertyNode_ptr& node,
41                             const Style& style,
42                             Element* parent )
43   {
44     ElementPtr el( new T(canvas, node, style, parent) );
45     el->setSelf(el);
46     return el;
47   }
48
49   //----------------------------------------------------------------------------
50   ElementFactories Group::_child_factories;
51
52   //----------------------------------------------------------------------------
53   Group::Group( const CanvasWeakPtr& canvas,
54                 const SGPropertyNode_ptr& node,
55                 const Style& parent_style,
56                 Element* parent ):
57     Element(canvas, node, parent_style, parent)
58   {
59     if( !isInit<Group>() )
60     {
61       _child_factories["group"] = &createElement<Group>;
62       _child_factories["image"] = &createElement<Image>;
63       _child_factories["map"  ] = &createElement<Map  >;
64       _child_factories["path" ] = &createElement<Path >;
65       _child_factories["text" ] = &createElement<Text >;
66     }
67   }
68
69   //----------------------------------------------------------------------------
70   Group::~Group()
71   {
72
73   }
74
75   //----------------------------------------------------------------------------
76   ElementPtr Group::createChild( const std::string& type,
77                                  const std::string& id )
78   {
79     SGPropertyNode* node = _node->addChild(type, 0, false);
80     if( !id.empty() )
81       node->setStringValue("id", id);
82
83     return getChild(node);
84   }
85
86   //----------------------------------------------------------------------------
87   ElementPtr Group::getChild(const SGPropertyNode* node)
88   {
89     ChildList::iterator child = findChild(node);
90     if( child == _children.end() )
91       return ElementPtr();
92
93     return child->second;
94   }
95
96   //----------------------------------------------------------------------------
97   ElementPtr Group::getChild(const std::string& id)
98   {
99     for( ChildList::iterator child = _children.begin();
100                              child != _children.end();
101                            ++child )
102     {
103       if( child->second->get<std::string>("id") == id )
104         return child->second;
105     }
106
107     return ElementPtr();
108   }
109
110   //----------------------------------------------------------------------------
111   ElementPtr Group::getElementById(const std::string& id)
112   {
113     std::vector<GroupPtr> groups;
114
115     BOOST_FOREACH( ChildList::value_type child, _children )
116     {
117       const ElementPtr& el = child.second;
118       if( el->getProps()->getStringValue("id") == id )
119         return el;
120
121       GroupPtr group = boost::dynamic_pointer_cast<Group>(el);
122       if( group )
123         groups.push_back(group);
124     }
125
126     BOOST_FOREACH( GroupPtr group, groups )
127     {
128       ElementPtr el = group->getElementById(id);
129       if( el )
130         return el;
131     }
132
133     return ElementPtr();
134   }
135
136   //----------------------------------------------------------------------------
137   void Group::clearEventListener()
138   {
139     BOOST_FOREACH( ChildList::value_type child, _children )
140       child.second->clearEventListener();
141
142     Element::clearEventListener();
143   }
144
145   //----------------------------------------------------------------------------
146   void Group::update(double dt)
147   {
148     BOOST_FOREACH( ChildList::value_type child, _children )
149       child.second->update(dt);
150
151     Element::update(dt);
152   }
153
154   //----------------------------------------------------------------------------
155   bool Group::traverse(EventVisitor& visitor)
156   {
157     // Iterate in reverse order as last child is displayed on top
158     BOOST_REVERSE_FOREACH( ChildList::value_type child, _children )
159     {
160       if( child.second->accept(visitor) )
161         return true;
162     }
163     return false;
164   }
165
166   //----------------------------------------------------------------------------
167   bool Group::setStyle(const SGPropertyNode* style)
168   {
169     // Don't propagate styles directly applicable to this group
170     if( Element::setStyle(style) )
171       return true;
172
173     if(    style->getParent() != _node
174         && _style.find(style->getNameString()) != _style.end() )
175       return false;
176
177     bool handled = false;
178     BOOST_FOREACH( ChildList::value_type child, _children )
179     {
180       if( child.second->setStyle(style) )
181         handled = true;
182     }
183
184     return handled;
185   }
186
187   //----------------------------------------------------------------------------
188   osg::BoundingBox Group::getTransformedBounds(const osg::Matrix& m) const
189   {
190     osg::BoundingBox bb;
191
192     BOOST_FOREACH( ChildList::value_type child, _children )
193     {
194       if( !child.second->getMatrixTransform()->getNodeMask() )
195         continue;
196
197       bb.expandBy
198       (
199         child.second->getTransformedBounds
200         (
201           child.second->getMatrixTransform()->getMatrix() * m
202         )
203       );
204     }
205
206     return bb;
207   }
208
209   //----------------------------------------------------------------------------
210   void Group::childAdded(SGPropertyNode* child)
211   {
212     if( child->getParent() != _node )
213       return;
214
215     ElementFactories::iterator child_factory =
216       _child_factories.find( child->getNameString() );
217     if( child_factory != _child_factories.end() )
218     {
219       ElementPtr element = child_factory->second(_canvas, child, _style, this);
220
221       // Add to osg scene graph...
222       _transform->addChild( element->getMatrixTransform() );
223       _children.push_back( ChildList::value_type(child, element) );
224
225       // ...and ensure correct ordering
226       handleZIndexChanged( --_children.end() );
227
228       return;
229     }
230
231     if( !Element::setStyle(child) )
232     {
233       // Only add style if not applicable to group itself
234       _style[ child->getNameString() ] = child;
235       setStyle(child);
236     }
237   }
238
239   //----------------------------------------------------------------------------
240   void Group::childRemoved(SGPropertyNode* node)
241   {
242     if( node->getParent() != _node )
243       return;
244
245     if( _child_factories.find(node->getNameString()) != _child_factories.end() )
246     {
247       ChildList::iterator child = findChild(node);
248       if( child == _children.end() )
249         SG_LOG
250         (
251           SG_GL,
252           SG_WARN,
253           "can't removed unknown child " << node->getDisplayName()
254         );
255       else
256       {
257         _transform->removeChild( child->second->getMatrixTransform() );
258         _children.erase(child);
259       }
260     }
261     else
262     {
263       Style::iterator style = _style.find(node->getNameString());
264       if( style != _style.end() )
265         _style.erase(style);
266     }
267   }
268
269   //----------------------------------------------------------------------------
270   void Group::childChanged(SGPropertyNode* node)
271   {
272     if(    node->getParent()->getParent() == _node
273         && node->getNameString() == "z-index" )
274       return handleZIndexChanged( findChild(node->getParent()),
275                                   node->getIntValue() );
276   }
277
278   //----------------------------------------------------------------------------
279   void Group::handleZIndexChanged(ChildList::iterator child, int z_index)
280   {
281     if( child == _children.end() )
282       return;
283
284     osg::Node* tf = child->second->getMatrixTransform();
285     int index = _transform->getChildIndex(tf),
286         index_new = index;
287
288     ChildList::iterator next = child;
289     ++next;
290
291     while(    next != _children.end()
292            && next->first->getIntValue("z-index", 0) <= z_index )
293     {
294       ++index_new;
295       ++next;
296     }
297
298     if( index_new != index )
299     {
300       _children.insert(next, *child);
301     }
302     else
303     {
304       ChildList::iterator prev = child,
305                           check = child;
306       while(    check != _children.begin()
307              && (--check)->first->getIntValue("z-index", 0) > z_index )
308       {
309         --index_new;
310         --prev;
311       }
312
313       if( index == index_new )
314         return;
315
316       _children.insert(prev, *child);
317     }
318
319     _transform->removeChild(index);
320     _transform->insertChild(index_new, tf);
321
322     _children.erase(child);
323
324     SG_LOG
325     (
326       SG_GENERAL,
327       SG_INFO,
328       "canvas::Group: Moved element " << index << " to position " << index_new
329     );
330   }
331
332   //----------------------------------------------------------------------------
333   Group::ChildList::iterator Group::findChild(const SGPropertyNode* node)
334   {
335     return std::find_if
336     (
337       _children.begin(),
338       _children.end(),
339       boost::bind(&ChildList::value_type::first, _1) == node
340     );
341   }
342
343 } // namespace canvas
344 } // namespace simgear