Skip to content

Commit d70c37c

Browse files
committed
Allow configuration of interpolation method for animations
1 parent ebea35d commit d70c37c

File tree

5 files changed

+187
-4
lines changed

5 files changed

+187
-4
lines changed

src/app/3d/qgs3danimationsettings.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@ Qgs3DAnimationSettings::Keyframe Qgs3DAnimationSettings::interpolate( float time
4545
// TODO: make easing curves configurable.
4646
// QEasingCurve is probably not flexible enough, we may need more granular
4747
// control with Bezier curves to allow smooth transition at keyframes
48-
QEasingCurve easing( QEasingCurve::Linear );
48+
49+
float totalTime = duration();
50+
float interpTime = mEasingCurve.valueForProgress( time / totalTime );
51+
float time2 = interpTime * totalTime;
4952

5053
for ( int i = 0; i < mKeyframes.size() - 1; i++ )
5154
{
5255
const Keyframe &k0 = mKeyframes.at( i );
5356
const Keyframe &k1 = mKeyframes.at( i + 1 );
54-
if ( time >= k0.time && time < k1.time )
57+
if ( time2 >= k0.time && time2 <= k1.time )
5558
{
56-
float ip = ( time - k0.time ) / ( k1.time - k0.time );
57-
float eIp = easing.valueForProgress( ip );
59+
float eIp = ( time2 - k0.time ) / ( k1.time - k0.time );
5860
float eIip = 1.0f - eIp;
5961

6062
Keyframe kf;
@@ -86,6 +88,8 @@ Qgs3DAnimationSettings::Keyframe Qgs3DAnimationSettings::interpolate( float time
8688

8789
void Qgs3DAnimationSettings::readXml( const QDomElement &elem )
8890
{
91+
mEasingCurve = QEasingCurve( ( QEasingCurve::Type ) elem.attribute( "interpolation", "0" ).toInt() );
92+
8993
mKeyframes.clear();
9094

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

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

src/app/3d/qgs3danimationsettings.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "qgsvector3d.h"
2020

21+
#include <QEasingCurve>
2122
#include <QVector>
2223

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

54+
//! Sets the interpolation method for transitions of the camera
55+
void setEasingCurve( const QEasingCurve &curve ) { mEasingCurve = curve; }
56+
//! Returns the interpolation method for transitions of the camera
57+
QEasingCurve easingCurve() const { return mEasingCurve; }
58+
5359
//! Returns duration of the whole animation in seconds
5460
float duration() const;
5561

@@ -63,6 +69,7 @@ class Qgs3DAnimationSettings
6369

6470
private:
6571
Keyframes mKeyframes;
72+
QEasingCurve mEasingCurve;
6673
};
6774

6875
Q_DECLARE_METATYPE( Qgs3DAnimationSettings::Keyframe )

src/app/3d/qgs3danimationwidget.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Qgs3DAnimationWidget::Qgs3DAnimationWidget( QWidget *parent )
4545
connect( btnRemoveKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onRemoveKeyframe );
4646
connect( btnEditKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onEditKeyframe );
4747
connect( btnDuplicateKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onDuplicateKeyframe );
48+
connect( cboInterpolation, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, &Qgs3DAnimationWidget::onInterpolationChanged );
4849

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

6970
void Qgs3DAnimationWidget::setAnimation( const Qgs3DAnimationSettings &animSettings )
7071
{
72+
cboInterpolation->setCurrentIndex( animSettings.easingCurve().type() );
73+
7174
// initialize GUI from the given animation
7275
cboKeyframe->clear();
7376
cboKeyframe->addItem( tr( "<none>" ) );
@@ -91,6 +94,7 @@ void Qgs3DAnimationWidget::initializeController( const Qgs3DAnimationSettings &a
9194
Qgs3DAnimationSettings Qgs3DAnimationWidget::animation() const
9295
{
9396
Qgs3DAnimationSettings animSettings;
97+
animSettings.setEasingCurve( QEasingCurve( ( QEasingCurve::Type ) cboInterpolation->currentIndex() ) );
9498
Qgs3DAnimationSettings::Keyframes keyframes;
9599
for ( int i = 1; i < cboKeyframe->count(); ++i )
96100
{
@@ -329,3 +333,11 @@ void Qgs3DAnimationWidget::onDuplicateKeyframe()
329333

330334
cboKeyframe->setCurrentIndex( newIndex + 1 );
331335
}
336+
337+
void Qgs3DAnimationWidget::onInterpolationChanged()
338+
{
339+
initializeController( animation() );
340+
341+
if ( cboKeyframe->currentIndex() <= 0 )
342+
onSliderValueChanged();
343+
}

src/app/3d/qgs3danimationwidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class Qgs3DAnimationWidget : public QWidget, private Ui::Animation3DWidget
5151
void onRemoveKeyframe();
5252
void onEditKeyframe();
5353
void onDuplicateKeyframe();
54+
void onInterpolationChanged();
5455

5556
private:
5657
void initializeController( const Qgs3DAnimationSettings &animSettings );

src/ui/3d/animation3dwidget.ui

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,162 @@
7979
</property>
8080
</spacer>
8181
</item>
82+
<item>
83+
<widget class="QLabel" name="label_2">
84+
<property name="text">
85+
<string>Interpolation</string>
86+
</property>
87+
</widget>
88+
</item>
89+
<item>
90+
<widget class="QComboBox" name="cboInterpolation">
91+
<item>
92+
<property name="text">
93+
<string>Linear</string>
94+
</property>
95+
</item>
96+
<item>
97+
<property name="text">
98+
<string>InQuad</string>
99+
</property>
100+
</item>
101+
<item>
102+
<property name="text">
103+
<string>OutQuad</string>
104+
</property>
105+
</item>
106+
<item>
107+
<property name="text">
108+
<string>InOutQuad</string>
109+
</property>
110+
</item>
111+
<item>
112+
<property name="text">
113+
<string>OutInQuad</string>
114+
</property>
115+
</item>
116+
<item>
117+
<property name="text">
118+
<string>InCubic</string>
119+
</property>
120+
</item>
121+
<item>
122+
<property name="text">
123+
<string>OutCubic</string>
124+
</property>
125+
</item>
126+
<item>
127+
<property name="text">
128+
<string>InOutCubic</string>
129+
</property>
130+
</item>
131+
<item>
132+
<property name="text">
133+
<string>OutInCubic</string>
134+
</property>
135+
</item>
136+
<item>
137+
<property name="text">
138+
<string>InQuart</string>
139+
</property>
140+
</item>
141+
<item>
142+
<property name="text">
143+
<string>OutQuart</string>
144+
</property>
145+
</item>
146+
<item>
147+
<property name="text">
148+
<string>InOutQuart</string>
149+
</property>
150+
</item>
151+
<item>
152+
<property name="text">
153+
<string>OutInQuart</string>
154+
</property>
155+
</item>
156+
<item>
157+
<property name="text">
158+
<string>InQuint</string>
159+
</property>
160+
</item>
161+
<item>
162+
<property name="text">
163+
<string>OutQuint</string>
164+
</property>
165+
</item>
166+
<item>
167+
<property name="text">
168+
<string>InOutQuint</string>
169+
</property>
170+
</item>
171+
<item>
172+
<property name="text">
173+
<string>OutInQuint</string>
174+
</property>
175+
</item>
176+
<item>
177+
<property name="text">
178+
<string>InSine</string>
179+
</property>
180+
</item>
181+
<item>
182+
<property name="text">
183+
<string>OutSine</string>
184+
</property>
185+
</item>
186+
<item>
187+
<property name="text">
188+
<string>InOutSine</string>
189+
</property>
190+
</item>
191+
<item>
192+
<property name="text">
193+
<string>OutInSine</string>
194+
</property>
195+
</item>
196+
<item>
197+
<property name="text">
198+
<string>InExpo</string>
199+
</property>
200+
</item>
201+
<item>
202+
<property name="text">
203+
<string>OutExpo</string>
204+
</property>
205+
</item>
206+
<item>
207+
<property name="text">
208+
<string>InOutExpo</string>
209+
</property>
210+
</item>
211+
<item>
212+
<property name="text">
213+
<string>OutInExpo</string>
214+
</property>
215+
</item>
216+
<item>
217+
<property name="text">
218+
<string>InCirc</string>
219+
</property>
220+
</item>
221+
<item>
222+
<property name="text">
223+
<string>OutCirc</string>
224+
</property>
225+
</item>
226+
<item>
227+
<property name="text">
228+
<string>InOutCirc</string>
229+
</property>
230+
</item>
231+
<item>
232+
<property name="text">
233+
<string>OutInCirc</string>
234+
</property>
235+
</item>
236+
</widget>
237+
</item>
82238
</layout>
83239
</item>
84240
<item>
@@ -126,8 +282,10 @@
126282
<tabstop>btnRemoveKeyframe</tabstop>
127283
<tabstop>btnEditKeyframe</tabstop>
128284
<tabstop>btnDuplicateKeyframe</tabstop>
285+
<tabstop>cboInterpolation</tabstop>
129286
<tabstop>btnPlayPause</tabstop>
130287
<tabstop>sliderTime</tabstop>
288+
<tabstop>btnRepeat</tabstop>
131289
</tabstops>
132290
<resources/>
133291
<connections/>

0 commit comments

Comments
 (0)