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 "clipanimator_p.h"
5#include <Qt3DAnimation/qclipanimator.h>
6#include <Qt3DAnimation/qchannelmapper.h>
7#include <Qt3DAnimation/qclock.h>
8#include <Qt3DAnimation/private/qclipanimator_p.h>
9#include <Qt3DAnimation/private/animationclip_p.h>
10#include <Qt3DAnimation/private/managers_p.h>
11#include <Qt3DAnimation/private/animationlogging_p.h>
12
13QT_BEGIN_NAMESPACE
14
15namespace Qt3DAnimation {
16namespace Animation {
17
18ClipAnimator::ClipAnimator()
19 : BackendNode(ReadWrite)
20 , m_clipId()
21 , m_mapperId()
22 , m_clockId()
23 , m_running(false)
24 , m_loops(1)
25 , m_lastGlobalTimeNS(0)
26 , m_lastLocalTime(0.0)
27 , m_mappingData()
28 , m_currentLoop(0)
29 , m_normalizedLocalTime(-1.0f)
30 , m_lastNormalizedLocalTime(-1.0f)
31{
32}
33
34void ClipAnimator::setClipId(Qt3DCore::QNodeId clipId)
35{
36 m_clipId = clipId;
37 setDirty(Handler::ClipAnimatorDirty);
38
39 // register at the clip to make sure we are marked dirty when the clip finished loading
40 AnimationClip *clip = m_handler->animationClipLoaderManager()->lookupResource(id: clipId);
41 if (clip)
42 clip->addDependingClipAnimator(id: peerId());
43}
44
45void ClipAnimator::setMapperId(Qt3DCore::QNodeId mapperId)
46{
47 m_mapperId = mapperId;
48 setDirty(Handler::ClipAnimatorDirty);
49}
50
51void ClipAnimator::setClockId(Qt3DCore::QNodeId clockId)
52{
53 m_clockId = clockId;
54 setDirty(Handler::ClipAnimatorDirty);
55}
56
57void ClipAnimator::setRunning(bool running)
58{
59 m_running = running;
60 if (!running)
61 m_currentLoop = 0;
62 setDirty(Handler::ClipAnimatorDirty);
63}
64
65void ClipAnimator::setNormalizedLocalTime(float normalizedTime, bool allowMarkDirty)
66{
67 m_normalizedLocalTime = normalizedTime;
68 if (isValidNormalizedTime(t: normalizedTime) && allowMarkDirty)
69 setDirty(Handler::ClipAnimatorDirty);
70}
71
72void ClipAnimator::cleanup()
73{
74 setEnabled(false);
75 m_handler = nullptr;
76 m_clipId = Qt3DCore::QNodeId();
77 m_mapperId = Qt3DCore::QNodeId();
78 m_clockId = Qt3DCore::QNodeId();
79 m_running = false;
80 m_loops = 1;
81 m_clipFormat = ClipFormat();
82 m_currentLoop = 0;
83 m_normalizedLocalTime = m_lastNormalizedLocalTime = -1.0f;
84}
85
86void ClipAnimator::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime)
87{
88 BackendNode::syncFromFrontEnd(frontEnd, firstTime);
89 const QClipAnimator *node = qobject_cast<const QClipAnimator *>(object: frontEnd);
90 if (!node)
91 return;
92
93 auto id = Qt3DCore::qIdForNode(node: node->clip());
94 if (m_clipId != id)
95 setClipId(id);
96 id = Qt3DCore::qIdForNode(node: node->channelMapper());
97 if (m_mapperId != id)
98 setMapperId(id);
99 id = Qt3DCore::qIdForNode(node: node->clock());
100 if (m_clockId != id)
101 setClockId(id);
102
103 if (m_running != node->isRunning())
104 setRunning(node->isRunning());
105 if (m_loops != node->loopCount())
106 m_loops = node->loopCount();
107 if (!qFuzzyCompare(p1: m_normalizedLocalTime, p2: node->normalizedTime()))
108 setNormalizedLocalTime(normalizedTime: node->normalizedTime());
109
110 if (firstTime)
111 setDirty(Handler::ClipAnimatorDirty);
112}
113
114qint64 ClipAnimator::nsSincePreviousFrame(qint64 currentGlobalTimeNS)
115{
116 return currentGlobalTimeNS - m_lastGlobalTimeNS;
117}
118
119void ClipAnimator::setLastGlobalTimeNS(qint64 lastGlobalTimeNS)
120{
121 m_lastGlobalTimeNS = lastGlobalTimeNS;
122}
123
124double ClipAnimator::lastLocalTime() const
125{
126 return m_lastLocalTime;
127}
128
129void ClipAnimator::setLastLocalTime(double lastLocalTime)
130{
131 m_lastLocalTime = lastLocalTime;
132}
133
134void ClipAnimator::setLastNormalizedLocalTime(float normalizedTime)
135{
136 m_lastNormalizedLocalTime = normalizedTime;
137}
138
139} // namespace Animation
140} // namespace Qt3DAnimation
141
142QT_END_NAMESPACE
143

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