|
| 1 | +/*************************************************************************** |
| 2 | + qgs3danimationwidget.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : July 2018 |
| 5 | + Copyright : (C) 2018 by Martin Dobias |
| 6 | + Email : wonder dot sk at gmail dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#include "qgs3danimationwidget.h" |
| 17 | + |
| 18 | +#include "qgs3danimationsettings.h" |
| 19 | +#include "qgsapplication.h" |
| 20 | + |
| 21 | +#include <Qt3DAnimation/QAnimationController> |
| 22 | +#include <Qt3DRender/QCamera> |
| 23 | +#include <QTimer> |
| 24 | + |
| 25 | +Qgs3DAnimationWidget::Qgs3DAnimationWidget( QWidget *parent ) |
| 26 | + : QWidget( parent ) |
| 27 | +{ |
| 28 | + setupUi( this ); |
| 29 | + |
| 30 | + btnAddKeyframe->setIcon( QIcon( QgsApplication::iconPath( "symbologyAdd.svg" ) ) ); |
| 31 | + btnRemoveKeyframe->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.svg" ) ) ); |
| 32 | + btnEditKeyframe->setIcon( QIcon( QgsApplication::iconPath( "symbologyEdit.svg" ) ) ); |
| 33 | + btnPlayPause->setIcon( QIcon( QgsApplication::iconPath( "mTaskRunning.svg" ) ) ); |
| 34 | + |
| 35 | + cboKeyframe->addItem( tr( "<none>" ) ); |
| 36 | + |
| 37 | + mAnimationTimer = new QTimer( this ); |
| 38 | + mAnimationTimer->setInterval( 10 ); |
| 39 | + connect( mAnimationTimer, &QTimer::timeout, this, &Qgs3DAnimationWidget::onAnimationTimer ); |
| 40 | + |
| 41 | + mAnimationController = new Qt3DAnimation::QAnimationController( this ); |
| 42 | + |
| 43 | + btnPlayPause->setCheckable( true ); |
| 44 | + connect( btnPlayPause, &QToolButton::clicked, this, &Qgs3DAnimationWidget::onPlayPause ); |
| 45 | + |
| 46 | + connect( sliderTime, &QSlider::valueChanged, this, &Qgs3DAnimationWidget::onSliderValueChanged ); |
| 47 | + |
| 48 | + connect( cboKeyframe, qgis::overload<int>::of( &QComboBox::currentIndexChanged ), this, &Qgs3DAnimationWidget::onKeyframeChanged ); |
| 49 | +} |
| 50 | + |
| 51 | +void Qgs3DAnimationWidget::setCamera( Qt3DRender::QCamera *camera ) |
| 52 | +{ |
| 53 | + mCamera = camera; |
| 54 | + connect( mCamera, &Qt3DRender::QCamera::viewMatrixChanged, this, &Qgs3DAnimationWidget::onCameraViewMatrixChanged ); |
| 55 | +} |
| 56 | + |
| 57 | +void Qgs3DAnimationWidget::setAnimation( const Qgs3DAnimationSettings &animSettings ) |
| 58 | +{ |
| 59 | + // initialize GUI from the given animation |
| 60 | + cboKeyframe->clear(); |
| 61 | + cboKeyframe->addItem( tr( "<none>" ) ); |
| 62 | + for ( const Qgs3DAnimationSettings::Keyframe &keyframe : animSettings.keyFrames() ) |
| 63 | + { |
| 64 | + cboKeyframe->addItem( QString( "%1 s" ).arg( keyframe.time ) ); |
| 65 | + 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 ); |
| 69 | + } |
| 70 | + |
| 71 | + initializeController( animSettings ); |
| 72 | +} |
| 73 | + |
| 74 | +void Qgs3DAnimationWidget::initializeController( const Qgs3DAnimationSettings &animSettings ) |
| 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 |
| 86 | + |
| 87 | + sliderTime->setMaximum( animSettings.duration() * 100 ); |
| 88 | +} |
| 89 | + |
| 90 | +Qgs3DAnimationSettings Qgs3DAnimationWidget::animation() const |
| 91 | +{ |
| 92 | + Qgs3DAnimationSettings animSettings; |
| 93 | + Qgs3DAnimationSettings::Keyframes keyframes; |
| 94 | + qDebug() << "---"; |
| 95 | + for ( int i = 1; i < cboKeyframe->count(); ++i ) |
| 96 | + { |
| 97 | + 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>(); |
| 101 | + keyframes << kf; |
| 102 | + qDebug() << "keyframe" << kf.time << kf.position << kf.rotation; |
| 103 | + } |
| 104 | + animSettings.setKeyframes( keyframes ); |
| 105 | + return animSettings; |
| 106 | +} |
| 107 | + |
| 108 | +void Qgs3DAnimationWidget::setDefaultAnimation() |
| 109 | +{ |
| 110 | + Qgs3DAnimationSettings animSettings; |
| 111 | + Qgs3DAnimationSettings::Keyframes kf; |
| 112 | + Qgs3DAnimationSettings::Keyframe f1, f2; |
| 113 | + f1.time = 0; |
| 114 | + f1.position = mCamera->transform()->translation(); |
| 115 | + f1.rotation = mCamera->transform()->rotation(); |
| 116 | + f2.time = 5; |
| 117 | + f2.position = f1.position + QVector3D( 0, 0, f1.position.z() / 2 ); |
| 118 | + f2.rotation = f1.rotation; |
| 119 | + kf << f1 << f2; |
| 120 | + animSettings.setKeyframes( kf ); |
| 121 | + |
| 122 | + setAnimation( animSettings ); |
| 123 | +} |
| 124 | + |
| 125 | +void Qgs3DAnimationWidget::onPlayPause() |
| 126 | +{ |
| 127 | + if ( mAnimationTimer->isActive() ) |
| 128 | + { |
| 129 | + mAnimationTimer->stop(); |
| 130 | + cboKeyframe->setEnabled( true ); |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + cboKeyframe->setCurrentIndex( 0 ); // unset active keyframe |
| 135 | + cboKeyframe->setEnabled( false ); |
| 136 | + mAnimationTimer->start(); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +void Qgs3DAnimationWidget::onAnimationTimer() |
| 141 | +{ |
| 142 | + float duration = sliderTime->maximum(); |
| 143 | + sliderTime->setValue( sliderTime->value() >= duration ? 0 : sliderTime->value() + 1 ); |
| 144 | +} |
| 145 | + |
| 146 | +void Qgs3DAnimationWidget::onSliderValueChanged() |
| 147 | +{ |
| 148 | + mAnimationController->setPosition( sliderTime->value() / 100. ); |
| 149 | +} |
| 150 | + |
| 151 | +void Qgs3DAnimationWidget::onCameraViewMatrixChanged() |
| 152 | +{ |
| 153 | + if ( cboKeyframe->currentIndex() <= 0 ) |
| 154 | + return; |
| 155 | + |
| 156 | + // update keyframe's camera position/rotation |
| 157 | + int i = cboKeyframe->currentIndex(); |
| 158 | + cboKeyframe->setItemData( i, mCamera->transform()->translation(), Qt::UserRole + 2 ); |
| 159 | + cboKeyframe->setItemData( i, mCamera->transform()->rotation(), Qt::UserRole + 3 ); |
| 160 | + |
| 161 | + initializeController( animation() ); |
| 162 | +} |
| 163 | + |
| 164 | +void Qgs3DAnimationWidget::onKeyframeChanged() |
| 165 | +{ |
| 166 | + if ( cboKeyframe->currentIndex() <= 0 ) |
| 167 | + return; |
| 168 | + |
| 169 | + // jump to the camera view of the keyframe |
| 170 | + float time = cboKeyframe->itemData( cboKeyframe->currentIndex(), Qt::UserRole + 1 ).toFloat(); |
| 171 | + sliderTime->setValue( time * 100 ); |
| 172 | +} |
0 commit comments