Skip to content

Commit

Permalink
add movement speed combobox
Browse files Browse the repository at this point in the history
  • Loading branch information
NEDJIMAbelgacem authored and nyalldawson committed Jan 13, 2021
1 parent 44d39be commit 0318ff7
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 32 deletions.
21 changes: 21 additions & 0 deletions python/3d/auto_generated/qgs3dmapsettings.sip.in
Expand Up @@ -565,6 +565,20 @@ Returns the navigation mode used by the camera
%Docstring
Sets the navigation mode for the camera

.. versionadded:: 3.18
%End

double cameraMovementSpeed() const;
%Docstring
Returns the camera movement speed

.. versionadded:: 3.18
%End

void setCameraMovementSpeed( double movementSpeed );
%Docstring
Sets the camera movement speed

.. versionadded:: 3.18
%End

Expand Down Expand Up @@ -804,6 +818,13 @@ Emitted when the camera lens projection type changes
%Docstring
Emitted when the camera navigation mode was changed

.. versionadded:: 3.18
%End

void cameraMovementSpeedChanged();
%Docstring
Emitted when the camera movement speed was changed

.. versionadded:: 3.18
%End

Expand Down
7 changes: 7 additions & 0 deletions src/3d/qgs3dmapscene.cpp
Expand Up @@ -143,6 +143,7 @@ Qgs3DMapScene::Qgs3DMapScene( const Qgs3DMapSettings &map, QgsAbstract3DEngine *
connect( &map, &Qgs3DMapSettings::debugDepthMapSettingsChanged, this, &Qgs3DMapScene::onDebugDepthMapSettingsChanged );
connect( &map, &Qgs3DMapSettings::fpsCounterEnabledChanged, this, &Qgs3DMapScene::fpsCounterEnabledChanged );
connect( &map, &Qgs3DMapSettings::cameraNavigationModeChanged, this, &Qgs3DMapScene::onCameraNavigationModeChanged );
connect( &map, &Qgs3DMapSettings::cameraMovementSpeedChanged, this, &Qgs3DMapScene::onCameraMovementSpeedChanged );

connect( QgsApplication::instance()->sourceCache(), &QgsSourceCache::remoteSourceFetched, this, [ = ]( const QString & url )
{
Expand Down Expand Up @@ -233,6 +234,7 @@ Qgs3DMapScene::Qgs3DMapScene( const Qgs3DMapSettings &map, QgsAbstract3DEngine *
onDebugDepthMapSettingsChanged();

onCameraNavigationModeChanged();
onCameraMovementSpeedChanged();
}

void Qgs3DMapScene::viewZoomFull()
Expand Down Expand Up @@ -1063,6 +1065,11 @@ void Qgs3DMapScene::onCameraNavigationModeChanged()
mCameraController->setCameraNavigationMode( mMap.cameraNavigationMode() );
}

void Qgs3DMapScene::onCameraMovementSpeedChanged()
{
mCameraController->setCameraMovementSpeed( mMap.cameraMovementSpeed() );
}

void Qgs3DMapScene::exportScene( const Qgs3DMapExportSettings &exportSettings )
{
QVector<QString> notParsedLayers;
Expand Down
1 change: 1 addition & 0 deletions src/3d/qgs3dmapscene.h
Expand Up @@ -162,6 +162,7 @@ class _3D_EXPORT Qgs3DMapScene : public Qt3DCore::QEntity
void onDebugShadowMapSettingsChanged();
void onDebugDepthMapSettingsChanged();
void onCameraNavigationModeChanged();
void onCameraMovementSpeedChanged();

private:
void addLayerEntity( QgsMapLayer *layer );
Expand Down
26 changes: 22 additions & 4 deletions src/3d/qgs3dmapsettings.cpp
Expand Up @@ -55,6 +55,8 @@ Qgs3DMapSettings::Qgs3DMapSettings( const Qgs3DMapSettings &other )
, mDirectionalLights( other.mDirectionalLights )
, mFieldOfView( other.mFieldOfView )
, mProjectionType( other.mProjectionType )
, mCameraNavigationMode( other.mCameraNavigationMode )
, mCameraMovementSpeed( other.mCameraMovementSpeed )
, mLayers( other.mLayers )
, mTerrainLayers( other.mTerrainLayers )
, mRenderers() // initialized in body
Expand Down Expand Up @@ -105,6 +107,7 @@ void Qgs3DMapSettings::readXml( const QDomElement &elem, const QgsReadWriteConte
mCameraNavigationMode = QgsCameraController::NavigationMode::BasicNavigation;
else if ( cameraNavigationMode == QStringLiteral( "fps-navigation" ) )
mCameraNavigationMode = QgsCameraController::NavigationMode::FPSNavigation;
mCameraMovementSpeed = elemCamera.attribute( QStringLiteral( "camera-movement-speed" ), QStringLiteral( "5.0" ) ).toDouble();
}

QDomElement elemColor = elem.firstChildElement( QStringLiteral( "color" ) );
Expand Down Expand Up @@ -298,10 +301,16 @@ QDomElement Qgs3DMapSettings::writeXml( QDomDocument &doc, const QgsReadWriteCon
QDomElement elemCamera = doc.createElement( QStringLiteral( "camera" ) );
elemCamera.setAttribute( QStringLiteral( "field-of-view" ), mFieldOfView );
elemCamera.setAttribute( QStringLiteral( "projection-type" ), static_cast< int >( mProjectionType ) );
if ( mCameraNavigationMode == QgsCameraController::BasicNavigation )
elemCamera.setAttribute( QStringLiteral( "camera-navigation-mode" ), QStringLiteral( "basic-navigation" ) );
else if ( mCameraNavigationMode == QgsCameraController::FPSNavigation )
elemCamera.setAttribute( QStringLiteral( "camera-navigation-mode" ), QStringLiteral( "fps-navigation" ) );
switch ( mCameraNavigationMode )
{
case QgsCameraController::BasicNavigation:
elemCamera.setAttribute( QStringLiteral( "camera-navigation-mode" ), QStringLiteral( "basic-navigation" ) );
break;
case QgsCameraController::FPSNavigation:
elemCamera.setAttribute( QStringLiteral( "camera-navigation-mode" ), QStringLiteral( "fps-navigation" ) );
break;
}
elemCamera.setAttribute( QStringLiteral( "camera-movement-speed" ), mCameraMovementSpeed );
elem.appendChild( elemCamera );

QDomElement elemColor = doc.createElement( QStringLiteral( "color" ) );
Expand Down Expand Up @@ -775,6 +784,15 @@ void Qgs3DMapSettings::setCameraNavigationMode( QgsCameraController::NavigationM
emit cameraNavigationModeChanged();
}

void Qgs3DMapSettings::setCameraMovementSpeed( double movementSpeed )
{
if ( mCameraMovementSpeed == movementSpeed )
return;

mCameraMovementSpeed = movementSpeed;
emit cameraMovementSpeedChanged();
}

void Qgs3DMapSettings::setSkyboxSettings( const QgsSkyboxSettings &skyboxSettings )
{
mSkyboxSettings = skyboxSettings;
Expand Down
19 changes: 19 additions & 0 deletions src/3d/qgs3dmapsettings.h
Expand Up @@ -489,6 +489,18 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject, public QgsTemporalRangeObjec
*/
void setCameraNavigationMode( QgsCameraController::NavigationMode navigationMode );

/**
* Returns the camera movement speed
* \since QGIS 3.18
*/
double cameraMovementSpeed() const { return mCameraMovementSpeed; }

/**
* Sets the camera movement speed
* \since QGIS 3.18
*/
void setCameraMovementSpeed( double movementSpeed );

/**
* Sets DPI used for conversion between real world units (e.g. mm) and pixels
* \param dpi the number of dot per inch
Expand Down Expand Up @@ -697,6 +709,12 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject, public QgsTemporalRangeObjec
*/
void cameraNavigationModeChanged();

/**
* Emitted when the camera movement speed was changed
* \since QGIS 3.18
*/
void cameraMovementSpeedChanged();

/**
* Emitted when skybox settings are changed
* \since QGIS 3.16
Expand Down Expand Up @@ -746,6 +764,7 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject, public QgsTemporalRangeObjec
float mFieldOfView = 45.0f; //<! Camera lens field of view value
Qt3DRender::QCameraLens::ProjectionType mProjectionType = Qt3DRender::QCameraLens::PerspectiveProjection; //<! Camera lens projection type
QgsCameraController::NavigationMode mCameraNavigationMode = QgsCameraController::NavigationMode::BasicNavigation;
double mCameraMovementSpeed = 5.0;
QList<QgsMapLayerRef> mLayers; //!< Layers to be rendered
QList<QgsMapLayerRef> mTerrainLayers; //!< Terrain layers to be rendered
QList<QgsAbstract3DRenderer *> mRenderers; //!< Extra stuff to render as 3D object
Expand Down
18 changes: 11 additions & 7 deletions src/3d/qgscameracontroller.cpp
Expand Up @@ -65,6 +65,11 @@ void QgsCameraController::setCameraNavigationMode( QgsCameraController::Navigati
mCameraNavigationMode = navigationMode;
}

void QgsCameraController::setCameraMovementSpeed( double movementSpeed )
{
mCameraMovementSpeed = movementSpeed;
}

void QgsCameraController::setTerrainEntity( QgsTerrainEntity *te )
{
mTerrainEntity = te;
Expand Down Expand Up @@ -492,35 +497,34 @@ void QgsCameraController::onKeyPressedFPSNavigation( Qt3DInput::QKeyEvent *event
QVector3D cameraLeft = QVector3D::crossProduct( cameraUp, cameraFront );

QVector3D cameraPosDiff( 0.0f, 0.0f, 0.0f );
float movementSpeed = 5.0f;

switch ( event->key() )
{
case Qt::Key_Left:
case Qt::Key_A:
cameraPosDiff = movementSpeed * cameraLeft;
cameraPosDiff = mCameraMovementSpeed * cameraLeft;
break;
case Qt::Key_Right:
case Qt::Key_D:
cameraPosDiff = - movementSpeed * cameraLeft;
cameraPosDiff = - mCameraMovementSpeed * cameraLeft;
break;

case Qt::Key_Up:
case Qt::Key_W:
cameraPosDiff = movementSpeed * cameraFront;
cameraPosDiff = mCameraMovementSpeed * cameraFront;
break;
case Qt::Key_Down:
case Qt::Key_S:
cameraPosDiff = - movementSpeed * cameraFront;
cameraPosDiff = - mCameraMovementSpeed * cameraFront;
break;

case Qt::Key_PageUp:
case Qt::Key_Q:
cameraPosDiff = movementSpeed * cameraUp;
cameraPosDiff = mCameraMovementSpeed * cameraUp;
break;
case Qt::Key_PageDown:
case Qt::Key_E:
cameraPosDiff = - movementSpeed * cameraUp;
cameraPosDiff = - mCameraMovementSpeed * cameraUp;
break;
}

Expand Down
18 changes: 14 additions & 4 deletions src/3d/qgscameracontroller.h
Expand Up @@ -81,14 +81,26 @@ class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
* Returns the nvigtion mode used by the camera controller
* \since QGIS 3.18
*/
QgsCameraController::NavigationMode cameraNavigationMode() { return mCameraNavigationMode; }
QgsCameraController::NavigationMode cameraNavigationMode() const { return mCameraNavigationMode; }

/**
* Sets the nvigtion mode used by the camera controller
* \since QGIS 3.18
*/
void setCameraNavigationMode( QgsCameraController::NavigationMode navigationMode );

/**
* Returns the camera movement speed
* \since QGIS 3.18
*/
double cameraMovementSpeed() const { return mCameraMovementSpeed; }

/**
* Sets the camera movement speed
* \since QGIS 3.18
*/
void setCameraMovementSpeed( double movementSpeed );

/**
* Connects to object picker attached to terrain entity. Called internally from 3D scene.
* This allows camera controller understand how far from the camera is the terrain under mouse cursor.
Expand Down Expand Up @@ -216,9 +228,7 @@ class _3D_EXPORT QgsCameraController : public Qt3DCore::QEntity
Qt3DInput::QMouseHandler *mMouseHandler = nullptr;
Qt3DInput::QKeyboardHandler *mKeyboardHandler = nullptr;
NavigationMode mCameraNavigationMode = NavigationMode::BasicNavigation;
double mDeltaTime = 0.0f;
QVector<double> mPastDeltaTime;
double mDeltaTimeAverage = 0.0f;
double mCameraMovementSpeed = 5.0;
};

#endif // QGSCAMERACONTROLLER_H
2 changes: 2 additions & 0 deletions src/app/3d/qgs3dmapconfigwidget.cpp
Expand Up @@ -135,6 +135,7 @@ Qgs3DMapConfigWidget::Qgs3DMapConfigWidget( Qgs3DMapSettings *map, QgsMapCanvas
spinCameraFieldOfView->setValue( mMap->fieldOfView() );
cboCameraProjectionType->setCurrentIndex( cboCameraProjectionType->findData( mMap->projectionType() ) );
mCameraNavigationMode->setCurrentIndex( mMap->cameraNavigationMode() );
mCameraMovementSpeed->setValue( mMap->cameraMovementSpeed() );
spinTerrainScale->setValue( mMap->terrainVerticalScale() );
spinMapResolution->setValue( mMap->mapTileResolution() );
spinScreenError->setValue( mMap->maxTerrainScreenError() );
Expand Down Expand Up @@ -319,6 +320,7 @@ void Qgs3DMapConfigWidget::apply()
mMap->setFieldOfView( spinCameraFieldOfView->value() );
mMap->setProjectionType( cboCameraProjectionType->currentData().value< Qt3DRender::QCameraLens::ProjectionType >() );
mMap->setCameraNavigationMode( static_cast<QgsCameraController::NavigationMode>( mCameraNavigationMode->currentIndex() ) );
mMap->setCameraMovementSpeed( mCameraMovementSpeed->value() );
mMap->setTerrainVerticalScale( spinTerrainScale->value() );
mMap->setMapTileResolution( spinMapResolution->value() );
mMap->setMaxTerrainScreenError( spinScreenError->value() );
Expand Down
54 changes: 37 additions & 17 deletions src/ui/3d/map3dconfigwidget.ui
Expand Up @@ -205,8 +205,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>704</width>
<height>604</height>
<width>309</width>
<height>292</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayoutTerrain">
Expand Down Expand Up @@ -410,8 +410,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>704</width>
<height>604</height>
<width>77</width>
<height>45</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayoutLight">
Expand Down Expand Up @@ -607,16 +607,6 @@
<string>Camera</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="0">
<widget class="QLabel" name="labelFieldofView">
<property name="text">
<string>Field of View</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QComboBox" name="cboCameraProjectionType"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelCameraProjectionType">
<property name="text">
Expand All @@ -634,6 +624,23 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="labelFieldofView">
<property name="text">
<string>Field of View</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QComboBox" name="cboCameraProjectionType"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="labelCameraNavigationMode">
<property name="text">
<string>Navigation mode</string>
</property>
</widget>
</item>
<item row="3" column="1" colspan="2">
<widget class="QComboBox" name="mCameraNavigationMode">
<item>
Expand All @@ -648,10 +655,23 @@
</item>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="labelCameraNavigationMode">
<item row="4" column="1" colspan="2">
<widget class="QDoubleSpinBox" name="mCameraMovementSpeed">
<property name="minimum">
<double>0.010000000000000</double>
</property>
<property name="singleStep">
<double>1.000000000000000</double>
</property>
<property name="value">
<double>5.000000000000000</double>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="labelMovementSpeed">
<property name="text">
<string>Navigation mode</string>
<string>Movement speed</string>
</property>
</widget>
</item>
Expand Down

0 comments on commit 0318ff7

Please sign in to comment.