17
17
18
18
#include " qgs3danimationsettings.h"
19
19
#include " qgsapplication.h"
20
+ #include " qgscameracontroller.h"
20
21
21
- #include < Qt3DAnimation/QAnimationController>
22
22
#include < Qt3DRender/QCamera>
23
23
#include < QTimer>
24
24
@@ -38,8 +38,6 @@ Qgs3DAnimationWidget::Qgs3DAnimationWidget( QWidget *parent )
38
38
mAnimationTimer ->setInterval ( 10 );
39
39
connect ( mAnimationTimer , &QTimer::timeout, this , &Qgs3DAnimationWidget::onAnimationTimer );
40
40
41
- mAnimationController = new Qt3DAnimation::QAnimationController ( this );
42
-
43
41
btnPlayPause->setCheckable ( true );
44
42
connect ( btnPlayPause, &QToolButton::clicked, this , &Qgs3DAnimationWidget::onPlayPause );
45
43
@@ -48,12 +46,16 @@ Qgs3DAnimationWidget::Qgs3DAnimationWidget( QWidget *parent )
48
46
connect ( cboKeyframe, qgis::overload<int >::of ( &QComboBox::currentIndexChanged ), this , &Qgs3DAnimationWidget::onKeyframeChanged );
49
47
}
50
48
51
- void Qgs3DAnimationWidget::setCamera ( Qt3DRender::QCamera *camera )
49
+ Qgs3DAnimationWidget::~Qgs3DAnimationWidget () = default ;
50
+
51
+
52
+ void Qgs3DAnimationWidget::setCameraController ( QgsCameraController *cameraController )
52
53
{
53
- mCamera = camera ;
54
- connect ( mCamera , &Qt3DRender::QCamera::viewMatrixChanged , this , &Qgs3DAnimationWidget::onCameraViewMatrixChanged );
54
+ mCameraController = cameraController ;
55
+ connect ( mCameraController , &QgsCameraController::cameraChanged , this , &Qgs3DAnimationWidget::onCameraChanged );
55
56
}
56
57
58
+
57
59
void Qgs3DAnimationWidget::setAnimation ( const Qgs3DAnimationSettings &animSettings )
58
60
{
59
61
// initialize GUI from the given animation
@@ -63,26 +65,15 @@ void Qgs3DAnimationWidget::setAnimation( const Qgs3DAnimationSettings &animSetti
63
65
{
64
66
cboKeyframe->addItem ( QString ( " %1 s" ).arg ( keyframe.time ) );
65
67
int lastIndex = cboKeyframe->count () - 1 ;
66
- cboKeyframe->setItemData ( lastIndex, keyframe.time , Qt::UserRole + 1 );
67
- cboKeyframe->setItemData ( lastIndex, keyframe.position , Qt::UserRole + 2 );
68
- cboKeyframe->setItemData ( lastIndex, keyframe.rotation , Qt::UserRole + 3 );
68
+ cboKeyframe->setItemData ( lastIndex, QVariant::fromValue<Qgs3DAnimationSettings::Keyframe>( keyframe ), Qt::UserRole + 1 );
69
69
}
70
70
71
71
initializeController ( animSettings );
72
72
}
73
73
74
74
void Qgs3DAnimationWidget::initializeController ( const Qgs3DAnimationSettings &animSettings )
75
75
{
76
- // set up animation in the controller
77
- Qt3DAnimation::QAnimationGroup *group = new Qt3DAnimation::QAnimationGroup;
78
- Qt3DAnimation::QKeyframeAnimation *animation = animSettings.createAnimation ( nullptr ); // TODO: who deletes transforms?
79
- animation->setParent ( group );
80
- animation->setTarget ( mCamera ->transform () );
81
- group->addAnimation ( animation ); // does not delete animations later
82
-
83
- QVector<Qt3DAnimation::QAnimationGroup *> groups;
84
- groups << group;
85
- mAnimationController ->setAnimationGroups ( groups ); // does not delete groups later
76
+ mAnimationSettings .reset ( new Qgs3DAnimationSettings ( animSettings ) );
86
77
87
78
sliderTime->setMaximum ( animSettings.duration () * 100 );
88
79
}
@@ -91,15 +82,11 @@ Qgs3DAnimationSettings Qgs3DAnimationWidget::animation() const
91
82
{
92
83
Qgs3DAnimationSettings animSettings;
93
84
Qgs3DAnimationSettings::Keyframes keyframes;
94
- qDebug () << " ---" ;
95
85
for ( int i = 1 ; i < cboKeyframe->count (); ++i )
96
86
{
97
87
Qgs3DAnimationSettings::Keyframe kf;
98
- kf.time = cboKeyframe->itemData ( i, Qt::UserRole + 1 ).toFloat ();
99
- kf.position = cboKeyframe->itemData ( i, Qt::UserRole + 2 ).value <QVector3D>();
100
- kf.rotation = cboKeyframe->itemData ( i, Qt::UserRole + 3 ).value <QQuaternion>();
88
+ kf = cboKeyframe->itemData ( i, Qt::UserRole + 1 ).value <Qgs3DAnimationSettings::Keyframe>();
101
89
keyframes << kf;
102
- qDebug () << " keyframe" << kf.time << kf.position << kf.rotation ;
103
90
}
104
91
animSettings.setKeyframes ( keyframes );
105
92
return animSettings;
@@ -111,11 +98,17 @@ void Qgs3DAnimationWidget::setDefaultAnimation()
111
98
Qgs3DAnimationSettings::Keyframes kf;
112
99
Qgs3DAnimationSettings::Keyframe f1, f2;
113
100
f1.time = 0 ;
114
- f1.position = mCamera ->transform ()->translation ();
115
- f1.rotation = mCamera ->transform ()->rotation ();
101
+ f1.point = mCameraController ->lookingAtPoint ();
102
+ f1.dist = mCameraController ->distance ();
103
+ f1.pitch = mCameraController ->pitch ();
104
+ f1.yaw = mCameraController ->yaw ();
105
+
116
106
f2.time = 5 ;
117
- f2.position = f1.position + QVector3D ( 0 , 0 , f1.position .z () / 2 );
118
- f2.rotation = f1.rotation ;
107
+ f2.point = f1.point ;
108
+ f2.dist = f1.dist * 2 ;
109
+ f2.pitch = f1.pitch ;
110
+ f2.yaw = f1.yaw ;
111
+
119
112
kf << f1 << f2;
120
113
animSettings.setKeyframes ( kf );
121
114
@@ -143,20 +136,30 @@ void Qgs3DAnimationWidget::onAnimationTimer()
143
136
sliderTime->setValue ( sliderTime->value () >= duration ? 0 : sliderTime->value () + 1 );
144
137
}
145
138
139
+
146
140
void Qgs3DAnimationWidget::onSliderValueChanged ()
147
141
{
148
- mAnimationController ->setPosition ( sliderTime->value () / 100 . );
142
+ // make sure we do not have an active keyframe
143
+ if ( cboKeyframe->currentIndex () != 0 )
144
+ cboKeyframe->setCurrentIndex ( 0 );
145
+
146
+ Qgs3DAnimationSettings::Keyframe kf = mAnimationSettings ->interpolate ( sliderTime->value () / 100 . );
147
+ mCameraController ->setLookingAtPoint ( kf.point , kf.dist , kf.pitch , kf.yaw );
149
148
}
150
149
151
- void Qgs3DAnimationWidget::onCameraViewMatrixChanged ()
150
+ void Qgs3DAnimationWidget::onCameraChanged ()
152
151
{
153
152
if ( cboKeyframe->currentIndex () <= 0 )
154
153
return ;
155
154
156
155
// update keyframe's camera position/rotation
157
156
int i = cboKeyframe->currentIndex ();
158
- cboKeyframe->setItemData ( i, mCamera ->transform ()->translation (), Qt::UserRole + 2 );
159
- cboKeyframe->setItemData ( i, mCamera ->transform ()->rotation (), Qt::UserRole + 3 );
157
+ Qgs3DAnimationSettings::Keyframe kf = cboKeyframe->itemData ( i, Qt::UserRole + 1 ).value <Qgs3DAnimationSettings::Keyframe>();
158
+ kf.point = mCameraController ->lookingAtPoint ();
159
+ kf.dist = mCameraController ->distance ();
160
+ kf.pitch = mCameraController ->pitch ();
161
+ kf.yaw = mCameraController ->yaw ();
162
+ cboKeyframe->setItemData ( i, QVariant::fromValue<Qgs3DAnimationSettings::Keyframe>( kf ), Qt::UserRole + 1 );
160
163
161
164
initializeController ( animation () );
162
165
}
@@ -167,6 +170,8 @@ void Qgs3DAnimationWidget::onKeyframeChanged()
167
170
return ;
168
171
169
172
// jump to the camera view of the keyframe
170
- float time = cboKeyframe->itemData ( cboKeyframe->currentIndex (), Qt::UserRole + 1 ).toFloat ();
171
- sliderTime->setValue ( time * 100 );
173
+ Qgs3DAnimationSettings::Keyframe kf = cboKeyframe->itemData ( cboKeyframe->currentIndex (), Qt::UserRole + 1 ).value <Qgs3DAnimationSettings::Keyframe>();
174
+
175
+ whileBlocking ( sliderTime )->setValue ( kf.time * 100 );
176
+ mCameraController ->setLookingAtPoint ( kf.point , kf.dist , kf.pitch , kf.yaw );
172
177
}
0 commit comments