1// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "loadanimationclipjob_p.h"
5
6#include <Qt3DCore/private/qaspectmanager_p.h>
7#include <Qt3DAnimation/qanimationcliploader.h>
8#include <Qt3DAnimation/private/qanimationcliploader_p.h>
9#include <Qt3DAnimation/private/animationclip_p.h>
10#include <Qt3DAnimation/private/qabstractanimationclip_p.h>
11#include <Qt3DAnimation/private/handler_p.h>
12#include <Qt3DAnimation/private/managers_p.h>
13#include <Qt3DAnimation/private/job_common_p.h>
14
15QT_BEGIN_NAMESPACE
16
17namespace Qt3DAnimation {
18namespace Animation {
19
20class LoadAnimationClipJobPrivate : public Qt3DCore::QAspectJobPrivate
21{
22public:
23 LoadAnimationClipJobPrivate() { }
24 ~LoadAnimationClipJobPrivate() override { }
25
26 void postFrame(Qt3DCore::QAspectManager *manager) override;
27
28 QList<AnimationClip *> m_updatedNodes;
29};
30
31LoadAnimationClipJob::LoadAnimationClipJob()
32 : Qt3DCore::QAspectJob(*new LoadAnimationClipJobPrivate)
33 , m_animationClipHandles()
34 , m_handler(nullptr)
35{
36 SET_JOB_RUN_STAT_TYPE(this, JobTypes::LoadAnimationClip, 0)
37}
38
39void LoadAnimationClipJob::addDirtyAnimationClips(const QVector<HAnimationClip> &animationClipHandles)
40{
41 for (const auto &handle : animationClipHandles) {
42 if (!m_animationClipHandles.contains(t: handle))
43 m_animationClipHandles.push_back(t: handle);
44 }
45}
46
47void LoadAnimationClipJob::clearDirtyAnimationClips()
48{
49 m_animationClipHandles.clear();
50}
51
52void LoadAnimationClipJob::run()
53{
54 Q_ASSERT(m_handler);
55 Q_D(LoadAnimationClipJob);
56
57 d->m_updatedNodes.reserve(asize: m_animationClipHandles.size());
58 AnimationClipLoaderManager *animationClipManager = m_handler->animationClipLoaderManager();
59 for (const auto &animationClipHandle : std::as_const(t&: m_animationClipHandles)) {
60 AnimationClip *animationClip = animationClipManager->data(handle: animationClipHandle);
61 Q_ASSERT(animationClip);
62 animationClip->loadAnimation();
63 d->m_updatedNodes.push_back(t: animationClip);
64 }
65
66 clearDirtyAnimationClips();
67}
68
69void LoadAnimationClipJobPrivate::postFrame(Qt3DCore::QAspectManager *manager)
70{
71 for (AnimationClip *clip: std::as_const(t&: m_updatedNodes)) {
72 QAbstractAnimationClip *node = qobject_cast<QAbstractAnimationClip *>(object: manager->lookupNode(id: clip->peerId()));
73 if (!node)
74 continue;
75
76 QAbstractAnimationClipPrivate *dnode = static_cast<QAbstractAnimationClipPrivate *>(Qt3DCore::QNodePrivate::get(q: node));
77 dnode->setDuration(clip->duration());
78
79 QAnimationClipLoader *loader = qobject_cast<QAnimationClipLoader *>(object: node);
80 if (loader) {
81 QAnimationClipLoaderPrivate *dloader = static_cast<QAnimationClipLoaderPrivate *>(dnode);
82 dloader->setStatus(clip->status());
83 }
84 }
85
86 m_updatedNodes.clear();
87}
88
89} // namespace Animation
90} // namespace Qt3DAnimation
91
92QT_END_NAMESPACE
93

source code of qt3d/src/animation/backend/loadanimationclipjob.cpp