diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index cc940046e..53b2f6269 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -151,6 +151,7 @@ IF(DYNAMIC_OPENSCENEGRAPH) ADD_SUBDIRECTORY(osgwidgetstyled) ADD_SUBDIRECTORY(osgwidgettable) ADD_SUBDIRECTORY(osgwidgetwindow) + ADD_SUBDIRECTORY(osguserdata) IF(OSG_CPP_EXCEPTIONS_AVAILABLE) ADD_SUBDIRECTORY(osgunittests) diff --git a/examples/osguserdata/CMakeLists.txt b/examples/osguserdata/CMakeLists.txt new file mode 100644 index 000000000..0cf0f1203 --- /dev/null +++ b/examples/osguserdata/CMakeLists.txt @@ -0,0 +1,4 @@ +SET(TARGET_SRC osguserdata.cpp) + +#### end var setup ### +SETUP_EXAMPLE(osguserdata) diff --git a/examples/osguserdata/osguserdata.cpp b/examples/osguserdata/osguserdata.cpp new file mode 100644 index 000000000..0712b9cc8 --- /dev/null +++ b/examples/osguserdata/osguserdata.cpp @@ -0,0 +1,195 @@ +/* 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 + +class MyGetValueVisitor : public osg::ValueObject::GetValueVisitor +{ + public: + virtual void apply(bool value) { OSG_NOTICE<<" bool "< +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="<getNumUserObjects()="<getNumUserObjects()<getNumUserObjects(); ++i) + { + MyGetValueVisitor mgvv; + osg::Object* userObject = node->getUserObject(i); + osg::ValueObject* valueObject = dynamic_cast(userObject); + OSG_NOTICE<<"userObject="<(userObject)<<" "; + if (valueObject) valueObject->get(mgvv); + OSG_NOTICE< node = new osg::Group; + + int i = 10; + node->setUserValue("Int value",i); + + std::string testString("All seems fine"); + node->setUserValue("Status",testString); + + node->setUserValue("Height",1.4); + + osg::ref_ptr drawable = new osg::Geometry; + drawable->setName("myDrawable"); + node->addUserObject(drawable.get()); + + node->setUserValue("fred",12); + node->setUserValue("john",1.1); + node->setUserValue("david",1.9f); + node->setUserValue("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<