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