2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2005-04-25 05:04:54 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This application is open source and may be redistributed and/or modified
|
2010-11-22 19:22:03 +08:00
|
|
|
* freely and without restriction, both in commercial and non commercial
|
2005-04-25 05:04:54 +08:00
|
|
|
* applications, as long as this copyright notice is maintained.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2005-04-25 05:04:54 +08:00
|
|
|
* This application is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <osg/StateAttribute>
|
2005-04-25 21:37:12 +08:00
|
|
|
#include <osg/StateSet>
|
2006-09-19 04:54:48 +08:00
|
|
|
#include <osg/State>
|
2014-06-06 00:26:13 +08:00
|
|
|
#include <osg/NodeVisitor>
|
2005-04-25 21:37:12 +08:00
|
|
|
#include <osg/Notify>
|
2005-04-25 05:04:54 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2007-08-22 18:34:11 +08:00
|
|
|
StateAttribute::StateAttribute()
|
|
|
|
:Object(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-25 05:04:54 +08:00
|
|
|
void StateAttribute::addParent(osg::StateSet* object)
|
|
|
|
{
|
2010-05-28 23:47:52 +08:00
|
|
|
OSG_DEBUG_FP<<"Adding parent"<<getRefMutex()<<std::endl;
|
2008-10-14 22:27:41 +08:00
|
|
|
OpenThreads::ScopedPointerLock<OpenThreads::Mutex> lock(getRefMutex());
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-25 05:04:54 +08:00
|
|
|
_parents.push_back(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateAttribute::removeParent(osg::StateSet* object)
|
|
|
|
{
|
2008-10-14 22:27:41 +08:00
|
|
|
OpenThreads::ScopedPointerLock<OpenThreads::Mutex> lock(getRefMutex());
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-25 05:04:54 +08:00
|
|
|
ParentList::iterator pitr = std::find(_parents.begin(),_parents.end(),object);
|
|
|
|
if (pitr!=_parents.end()) _parents.erase(pitr);
|
|
|
|
}
|
2005-04-25 21:37:12 +08:00
|
|
|
|
|
|
|
|
2009-10-22 18:33:16 +08:00
|
|
|
void StateAttribute::setUpdateCallback(StateAttributeCallback* uc)
|
2005-04-25 21:37:12 +08:00
|
|
|
{
|
2017-01-21 01:31:09 +08:00
|
|
|
OSG_DEBUG<<"StateAttribute::Setting Update callbacks"<<std::endl;
|
2005-04-25 21:37:12 +08:00
|
|
|
|
|
|
|
if (_updateCallback==uc) return;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-25 21:37:12 +08:00
|
|
|
int delta = 0;
|
|
|
|
if (_updateCallback.valid()) --delta;
|
|
|
|
if (uc) ++delta;
|
|
|
|
|
|
|
|
_updateCallback = uc;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-25 21:37:12 +08:00
|
|
|
if (delta!=0)
|
|
|
|
{
|
|
|
|
for(ParentList::iterator itr=_parents.begin();
|
|
|
|
itr!=_parents.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
(*itr)->setNumChildrenRequiringUpdateTraversal((*itr)->getNumChildrenRequiringUpdateTraversal()+delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-22 18:33:16 +08:00
|
|
|
void StateAttribute::setEventCallback(StateAttributeCallback* ec)
|
2005-04-25 21:37:12 +08:00
|
|
|
{
|
2017-01-21 01:31:09 +08:00
|
|
|
OSG_DEBUG<<"StateAttribute::Setting Event callbacks"<<std::endl;
|
2005-04-25 21:37:12 +08:00
|
|
|
|
2005-05-19 02:31:03 +08:00
|
|
|
if (_eventCallback==ec) return;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-25 21:37:12 +08:00
|
|
|
int delta = 0;
|
2005-05-19 02:31:03 +08:00
|
|
|
if (_eventCallback.valid()) --delta;
|
2005-04-25 21:37:12 +08:00
|
|
|
if (ec) ++delta;
|
|
|
|
|
2005-05-19 02:31:03 +08:00
|
|
|
_eventCallback = ec;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2005-04-25 21:37:12 +08:00
|
|
|
if (delta!=0)
|
|
|
|
{
|
|
|
|
for(ParentList::iterator itr=_parents.begin();
|
|
|
|
itr!=_parents.end();
|
|
|
|
++itr)
|
|
|
|
{
|
2005-05-19 02:31:03 +08:00
|
|
|
(*itr)->setNumChildrenRequiringEventTraversal((*itr)->getNumChildrenRequiringEventTraversal()+delta);
|
2005-04-25 21:37:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-25 19:24:19 +08:00
|
|
|
|
|
|
|
StateAttribute::ReassignToParents::ReassignToParents(osg::StateAttribute* attr)
|
|
|
|
{
|
|
|
|
if (!attr->isTextureAttribute() && !attr->getParents().empty())
|
|
|
|
{
|
|
|
|
// take a reference to this clip plane to prevent it from going out of scope
|
|
|
|
// when we remove it temporarily from its parents.
|
|
|
|
attribute = attr;
|
|
|
|
|
|
|
|
// copy the parents as they _parents list will be changed by the subsequent removeAttributes.
|
|
|
|
parents = attr->getParents();
|
|
|
|
|
|
|
|
// remove this attribute from its parents as its position is being changed
|
|
|
|
// and would no longer be valid.
|
|
|
|
for(ParentList::iterator itr = parents.begin();
|
|
|
|
itr != parents.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
osg::StateSet* stateset = *itr;
|
|
|
|
stateset->removeAttribute(attr);
|
|
|
|
|
|
|
|
OSG_NOTICE<<" Removed from parent "<<stateset<<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StateAttribute::ReassignToParents::~ReassignToParents()
|
|
|
|
{
|
|
|
|
// add attribute back into its original parents with its new position
|
|
|
|
for(ParentList::iterator itr = parents.begin();
|
|
|
|
itr != parents.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
osg::StateSet* stateset = *itr;
|
|
|
|
stateset->setAttribute(attribute.get());
|
|
|
|
OSG_NOTICE<<" Added back to parent "<<stateset<<std::endl;
|
|
|
|
}
|
|
|
|
}
|