Improved support for setting/getting properties via the osg::UserDataContainer.

This commit is contained in:
Robert Osfield 2014-02-11 18:07:37 +00:00
parent e3a003a08f
commit 2797e8cb7c
4 changed files with 60 additions and 8 deletions

View File

@ -187,6 +187,15 @@ protected:
ObjectList _objectList;
};
/** Convinience function for getting the User Object associated with specificed name from an Object's UserDataContainer.*/
inline Object* getUserObject(osg::Object* object, const std::string& name)
{
osg::UserDataContainer* udc = object->getUserDataContainer();
return udc ? udc->getUserObject(name) : 0;
}
}
#endif

View File

@ -247,13 +247,38 @@ typedef osg::Object* ObjectPtr;
template<>
inline bool PropertyInterface::getProperty(const osg::Object* object, const std::string& propertyName, ObjectPtr& value)
{
return copyPropertyObjectFromObject(object, propertyName, &value, sizeof(ObjectPtr), getTypeEnum<ObjectPtr>());
if (copyPropertyObjectFromObject(object, propertyName, &value, sizeof(ObjectPtr), getTypeEnum<ObjectPtr>())) return true;
else
{
OSG_NOTICE<<"PropertyInterface::getProperty("<<propertyName<<", "<<value->className()<<") Checking UserDataContainer for object ptr"<<std::endl;
const osg::UserDataContainer* udc = object->getUserDataContainer();
if (udc)
{
OSG_NOTICE<<" Checking UserDataContainer for object ptr"<<std::endl;
const osg::Object* ptr = udc->getUserObject(propertyName);
if (ptr)
{
value = const_cast<ObjectPtr>(ptr);
return true;
}
}
return false;
}
}
template<>
inline bool PropertyInterface::setProperty(osg::Object* object, const std::string& propertyName, const ObjectPtr& value)
{
return copyPropertyObjectToObject(object, propertyName, &value, sizeof(ObjectPtr), getTypeEnum<ObjectPtr>());
if (copyPropertyObjectToObject(object, propertyName, &value, sizeof(ObjectPtr), getTypeEnum<ObjectPtr>())) return true;
else
{
OSG_NOTICE<<"PropertyInterface::setProperty("<<propertyName<<", "<<value->className()<<") Adding object to UserDataContainer"<<std::endl;
// fallback to using user data to store property data
osg::UserDataContainer* udc = object->getOrCreateUserDataContainer();
value->setName(propertyName);
udc->addUserObject(value);
return true;
}
}
}

View File

@ -292,7 +292,18 @@ osgDB::BaseSerializer* PropertyInterface::getSerializer(const osg::Object* objec
osg::Object* PropertyInterface::createObject(const std::string& compoundClassName) const
{
osgDB::ObjectWrapper* ow = osgDB::Registry::instance()->getObjectWrapperManager()->findWrapper(compoundClassName);
return (ow!=0) ? ow->createInstance() : 0;
if (ow)
{
osg::Object* object = ow->createInstance();
OSG_NOTICE<<"PropertyInterface::createObject("<<compoundClassName<<"), wrapper found, created object="<<object<<std::endl;
return object;
}
else
{
OSG_NOTICE<<"PropertyInterface::createObject("<<compoundClassName<<"), No object wrapper avaiable."<<std::endl;
return 0;
}
// return (ow!=0) ? ow->createInstance() : 0;
}
bool PropertyInterface::copyPropertyDataFromObject(const osg::Object* object, const std::string& propertyName, void* valuePtr, unsigned int valueSize, osgDB::BaseSerializer::Type valueType)
@ -366,7 +377,7 @@ bool PropertyInterface::copyPropertyDataToObject(osg::Object* object, const std:
}
else
{
OSG_INFO<<"PropertyInterface::copyPropertyDataFromObject() no serializer available."<<std::endl;
OSG_NOTICE<<"PropertyInterface::copyPropertyDataFromObject() no serializer available."<<std::endl;
return false;
}
}
@ -389,7 +400,7 @@ bool PropertyInterface::copyPropertyObjectFromObject(const osg::Object* object,
}
else
{
OSG_INFO<<"PropertyInterface::copyPropertyObjectFromObject() no serializer available."<<std::endl;
OSG_NOTICE<<"PropertyInterface::copyPropertyObjectFromObject() no serializer available."<<std::endl;
return false;
}
}

View File

@ -537,6 +537,8 @@ public:
int LuaScriptEngine::pushPropertyToStack(osg::Object* object, const std::string& propertyName) const
{
OSG_NOTICE<<"LuaScriptEngine::pushPropertyToStack("<<object<<", "<<propertyName<<")"<<std::endl;
osgDB::BaseSerializer::Type type;
if (!_pi.getPropertyType(object, propertyName, type))
{
@ -549,15 +551,20 @@ int LuaScriptEngine::pushPropertyToStack(osg::Object* object, const std::string&
return 1;
}
osg::CallbackObject* co = osg::getCallbackObject(object, propertyName);
LuaCallbackObject* lco = dynamic_cast<LuaCallbackObject*>(co);
osg::Object* uo = osg::getUserObject(object, propertyName);
LuaCallbackObject* lco = dynamic_cast<LuaCallbackObject*>(uo);
if (lco)
{
lua_rawgeti(_lua, LUA_REGISTRYINDEX, lco->getRef());
return 1;
}
else if (uo)
{
pushObject(uo);
return 1;
}
OSG_INFO<<"LuaScriptEngine::pushPropertyToStack("<<object<<", "<<propertyName<<") no property found."<<std::endl;
OSG_NOTICE<<"LuaScriptEngine::pushPropertyToStack("<<object<<", "<<propertyName<<") no property found."<<std::endl;
return 0;
}