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