Skip to content

Commit 031e15e

Browse files
committedJul 8, 2018
Read/write animation associated with a 3d map view
1 parent 3c855b2 commit 031e15e

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed
 

‎src/app/3d/qgs3danimationsettings.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "qgs3danimationsettings.h"
1717

1818
#include <QEasingCurve>
19+
#include <QDomDocument>
1920

2021
Qgs3DAnimationSettings::Qgs3DAnimationSettings()
2122
{
@@ -40,14 +41,19 @@ Qgs3DAnimationSettings::Keyframe Qgs3DAnimationSettings::interpolate( float time
4041
}
4142
else
4243
{
44+
// TODO: make easing curves configurable.
45+
// QEasingCurve is probably not flexible enough, we may need more granular
46+
// control with Bezier curves to allow smooth transition at keyframes
47+
QEasingCurve easing( QEasingCurve::InOutQuad );
48+
4349
for ( int i = 0; i < mKeyframes.size() - 1; i++ )
4450
{
4551
const Keyframe &k0 = mKeyframes.at( i );
4652
const Keyframe &k1 = mKeyframes.at( i + 1 );
4753
if ( time >= k0.time && time < k1.time )
4854
{
4955
float ip = ( time - k0.time ) / ( k1.time - k0.time );
50-
float eIp = QEasingCurve( QEasingCurve::InOutQuad ).valueForProgress( ip );
56+
float eIp = easing.valueForProgress( ip );
5157
float eIip = 1.0f - eIp;
5258

5359
Keyframe kf;
@@ -76,3 +82,48 @@ Qgs3DAnimationSettings::Keyframe Qgs3DAnimationSettings::interpolate( float time
7682
Q_ASSERT( false );
7783
return Keyframe();
7884
}
85+
86+
void Qgs3DAnimationSettings::readXml( const QDomElement &elem )
87+
{
88+
mKeyframes.clear();
89+
90+
QDomElement elemKeyframes = elem.firstChildElement( "keyframes" );
91+
QDomElement elemKeyframe = elemKeyframes.firstChildElement( "keyframe" );
92+
while ( !elemKeyframe.isNull() )
93+
{
94+
Keyframe kf;
95+
kf.time = elemKeyframe.attribute( "time" ).toFloat();
96+
kf.point.set( elemKeyframe.attribute( "x" ).toDouble(),
97+
elemKeyframe.attribute( "y" ).toDouble(),
98+
elemKeyframe.attribute( "z" ).toDouble() );
99+
kf.dist = elemKeyframe.attribute( "dist" ).toFloat();
100+
kf.pitch = elemKeyframe.attribute( "pitch" ).toFloat();
101+
kf.yaw = elemKeyframe.attribute( "yaw" ).toFloat();
102+
mKeyframes.append( kf );
103+
elemKeyframe = elemKeyframe.nextSiblingElement( "keyframe" );
104+
}
105+
}
106+
107+
QDomElement Qgs3DAnimationSettings::writeXml( QDomDocument &doc ) const
108+
{
109+
QDomElement elem = doc.createElement( "animation3d" );
110+
111+
QDomElement elemKeyframes = doc.createElement( "keyframes" );
112+
113+
for ( const Keyframe &keyframe : mKeyframes )
114+
{
115+
QDomElement elemKeyframe = doc.createElement( "keyframe" );
116+
elemKeyframe.setAttribute( "time", keyframe.time );
117+
elemKeyframe.setAttribute( "x", keyframe.point.x() );
118+
elemKeyframe.setAttribute( "y", keyframe.point.y() );
119+
elemKeyframe.setAttribute( "z", keyframe.point.z() );
120+
elemKeyframe.setAttribute( "dist", keyframe.dist );
121+
elemKeyframe.setAttribute( "pitch", keyframe.pitch );
122+
elemKeyframe.setAttribute( "yaw", keyframe.yaw );
123+
elemKeyframes.appendChild( elemKeyframe );
124+
}
125+
126+
elem.appendChild( elemKeyframes );
127+
128+
return elem;
129+
}

‎src/app/3d/qgs3danimationsettings.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,13 @@
1616
#ifndef QGS3DANIMATIONSETTINGS_H
1717
#define QGS3DANIMATIONSETTINGS_H
1818

19-
#include <QVector3D>
20-
#include <QQuaternion>
2119
#include "qgsvector3d.h"
2220

23-
namespace Qt3DCore
24-
{
25-
class QNode;
26-
}
21+
#include <QVector>
2722

28-
namespace Qt3DAnimation
29-
{
30-
class QKeyframeAnimation;
31-
}
23+
class QDomDocument;
24+
class QDomElement;
25+
class QgsReadWriteContext;
3226

3327
/**
3428
* Class that holds information about animation in 3D view. The animation is defined
@@ -61,7 +55,10 @@ class Qgs3DAnimationSettings
6155
//! Interpolates camera position and rotation at the given point in time
6256
Keyframe interpolate( float time ) const;
6357

64-
// TODO: read/write routines
58+
//! Reads configuration from a DOM element previously written by writeXml()
59+
void readXml( const QDomElement &elem );
60+
//! Writes configuration to a DOM element, to be used later with readXml()
61+
QDomElement writeXml( QDomDocument &doc ) const;
6562

6663
private:
6764
Keyframes mKeyframes;

‎src/app/3d/qgs3dmapcanvasdockwidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Qgs3DMapCanvasDockWidget : public QgsDockWidget
3939
void setMainCanvas( QgsMapCanvas *canvas );
4040

4141
Qgs3DMapCanvas *mapCanvas3D() { return mCanvas; }
42+
Qgs3DAnimationWidget *animationWidget() { return mAnimationWidget; }
4243

4344
private slots:
4445
void resetView();

‎src/app/qgisapp.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282

8383
#ifdef HAVE_3D
8484
#include "qgsabstract3drenderer.h"
85+
#include "qgs3danimationsettings.h"
86+
#include "qgs3danimationwidget.h"
8587
#include "qgs3dmapcanvasdockwidget.h"
8688
#include "qgs3drendererregistry.h"
8789
#include "qgs3dmapcanvas.h"
@@ -12865,6 +12867,8 @@ void QgisApp::writeProject( QDomDocument &doc )
1286512867
elem3DMap.appendChild( elem3DMapSettings );
1286612868
QDomElement elemCamera = w->mapCanvas3D()->cameraController()->writeXml( doc );
1286712869
elem3DMap.appendChild( elemCamera );
12870+
QDomElement elemAnimation = w->animationWidget()->animation().writeXml( doc );
12871+
elem3DMap.appendChild( elemAnimation );
1286812872
writeDockWidgetSettings( w, elem3DMap );
1286912873
elem3DMaps.appendChild( elem3DMap );
1287012874
}
@@ -13022,6 +13026,14 @@ void QgisApp::readProject( const QDomDocument &doc )
1302213026
mapCanvasDock3D->mapCanvas3D()->cameraController()->readXml( elemCamera );
1302313027
}
1302413028

13029+
QDomElement elemAnimation = elem3DMap.firstChildElement( QStringLiteral( "animation3d" ) );
13030+
if ( !elemAnimation.isNull() )
13031+
{
13032+
Qgs3DAnimationSettings animationSettings;
13033+
animationSettings.readXml( elemAnimation );
13034+
mapCanvasDock3D->animationWidget()->setAnimation( animationSettings );
13035+
}
13036+
1302513037
elem3DMap = elem3DMap.nextSiblingElement( QStringLiteral( "view" ) );
1302613038
}
1302713039
}

0 commit comments

Comments
 (0)