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