1 #include <osg/Switch> 2 #include <osg/ValueObject> 3 #include <osgDB/ObjectWrapper> 4 #include <osgDB/InputStream> 5 #include <osgDB/OutputStream> 6 7 struct SwitchGetValue : public osgDB::MethodObject 8 { runSwitchGetValue9 virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const 10 { 11 if (inputParameters.empty()) return false; 12 13 osg::Object* indexObject = inputParameters[0].get(); 14 15 unsigned int index = 0; 16 osg::DoubleValueObject* dvo = dynamic_cast<osg::DoubleValueObject*>(indexObject); 17 if (dvo) index = static_cast<unsigned int>(dvo->getValue()); 18 else 19 { 20 osg::UIntValueObject* uivo = dynamic_cast<osg::UIntValueObject*>(indexObject); 21 if (uivo) index = uivo->getValue(); 22 } 23 24 osg::Switch* sw = reinterpret_cast<osg::Switch*>(objectPtr); 25 outputParameters.push_back(new osg::BoolValueObject("return", sw->getValue(index))); 26 27 return true; 28 } 29 }; 30 31 32 struct SwitchSetValue : public osgDB::MethodObject 33 { runSwitchSetValue34 virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const 35 { 36 if (inputParameters.size()<2) return false; 37 38 osg::Object* indexObject = inputParameters[0].get(); 39 40 unsigned int index = 0; 41 osg::DoubleValueObject* dvo = dynamic_cast<osg::DoubleValueObject*>(indexObject); 42 if (dvo) index = static_cast<unsigned int>(dvo->getValue()); 43 else 44 { 45 osg::UIntValueObject* uivo = dynamic_cast<osg::UIntValueObject*>(indexObject); 46 if (uivo) index = uivo->getValue(); 47 } 48 49 bool enabled = false; 50 osg::Object* valueObject = inputParameters[1].get(); 51 if (!valueObject) return false; 52 53 dvo = dynamic_cast<osg::DoubleValueObject*>(valueObject); 54 if (dvo) 55 { 56 enabled = dvo->getValue()!=0.0; 57 } 58 else 59 { 60 osg::UIntValueObject* uivo = dynamic_cast<osg::UIntValueObject*>(valueObject); 61 if (uivo) 62 { 63 enabled = uivo->getValue()!=0; 64 } 65 else 66 { 67 osg::BoolValueObject* bo = dynamic_cast<osg::BoolValueObject*>(valueObject); 68 if (bo) 69 { 70 enabled = bo->getValue(); 71 } 72 } 73 } 74 75 osg::Switch* sw = reinterpret_cast<osg::Switch*>(objectPtr); 76 sw->setValue(index, enabled); 77 78 return true; 79 } 80 }; 81 82 REGISTER_OBJECT_WRAPPER( Switch, 83 new osg::Switch, 84 osg::Switch, 85 "osg::Object osg::Node osg::Group osg::Switch" ) 86 { 87 ADD_BOOL_SERIALIZER( NewChildDefaultValue, true ); // _newChildDefaultValue 88 ADD_LIST_SERIALIZER( ValueList, osg::Switch::ValueList ); // _values 89 90 ADD_METHOD_OBJECT( "getValue", SwitchGetValue ); 91 ADD_METHOD_OBJECT( "setValue", SwitchSetValue ); 92 } 93