1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
4 | ** Contact: http://www.qt-project.org/legal |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at http://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or later as published by the Free |
28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
29 | ** the packaging of this file. Please review the following information to |
30 | ** ensure the GNU General Public License version 2.0 requirements will be |
31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
32 | ** |
33 | ** $QT_END_LICENSE$ |
34 | ** |
35 | ****************************************************************************/ |
36 | |
37 | #include "clipanimator_p.h" |
38 | #include <Qt3DAnimation/qclipanimator.h> |
39 | #include <Qt3DAnimation/qchannelmapper.h> |
40 | #include <Qt3DAnimation/qclock.h> |
41 | #include <Qt3DAnimation/private/qclipanimator_p.h> |
42 | #include <Qt3DAnimation/private/animationclip_p.h> |
43 | #include <Qt3DAnimation/private/managers_p.h> |
44 | #include <Qt3DAnimation/private/animationlogging_p.h> |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | namespace Qt3DAnimation { |
49 | namespace Animation { |
50 | |
51 | ClipAnimator::ClipAnimator() |
52 | : BackendNode(ReadWrite) |
53 | , m_clipId() |
54 | , m_mapperId() |
55 | , m_clockId() |
56 | , m_running(false) |
57 | , m_loops(1) |
58 | , m_lastGlobalTimeNS(0) |
59 | , m_lastLocalTime(0.0) |
60 | , m_mappingData() |
61 | , m_currentLoop(0) |
62 | , m_normalizedLocalTime(-1.0f) |
63 | , m_lastNormalizedLocalTime(-1.0f) |
64 | { |
65 | } |
66 | |
67 | void ClipAnimator::setClipId(Qt3DCore::QNodeId clipId) |
68 | { |
69 | m_clipId = clipId; |
70 | setDirty(Handler::ClipAnimatorDirty); |
71 | |
72 | // register at the clip to make sure we are marked dirty when the clip finished loading |
73 | AnimationClip *clip = m_handler->animationClipLoaderManager()->lookupResource(id: clipId); |
74 | if (clip) |
75 | clip->addDependingClipAnimator(id: peerId()); |
76 | } |
77 | |
78 | void ClipAnimator::setMapperId(Qt3DCore::QNodeId mapperId) |
79 | { |
80 | m_mapperId = mapperId; |
81 | setDirty(Handler::ClipAnimatorDirty); |
82 | } |
83 | |
84 | void ClipAnimator::setClockId(Qt3DCore::QNodeId clockId) |
85 | { |
86 | m_clockId = clockId; |
87 | setDirty(Handler::ClipAnimatorDirty); |
88 | } |
89 | |
90 | void ClipAnimator::setRunning(bool running) |
91 | { |
92 | m_running = running; |
93 | if (!running) |
94 | m_currentLoop = 0; |
95 | setDirty(Handler::ClipAnimatorDirty); |
96 | } |
97 | |
98 | void ClipAnimator::setNormalizedLocalTime(float normalizedTime, bool allowMarkDirty) |
99 | { |
100 | m_normalizedLocalTime = normalizedTime; |
101 | if (isValidNormalizedTime(t: normalizedTime) && allowMarkDirty) |
102 | setDirty(Handler::ClipAnimatorDirty); |
103 | } |
104 | |
105 | void ClipAnimator::cleanup() |
106 | { |
107 | setEnabled(false); |
108 | m_handler = nullptr; |
109 | m_clipId = Qt3DCore::QNodeId(); |
110 | m_mapperId = Qt3DCore::QNodeId(); |
111 | m_clockId = Qt3DCore::QNodeId(); |
112 | m_running = false; |
113 | m_loops = 1; |
114 | m_clipFormat = ClipFormat(); |
115 | m_normalizedLocalTime = m_lastNormalizedLocalTime = -1.0f; |
116 | } |
117 | |
118 | void ClipAnimator::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime) |
119 | { |
120 | BackendNode::syncFromFrontEnd(frontEnd, firstTime); |
121 | const QClipAnimator *node = qobject_cast<const QClipAnimator *>(object: frontEnd); |
122 | if (!node) |
123 | return; |
124 | |
125 | auto id = Qt3DCore::qIdForNode(node: node->clip()); |
126 | if (m_clipId != id) |
127 | setClipId(id); |
128 | id = Qt3DCore::qIdForNode(node: node->channelMapper()); |
129 | if (m_mapperId != id) |
130 | setMapperId(id); |
131 | id = Qt3DCore::qIdForNode(node: node->clock()); |
132 | if (m_clockId != id) |
133 | setClockId(id); |
134 | |
135 | if (m_running != node->isRunning()) |
136 | setRunning(node->isRunning()); |
137 | if (m_loops != node->loopCount()) |
138 | m_loops = node->loopCount(); |
139 | if (!qFuzzyCompare(p1: m_normalizedLocalTime, p2: node->normalizedTime())) |
140 | setNormalizedLocalTime(normalizedTime: node->normalizedTime()); |
141 | |
142 | if (firstTime) |
143 | setDirty(Handler::ClipAnimatorDirty); |
144 | } |
145 | |
146 | qint64 ClipAnimator::nsSincePreviousFrame(qint64 currentGlobalTimeNS) |
147 | { |
148 | return currentGlobalTimeNS - m_lastGlobalTimeNS; |
149 | } |
150 | |
151 | void ClipAnimator::setLastGlobalTimeNS(qint64 lastGlobalTimeNS) |
152 | { |
153 | m_lastGlobalTimeNS = lastGlobalTimeNS; |
154 | } |
155 | |
156 | double ClipAnimator::lastLocalTime() const |
157 | { |
158 | return m_lastLocalTime; |
159 | } |
160 | |
161 | void ClipAnimator::setLastLocalTime(double lastLocalTime) |
162 | { |
163 | m_lastLocalTime = lastLocalTime; |
164 | } |
165 | |
166 | void ClipAnimator::setLastNormalizedLocalTime(float normalizedTime) |
167 | { |
168 | m_lastNormalizedLocalTime = normalizedTime; |
169 | } |
170 | |
171 | } // namespace Animation |
172 | } // namespace Qt3DAnimation |
173 | |
174 | QT_END_NAMESPACE |
175 | |