/* OpenSceneGraph example, osguserdata. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include #include #include #include #include #include #include namespace MyNamespace { /** Provide an simple example of customizing the default UserDataContainer.*/ class MyUserDataContainer : public osg::DefaultUserDataContainer { public: MyUserDataContainer() {} MyUserDataContainer(const MyUserDataContainer& udc, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): DefaultUserDataContainer(udc, copyop) {} META_Object(MyNamespace, MyUserDataContainer) virtual Object* getUserObject(unsigned int i) { OSG_NOTICE<<"MyUserDataContainer::getUserObject("< class GetNumeric : public osg::ValueObject::GetValueVisitor { public: GetNumeric(): _set(false), _value(0) {} virtual void apply(bool value) { _value = value; _set = true; } virtual void apply(char value) { _value = value; _set = true; } virtual void apply(unsigned char value) { _value = value; _set = true; } virtual void apply(short value) { _value = value; _set = true; } virtual void apply(unsigned short value) { _value = value; _set = true; } virtual void apply(int value) { _value = value; _set = true; } virtual void apply(unsigned int value) { _value = value; _set = true; } virtual void apply(float value) { _value = value; _set = true; } virtual void apply(double value) { _value = value; _set = true; } bool _set; T _value; }; template T getNumeric(osg::Object* object) { osg::ValueObject* bvo = dynamic_cast(object); if (bvo) { GetNumeric gn; if (bvo->get(gn) && gn._set) return gn._value; } return T(0); } void testResults(osg::Node* node) { int j = 0; if (node->getUserValue("Int value",j)) { OSG_NOTICE<<"Int value="<getUserValue("Status",readString)) { OSG_NOTICE<<"Status="<getUserValue("Height",height)) { OSG_NOTICE<<"Height="<getUserDataContainer(); if (udc) { OSG_NOTICE<<"udc->getNumUserObjects()="<getNumUserObjects()<getNumUserObjects(); ++i) { MyGetValueVisitor mgvv; osg::Object* userObject = udc->getUserObject(i); osg::ValueObject* valueObject = dynamic_cast(userObject); OSG_NOTICE<<"userObject="<(userObject)<<" "; if (valueObject) valueObject->get(mgvv); OSG_NOTICE< node = new osg::Group; if (arguments.read("--MyUserDataContainer") || arguments.read("--mydc")) { node->setUserDataContainer(new MyNamespace::MyUserDataContainer); } int i = 10; node->setUserValue("Int value",i); std::string testString("All seems fine"); node->setUserValue("Status",testString); node->setUserValue("Height",float(1.4)); osg::ref_ptr drawable = new osg::Geometry; drawable->setName("myDrawable"); node->getOrCreateUserDataContainer()->addUserObject(drawable.get()); node->setUserValue("fred",12); node->setUserValue("john",1.1); node->setUserValue("david",1.9f); node->setUserValue("char",char(65)); node->setUserValue("matrixd",osg::Matrixd::translate(1.0,2.0,3.0)); node->setUserValue("flag-on",true); node->setUserValue("flag-off",false); OSG_NOTICE<<"Testing results for values set directly on scene graph"< from_osgt = osgDB::readNodeFile("results.osgt"); if (from_osgt.valid()) { OSG_NOTICE< from_osgb = osgDB::readNodeFile("results.osgb"); if (from_osgb.valid()) { OSG_NOTICE< from_osgx = osgDB::readNodeFile("results.osgx"); if (from_osgx.valid()) { OSG_NOTICE<