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