添加鼠标控制场景:
按下鼠标左键左右拖动,场景左右移动;
按下鼠标左键上下拖动,场景上下移动。
//记录坐标
int mLeftX, mLeftY;
bool mLeftDown;
bool TravelManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us) {
switch (ea.getEventType()) {
case osgGA::GUIEventAdapter::KEYDOWN:
if (ea.getKey()=='w'||ea.getKey()==osgGA::GUIEventAdapter::KEY_Up){
mPosition[2] -= 0.5;
} else if (ea.getKey() == 's' || ea.getKey() == osgGA::GUIEventAdapter::KEY_Down) {
mPosition[2] += 0.5;
} else if (ea.getKey() == 'a' || ea.getKey() == osgGA::GUIEventAdapter::KEY_Left) {
mPosition[0] -= 0.5;
} else if (ea.getKey() == 'd' || ea.getKey() == osgGA::GUIEventAdapter::KEY_Right) {
mPosition[0] += 0.5;
}else if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Home){
mPosition[1] -= 0.5;
} else if (ea.getKey() == osgGA::GUIEventAdapter::KEY_End) {
mPosition[1] += 0.5;
}
break;
case osgGA::GUIEventAdapter::PUSH:
if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON){
mLeftDown = true;
mLeftX = ea.getX();
mLeftY = ea.getY();
}
break;
case osgGA::GUIEventAdapter::DRAG:
if (mLeftDown){
int deltaX = ea.getX() - mLeftX;
int deltaY = ea.getY() - mLeftY;
mPosition[0] -= osg::DegreesToRadians(0.01*deltaX);
mPosition[1] -= osg::DegreesToRadians(0.01*deltaY);
}
break;
case osgGA::GUIEventAdapter::RELEASE:
if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) {
mLeftDown = false;
}
break;
default:
break;
}
return false;
}