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 "qcallbackmapping.h"
38#include "qcallbackmapping_p.h"
39
40#include <Qt3DAnimation/private/qchannelmappingcreatedchange_p.h>
41#include <Qt3DCore/qpropertyupdatedchange.h>
42
43#include <QtCore/qmetaobject.h>
44#include <QtCore/QMetaProperty>
45
46QT_BEGIN_NAMESPACE
47
48namespace Qt3DAnimation {
49
50QCallbackMappingPrivate::QCallbackMappingPrivate()
51 : QAbstractChannelMappingPrivate()
52 , m_channelName()
53 , m_type(static_cast<int>(QVariant::Invalid))
54 , m_callback(nullptr)
55 , m_callbackFlags(0)
56{
57 m_mappingType = QChannelMappingCreatedChangeBase::CallbackMapping;
58}
59
60/*!
61 \class QCallbackMapping
62 \inherits Qt3DCore::QAbstractChannelMapping
63 \inmodule Qt3DAnimation
64 \brief Allows to map the channels within the clip onto an invocation
65 of a callback object.
66*/
67
68QCallbackMapping::QCallbackMapping(Qt3DCore::QNode *parent)
69 : QAbstractChannelMapping(*new QCallbackMappingPrivate, parent)
70{
71}
72
73QCallbackMapping::QCallbackMapping(QCallbackMappingPrivate &dd, Qt3DCore::QNode *parent)
74 : QAbstractChannelMapping(dd, parent)
75{
76}
77
78QCallbackMapping::~QCallbackMapping()
79{
80}
81
82QString QCallbackMapping::channelName() const
83{
84 Q_D(const QCallbackMapping);
85 return d->m_channelName;
86}
87
88QAnimationCallback *QCallbackMapping::callback() const
89{
90 Q_D(const QCallbackMapping);
91 return d->m_callback;
92}
93
94void QCallbackMapping::setChannelName(const QString &channelName)
95{
96 Q_D(QCallbackMapping);
97 if (d->m_channelName == channelName)
98 return;
99
100 d->m_channelName = channelName;
101 emit channelNameChanged(channelName);
102}
103
104/*!
105 Associates a \a callback object with this channel mapping.
106
107 Such mappings do not have to have a target object and property name. When
108 the \a callback object is set, every change in the animated value will lead
109 to invoking the callback's
110 \l {Qt3DAnimation::QAnimationCallback::valueChanged}{valueChanged} function either
111 on the gui/main thread, or directly on one of the thread pool's worker
112 thread. This is controlled by \a flags.
113
114 \a type specifies the type (for example, QVariant::Vector3D,
115 QVariant::Color, or QMetaType::Float) of the animated value. When animating
116 node properties this does not need to be provided separately, however it
117 becomes important to supply this when there is only a callback.
118
119 \note A mapping can be associated both with a node property and a
120 callback. It is important however that \a type matches the type of the
121 property in this case. Note also that for properties of type QVariant (for
122 example, QParameter::value), the \a type is the type of the value stored in
123 the QVariant.
124
125 \note The \a callback pointer is expected to stay valid while any
126 associated animators are running.
127 */
128void QCallbackMapping::setCallback(int type, QAnimationCallback *callback, QAnimationCallback::Flags flags)
129{
130 Q_D(QCallbackMapping);
131 if (d->m_type != type) {
132 d->m_type = type;
133 auto e = Qt3DCore::QPropertyUpdatedChangePtr::create(id());
134 e->setPropertyName("type");
135 e->setValue(QVariant(d->m_type));
136 notifyObservers(e);
137 }
138 if (d->m_callback != callback) {
139 d->m_callback = callback;
140 auto e = Qt3DCore::QPropertyUpdatedChangePtr::create(id());
141 e->setPropertyName("callback");
142 e->setValue(QVariant::fromValue(static_cast<void *>(d->m_callback)));
143 notifyObservers(e);
144 }
145 if (d->m_callbackFlags != flags) {
146 d->m_callbackFlags = flags;
147 auto e = Qt3DCore::QPropertyUpdatedChangePtr::create(id());
148 e->setPropertyName("callbackFlags");
149 e->setValue(QVariant::fromValue(int(d->m_callbackFlags)));
150 notifyObservers(e);
151 }
152}
153
154Qt3DCore::QNodeCreatedChangeBasePtr QCallbackMapping::createNodeCreationChange() const
155{
156 auto creationChange = QChannelMappingCreatedChangePtr<QCallbackMappingData>::create(this);
157 auto &data = creationChange->data;
158 Q_D(const QCallbackMapping);
159 data.channelName = d->m_channelName;
160 data.type = d->m_type;
161 data.callback = d->m_callback;
162 data.callbackFlags = d->m_callbackFlags;
163 return creationChange;
164}
165
166} // namespace Qt3DAnimation
167
168QT_END_NAMESPACE
169