1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qtimelineanimationnode_p.h"
5
6QT_BEGIN_NAMESPACE
7
8/*!
9 \qmltype TimelineAnimationNode
10 \inherits QBlendTreeNode
11 \nativetype QTimelineAnimationNode
12 \inqmlmodule QtQuick.Timeline.BlendTrees
13 \ingroup qtqmltypes
14
15 \brief A blend tree source node that plays a timeline animation.
16
17 TimelineAnimationNode is a blend tree source node that plays a timeline
18 animation and outputs the animation's frame data. This node wraps a
19 TimelineAnimation and its associated Timeline and provides a way to
20 intercept the animation's frame data and output it to the blend tree.
21*/
22
23/*!
24 \qmlproperty TimelineAnimation TimelineAnimationNode::animation
25
26 This property holds the timeline animation to play.
27*/
28
29/*!
30 \qmlproperty Timeline TimelineAnimationNode::timeline
31
32 This property holds the timeline that the animation is played on.
33*/
34
35/*!
36 \qmlproperty real TimelineAnimationNode::currentFrame
37
38 This property holds the current frame of the animation.
39*/
40
41QTimelineAnimationNode::QTimelineAnimationNode(QObject *parent)
42 : QBlendTreeNode(parent)
43{
44
45}
46
47QQuickTimelineAnimation *QTimelineAnimationNode::animation() const
48{
49 return m_animation;
50}
51
52void QTimelineAnimationNode::setAnimation(QQuickTimelineAnimation *newAnimation)
53{
54 if (m_animation == newAnimation)
55 return;
56
57 if (m_animation)
58 disconnect(m_animationDestroyedConnection);
59
60 m_animation = newAnimation;
61
62 if (m_animation)
63 m_animationDestroyedConnection = connect(sender: m_animation,
64 signal: &QObject::destroyed,
65 context: this,
66 slot: [this] {setAnimation(nullptr);});
67
68 updateAnimationTarget();
69 updateFrameData();
70 Q_EMIT animationChanged();
71}
72
73QQuickTimeline *QTimelineAnimationNode::timeline() const
74{
75 return m_timeline;
76}
77
78void QTimelineAnimationNode::setTimeline(QQuickTimeline *newTimeline)
79{
80 if (m_timeline == newTimeline)
81 return;
82
83 if (m_timeline)
84 disconnect(m_timelineDestroyedConnection);
85
86 m_timeline = newTimeline;
87
88 if (m_timeline)
89 m_timelineDestroyedConnection = connect(sender: m_timeline,
90 signal: &QObject::destroyed,
91 context: this,
92 slot: [this] {setTimeline(nullptr);});
93
94 updateFrameData();
95 Q_EMIT timelineChanged();
96}
97
98qreal QTimelineAnimationNode::currentFrame() const
99{
100 return m_currentFrame;
101}
102
103void QTimelineAnimationNode::setCurrentFrame(qreal newCurrentFrame)
104{
105 if (qFuzzyCompare(p1: m_currentFrame, p2: newCurrentFrame))
106 return;
107 m_currentFrame = newCurrentFrame;
108 updateFrameData();
109 Q_EMIT currentFrameChanged();
110}
111
112static QHash<QQmlProperty, QVariant> getFrameData(QQuickTimeline *timeline, qreal frame)
113{
114 QHash<QQmlProperty, QVariant> frameData;
115 if (timeline) {
116 QQmlListReference keyframeGroups(timeline, "keyframeGroups");
117 if (keyframeGroups.isValid() && keyframeGroups.isReadable()) {
118 for (int i = 0; i < keyframeGroups.count(); ++i) {
119 QQuickKeyframeGroup *keyframeGroup = qobject_cast<QQuickKeyframeGroup *>(object: keyframeGroups.at(i));
120 if (keyframeGroup && keyframeGroup->target()) {
121 QQmlProperty qmlProperty(keyframeGroup->target(), keyframeGroup->property());
122 QVariant value = keyframeGroup->evaluate(frame);
123 frameData.insert(key: qmlProperty, value);
124 }
125 }
126 }
127 }
128
129 return frameData;
130}
131
132void QTimelineAnimationNode::updateFrameData()
133{
134 if (!m_animation || !m_timeline)
135 return;
136
137 m_frameData = getFrameData(timeline: m_timeline, frame: m_currentFrame);
138 Q_EMIT frameDataChanged();
139}
140
141void QTimelineAnimationNode::updateAnimationTarget()
142{
143 if (!m_animation)
144 return;
145 // Property should already be set to "currentFrame"
146 m_animation->setTargetObject(this);
147}
148
149QT_END_NAMESPACE
150

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

source code of qtquicktimeline/src/timeline/blendtrees/qtimelineanimationnode.cpp