31#define DEFAULT_MOVESPEED_MIN 1.0
38 {
"Terrain", MODE_TERRAIN}
47GUIOSGManipulator::GUIOSGManipulator(GUIOSGView* parent, ManipulatorMode initMode,
bool verticalFixed,
double ) :
48 myParent(parent), myCurrentMode(initMode), myMoveSpeed(DEFAULT_MOVESPEED_MIN) {
50 setVerticalAxisFixed(verticalFixed);
54GUIOSGManipulator::~GUIOSGManipulator() {
59GUIOSGManipulator::performMovementLeftMouseButton(
const double eventTimeDelta,
const double dx,
const double dy) {
60 if (myCurrentMode == MODE_TERRAIN) {
61 return osgGA::TerrainManipulator::performMovementMiddleMouseButton(eventTimeDelta, dx, dy);
68GUIOSGManipulator::performMovementMiddleMouseButton(
const double eventTimeDelta,
const double dx,
const double dy) {
69 if (myCurrentMode == MODE_TERRAIN) {
70 return osgGA::TerrainManipulator::performMovementLeftMouseButton(eventTimeDelta, dx, dy);
77GUIOSGManipulator::performMovementRightMouseButton(
const double eventTimeDelta,
const double dx,
const double dy) {
78 if (myCurrentMode == MODE_TERRAIN) {
79 return osgGA::TerrainManipulator::performMovementRightMouseButton(eventTimeDelta, dx, -dy);
86GUIOSGManipulator::handleMouseMove(
const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
87 if (myCurrentMode == MODE_EGO || myCurrentMode == MODE_WALK) {
88 return handleMouseDeltaMovement(ea, aa);
95GUIOSGManipulator::handleMouseDrag(
const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
96 if (myCurrentMode == MODE_TERRAIN) {
97 return osgGA::TerrainManipulator::handleMouseDrag(ea, aa);
99 return handleMouseDeltaMovement(ea, aa);
104GUIOSGManipulator::handleMousePush(
const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
105 if (myCurrentMode == MODE_TERRAIN) {
106 return osgGA::TerrainManipulator::handleMousePush(ea, aa);
108 return handleMouseDeltaMovement(ea, aa);
113GUIOSGManipulator::handleMouseRelease(
const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
114 if (myCurrentMode == MODE_TERRAIN) {
115 return osgGA::TerrainManipulator::handleMouseRelease(ea, aa);
122GUIOSGManipulator::handleMouseDeltaMovement(
const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
124 if (_ga_t0.get() == NULL || _ga_t1.get() == NULL) {
127 float dt =
static_cast<float>(_ga_t0->getTime() - _ga_t1->getTime());
131 float intensity = 50.;
132 float dx = _ga_t0->getXnormalized() * intensity * dt;
133 float dy = _ga_t0->getYnormalized() * intensity * dt;
134 if (dx == 0. && dy == 0.) {
137 centerMousePointer(ea, aa);
139 if (performMouseDeltaMovement(dx, dy)) {
147GUIOSGManipulator::performMouseDeltaMovement(
const float dx,
const float dy) {
148 rotateYawPitch(_rotation, dx, dy, osg::Z_AXIS);
153void GUIOSGManipulator::centerMousePointer(
const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) {
154 _mouseCenterX = (ea.getXmin() + ea.getXmax()) / 2.0f;
155 _mouseCenterY = (ea.getYmin() + ea.getYmax()) / 2.0f;
156 aa.requestWarpPointer(_mouseCenterX, _mouseCenterY);
161GUIOSGManipulator::rotateYawPitch(osg::Quat& rotation,
const double yaw,
const double pitch,
const osg::Vec3d& localUp) {
162 bool verticalAxisFixed = (localUp != osg::Vec3d(0., 0., 0.));
165 if (verticalAxisFixed) {
166 fixVerticalAxis(rotation, localUp,
true);
170 osg::Quat rotateYaw(-yaw, verticalAxisFixed ? localUp : rotation * osg::Vec3d(0., 1., 0.));
171 osg::Quat rotatePitch;
172 osg::Quat newRotation;
173 osg::Vec3d cameraRight(rotation * osg::Vec3d(1., 0., 0.));
175 double my_dy = pitch;
177 osg::Vec3f eye = _center - _rotation * osg::Vec3d(0., 0., -_distance);
180 rotatePitch.makeRotate(my_dy, cameraRight);
181 newRotation = rotation * rotateYaw * rotatePitch;
184 if (verticalAxisFixed) {
185 fixVerticalAxis(newRotation, localUp,
false);
189 osg::Vec3d newCameraUp = newRotation * osg::Vec3d(0., 1., 0.);
190 if (newCameraUp * localUp > 0.) {
192 setByMatrix(osg::Matrixd::rotate(newRotation) * osg::Matrixd::translate(eye));
197 setByMatrix(osg::Matrixd::rotate(rotation) * osg::Matrixd::rotate(rotateYaw) * osg::Matrixd::translate(eye));
205GUIOSGManipulator::handleKeyDown(
const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& ) {
207 switch (ea.getKey()) {
208 case osgGA::GUIEventAdapter::KEY_Up:
209 myMove.z() -= myMoveSpeed;
212 case osgGA::GUIEventAdapter::KEY_Down:
213 myMove.z() += myMoveSpeed;
216 case osgGA::GUIEventAdapter::KEY_Right:
217 myMove.x() += myMoveSpeed;
220 case osgGA::GUIEventAdapter::KEY_Left:
221 myMove.x() -= myMoveSpeed;
225 _center += getMatrix().getRotate() * myMove;
231GUIOSGManipulator::handleKeyUp(
const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& ) {
233 switch (ea.getKey()) {
234 case osgGA::GUIEventAdapter::KEY_Up:
235 case osgGA::GUIEventAdapter::KEY_Down:
236 case osgGA::GUIEventAdapter::KEY_Right:
237 case osgGA::GUIEventAdapter::KEY_Left:
240 case osgGA::GUIEventAdapter::KEY_F:
241 if (myCurrentMode == MODE_EGO) {
242 myCurrentMode = MODE_TERRAIN;
243 }
else if (myCurrentMode == MODE_WALK) {
244 myCurrentMode = MODE_TERRAIN;
246 myCurrentMode = MODE_EGO;
256GUIOSGManipulator::updateHUDText() {
257 myParent->updateHUDText(
TLF(
"Currently in % camera mode. Press [F] to switch.", ModeText.getString(myCurrentMode)));
262GUIOSGManipulator::setByMatrix(
const osg::Matrixd& matrix) {
263 _center = osg::Vec3d(0., 0., -_distance) * matrix;
264 _rotation = matrix.getRotate();
267 if (getVerticalAxisFixed()) {
268 fixVerticalAxis(_center, _rotation,
true);
273osg::Matrixd GUIOSGManipulator::getMatrix()
const {
274 if (myCurrentMode == MODE_TERRAIN) {
275 return osg::Matrixd::translate(0., 0., _distance) *
276 osg::Matrixd::rotate(_rotation) *
277 osg::Matrixd::translate(_center);
279 osg::Vec3f eye = _center - _rotation * osg::Vec3d(0., 0., -_distance);
280 return osg::Matrixd::rotate(_rotation) * osg::Matrixd::translate(eye);
285osg::Matrixd GUIOSGManipulator::getInverseMatrix()
const {
286 if (myCurrentMode == MODE_TERRAIN) {
287 return osg::Matrixd::translate(-_center) *
288 osg::Matrixd::rotate(_rotation.inverse()) *
289 osg::Matrixd::translate(0.0, 0.0, -_distance);
291 osg::Vec3f eye = _center - _rotation * osg::Vec3d(0., 0., -_distance);
292 return osg::Matrixd::translate(-eye) * osg::Matrixd::rotate(_rotation.inverse());