/
qgs3danimationwidget.cpp
355 lines (282 loc) · 11.1 KB
/
qgs3danimationwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/***************************************************************************
qgs3danimationwidget.cpp
--------------------------------------
Date : July 2018
Copyright : (C) 2018 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgs3danimationwidget.h"
#include "qgs3danimationsettings.h"
#include "qgsapplication.h"
#include "qgscameracontroller.h"
#include <QInputDialog>
#include <QMessageBox>
#include <QTimer>
Qgs3DAnimationWidget::Qgs3DAnimationWidget( QWidget *parent )
: QWidget( parent )
{
setupUi( this );
btnAddKeyframe->setIcon( QIcon( QgsApplication::iconPath( "symbologyAdd.svg" ) ) );
btnRemoveKeyframe->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.svg" ) ) );
btnEditKeyframe->setIcon( QIcon( QgsApplication::iconPath( "symbologyEdit.svg" ) ) );
btnPlayPause->setIcon( QIcon( QgsApplication::iconPath( "mTaskRunning.svg" ) ) );
btnDuplicateKeyframe->setIcon( QIcon( QgsApplication::iconPath( "mActionEditCopy.svg" ) ) );
btnRepeat->setIcon( QIcon( QgsApplication::iconPath( "mActionRefresh.svg" ) ) );
cboKeyframe->addItem( tr( "<none>" ) );
mAnimationTimer = new QTimer( this );
mAnimationTimer->setInterval( 10 );
connect( mAnimationTimer, &QTimer::timeout, this, &Qgs3DAnimationWidget::onAnimationTimer );
connect( btnAddKeyframe, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onAddKeyframe );
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 );
btnRepeat->setCheckable( true );
connect( sliderTime, &QSlider::valueChanged, this, &Qgs3DAnimationWidget::onSliderValueChanged );
connect( cboKeyframe, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, &Qgs3DAnimationWidget::onKeyframeChanged );
}
Qgs3DAnimationWidget::~Qgs3DAnimationWidget() = default;
void Qgs3DAnimationWidget::setCameraController( QgsCameraController *cameraController )
{
mCameraController = cameraController;
connect( mCameraController, &QgsCameraController::cameraChanged, this, &Qgs3DAnimationWidget::onCameraChanged );
}
void Qgs3DAnimationWidget::setAnimation( const Qgs3DAnimationSettings &animSettings )
{
cboInterpolation->setCurrentIndex( animSettings.easingCurve().type() );
// initialize GUI from the given animation
cboKeyframe->clear();
cboKeyframe->addItem( tr( "<none>" ) );
for ( const Qgs3DAnimationSettings::Keyframe &keyframe : animSettings.keyFrames() )
{
cboKeyframe->addItem( QString( "%1 s" ).arg( keyframe.time ) );
int lastIndex = cboKeyframe->count() - 1;
cboKeyframe->setItemData( lastIndex, QVariant::fromValue<Qgs3DAnimationSettings::Keyframe>( keyframe ), Qt::UserRole + 1 );
}
initializeController( animSettings );
}
void Qgs3DAnimationWidget::initializeController( const Qgs3DAnimationSettings &animSettings )
{
mAnimationSettings.reset( new Qgs3DAnimationSettings( animSettings ) );
sliderTime->setMaximum( animSettings.duration() * 100 );
}
Qgs3DAnimationSettings Qgs3DAnimationWidget::animation() const
{
Qgs3DAnimationSettings animSettings;
animSettings.setEasingCurve( QEasingCurve( ( QEasingCurve::Type ) cboInterpolation->currentIndex() ) );
Qgs3DAnimationSettings::Keyframes keyframes;
for ( int i = 1; i < cboKeyframe->count(); ++i )
{
Qgs3DAnimationSettings::Keyframe kf;
kf = cboKeyframe->itemData( i, Qt::UserRole + 1 ).value<Qgs3DAnimationSettings::Keyframe>();
keyframes << kf;
}
animSettings.setKeyframes( keyframes );
return animSettings;
}
void Qgs3DAnimationWidget::setDefaultAnimation()
{
Qgs3DAnimationSettings animSettings;
Qgs3DAnimationSettings::Keyframes kf;
Qgs3DAnimationSettings::Keyframe f1, f2;
f1.time = 0;
f1.point = mCameraController->lookingAtPoint();
f1.dist = mCameraController->distance();
f1.pitch = mCameraController->pitch();
f1.yaw = mCameraController->yaw();
f2.time = 5;
f2.point = f1.point;
f2.dist = f1.dist * 2;
f2.pitch = f1.pitch;
f2.yaw = f1.yaw;
kf << f1 << f2;
animSettings.setKeyframes( kf );
setAnimation( animSettings );
}
void Qgs3DAnimationWidget::setEditControlsEnabled( bool enabled )
{
cboKeyframe->setEnabled( enabled );
btnAddKeyframe->setEnabled( enabled );
cboInterpolation->setEnabled( enabled );
}
void Qgs3DAnimationWidget::onPlayPause()
{
if ( mAnimationTimer->isActive() )
{
mAnimationTimer->stop();
setEditControlsEnabled( true );
}
else
{
if ( sliderTime->value() >= sliderTime->maximum() )
{
sliderTime->setValue( 0 );
}
cboKeyframe->setCurrentIndex( 0 ); // unset active keyframe
setEditControlsEnabled( false );
mAnimationTimer->start();
}
}
void Qgs3DAnimationWidget::onAnimationTimer()
{
if ( sliderTime->value() >= sliderTime->maximum() )
{
if ( btnRepeat->isChecked() )
sliderTime->setValue( 0 );
else
{
// stop playback
onPlayPause();
btnPlayPause->setChecked( false );
}
}
else
{
sliderTime->setValue( sliderTime->value() + 1 );
}
}
void Qgs3DAnimationWidget::onSliderValueChanged()
{
// make sure we do not have an active keyframe
if ( cboKeyframe->currentIndex() != 0 )
cboKeyframe->setCurrentIndex( 0 );
Qgs3DAnimationSettings::Keyframe kf = mAnimationSettings->interpolate( sliderTime->value() / 100. );
mCameraController->setLookingAtPoint( kf.point, kf.dist, kf.pitch, kf.yaw );
}
void Qgs3DAnimationWidget::onCameraChanged()
{
if ( cboKeyframe->currentIndex() <= 0 )
return;
// update keyframe's camera position/rotation
int i = cboKeyframe->currentIndex();
Qgs3DAnimationSettings::Keyframe kf = cboKeyframe->itemData( i, Qt::UserRole + 1 ).value<Qgs3DAnimationSettings::Keyframe>();
kf.point = mCameraController->lookingAtPoint();
kf.dist = mCameraController->distance();
kf.pitch = mCameraController->pitch();
kf.yaw = mCameraController->yaw();
cboKeyframe->setItemData( i, QVariant::fromValue<Qgs3DAnimationSettings::Keyframe>( kf ), Qt::UserRole + 1 );
initializeController( animation() );
}
void Qgs3DAnimationWidget::onKeyframeChanged()
{
bool hasKeyframe = cboKeyframe->currentIndex() > 0;
btnRemoveKeyframe->setEnabled( hasKeyframe );
btnEditKeyframe->setEnabled( hasKeyframe );
btnDuplicateKeyframe->setEnabled( hasKeyframe );
if ( !hasKeyframe )
return;
// jump to the camera view of the keyframe
Qgs3DAnimationSettings::Keyframe kf = cboKeyframe->itemData( cboKeyframe->currentIndex(), Qt::UserRole + 1 ).value<Qgs3DAnimationSettings::Keyframe>();
whileBlocking( sliderTime )->setValue( kf.time * 100 );
mCameraController->setLookingAtPoint( kf.point, kf.dist, kf.pitch, kf.yaw );
}
int Qgs3DAnimationWidget::findIndexForKeyframe( float time )
{
int newIndex = 0;
for ( const Qgs3DAnimationSettings::Keyframe &keyframe : mAnimationSettings->keyFrames() )
{
if ( keyframe.time > time )
break;
newIndex++;
}
return newIndex;
}
float Qgs3DAnimationWidget::askForKeyframeTime( float defaultTime, bool *ok )
{
double t = QInputDialog::getDouble( this, tr( "Keyframe time" ), tr( "Keyframe time [seconds]:" ), defaultTime, 0, 9999, 2, ok );
if ( !*ok )
return 0;
// figure out position of this keyframe
for ( const Qgs3DAnimationSettings::Keyframe &keyframe : mAnimationSettings->keyFrames() )
{
if ( keyframe.time == t )
{
QMessageBox::warning( this, tr( "Keyframe time" ), tr( "There is already a keyframe at the given time" ) );
*ok = false;
return 0;
}
}
*ok = true;
return t;
}
void Qgs3DAnimationWidget::onAddKeyframe()
{
bool ok;
float t = askForKeyframeTime( sliderTime->value() / 100., &ok );
if ( !ok )
return;
int index = findIndexForKeyframe( t );
Qgs3DAnimationSettings::Keyframe kf;
kf.time = t;
kf.point = mCameraController->lookingAtPoint();
kf.dist = mCameraController->distance();
kf.pitch = mCameraController->pitch();
kf.yaw = mCameraController->yaw();
cboKeyframe->insertItem( index + 1, QString( "%1 s" ).arg( kf.time ) );
cboKeyframe->setItemData( index + 1, QVariant::fromValue<Qgs3DAnimationSettings::Keyframe>( kf ), Qt::UserRole + 1 );
initializeController( animation() );
cboKeyframe->setCurrentIndex( index + 1 );
}
void Qgs3DAnimationWidget::onRemoveKeyframe()
{
int index = cboKeyframe->currentIndex();
if ( index <= 0 )
return;
cboKeyframe->setCurrentIndex( 0 );
cboKeyframe->removeItem( index );
initializeController( animation() );
}
void Qgs3DAnimationWidget::onEditKeyframe()
{
int index = cboKeyframe->currentIndex();
if ( index <= 0 )
return;
Qgs3DAnimationSettings::Keyframe kf = cboKeyframe->itemData( index, Qt::UserRole + 1 ).value<Qgs3DAnimationSettings::Keyframe>();
bool ok;
float t = askForKeyframeTime( kf.time, &ok );
if ( !ok )
return;
cboKeyframe->setCurrentIndex( 0 );
cboKeyframe->removeItem( index );
initializeController( animation() );
// figure out position of this keyframe
int newIndex = findIndexForKeyframe( t );
kf.time = t;
cboKeyframe->insertItem( newIndex + 1, QString( "%1 s" ).arg( kf.time ) );
cboKeyframe->setItemData( newIndex + 1, QVariant::fromValue<Qgs3DAnimationSettings::Keyframe>( kf ), Qt::UserRole + 1 );
initializeController( animation() );
cboKeyframe->setCurrentIndex( newIndex + 1 );
}
void Qgs3DAnimationWidget::onDuplicateKeyframe()
{
int index = cboKeyframe->currentIndex();
if ( index <= 0 )
return;
Qgs3DAnimationSettings::Keyframe kf = cboKeyframe->itemData( index, Qt::UserRole + 1 ).value<Qgs3DAnimationSettings::Keyframe>();
bool ok;
float t = askForKeyframeTime( kf.time, &ok );
if ( !ok )
return;
// figure out position of this keyframe
int newIndex = findIndexForKeyframe( t );
kf.time = t;
cboKeyframe->insertItem( newIndex + 1, QString( "%1 s" ).arg( kf.time ) );
cboKeyframe->setItemData( newIndex + 1, QVariant::fromValue<Qgs3DAnimationSettings::Keyframe>( kf ), Qt::UserRole + 1 );
initializeController( animation() );
cboKeyframe->setCurrentIndex( newIndex + 1 );
}
void Qgs3DAnimationWidget::onInterpolationChanged()
{
initializeController( animation() );
if ( cboKeyframe->currentIndex() <= 0 )
onSliderValueChanged();
}