]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasGroup.cxx
Refactor Canvas and add some helpers.
[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/foreach.hpp>
26
27 namespace simgear
28 {
29 namespace canvas
30 {
31
32   //----------------------------------------------------------------------------
33   Group::Group( const CanvasWeakPtr& canvas,
34                 SGPropertyNode_ptr node,
35                 const Style& parent_style ):
36     Element(canvas, node, parent_style)
37   {
38
39   }
40
41   //----------------------------------------------------------------------------
42   Group::~Group()
43   {
44
45   }
46
47   //----------------------------------------------------------------------------
48   void Group::update(double dt)
49   {
50     BOOST_FOREACH( ChildList::value_type child, _children )
51       child.second->update(dt);
52
53     Element::update(dt);
54   }
55
56   //----------------------------------------------------------------------------
57   bool Group::handleLocalMouseEvent(const canvas::MouseEvent& event)
58   {
59     // Iterate in reverse order as last child is displayed on top
60     BOOST_REVERSE_FOREACH( ChildList::value_type child, _children )
61     {
62       if( child.second->handleMouseEvent(event) )
63         return true;
64     }
65     return false;
66   }
67
68   //----------------------------------------------------------------------------
69   void Group::childAdded(SGPropertyNode* child)
70   {
71     if( child->getParent() != _node )
72       return;
73
74     boost::shared_ptr<Element> element;
75
76     // TODO create map of child factories and use also to check for element
77     //      on deletion in ::childRemoved
78     if( child->getNameString() == "text" )
79       element.reset( new Text(_canvas, child, _style) );
80     else if( child->getNameString() == "group" )
81       element.reset( new Group(_canvas, child, _style) );
82     else if( child->getNameString() == "map" )
83       element.reset( new Map(_canvas, child, _style) );
84     else if( child->getNameString() == "path" )
85       element.reset( new Path(_canvas, child, _style) );
86     else if( child->getNameString() == "image" )
87       element.reset( new Image(_canvas, child, _style) );
88
89     if( element )
90     {
91       // Add to osg scene graph...
92       _transform->addChild( element->getMatrixTransform() );
93       _children.push_back( ChildList::value_type(child, element) );
94       return;
95     }
96
97     _style[ child->getNameString() ] = child;
98   }
99
100   //----------------------------------------------------------------------------
101   struct ChildFinder
102   {
103     public:
104       ChildFinder(SGPropertyNode *node):
105         _node(node)
106       {}
107
108       bool operator()(const Group::ChildList::value_type& el) const
109       {
110         return el.first == _node;
111       }
112
113     private:
114       SGPropertyNode *_node;
115   };
116
117   //----------------------------------------------------------------------------
118   void Group::childRemoved(SGPropertyNode* node)
119   {
120     if( node->getParent() != _node )
121       return;
122
123     if(    node->getNameString() == "text"
124         || node->getNameString() == "group"
125         || node->getNameString() == "map"
126         || node->getNameString() == "path"
127         || node->getNameString() == "image" )
128     {
129       ChildFinder pred(node);
130       ChildList::iterator child =
131         std::find_if(_children.begin(), _children.end(), pred);
132
133       if( child == _children.end() )
134         SG_LOG
135         (
136           SG_GL,
137           SG_WARN,
138           "can't removed unknown child " << node->getDisplayName()
139         );
140       else
141       {
142         _transform->removeChild( child->second->getMatrixTransform() );
143         _children.erase(child);
144       }
145     }
146     else
147     {
148       Style::iterator style = _style.find(node->getNameString());
149       if( style != _style.end() )
150         _style.erase(style);
151     }
152   }
153
154   //----------------------------------------------------------------------------
155   void Group::childChanged(SGPropertyNode* node)
156   {
157     if(    node->getParent()->getParent() == _node
158         && node->getNameString() == "z-index" )
159       return handleZIndexChanged(node->getParent(), node->getIntValue());
160   }
161
162   //----------------------------------------------------------------------------
163   void Group::handleZIndexChanged(SGPropertyNode* node, int z_index)
164   {
165     ChildFinder pred(node);
166     ChildList::iterator child =
167       std::find_if(_children.begin(), _children.end(), pred);
168
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 } // namespace canvas
219 } // namespace simgear