Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Read/write animation associated with a 3d map view
  • Loading branch information
wonder-sk committed Jul 8, 2018
1 parent 3c855b2 commit 031e15e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
53 changes: 52 additions & 1 deletion src/app/3d/qgs3danimationsettings.cpp
Expand Up @@ -16,6 +16,7 @@
#include "qgs3danimationsettings.h"

#include <QEasingCurve>
#include <QDomDocument>

Qgs3DAnimationSettings::Qgs3DAnimationSettings()
{
Expand All @@ -40,14 +41,19 @@ Qgs3DAnimationSettings::Keyframe Qgs3DAnimationSettings::interpolate( float time
}
else
{
// TODO: make easing curves configurable.
// QEasingCurve is probably not flexible enough, we may need more granular
// control with Bezier curves to allow smooth transition at keyframes
QEasingCurve easing( QEasingCurve::InOutQuad );

for ( int i = 0; i < mKeyframes.size() - 1; i++ )
{
const Keyframe &k0 = mKeyframes.at( i );
const Keyframe &k1 = mKeyframes.at( i + 1 );
if ( time >= k0.time && time < k1.time )
{
float ip = ( time - k0.time ) / ( k1.time - k0.time );
float eIp = QEasingCurve( QEasingCurve::InOutQuad ).valueForProgress( ip );
float eIp = easing.valueForProgress( ip );
float eIip = 1.0f - eIp;

Keyframe kf;
Expand Down Expand Up @@ -76,3 +82,48 @@ Qgs3DAnimationSettings::Keyframe Qgs3DAnimationSettings::interpolate( float time
Q_ASSERT( false );
return Keyframe();
}

void Qgs3DAnimationSettings::readXml( const QDomElement &elem )
{
mKeyframes.clear();

QDomElement elemKeyframes = elem.firstChildElement( "keyframes" );
QDomElement elemKeyframe = elemKeyframes.firstChildElement( "keyframe" );
while ( !elemKeyframe.isNull() )
{
Keyframe kf;
kf.time = elemKeyframe.attribute( "time" ).toFloat();
kf.point.set( elemKeyframe.attribute( "x" ).toDouble(),
elemKeyframe.attribute( "y" ).toDouble(),
elemKeyframe.attribute( "z" ).toDouble() );
kf.dist = elemKeyframe.attribute( "dist" ).toFloat();
kf.pitch = elemKeyframe.attribute( "pitch" ).toFloat();
kf.yaw = elemKeyframe.attribute( "yaw" ).toFloat();
mKeyframes.append( kf );
elemKeyframe = elemKeyframe.nextSiblingElement( "keyframe" );
}
}

QDomElement Qgs3DAnimationSettings::writeXml( QDomDocument &doc ) const
{
QDomElement elem = doc.createElement( "animation3d" );

QDomElement elemKeyframes = doc.createElement( "keyframes" );

for ( const Keyframe &keyframe : mKeyframes )
{
QDomElement elemKeyframe = doc.createElement( "keyframe" );
elemKeyframe.setAttribute( "time", keyframe.time );
elemKeyframe.setAttribute( "x", keyframe.point.x() );
elemKeyframe.setAttribute( "y", keyframe.point.y() );
elemKeyframe.setAttribute( "z", keyframe.point.z() );
elemKeyframe.setAttribute( "dist", keyframe.dist );
elemKeyframe.setAttribute( "pitch", keyframe.pitch );
elemKeyframe.setAttribute( "yaw", keyframe.yaw );
elemKeyframes.appendChild( elemKeyframe );
}

elem.appendChild( elemKeyframes );

return elem;
}
19 changes: 8 additions & 11 deletions src/app/3d/qgs3danimationsettings.h
Expand Up @@ -16,19 +16,13 @@
#ifndef QGS3DANIMATIONSETTINGS_H
#define QGS3DANIMATIONSETTINGS_H

#include <QVector3D>
#include <QQuaternion>
#include "qgsvector3d.h"

namespace Qt3DCore
{
class QNode;
}
#include <QVector>

namespace Qt3DAnimation
{
class QKeyframeAnimation;
}
class QDomDocument;
class QDomElement;
class QgsReadWriteContext;

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

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

private:
Keyframes mKeyframes;
Expand Down
1 change: 1 addition & 0 deletions src/app/3d/qgs3dmapcanvasdockwidget.h
Expand Up @@ -39,6 +39,7 @@ class Qgs3DMapCanvasDockWidget : public QgsDockWidget
void setMainCanvas( QgsMapCanvas *canvas );

Qgs3DMapCanvas *mapCanvas3D() { return mCanvas; }
Qgs3DAnimationWidget *animationWidget() { return mAnimationWidget; }

private slots:
void resetView();
Expand Down
12 changes: 12 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -82,6 +82,8 @@

#ifdef HAVE_3D
#include "qgsabstract3drenderer.h"
#include "qgs3danimationsettings.h"
#include "qgs3danimationwidget.h"
#include "qgs3dmapcanvasdockwidget.h"
#include "qgs3drendererregistry.h"
#include "qgs3dmapcanvas.h"
Expand Down Expand Up @@ -12865,6 +12867,8 @@ void QgisApp::writeProject( QDomDocument &doc )
elem3DMap.appendChild( elem3DMapSettings );
QDomElement elemCamera = w->mapCanvas3D()->cameraController()->writeXml( doc );
elem3DMap.appendChild( elemCamera );
QDomElement elemAnimation = w->animationWidget()->animation().writeXml( doc );
elem3DMap.appendChild( elemAnimation );
writeDockWidgetSettings( w, elem3DMap );
elem3DMaps.appendChild( elem3DMap );
}
Expand Down Expand Up @@ -13022,6 +13026,14 @@ void QgisApp::readProject( const QDomDocument &doc )
mapCanvasDock3D->mapCanvas3D()->cameraController()->readXml( elemCamera );
}

QDomElement elemAnimation = elem3DMap.firstChildElement( QStringLiteral( "animation3d" ) );
if ( !elemAnimation.isNull() )
{
Qgs3DAnimationSettings animationSettings;
animationSettings.readXml( elemAnimation );
mapCanvasDock3D->animationWidget()->setAnimation( animationSettings );
}

elem3DMap = elem3DMap.nextSiblingElement( QStringLiteral( "view" ) );
}
}
Expand Down

0 comments on commit 031e15e

Please sign in to comment.