]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasGroup.cxx
Canvas: Prepare for DOM Level 2 like event model.
[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& name )
70   {
71     SGPropertyNode* node = _node->addChild(type, 0, false);
72     if( !name.empty() )
73       node->setStringValue("name", name);
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   void Group::update(double dt)
90   {
91     BOOST_FOREACH( ChildList::value_type child, _children )
92       child.second->update(dt);
93
94     Element::update(dt);
95   }
96
97   //----------------------------------------------------------------------------
98   bool Group::traverse(EventVisitor& visitor)
99   {
100     // Iterate in reverse order as last child is displayed on top
101     BOOST_REVERSE_FOREACH( ChildList::value_type child, _children )
102     {
103       if( child.second->accept(visitor) )
104         return true;
105     }
106     return false;
107   }
108
109   //----------------------------------------------------------------------------
110   void Group::childAdded(SGPropertyNode* child)
111   {
112     if( child->getParent() != _node )
113       return;
114
115     ChildFactories::iterator child_factory =
116       _child_factories.find( child->getNameString() );
117     if( child_factory != _child_factories.end() )
118     {
119       ElementPtr element = child_factory->second(_canvas, child, _style, this);
120
121       // Add to osg scene graph...
122       _transform->addChild( element->getMatrixTransform() );
123       _children.push_back( ChildList::value_type(child, element) );
124
125       return;
126     }
127
128     _style[ child->getNameString() ] = child;
129   }
130
131   //----------------------------------------------------------------------------
132   void Group::childRemoved(SGPropertyNode* node)
133   {
134     if( node->getParent() != _node )
135       return;
136
137     if( _child_factories.find(node->getNameString()) != _child_factories.end() )
138     {
139       ChildList::iterator child = findChild(node);
140       if( child == _children.end() )
141         SG_LOG
142         (
143           SG_GL,
144           SG_WARN,
145           "can't removed unknown child " << node->getDisplayName()
146         );
147       else
148       {
149         _transform->removeChild( child->second->getMatrixTransform() );
150         _children.erase(child);
151       }
152     }
153     else
154     {
155       Style::iterator style = _style.find(node->getNameString());
156       if( style != _style.end() )
157         _style.erase(style);
158     }
159   }
160
161   //----------------------------------------------------------------------------
162   void Group::childChanged(SGPropertyNode* node)
163   {
164     if(    node->getParent()->getParent() == _node
165         && node->getNameString() == "z-index" )
166       return handleZIndexChanged(node->getParent(), node->getIntValue());
167   }
168
169   //----------------------------------------------------------------------------
170   void Group::handleZIndexChanged(SGPropertyNode* node, int z_index)
171   {
172     ChildList::iterator child = findChild(node);
173     if( child == _children.end() )
174       return;
175
176     osg::Node* tf = child->second->getMatrixTransform();
177     int index = _transform->getChildIndex(tf),
178         index_new = index;
179
180     ChildList::iterator next = child;
181     ++next;
182
183     while(    next != _children.end()
184            && next->first->getIntValue("z-index", 0) <= z_index )
185     {
186       ++index_new;
187       ++next;
188     }
189
190     if( index_new != index )
191     {
192       _children.insert(next, *child);
193     }
194     else
195     {
196       ChildList::iterator prev = child;
197       while(    prev != _children.begin()
198              && (--prev)->first->getIntValue("z-index", 0) > z_index)
199       {
200         --index_new;
201       }
202
203       if( index == index_new )
204         return;
205
206       _children.insert(prev, *child);
207     }
208
209     _transform->removeChild(index);
210     _transform->insertChild(index_new, tf);
211
212     _children.erase(child);
213
214     SG_LOG
215     (
216       SG_GENERAL,
217       SG_INFO,
218       "canvas::Group: Moved element " << index << " to position " << index_new
219     );
220   }
221
222   //----------------------------------------------------------------------------
223   Group::ChildList::iterator Group::findChild(const SGPropertyNode* node)
224   {
225     return std::find_if
226     (
227       _children.begin(),
228       _children.end(),
229       boost::bind(&ChildList::value_type::first, _1) == node
230     );
231   }
232
233 } // namespace canvas
234 } // namespace simgear