Skip to content

Commit

Permalink
fix drag zoom outside viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
uclaros authored and wonder-sk committed Jan 31, 2023
1 parent 8162042 commit 90bfb51
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/3d/qgscameracontroller.cpp
Expand Up @@ -19,6 +19,8 @@
#include "qgsvector3d.h"
#include "qgssettings.h"
#include "qgs3dutils.h"
#include "qgswindow3dengine.h"
#include "qgs3dmapscene.h"

#include "qgis.h"

Expand Down Expand Up @@ -68,6 +70,10 @@ QgsCameraController::QgsCameraController( Qt3DCore::QNode *parent )
mFpsNavTimer->setInterval( 10 );
connect( mFpsNavTimer, &QTimer::timeout, this, &QgsCameraController::applyFlyModeKeyMovements );
mFpsNavTimer->start();

Qgs3DMapScene *scene = qobject_cast< Qgs3DMapScene * >( parent );
if ( scene )
mWindowEngine = qobject_cast<QgsWindow3DEngine *>( scene->engine() );
}

void QgsCameraController::setCameraNavigationMode( QgsCameraController::NavigationMode navigationMode )
Expand Down Expand Up @@ -485,17 +491,19 @@ void QgsCameraController::onPositionChangedTerrainNavigation( Qt3DInput::QMouseE

float dist = ( mCameraBeforeDrag->position() - mDragPoint ).length();

const int yOffset = mWindowEngine->window()->mapToGlobal( QPoint( 0, 0 ) ).y();
const int screenHeight = mWindowEngine->window()->screen()->size().height();
// Applies smoothing
if ( mMousePos.y() > mDragButtonClickPos.y() ) // zoom in
{
double f = ( double )( mMousePos.y() - mDragButtonClickPos.y() ) / ( double )( mViewport.height() - mDragButtonClickPos.y() );
double f = ( double )( mMousePos.y() - mDragButtonClickPos.y() ) / ( double )( screenHeight - mDragButtonClickPos.y() - yOffset );
f = std::max( 0.0, std::min( 1.0, f ) );
f = 1 - ( std::expm1( -2 * f ) ) / ( std::expm1( -2 ) );
dist = dist * f;
}
else // zoom out
{
double f = 1 - ( double )( mMousePos.y() ) / ( double )( mDragButtonClickPos.y() );
double f = 1 - ( double )( mMousePos.y() + yOffset ) / ( double )( mDragButtonClickPos.y() + yOffset );
f = std::max( 0.0, std::min( 1.0, f ) );
f = ( std::expm1( 2 * f ) ) / ( std::expm1( 2 ) );
dist = dist + 2 * dist * f;
Expand Down
3 changes: 3 additions & 0 deletions src/3d/qgscameracontroller.h
Expand Up @@ -49,6 +49,7 @@ class QDomElement;
class QgsCameraPose;
class QgsTerrainEntity;
class QgsVector3D;
class QgsWindow3DEngine;

#define SIP_NO_FILE

Expand Down Expand Up @@ -287,8 +288,10 @@ class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
private:
//! Camera that is being controlled
Qt3DRender::QCamera *mCamera = nullptr;

//! used for computation of translation when dragging mouse
QRect mViewport;
QgsWindow3DEngine *mWindowEngine = nullptr;
//! height of terrain when mouse button was last pressed - for camera control
float mLastPressedHeight = 0;

Expand Down

0 comments on commit 90bfb51

Please sign in to comment.