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