]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/property_helper.hxx
Canvas: Image/Window unifying and allow using canvas inside canvas.
[flightgear.git] / src / Canvas / property_helper.hxx
1 // Some helper functions for accessing the property tree
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 #ifndef PROPERTY_HELPER_HXX_
20 #define PROPERTY_HELPER_HXX_
21
22 #include <simgear/props/props.hxx>
23 #include <osg/Vec4>
24
25 namespace canvas
26 {
27
28   /**
29    * Get property node with default value
30    */
31   template<typename T>
32   SGPropertyNode* getChildDefault( SGPropertyNode* parent,
33                                    const char* name,
34                                    T def_val )
35   {
36     SGPropertyNode* node = parent->getNode(name);
37     if( node )
38       // also set value for existing nodes to enforce type...
39       def_val = getValue<T>(node);
40     else
41       node = parent->getChild(name, 0, true);
42
43     setValue(node, def_val);
44     return node;
45   }
46
47   /**
48    * Get vector of properties
49    */
50   template<typename T, typename T_get /* = T */> // TODO use C++11 or traits
51   std::vector<T> getVectorFromChildren( const SGPropertyNode* parent,
52                                         const char* child_name )
53   {
54     const simgear::PropertyList& props = parent->getChildren(child_name);
55     std::vector<T> values( props.size() );
56
57     for( size_t i = 0; i < props.size(); ++i )
58       values[i] = getValue<T_get>(props[i]);
59
60     return values;
61   }
62
63   /**
64    * @param name    Name of color node
65    * @param parent  Parent for color channel nodes
66    * @param nodes   Vector to push color nodes into
67    * @param def     Default color
68    *
69    * <name>
70    *   <red type="float">def[0] or existing value</red>
71    *   <green type="float">def[1] or existing value</green>
72    *   <blue type="float">def[2] or existing value</blue>
73    *   <alpha type="float">def[3] or existing value</alpha>
74    * </name>
75    */
76   void linkColorNodes( const char* name,
77                        SGPropertyNode* parent,
78                        std::vector<SGPropertyNode_ptr>& nodes,
79                        const osg::Vec4& def = osg::Vec4(0,0,0,1) );
80
81   /**
82    * Trigger a childAdded and valueChanged event for every child of node
83    * (Unlimited depth) and node itself.
84    */
85   void triggerChangeRecursive(SGPropertyNode* node);
86
87 } // namespace canvas
88
89 #endif /* PROPERTY_HELPER_HXX_ */