+
+template<typename ObjType, typename OSGParamType>
+class ScalarChangeListener
+ : public SGPropertyChangeListener, public InitializeWhenAdded,
+ public Effect::Updater
+{
+public:
+ typedef void (ObjType::*setter_type)(const OSGParamType);
+ ScalarChangeListener(ObjType* obj, setter_type setter,
+ const std::string& propName)
+ : _obj(obj), _setter(setter)
+ {
+ _propName = new std::string(propName);
+ }
+ virtual ~ScalarChangeListener()
+ {
+ delete _propName;
+ _propName = 0;
+ }
+ void valueChanged(SGPropertyNode* node)
+ {
+ _obj->*setter(node->getValue<OSGParamType>());
+ }
+ void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
+ {
+ SGPropertyNode* listenProp = makeNode(propRoot, *_propName);
+ delete _propName;
+ _propName = 0;
+ if (listenProp)
+ listenProp->addChangeListener(this, true);
+ }
+private:
+ osg::ref_ptr<ObjType> _obj;
+ setter_type _setter;
+ std::string* _propName;
+};
+
+template<typename T, typename Func>
+class EffectExtendedPropListener : public InitializeWhenAdded,
+ public Effect::Updater
+{
+public:
+ template<typename Itr>
+ EffectExtendedPropListener(const Func& func,
+ const std::string& propName, Itr childNamesBegin,
+ Itr childNamesEnd)
+ : _func(func)
+ {
+ _propName = new std::string(propName);
+ _childNames = new std::vector<std::string>(childNamesBegin,
+ childNamesEnd);
+ }
+ virtual ~EffectExtendedPropListener()
+ {
+ delete _propName;
+ delete _childNames;
+ }
+ void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
+ {
+ SGPropertyNode* parent = propRoot->getNode(*_propName, true);
+ _propListener
+ = new ExtendedPropListener<T, Func>(parent, _childNames->begin(),
+ _childNames->end(),
+ _func, true);
+ delete _propName;
+ _propName = 0;
+ delete _childNames;
+ _childNames = 0;
+ }
+private:
+ std::string* _propName;
+ std::vector<std::string>* _childNames;
+ SGSharedPtr<ExtendedPropListener<T, Func> > _propListener;
+ Func _func;
+};
+
+/**
+ * Initialize the value and the possible updating of an effect
+ * attribute. If the value is specified directly, set it. Otherwise,
+ * use the <use> tag to look at the parameters. Again, if there is a
+ * value there set it directly. Otherwise, the parameter contains its
+ * own <use> tag referring to a property in the global property tree;
+ * install a change listener that will set the attribute when the
+ * property changes.
+ */
+template<typename ObjType, typename OSGParamType>
+void
+initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
+ void (ObjType::*setter)(const OSGParamType))
+{
+ const SGPropertyNode* valProp = getEffectPropertyNode(effect, prop);
+ if (!valProp)
+ return;
+ if (valProp->nChildren() == 0) {
+ obj->*setter(valProp->getValue<OSGParamType>());
+ } else {
+ std::string propName = getGlobalProperty(prop);
+ ScalarChangeListener<ObjType, OSGParamType>* listener
+ = new ScalarChangeListener<ObjType, OSGParamType>(obj, setter,
+ propName);
+ effect->addUpdater(listener);
+ }
+}
+
+template<typename ObjType, typename OSGParamType, typename NameItrType>
+void
+initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
+ void (ObjType::*setter)(const OSGParamType&),
+ NameItrType nameItr)
+{
+ typedef typename OSGBridge<OSGParamType>::sg_type sg_type;
+ const SGPropertyNode* valProp = getEffectPropertyNode(effect, prop);
+ if (!valProp)
+ return;
+ if (valProp->nChildren() == 0) {
+ (obj->*setter)(OSGBridge<OSGParamType>
+ ::getOsgType(valProp->getValue<sg_type>()));
+ } else {
+ string listenPropName = getGlobalProperty(valProp);
+ if (listenPropName.empty())
+ return;
+ typedef OSGFunctor<ObjType, OSGParamType> Functor;
+ Effect::Updater* listener
+ = new EffectExtendedPropListener<sg_type, Functor>
+ (Functor(obj, setter), listenPropName, nameItr,
+ nameItr + props::NumComponents<sg_type>::num_components);
+ effect->addUpdater(listener);
+ }
+}
+
+extern const char* colorFields[];
+}