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;
|
|
|
|
|
2010-05-27 23:54:37 +08:00
|
|
|
void KeySwitchMatrixManipulator::addMatrixManipulator(int key, std::string name, CameraManipulator *cm)
|
2002-05-09 18:31:03 +08:00
|
|
|
{
|
|
|
|
if(!cm) return;
|
|
|
|
|
2010-05-27 23:54:37 +08:00
|
|
|
_manips[key]=std::make_pair(name,osg::ref_ptr<CameraManipulator>(cm));
|
2010-05-25 20:05:13 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-27 23:54:37 +08:00
|
|
|
void KeySwitchMatrixManipulator::addNumberedMatrixManipulator(CameraManipulator *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)
|
|
|
|
{
|
|
|
|
}
|
2010-05-25 20:05:13 +08:00
|
|
|
|
2003-01-22 23:30:17 +08:00
|
|
|
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());
|
|
|
|
}
|
2010-05-25 20:05:13 +08:00
|
|
|
|
2004-05-06 19:01:16 +08:00
|
|
|
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
|
|
|
|
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
|
|
|
{
|
2010-05-27 23:54:37 +08:00
|
|
|
CameraManipulator::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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
void KeySwitchMatrixManipulator::computeHomePosition()
|
2004-07-09 23:21:33 +08:00
|
|
|
{
|
|
|
|
for(KeyManipMap::iterator itr=_manips.begin();
|
|
|
|
itr!=_manips.end();
|
|
|
|
++itr)
|
|
|
|
{
|
2010-05-25 20:05:13 +08:00
|
|
|
itr->second.second->computeHomePosition();
|
2004-07-09 23:21:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-25 20:05:13 +08:00
|
|
|
void KeySwitchMatrixManipulator::home(const GUIEventAdapter& ee,GUIActionAdapter& aa)
|
2004-07-13 03:54:54 +08:00
|
|
|
{
|
2010-05-25 20:05:13 +08:00
|
|
|
// call home for all child manipulators
|
|
|
|
// (this can not be done just for current manipulator,
|
|
|
|
// because it is not possible to transfer some manipulator
|
|
|
|
// settings across manipulators using just MatrixManipulator interface
|
|
|
|
// (one problematic variable is for example OrbitManipulator::distance
|
|
|
|
// that can not be passed by setByMatrix method),
|
|
|
|
// thus we have to call home on all of them)
|
2004-07-13 03:54:54 +08:00
|
|
|
for(KeyManipMap::iterator itr=_manips.begin();
|
|
|
|
itr!=_manips.end();
|
|
|
|
++itr)
|
|
|
|
{
|
2010-05-25 20:05:13 +08:00
|
|
|
itr->second.second->home(ee,aa);
|
2004-07-13 03:54:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-27 23:54:37 +08:00
|
|
|
CameraManipulator* 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)
|
|
|
|
{
|
2010-05-25 20:05:13 +08:00
|
|
|
if (i==index) return itr->second.second.get();
|
2005-11-14 19:00:36 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-27 23:54:37 +08:00
|
|
|
const CameraManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithIndex(unsigned int index) const
|
2005-11-14 19:00:36 +08:00
|
|
|
{
|
|
|
|
unsigned i=0;
|
|
|
|
for(KeyManipMap::const_iterator itr = _manips.begin();
|
|
|
|
itr != _manips.end();
|
|
|
|
++itr, ++i)
|
|
|
|
{
|
2010-05-25 20:05:13 +08:00
|
|
|
if (i==index) return itr->second.second.get();
|
2005-11-14 19:00:36 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-27 23:54:37 +08:00
|
|
|
CameraManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithKey(unsigned int key)
|
2005-11-14 19:00:36 +08:00
|
|
|
{
|
2010-05-25 20:05:13 +08:00
|
|
|
KeyManipMap::iterator itr = _manips.find(key);
|
|
|
|
if (itr!=_manips.end()) return itr->second.second.get();
|
2003-03-20 05:14:50 +08:00
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-27 23:54:37 +08:00
|
|
|
const CameraManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithKey(unsigned int key) const
|
2003-03-20 05:14:50 +08:00
|
|
|
{
|
2010-05-25 20:05:13 +08:00
|
|
|
KeyManipMap::const_iterator itr = _manips.find(key);
|
|
|
|
if (itr!=_manips.end()) return itr->second.second.get();
|
2003-03-20 05:14:50 +08:00
|
|
|
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())
|
|
|
|
{
|
2012-09-07 16:33:24 +08:00
|
|
|
CameraManipulator* selectedManipulator = it->second.second.get();
|
|
|
|
if (selectedManipulator!=_current)
|
|
|
|
{
|
|
|
|
OSG_INFO<<"Switching to manipulator: "<<it->second.first<<std::endl;
|
|
|
|
if ( !selectedManipulator->getNode() )
|
|
|
|
{
|
|
|
|
selectedManipulator->setNode(_current->getNode());
|
|
|
|
}
|
|
|
|
selectedManipulator->setByMatrix(_current->getMatrix());
|
|
|
|
selectedManipulator->init(ea,aa);
|
|
|
|
|
|
|
|
_current = selectedManipulator;
|
2003-04-08 21:10:47 +08:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|