Skip to content

Commit

Permalink
Use middle button for rotation/tilt, right button for zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Sep 15, 2017
1 parent 7ad57e6 commit 33f7c17
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
45 changes: 25 additions & 20 deletions src/3d/cameracontroller.cpp
Expand Up @@ -15,6 +15,8 @@ CameraController::CameraController( Qt3DCore::QNode *parent )
, mLeftMouseButtonInput( new Qt3DInput::QActionInput() )
, mMiddleMouseButtonAction( new Qt3DInput::QAction() )
, mMiddleMouseButtonInput( new Qt3DInput::QActionInput() )
, mRightMouseButtonAction( new Qt3DInput::QAction() )
, mRightMouseButtonInput( new Qt3DInput::QActionInput() )
, mShiftAction( new Qt3DInput::QAction() )
, mShiftInput( new Qt3DInput::QActionInput() )
, mWheelAxis( new Qt3DInput::QAxis() )
Expand Down Expand Up @@ -47,6 +49,11 @@ CameraController::CameraController( Qt3DCore::QNode *parent )
mMiddleMouseButtonInput->setSourceDevice( mMouseDevice );
mMiddleMouseButtonAction->addInput( mMiddleMouseButtonInput );

// right mouse button
mRightMouseButtonInput->setButtons( QVector<int>() << Qt::RightButton );
mRightMouseButtonInput->setSourceDevice( mMouseDevice );
mRightMouseButtonAction->addInput( mRightMouseButtonInput );

// Mouse Wheel (Y)
// TODO: zoom with mouse wheel in Qt < 5.8
#if QT_VERSION >= 0x050800
Expand Down Expand Up @@ -86,6 +93,7 @@ CameraController::CameraController( Qt3DCore::QNode *parent )

mLogicalDevice->addAction( mLeftMouseButtonAction );
mLogicalDevice->addAction( mMiddleMouseButtonAction );
mLogicalDevice->addAction( mRightMouseButtonAction );
mLogicalDevice->addAction( mShiftAction );
mLogicalDevice->addAxis( mWheelAxis );
mLogicalDevice->addAxis( mTxAxis );
Expand Down Expand Up @@ -178,7 +186,7 @@ void CameraController::frameTriggered( float dt )

cd.dist -= cd.dist * mWheelAxis->value() * 10 * dt;

if ( mMiddleMouseButtonAction->isActive() )
if ( mRightMouseButtonAction->isActive() )
{
cd.dist -= cd.dist * dy * 0.01;
}
Expand All @@ -197,26 +205,23 @@ void CameraController::frameTriggered( float dt )
cd.y += dy;
}

if ( mLeftMouseButtonAction->isActive() )
if ( ( mLeftMouseButtonAction->isActive() && mShiftAction->isActive() ) || mMiddleMouseButtonAction->isActive() )
{
cd.pitch += dy;
cd.yaw -= dx / 2;
}
else if ( mLeftMouseButtonAction->isActive() && !mShiftAction->isActive() )
{
if ( mShiftAction->isActive() )
{
cd.pitch += dy;
cd.yaw -= dx / 2;
}
else
{
// translation works as if one grabbed a point on the plane and dragged it
// i.e. find out x,z of the previous mouse point, find out x,z of the current mouse point
// and use the difference

float z = mLastPressedHeight;
QPointF p1 = screen_point_to_point_on_plane( QPointF( mMousePos - QPoint( dx, dy ) ), mViewport, mCamera, z );
QPointF p2 = screen_point_to_point_on_plane( QPointF( mMousePos ), mViewport, mCamera, z );

cd.x -= p2.x() - p1.x();
cd.y -= p2.y() - p1.y();
}
// translation works as if one grabbed a point on the plane and dragged it
// i.e. find out x,z of the previous mouse point, find out x,z of the current mouse point
// and use the difference

float z = mLastPressedHeight;
QPointF p1 = screen_point_to_point_on_plane( QPointF( mMousePos - QPoint( dx, dy ) ), mViewport, mCamera, z );
QPointF p2 = screen_point_to_point_on_plane( QPointF( mMousePos ), mViewport, mCamera, z );

cd.x -= p2.x() - p1.x();
cd.y -= p2.y() - p1.y();
}

if ( qIsNaN( cd.x ) || qIsNaN( cd.y ) )
Expand Down
3 changes: 3 additions & 0 deletions src/3d/cameracontroller.h
Expand Up @@ -102,6 +102,9 @@ class _3D_EXPORT CameraController : public Qt3DCore::QEntity
Qt3DInput::QAction *mMiddleMouseButtonAction;
Qt3DInput::QActionInput *mMiddleMouseButtonInput;

Qt3DInput::QAction *mRightMouseButtonAction;
Qt3DInput::QActionInput *mRightMouseButtonInput;

Qt3DInput::QAction *mShiftAction;
Qt3DInput::QActionInput *mShiftInput;

Expand Down

0 comments on commit 33f7c17

Please sign in to comment.