2003-05-20 04:18:37 +08:00
|
|
|
#include <osgGA/KeySwitchMatrixManipulator>
|
2002-05-09 18:31:03 +08:00
|
|
|
#include <osg/Notify>
|
|
|
|
|
|
|
|
using namespace osgGA;
|
|
|
|
|
2003-05-20 04:18:37 +08:00
|
|
|
void KeySwitchMatrixManipulator::addMatrixManipulator(int key, std::string name, MatrixManipulator *cm)
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
|
|
|
if(!cm) return;
|
|
|
|
|
2003-05-20 04:18:37 +08:00
|
|
|
_manips[key]=std::make_pair(name,osg::ref_ptr<MatrixManipulator>(cm));
|
2007-01-10 21:52:22 +08:00
|
|
|
|
|
|
|
if(!_current)
|
|
|
|
{
|
2002-05-09 18:31:03 +08:00
|
|
|
_current=cm;
|
2004-09-01 18:18:46 +08:00
|
|
|
_current->setHomePosition(_homeEye,_homeCenter,_homeUp,_autoComputeHomePosition);
|
2004-07-13 03:54:54 +08:00
|
|
|
_current->setNode(0);
|
2004-05-06 19:01:16 +08:00
|
|
|
_current->setCoordinateFrameCallback(getCoordinateFrameCallback());
|
|
|
|
_current->setByMatrix(getMatrix());
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-20 04:18:37 +08:00
|
|
|
void KeySwitchMatrixManipulator::addNumberedMatrixManipulator(MatrixManipulator *cm)
|
2002-07-17 18:00:50 +08:00
|
|
|
{
|
|
|
|
if(!cm) return;
|
2003-05-20 04:18:37 +08:00
|
|
|
addMatrixManipulator('1'+_manips.size(),cm->className(),cm);
|
2002-07-17 18:00:50 +08:00
|
|
|
}
|
|
|
|
|
2003-05-20 04:18:37 +08:00
|
|
|
void KeySwitchMatrixManipulator::selectMatrixManipulator(unsigned int num)
|
2003-01-22 23:30:17 +08:00
|
|
|
{
|
|
|
|
unsigned int manipNo = 0;
|
|
|
|
KeyManipMap::iterator itr;
|
|
|
|
for(itr=_manips.begin();
|
|
|
|
manipNo!=num && itr!=_manips.end();
|
|
|
|
++itr,++manipNo)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itr!=_manips.end())
|
|
|
|
{
|
2004-09-01 18:18:46 +08:00
|
|
|
itr->second.second->setHomePosition(_homeEye,_homeCenter,_homeUp,_autoComputeHomePosition);
|
2004-07-13 03:54:54 +08:00
|
|
|
|
2003-01-22 23:30:17 +08:00
|
|
|
if (_current.valid())
|
|
|
|
{
|
2004-05-06 19:01:16 +08:00
|
|
|
if ( !itr->second.second->getCoordinateFrameCallback() )
|
|
|
|
{
|
|
|
|
itr->second.second->setCoordinateFrameCallback(_current->getCoordinateFrameCallback());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !itr->second.second->getNode() )
|
|
|
|
{
|
2003-04-08 21:10:47 +08:00
|
|
|
itr->second.second->setNode(_current->getNode());
|
|
|
|
}
|
2003-05-20 04:18:37 +08:00
|
|
|
itr->second.second->setByMatrix(_current->getMatrix());
|
2003-01-22 23:30:17 +08:00
|
|
|
}
|
|
|
|
_current = itr->second.second;
|
|
|
|
}
|
|
|
|
}
|
2002-07-17 18:00:50 +08:00
|
|
|
|
2010-01-05 19:09:18 +08:00
|
|
|
/** Set the distance parameter (used by TrackballManipulator etc.) */
|
|
|
|
void KeySwitchMatrixManipulator::setDistance(double distance)
|
|
|
|
{
|
|
|
|
for(KeyManipMap::iterator itr=_manips.begin();
|
|
|
|
itr!=_manips.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
itr->second.second->setDistance(distance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double KeySwitchMatrixManipulator::getDistance() const
|
|
|
|
{
|
|
|
|
if(!_current)
|
|
|
|
{
|
|
|
|
return _current->getDistance();
|
|
|
|
}
|
|
|
|
else return 1.0;
|
|
|
|
}
|
|
|
|
|
2003-05-20 04:18:37 +08:00
|
|
|
void KeySwitchMatrixManipulator::setNode(osg::Node* node)
|
2003-04-08 21:10:47 +08:00
|
|
|
{
|
|
|
|
for(KeyManipMap::iterator itr=_manips.begin();
|
|
|
|
itr!=_manips.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
itr->second.second->setNode(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-01 18:18:46 +08:00
|
|
|
void KeySwitchMatrixManipulator::setHomePosition(const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up, bool autoComputeHomePosition)
|
2004-07-13 03:54:54 +08:00
|
|
|
{
|
2004-09-01 18:18:46 +08:00
|
|
|
MatrixManipulator::setHomePosition(eye, center, up, autoComputeHomePosition);
|
2004-07-13 03:54:54 +08:00
|
|
|
for(KeyManipMap::iterator itr=_manips.begin();
|
|
|
|
itr!=_manips.end();
|
|
|
|
++itr)
|
|
|
|
{
|
2004-09-01 18:18:46 +08:00
|
|
|
itr->second.second->setHomePosition(eye, center, up, autoComputeHomePosition);
|
2004-07-13 03:54:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeySwitchMatrixManipulator::setAutoComputeHomePosition(bool flag)
|
|
|
|
{
|
|
|
|
_autoComputeHomePosition = flag;
|
|
|
|
for(KeyManipMap::iterator itr=_manips.begin();
|
|
|
|
itr!=_manips.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
itr->second.second->setAutoComputeHomePosition(flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-09 23:21:33 +08:00
|
|
|
void KeySwitchMatrixManipulator::setMinimumDistance(float minimumDistance)
|
|
|
|
{
|
|
|
|
_minimumDistance = minimumDistance;
|
|
|
|
for(KeyManipMap::iterator itr=_manips.begin();
|
|
|
|
itr!=_manips.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
itr->second.second->setMinimumDistance(minimumDistance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-13 03:54:54 +08:00
|
|
|
void KeySwitchMatrixManipulator::computeHomePosition()
|
|
|
|
{
|
|
|
|
for(KeyManipMap::iterator itr=_manips.begin();
|
|
|
|
itr!=_manips.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
itr->second.second->computeHomePosition();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-06 19:01:16 +08:00
|
|
|
void KeySwitchMatrixManipulator::setCoordinateFrameCallback(CoordinateFrameCallback* cb)
|
|
|
|
{
|
|
|
|
_coordinateFrameCallback = cb;
|
|
|
|
for(KeyManipMap::iterator itr=_manips.begin();
|
|
|
|
itr!=_manips.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
itr->second.second->setCoordinateFrameCallback(cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:36 +08:00
|
|
|
MatrixManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithIndex(unsigned int index)
|
2003-03-20 05:14:50 +08:00
|
|
|
{
|
2005-11-14 19:00:36 +08:00
|
|
|
unsigned i=0;
|
|
|
|
for(KeyManipMap::iterator itr = _manips.begin();
|
|
|
|
itr != _manips.end();
|
|
|
|
++itr, ++i)
|
|
|
|
{
|
|
|
|
if (i==index) return itr->second.second.get();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MatrixManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithIndex(unsigned int index) const
|
|
|
|
{
|
|
|
|
unsigned i=0;
|
|
|
|
for(KeyManipMap::const_iterator itr = _manips.begin();
|
|
|
|
itr != _manips.end();
|
|
|
|
++itr, ++i)
|
|
|
|
{
|
|
|
|
if (i==index) return itr->second.second.get();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MatrixManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithKey(unsigned int key)
|
|
|
|
{
|
|
|
|
KeyManipMap::iterator itr = _manips.find(key);
|
2003-03-20 05:14:50 +08:00
|
|
|
if (itr!=_manips.end()) return itr->second.second.get();
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:00:36 +08:00
|
|
|
const MatrixManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithKey(unsigned int key) const
|
2003-03-20 05:14:50 +08:00
|
|
|
{
|
2005-11-14 19:00:36 +08:00
|
|
|
KeyManipMap::const_iterator itr = _manips.find(key);
|
2003-03-20 05:14:50 +08:00
|
|
|
if (itr!=_manips.end()) return itr->second.second.get();
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
2003-05-20 04:18:37 +08:00
|
|
|
bool KeySwitchMatrixManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& aa)
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
2007-01-10 21:52:22 +08:00
|
|
|
if (!_current) return false;
|
|
|
|
|
2007-07-03 21:26:48 +08:00
|
|
|
bool handled = false;
|
|
|
|
|
|
|
|
if (!ea.getHandled() && ea.getEventType()==GUIEventAdapter::KEYDOWN)
|
2007-01-10 21:52:22 +08:00
|
|
|
{
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
KeyManipMap::iterator it=_manips.find(ea.getKey());
|
2007-01-10 21:52:22 +08:00
|
|
|
if(it != _manips.end())
|
|
|
|
{
|
2002-05-10 03:02:35 +08:00
|
|
|
osg::notify(osg::INFO)<<"Switching to manipulator: "<<(*it).second.first<<std::endl;
|
2007-01-10 21:52:22 +08:00
|
|
|
if ( !it->second.second->getNode() )
|
|
|
|
{
|
2003-04-08 21:10:47 +08:00
|
|
|
it->second.second->setNode(_current->getNode());
|
|
|
|
}
|
2003-05-20 04:18:37 +08:00
|
|
|
it->second.second->setByMatrix(_current->getMatrix());
|
2002-05-09 18:31:03 +08:00
|
|
|
it->second.second->init(ea,aa);
|
|
|
|
_current = it->second.second;
|
|
|
|
|
|
|
|
//_cameraManipChangeCallbacks.notify(this);
|
2007-07-03 21:26:48 +08:00
|
|
|
|
|
|
|
handled = true;
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-03 21:26:48 +08:00
|
|
|
return _current->handle(ea,aa) || handled;
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
2003-02-19 18:43:02 +08:00
|
|
|
|
2003-05-20 04:18:37 +08:00
|
|
|
void KeySwitchMatrixManipulator::getUsage(osg::ApplicationUsage& usage) const
|
2003-02-19 18:43:02 +08:00
|
|
|
{
|
|
|
|
for(KeyManipMap::const_iterator itr=_manips.begin();
|
|
|
|
itr!=_manips.end();
|
|
|
|
++itr)
|
|
|
|
{
|
2003-02-20 05:37:12 +08:00
|
|
|
std::string key; key += (char)(itr->first);
|
|
|
|
std::string explanation(std::string("Select '")+itr->second.first+std::string("' camera manipulator"));
|
2003-02-19 18:43:02 +08:00
|
|
|
if (_current==itr->second.second) explanation += " (default)";
|
|
|
|
|
|
|
|
usage.addKeyboardMouseBinding(key,explanation);
|
|
|
|
itr->second.second->getUsage(usage);
|
|
|
|
}
|
|
|
|
}
|