2003-01-22 00:45:36 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library 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. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
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)
|
|
|
|
{
|
2002-12-08 05:18:12 +08:00
|
|
|
if (nv.getTraversalMode()==NodeVisitor::TRAVERSE_ACTIVE_CHILDREN)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-12-08 05:18:12 +08:00
|
|
|
for(unsigned int pos=0;pos<_children.size();++pos)
|
2002-10-04 22:50:33 +08:00
|
|
|
{
|
2002-12-08 05:18:12 +08:00
|
|
|
if (_values[pos]) _children[pos]->accept(nv);
|
2002-10-04 22:50:33 +08:00
|
|
|
}
|
2002-12-08 05:18:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Group::traverse(nv);
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-11-20 20:03:51 +08:00
|
|
|
bool Switch::insertChild( unsigned int index, Node *child )
|
|
|
|
{
|
|
|
|
return insertChild(index,child,_newChildDefaultValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Switch::insertChild( unsigned int index, Node *child, bool value )
|
|
|
|
{
|
|
|
|
if (Group::insertChild(index,child))
|
|
|
|
{
|
|
|
|
if (index>=_values.size())
|
|
|
|
{
|
|
|
|
_values.push_back(value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_values.insert(_values.begin()+index, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-10-04 22:50:33 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-12-08 05:18:12 +08:00
|
|
|
void Switch::setChildValue(const Node* child,bool value)
|
2002-10-04 22:50:33 +08:00
|
|
|
{
|
|
|
|
// 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];
|
|
|
|
}
|
|
|
|
|
2002-12-08 05:18:12 +08:00
|
|
|
bool Switch::getChildValue(const Node* child) const
|
2002-10-04 22:50:33 +08:00
|
|
|
{
|
|
|
|
// 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];
|
|
|
|
}
|
|
|
|
|
2002-12-08 05:18:12 +08:00
|
|
|
bool Switch::setAllChildrenOff()
|
|
|
|
{
|
|
|
|
_newChildDefaultValue = false;
|
|
|
|
for(ValueList::iterator itr=_values.begin();
|
|
|
|
itr!=_values.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr = false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Switch::setAllChildrenOn()
|
|
|
|
{
|
|
|
|
_newChildDefaultValue = true;
|
|
|
|
for(ValueList::iterator itr=_values.begin();
|
|
|
|
itr!=_values.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Switch::setSingleChildOn(unsigned int pos)
|
|
|
|
{
|
|
|
|
for(ValueList::iterator itr=_values.begin();
|
|
|
|
itr!=_values.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr = false;
|
|
|
|
}
|
|
|
|
setValue(pos,true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_DEPRECTATED_API
|
2002-10-04 22:50:33 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
2002-12-08 05:18:12 +08:00
|
|
|
#endif
|