diff options
author | Sean Harmer <sh@theharmers.co.uk> | 2018-07-16 15:14:21 +0100 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2018-08-02 08:57:18 +0000 |
commit | 0b10ab797fea863ff2c4897b1c4eb993b21b153d (patch) | |
tree | 55e700935216320329fbe73ae2eccbc22a3e4966 | |
parent | 80a51b60c2f501a726b98e20eaba7f9551bee720 (diff) |
Apply similar logic to the native Qt 3D animation clip selection
If the file contains a single animation use that clip. Otherwise we
then consider animationName and animationIndex with the index having
priority.
Change-Id: Id974b1d446d6067bbe4c5ecce9f20891cb1086b0
Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
-rw-r--r-- | src/animation/backend/animationclip.cpp | 38 | ||||
-rw-r--r-- | src/animation/backend/gltfimporter.cpp | 6 | ||||
-rw-r--r-- | src/animation/frontend/qanimationcliploader.cpp | 4 |
3 files changed, 42 insertions, 6 deletions
diff --git a/src/animation/backend/animationclip.cpp b/src/animation/backend/animationclip.cpp index cc0a91ce0..3e0b8ce2b 100644 --- a/src/animation/backend/animationclip.cpp +++ b/src/animation/backend/animationclip.cpp @@ -251,17 +251,47 @@ void AnimationClip::loadAnimationFromUrl() QJsonObject rootObject = document.object(); // TODO: Allow loading of a named animation from a file containing many - QJsonArray animationsArray = rootObject[QLatin1String("animations")].toArray(); + const QJsonArray animationsArray = rootObject[QLatin1String("animations")].toArray(); qCDebug(Jobs) << "Found" << animationsArray.size() << "animations:"; for (int i = 0; i < animationsArray.size(); ++i) { QJsonObject animation = animationsArray.at(i).toObject(); qCDebug(Jobs) << "Animation Name:" << animation[QLatin1String("animationName")].toString(); } - // For now just load the first animation - // TODO: Allow loading a named animation from within the file analogous to QMesh - QJsonObject animation = animationsArray.at(0).toObject(); + // Find which animation clip to load from the file. + // Give priority to animationIndex over animationName + if (animationIndex >= animationsArray.size()) { + qCWarning(Jobs) << "Invalid animation index. Skipping."; + return; + } + + if (animationsArray.size() == 1) { + animationIndex = 0; + } else if (animationIndex < 0 && !animationName.isEmpty()) { + // Can we find an animation of the correct name? + bool foundAnimation = false; + for (int i = 0; i < animationsArray.size(); ++i) { + if (animationsArray.at(i)[ANIMATION_NAME_KEY].toString() == animationName) { + animationIndex = i; + foundAnimation = true; + break; + } + } + + if (!foundAnimation) { + qCWarning(Jobs) << "Invalid animation name. Skipping."; + return; + } + } + + if (animationIndex < 0 || animationIndex >= animationsArray.size()) { + qCWarning(Jobs) << "Failed to find animation. Skipping."; + return; + } + + QJsonObject animation = animationsArray.at(animationIndex).toObject(); m_name = animation[QLatin1String("animationName")].toString(); + QJsonArray channelsArray = animation[QLatin1String("channels")].toArray(); const int channelCount = channelsArray.size(); m_channels.resize(channelCount); diff --git a/src/animation/backend/gltfimporter.cpp b/src/animation/backend/gltfimporter.cpp index e91dd5661..ed10c3e25 100644 --- a/src/animation/backend/gltfimporter.cpp +++ b/src/animation/backend/gltfimporter.cpp @@ -474,7 +474,9 @@ GLTFImporter::AnimationNameAndChannels GLTFImporter::createAnimationData(int ani return nameAndChannels; } - if (animationIndex == -1 && !animationName.isEmpty()) { + if (m_animations.size() == 1) { + animationIndex = 0; + } else if (animationIndex < 0 && !animationName.isEmpty()) { for (int i = 0; i < m_animations.size(); ++i) { if (m_animations[i].name == animationName) { animationIndex = i; @@ -483,7 +485,7 @@ GLTFImporter::AnimationNameAndChannels GLTFImporter::createAnimationData(int ani } } - if (animationIndex >= m_animations.size()) { + if (animationIndex < 0 || animationIndex >= m_animations.size()) { qCWarning(Jobs) << "Invalid animation index. Skipping."; return nameAndChannels; } diff --git a/src/animation/frontend/qanimationcliploader.cpp b/src/animation/frontend/qanimationcliploader.cpp index 423ca136c..d31f9bd5d 100644 --- a/src/animation/frontend/qanimationcliploader.cpp +++ b/src/animation/frontend/qanimationcliploader.cpp @@ -84,6 +84,10 @@ void QAnimationClipLoaderPrivate::setStatus(QAnimationClipLoader::Status status) to select which animation should be loaded by way of query parameters on the source url. The accepted query parameters are animationIndex and animationName. If both are specified, animationName is ignored. + + If a file contains only a single animation, there is no need to specify + the animationIndex or animationName. We simply use the one available + animation. */ /*! \class Qt3DAnimation::QAnimationClipLoader |