/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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. */ #include #include using namespace osgSim; using namespace osg; void dump(std::string Label, osgSim::MultiSwitch::SwitchSetList values) { OSG_ALWAYS << Label << std::endl; for (int i = 0; i < values.size(); i++) { osgSim::MultiSwitch::ValueList list = values[i]; OSG_ALWAYS << "values[" << i << "]" << std::endl; for (int j = 0; j < list.size(); j++) { OSG_ALWAYS << " list[" << j << "] = " << list[j] << std::endl; } } OSG_ALWAYS << "=============== " << std::endl; } MultiSwitch::MultiSwitch(): _newChildDefaultValue(true), _activeSwitchSet(0) { } MultiSwitch::MultiSwitch(const MultiSwitch& sw,const osg::CopyOp& copyop): osg::Group(sw,copyop), _newChildDefaultValue(sw._newChildDefaultValue), _activeSwitchSet(sw._activeSwitchSet), _values(sw._values) { } void MultiSwitch::traverse(osg::NodeVisitor& nv) { if (nv.getTraversalMode()==osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN) { if (_activeSwitchSet<_values.size()) { for(unsigned int pos=0;pos<_children.size();++pos) { if (_values[_activeSwitchSet][pos]) _children[pos]->accept(nv); } } } else { Group::traverse(nv); } } bool MultiSwitch::addChild( osg::Node *child) { unsigned int childPosition = _children.size(); dump("addChild " + getName(), _values); if (Group::addChild(child)) { for(SwitchSetList::iterator itr=_values.begin(); itr!=_values.end(); ++itr) { ValueList& values = *itr; if (_children.size()>values.size()) { dump("After addChild before resize", _values); values.resize(_children.size(),_newChildDefaultValue); dump("After resize", _values); values[childPosition]=_newChildDefaultValue; dump("After addChild", _values); } } return true; } return false; } bool MultiSwitch::insertChild( unsigned int index, osg::Node *child) { if (Group::insertChild(index,child)) { for(SwitchSetList::iterator itr=_values.begin(); itr!=_values.end(); ++itr) { ValueList& values = *itr; if (index>=values.size()) { values.push_back(_newChildDefaultValue); } else { values.insert(values.begin()+index, _newChildDefaultValue); } } return true; } return false; } bool MultiSwitch::removeChild( osg::Node *child ) { // find the child's position. unsigned int pos=getChildIndex(child); if (pos==_children.size()) return false; for(SwitchSetList::iterator itr=_values.begin(); itr!=_values.end(); ++itr) { ValueList& values = *itr; values.erase(values.begin()+pos); } return Group::removeChild(child); } void MultiSwitch::setValue(unsigned int switchSet, unsigned int pos,bool value) { OSG_ALWAYS << "Which = " << getName() << std::endl; OSG_ALWAYS << "Switch set = " << switchSet << std::endl; OSG_ALWAYS << "Pos = " << pos << std::endl; OSG_ALWAYS << "Value = " << value << std::endl; dump("setValue before expand ", _values); expandToEncompassSwitchSet(switchSet); dump("After expand", _values); ValueList& values = _values[switchSet]; if (pos>=values.size()) values.resize(pos+1,_newChildDefaultValue); values[pos]=value; dump("After Assign " + getName(), _values); } void MultiSwitch::setChildValue(const osg::Node* child,unsigned int switchSet, bool value) { expandToEncompassSwitchSet(switchSet); // find the child's position. unsigned int pos=getChildIndex(child); if (pos==_children.size()) return; ValueList& values = _values[switchSet]; values[pos]=value; } bool MultiSwitch::getValue(unsigned int switchSet, unsigned int pos) const { if (switchSet>=_values.size()) return false; const ValueList& values = _values[switchSet]; if (pos>=values.size()) return false; return values[pos]; } bool MultiSwitch::getChildValue(const osg::Node* child, unsigned int switchSet) const { if (switchSet>=_values.size()) return false; // find the child's position. unsigned int pos=getChildIndex(child); if (pos==_children.size()) return false; const ValueList& values = _values[switchSet]; return values[pos]; } void MultiSwitch::expandToEncompassSwitchSet(unsigned int switchSet) { if (switchSet>=_values.size()) { // need to expand arrays. unsigned int originalSize = _values.size(); _values.resize(switchSet+1); _valueNames.resize(switchSet+1); for(unsigned int i=originalSize;i<=switchSet;++i) { ValueList& values = _values[i]; values.resize(_children.size(),_newChildDefaultValue); } } } bool MultiSwitch::setAllChildrenOff(unsigned int switchSet) { _newChildDefaultValue = false; expandToEncompassSwitchSet(switchSet); ValueList& values = _values[switchSet]; for(ValueList::iterator itr=values.begin(); itr!=values.end(); ++itr) { *itr = false; } return true; } bool MultiSwitch::setAllChildrenOn(unsigned int switchSet) { _newChildDefaultValue = true; expandToEncompassSwitchSet(switchSet); ValueList& values = _values[switchSet]; for(ValueList::iterator itr=values.begin(); itr!=values.end(); ++itr) { *itr = true; } return true; } bool MultiSwitch::setSingleChildOn(unsigned int switchSet, unsigned int pos) { expandToEncompassSwitchSet(switchSet); ValueList& values = _values[switchSet]; for(ValueList::iterator itr=values.begin(); itr!=values.end(); ++itr) { *itr = false; } setValue(switchSet, pos,true); return true; } void MultiSwitch::setSwitchSetList(const SwitchSetList& switchSetList) { expandToEncompassSwitchSet(switchSetList.size()); _values = switchSetList; } void MultiSwitch::setValueList(unsigned int switchSet, const ValueList& values) { dump("setValue List prior to expand", _values); expandToEncompassSwitchSet(switchSet); dump("setValue List after expand", _values); _values[switchSet] = values; dump("setValue List at the end", _values); } void MultiSwitch::setValueName(unsigned int switchSet, const std::string& name) { expandToEncompassSwitchSet(switchSet); _valueNames[switchSet] = name; }