]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/property_helper.hxx
Canvas: CSS like property value inheritance.
[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    * Split a string by a delimter and convert the values to float
65    *
66    * TODO do we need other types than float?
67    */
68   std::vector<float> splitAndConvert(const char del[], const std::string& str);
69
70   /**
71    * Parse a (CSS) color
72    */
73   osg::Vec4 parseColor(std::string str);
74
75   /**
76    * @param name    Name of color node
77    * @param parent  Parent for color channel nodes
78    * @param nodes   Vector to push color nodes into
79    * @param def     Default color
80    *
81    * @deprecated Use only a single property instead and parse with #parseColor
82    *
83    * <name>
84    *   <red type="float">def[0] or existing value</red>
85    *   <green type="float">def[1] or existing value</green>
86    *   <blue type="float">def[2] or existing value</blue>
87    *   <alpha type="float">def[3] or existing value</alpha>
88    * </name>
89    */
90   void linkColorNodes( const char* name,
91                        SGPropertyNode* parent,
92                        std::vector<SGPropertyNode_ptr>& nodes,
93                        const osg::Vec4& def = osg::Vec4(0,0,0,1) );
94
95   /**
96    * Trigger a childAdded and valueChanged event for every child of node
97    * (Unlimited depth) and node itself.
98    */
99   void triggerChangeRecursive(SGPropertyNode* node);
100
101 } // namespace canvas
102
103 #endif /* PROPERTY_HELPER_HXX_ */