1 // Some helper functions for accessing the property tree
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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.
19 #ifndef PROPERTY_HELPER_HXX_
20 #define PROPERTY_HELPER_HXX_
22 #include <simgear/props/props.hxx>
29 * Get property node with default value
32 SGPropertyNode* getChildDefault( SGPropertyNode* parent,
36 SGPropertyNode* node = parent->getNode(name);
38 // also set value for existing nodes to enforce type...
39 def_val = getValue<T>(node);
41 node = parent->getChild(name, 0, true);
43 setValue(node, def_val);
48 * Get vector of properties
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 )
54 const simgear::PropertyList& props = parent->getChildren(child_name);
55 std::vector<T> values( props.size() );
57 for( size_t i = 0; i < props.size(); ++i )
58 values[i] = getValue<T_get>(props[i]);
64 * Split a string by a delimter and convert the values to float
66 * TODO do we need other types than float?
68 std::vector<float> splitAndConvert(const char del[], const std::string& str);
73 osg::Vec4 parseColor(std::string str);
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
81 * @deprecated Use only a single property instead and parse with #parseColor
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>
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) );
96 * Trigger a childAdded and valueChanged event for every child of node
97 * (Unlimited depth) and node itself.
99 void triggerChangeRecursive(SGPropertyNode* node);
101 } // namespace canvas
103 #endif /* PROPERTY_HELPER_HXX_ */