2001-10-22 05:27:40 +08:00
|
|
|
#include <osg/Switch>
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2002-10-08 04:17:57 +08:00
|
|
|
Switch::Switch():
|
|
|
|
_newChildDefaultValue(true)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-01-29 22:04:06 +08:00
|
|
|
Switch::Switch(const Switch& sw,const CopyOp& copyop):
|
|
|
|
Group(sw,copyop),
|
2002-10-08 04:17:57 +08:00
|
|
|
_newChildDefaultValue(sw._newChildDefaultValue),
|
2002-10-04 22:50:33 +08:00
|
|
|
_values(sw._values)
|
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
|
|
|
{
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
void Switch::traverse(NodeVisitor& nv)
|
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
switch(nv.getTraversalMode())
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
case(NodeVisitor::TRAVERSE_ALL_CHILDREN):
|
2002-10-04 22:50:33 +08:00
|
|
|
{
|
2001-01-11 00:32:10 +08:00
|
|
|
std::for_each(_children.begin(),_children.end(),NodeAcceptOp(nv));
|
|
|
|
break;
|
2002-10-04 22:50:33 +08:00
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
case(NodeVisitor::TRAVERSE_ACTIVE_CHILDREN):
|
2002-10-04 22:50:33 +08:00
|
|
|
{
|
|
|
|
for(unsigned int pos=0;pos<_children.size();++pos)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2002-10-04 22:50:33 +08:00
|
|
|
if (_values[pos]) _children[pos]->accept(nv);
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
break;
|
2002-10-04 22:50:33 +08:00
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-04 22:50:33 +08:00
|
|
|
bool Switch::addChild( Node *child )
|
2002-10-08 04:17:57 +08:00
|
|
|
{
|
|
|
|
return addChild(child,_newChildDefaultValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Switch::addChild( Node *child, bool value )
|
2002-10-04 22:50:33 +08:00
|
|
|
{
|
2002-10-30 18:07:16 +08:00
|
|
|
unsigned int childPosition = _children.size();
|
2002-10-04 22:50:33 +08:00
|
|
|
if (Group::addChild(child))
|
|
|
|
{
|
2002-10-30 18:07:16 +08:00
|
|
|
if (_children.size()>_values.size())
|
|
|
|
{
|
|
|
|
_values.resize(_children.size(),_newChildDefaultValue);
|
|
|
|
_values[childPosition]=value;
|
|
|
|
}
|
|
|
|
return true;
|
2002-10-04 22:50:33 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Switch::removeChild( Node *child )
|
|
|
|
{
|
|
|
|
// find the child's position.
|
2002-11-21 17:07:11 +08:00
|
|
|
unsigned int pos=getChildIndex(child);
|
2002-10-04 22:50:33 +08:00
|
|
|
if (pos==_children.size()) return false;
|
|
|
|
|
|
|
|
_values.erase(_values.begin()+pos);
|
|
|
|
|
2002-10-05 00:40:45 +08:00
|
|
|
return Group::removeChild(child);
|
2002-10-04 22:50:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Switch::setValue(unsigned int pos,bool value)
|
|
|
|
{
|
2002-10-08 04:17:57 +08:00
|
|
|
if (pos>=_values.size()) _values.resize(pos+1,_newChildDefaultValue);
|
2002-10-04 22:50:33 +08:00
|
|
|
_values[pos]=value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Switch::setValue(const Node* child,bool value)
|
|
|
|
{
|
|
|
|
// find the child's position.
|
2002-11-21 17:07:11 +08:00
|
|
|
unsigned int pos=getChildIndex(child);
|
2002-10-04 22:50:33 +08:00
|
|
|
if (pos==_children.size()) return;
|
|
|
|
|
|
|
|
_values[pos]=value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Switch::getValue(unsigned int pos) const
|
|
|
|
{
|
|
|
|
if (pos>=_values.size()) return false;
|
|
|
|
return _values[pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Switch::getValue(const Node* child) const
|
|
|
|
{
|
|
|
|
// find the child's position.
|
2002-11-21 17:07:11 +08:00
|
|
|
unsigned int pos=getChildIndex(child);
|
2002-10-04 22:50:33 +08:00
|
|
|
if (pos==_children.size()) return false;
|
|
|
|
|
|
|
|
return _values[pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
void Switch::setValue(int value)
|
|
|
|
{
|
|
|
|
switch(value)
|
|
|
|
{
|
|
|
|
case(MULTIPLE_CHILDREN_ON):
|
|
|
|
// do nothing...
|
|
|
|
break;
|
|
|
|
case(ALL_CHILDREN_OFF):
|
|
|
|
{
|
2002-10-08 04:17:57 +08:00
|
|
|
_newChildDefaultValue = false;
|
2002-10-04 22:50:33 +08:00
|
|
|
for(ValueList::iterator itr=_values.begin();
|
|
|
|
itr!=_values.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(ALL_CHILDREN_ON):
|
|
|
|
{
|
2002-10-08 04:17:57 +08:00
|
|
|
_newChildDefaultValue = true;
|
2002-10-04 22:50:33 +08:00
|
|
|
for(ValueList::iterator itr=_values.begin();
|
|
|
|
itr!=_values.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
for(ValueList::iterator itr=_values.begin();
|
|
|
|
itr!=_values.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr = false;
|
|
|
|
}
|
|
|
|
setValue(value,true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Switch::getValue() const
|
|
|
|
{
|
|
|
|
if (_values.empty()) return ALL_CHILDREN_OFF;
|
|
|
|
|
|
|
|
unsigned int noChildrenSwitchedOn=0;
|
|
|
|
int firstChildSelected=ALL_CHILDREN_OFF;
|
|
|
|
for(unsigned int i=0; i<_values.size();++i)
|
|
|
|
{
|
|
|
|
if (_values[i])
|
|
|
|
{
|
|
|
|
++noChildrenSwitchedOn;
|
|
|
|
if (firstChildSelected==ALL_CHILDREN_OFF) firstChildSelected=i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noChildrenSwitchedOn>1)
|
|
|
|
{
|
|
|
|
if (noChildrenSwitchedOn==_values.size()) return ALL_CHILDREN_ON;
|
|
|
|
else return MULTIPLE_CHILDREN_ON;
|
|
|
|
}
|
|
|
|
return firstChildSelected;
|
|
|
|
|
|
|
|
}
|