Skip to content

Commit

Permalink
Allow configuration of interpolation method for animations
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jul 9, 2018
1 parent ebea35d commit d70c37c
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/app/3d/qgs3danimationsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ Qgs3DAnimationSettings::Keyframe Qgs3DAnimationSettings::interpolate( float time
// 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::Linear );

float totalTime = duration();
float interpTime = mEasingCurve.valueForProgress( time / totalTime );
float time2 = interpTime * totalTime;

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 )
if ( time2 >= k0.time && time2 <= k1.time )
{
float ip = ( time - k0.time ) / ( k1.time - k0.time );
float eIp = easing.valueForProgress( ip );
float eIp = ( time2 - k0.time ) / ( k1.time - k0.time );
float eIip = 1.0f - eIp;

Keyframe kf;
Expand Down Expand Up @@ -86,6 +88,8 @@ Qgs3DAnimationSettings::Keyframe Qgs3DAnimationSettings::interpolate( float time

void Qgs3DAnimationSettings::readXml( const QDomElement &elem )
{
mEasingCurve = QEasingCurve( ( QEasingCurve::Type ) elem.attribute( "interpolation", "0" ).toInt() );

mKeyframes.clear();

QDomElement elemKeyframes = elem.firstChildElement( "keyframes" );
Expand All @@ -108,6 +112,7 @@ void Qgs3DAnimationSettings::readXml( const QDomElement &elem )
QDomElement Qgs3DAnimationSettings::writeXml( QDomDocument &doc ) const
{
QDomElement elem = doc.createElement( "animation3d" );
elem.setAttribute( "interpolation", mEasingCurve.type() );

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

Expand Down
7 changes: 7 additions & 0 deletions src/app/3d/qgs3danimationsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "qgsvector3d.h"

#include <QEasingCurve>
#include <QVector>

class QDomDocument;
Expand Down Expand Up @@ -50,6 +51,11 @@ class Qgs3DAnimationSettings
//! Returns keyframes of the animation
Keyframes keyFrames() const { return mKeyframes; }

//! Sets the interpolation method for transitions of the camera
void setEasingCurve( const QEasingCurve &curve ) { mEasingCurve = curve; }
//! Returns the interpolation method for transitions of the camera
QEasingCurve easingCurve() const { return mEasingCurve; }

//! Returns duration of the whole animation in seconds
float duration() const;

Expand All @@ -63,6 +69,7 @@ class Qgs3DAnimationSettings

private:
Keyframes mKeyframes;
QEasingCurve mEasingCurve;
};

Q_DECLARE_METATYPE( Qgs3DAnimationSettings::Keyframe )
Expand Down
12 changes: 12 additions & 0 deletions src/app/3d/qgs3danimationwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Qgs3DAnimationWidget::Qgs3DAnimationWidget( QWidget *parent )
connect( btnRemoveKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onRemoveKeyframe );
connect( btnEditKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onEditKeyframe );
connect( btnDuplicateKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onDuplicateKeyframe );
connect( cboInterpolation, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, &Qgs3DAnimationWidget::onInterpolationChanged );

btnPlayPause->setCheckable( true );
connect( btnPlayPause, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onPlayPause );
Expand All @@ -68,6 +69,8 @@ void Qgs3DAnimationWidget::setCameraController( QgsCameraController *cameraContr

void Qgs3DAnimationWidget::setAnimation( const Qgs3DAnimationSettings &animSettings )
{
cboInterpolation->setCurrentIndex( animSettings.easingCurve().type() );

// initialize GUI from the given animation
cboKeyframe->clear();
cboKeyframe->addItem( tr( "<none>" ) );
Expand All @@ -91,6 +94,7 @@ void Qgs3DAnimationWidget::initializeController( const Qgs3DAnimationSettings &a
Qgs3DAnimationSettings Qgs3DAnimationWidget::animation() const
{
Qgs3DAnimationSettings animSettings;
animSettings.setEasingCurve( QEasingCurve( ( QEasingCurve::Type ) cboInterpolation->currentIndex() ) );
Qgs3DAnimationSettings::Keyframes keyframes;
for ( int i = 1; i < cboKeyframe->count(); ++i )
{
Expand Down Expand Up @@ -329,3 +333,11 @@ void Qgs3DAnimationWidget::onDuplicateKeyframe()

cboKeyframe->setCurrentIndex( newIndex + 1 );
}

void Qgs3DAnimationWidget::onInterpolationChanged()
{
initializeController( animation() );

if ( cboKeyframe->currentIndex() <= 0 )
onSliderValueChanged();
}
1 change: 1 addition & 0 deletions src/app/3d/qgs3danimationwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Qgs3DAnimationWidget : public QWidget, private Ui::Animation3DWidget
void onRemoveKeyframe();
void onEditKeyframe();
void onDuplicateKeyframe();
void onInterpolationChanged();

private:
void initializeController( const Qgs3DAnimationSettings &animSettings );
Expand Down
158 changes: 158 additions & 0 deletions src/ui/3d/animation3dwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,162 @@
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Interpolation</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cboInterpolation">
<item>
<property name="text">
<string>Linear</string>
</property>
</item>
<item>
<property name="text">
<string>InQuad</string>
</property>
</item>
<item>
<property name="text">
<string>OutQuad</string>
</property>
</item>
<item>
<property name="text">
<string>InOutQuad</string>
</property>
</item>
<item>
<property name="text">
<string>OutInQuad</string>
</property>
</item>
<item>
<property name="text">
<string>InCubic</string>
</property>
</item>
<item>
<property name="text">
<string>OutCubic</string>
</property>
</item>
<item>
<property name="text">
<string>InOutCubic</string>
</property>
</item>
<item>
<property name="text">
<string>OutInCubic</string>
</property>
</item>
<item>
<property name="text">
<string>InQuart</string>
</property>
</item>
<item>
<property name="text">
<string>OutQuart</string>
</property>
</item>
<item>
<property name="text">
<string>InOutQuart</string>
</property>
</item>
<item>
<property name="text">
<string>OutInQuart</string>
</property>
</item>
<item>
<property name="text">
<string>InQuint</string>
</property>
</item>
<item>
<property name="text">
<string>OutQuint</string>
</property>
</item>
<item>
<property name="text">
<string>InOutQuint</string>
</property>
</item>
<item>
<property name="text">
<string>OutInQuint</string>
</property>
</item>
<item>
<property name="text">
<string>InSine</string>
</property>
</item>
<item>
<property name="text">
<string>OutSine</string>
</property>
</item>
<item>
<property name="text">
<string>InOutSine</string>
</property>
</item>
<item>
<property name="text">
<string>OutInSine</string>
</property>
</item>
<item>
<property name="text">
<string>InExpo</string>
</property>
</item>
<item>
<property name="text">
<string>OutExpo</string>
</property>
</item>
<item>
<property name="text">
<string>InOutExpo</string>
</property>
</item>
<item>
<property name="text">
<string>OutInExpo</string>
</property>
</item>
<item>
<property name="text">
<string>InCirc</string>
</property>
</item>
<item>
<property name="text">
<string>OutCirc</string>
</property>
</item>
<item>
<property name="text">
<string>InOutCirc</string>
</property>
</item>
<item>
<property name="text">
<string>OutInCirc</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
Expand Down Expand Up @@ -126,8 +282,10 @@
<tabstop>btnRemoveKeyframe</tabstop>
<tabstop>btnEditKeyframe</tabstop>
<tabstop>btnDuplicateKeyframe</tabstop>
<tabstop>cboInterpolation</tabstop>
<tabstop>btnPlayPause</tabstop>
<tabstop>sliderTime</tabstop>
<tabstop>btnRepeat</tabstop>
</tabstops>
<resources/>
<connections/>
Expand Down

0 comments on commit d70c37c

Please sign in to comment.