summaryrefslogtreecommitdiffstats
path: root/src/animation/frontend
diff options
context:
space:
mode:
authorLaszlo Agocs <laszlo.agocs@qt.io>2017-06-15 15:17:04 +0200
committerLaszlo Agocs <laszlo.agocs@qt.io>2017-06-23 10:05:22 +0000
commit6f40494310dab964600375f04391f21b4eccdb44 (patch)
treea2dc6542a70672c3bf901a36723cff35089b6b11 /src/animation/frontend
parent11f092ce7c555814baeffc204f7fadc0f36fdcbb (diff)
Add support for animation callbacks
Let QChannelMapping optionally specify a callback, which then gets invoked either directly on a worker thread or the gui thread. Change-Id: I871776f47cc921ceb254f9dc1a7b89ae5c5618d8 Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/animation/frontend')
-rw-r--r--src/animation/frontend/frontend.pri7
-rw-r--r--src/animation/frontend/qanimationcallback.h66
-rw-r--r--src/animation/frontend/qanimationcallbacktrigger.cpp54
-rw-r--r--src/animation/frontend/qanimationcallbacktrigger_p.h85
-rw-r--r--src/animation/frontend/qchannelmapping.cpp60
-rw-r--r--src/animation/frontend/qchannelmapping.h4
-rw-r--r--src/animation/frontend/qchannelmapping_p.h5
-rw-r--r--src/animation/frontend/qclipanimator.cpp11
-rw-r--r--src/animation/frontend/qclipanimator.h1
9 files changed, 291 insertions, 2 deletions
diff --git a/src/animation/frontend/frontend.pri b/src/animation/frontend/frontend.pri
index 9ea438395..7f397462b 100644
--- a/src/animation/frontend/frontend.pri
+++ b/src/animation/frontend/frontend.pri
@@ -44,7 +44,9 @@ HEADERS += \
$$PWD/qchannelcomponent.h \
$$PWD/qkeyframe.h \
$$PWD/qanimationclip.h \
- $$PWD/qanimationclip_p.h
+ $$PWD/qanimationclip_p.h \
+ $$PWD/qanimationcallback.h \
+ $$PWD/qanimationcallbacktrigger_p.h
SOURCES += \
$$PWD/qanimationaspect.cpp \
@@ -71,6 +73,7 @@ SOURCES += \
$$PWD/qchannel.cpp \
$$PWD/qchannelcomponent.cpp \
$$PWD/qkeyframe.cpp \
- $$PWD/qanimationclip.cpp
+ $$PWD/qanimationclip.cpp \
+ $$PWD/qanimationcallbacktrigger.cpp
INCLUDEPATH += $$PWD
diff --git a/src/animation/frontend/qanimationcallback.h b/src/animation/frontend/qanimationcallback.h
new file mode 100644
index 000000000..63e094918
--- /dev/null
+++ b/src/animation/frontend/qanimationcallback.h
@@ -0,0 +1,66 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
+**
+** This file is part of the Qt3D module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL3$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPLv3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or later as published by the Free
+** Software Foundation and appearing in the file LICENSE.GPL included in
+** the packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 2.0 requirements will be
+** met: http://www.gnu.org/licenses/gpl-2.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QT3DANIMATION_QANIMATIONCALLBACK_H
+#define QT3DANIMATION_QANIMATIONCALLBACK_H
+
+#include <Qt3DAnimation/qt3danimation_global.h>
+#include <Qt3DCore/qnode.h>
+
+QT_BEGIN_NAMESPACE
+
+namespace Qt3DAnimation {
+
+class QT3DANIMATIONSHARED_EXPORT QAnimationCallback
+{
+public:
+ enum Flag {
+ OnThreadPool = 0x01
+ };
+ Q_DECLARE_FLAGS(Flags, Flag)
+
+ virtual ~QAnimationCallback() { }
+
+ virtual void valueChanged(const QVariant &value) = 0;
+};
+
+} // namespace Qt3DAnimation
+
+Q_DECLARE_OPERATORS_FOR_FLAGS(Qt3DAnimation::QAnimationCallback::Flags)
+
+QT_END_NAMESPACE
+
+#endif // QT3DANIMATION_QANIMATIONCALLBACK_H
diff --git a/src/animation/frontend/qanimationcallbacktrigger.cpp b/src/animation/frontend/qanimationcallbacktrigger.cpp
new file mode 100644
index 000000000..83fccb5a4
--- /dev/null
+++ b/src/animation/frontend/qanimationcallbacktrigger.cpp
@@ -0,0 +1,54 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt3D module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qanimationcallbacktrigger_p.h"
+
+QT_BEGIN_NAMESPACE
+
+namespace Qt3DAnimation {
+
+QAnimationCallbackTrigger::QAnimationCallbackTrigger(Qt3DCore::QNodeId subjectId)
+ : Qt3DCore::QSceneChange(Qt3DCore::CallbackTriggered, subjectId),
+ m_callback(nullptr)
+{
+}
+
+} // namespace Qt3DAnimation
+
+QT_END_NAMESPACE
diff --git a/src/animation/frontend/qanimationcallbacktrigger_p.h b/src/animation/frontend/qanimationcallbacktrigger_p.h
new file mode 100644
index 000000000..9a05d421f
--- /dev/null
+++ b/src/animation/frontend/qanimationcallbacktrigger_p.h
@@ -0,0 +1,85 @@
+/****************************************************************************
+**
+** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt3D module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QT3DANIMATION_QANIMATIONCALLBACKTRIGGER_P_H
+#define QT3DANIMATION_QANIMATIONCALLBACKTRIGGER_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of other Qt classes. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <Qt3DAnimation/qt3danimation_global.h>
+#include <Qt3DAnimation/qanimationcallback.h>
+#include <Qt3DCore/qscenechange.h>
+#include <QtCore/qsharedpointer.h>
+
+QT_BEGIN_NAMESPACE
+
+namespace Qt3DAnimation {
+
+class Q_AUTOTEST_EXPORT QAnimationCallbackTrigger : public Qt3DCore::QSceneChange
+{
+public:
+ QAnimationCallbackTrigger(Qt3DCore::QNodeId subjectId);
+
+ void setCallback(QAnimationCallback *callback) { m_callback = callback; }
+ QAnimationCallback *callback() const { return m_callback; }
+
+ void setValue(const QVariant &value) { m_value = value; }
+ QVariant value() const { return m_value; }
+
+private:
+ QAnimationCallback *m_callback;
+ QVariant m_value;
+};
+
+typedef QSharedPointer<QAnimationCallbackTrigger> QAnimationCallbackTriggerPtr;
+
+} // namespace Qt3DAnimation
+
+QT_END_NAMESPACE
+
+#endif // QT3DANIMATION_QANIMATIONCALLBACKTRIGGER_P_H
diff --git a/src/animation/frontend/qchannelmapping.cpp b/src/animation/frontend/qchannelmapping.cpp
index faa77f5db..ec18332c7 100644
--- a/src/animation/frontend/qchannelmapping.cpp
+++ b/src/animation/frontend/qchannelmapping.cpp
@@ -53,6 +53,8 @@ QChannelMappingPrivate::QChannelMappingPrivate()
, m_property()
, m_propertyName(nullptr)
, m_type(static_cast<int>(QVariant::Invalid))
+ , m_callback(nullptr)
+ , m_callbackFlags(0)
{
}
@@ -148,6 +150,12 @@ QString QChannelMapping::property() const
return d->m_property;
}
+QAnimationCallback *QChannelMapping::callback() const
+{
+ Q_D(const QChannelMapping);
+ return d->m_callback;
+}
+
void QChannelMapping::setChannelName(const QString &channelName)
{
Q_D(QChannelMapping);
@@ -190,6 +198,56 @@ void QChannelMapping::setProperty(const QString &property)
d->updatePropertyNameAndType();
}
+/*!
+ Associates a \a callback object with this channel mapping.
+
+ Such mappings do not have to have a target object and property name. When
+ the \a callback object is set, every change in the animated value will lead
+ to invoking the callback's
+ \l{QAnimationCallback::onValueChanged()}{onValueChanged()} function either
+ on the gui/main thread, or directly on one of the thread pool's worker
+ thread. This is controlled by \a flags.
+
+ \a type specifies the type (for example, QVariant::Vector3D,
+ QVariant::Color, or QMetaType::Float) of the animated value. When animating
+ node properties this does not need to be provided separately, however it
+ becomes important to supply this when there is only a callback.
+
+ \note A mapping can be associated both with a node property and a
+ callback. It is important however that \a type matches the type of the
+ property in this case. Note also that for properties of type QVariant (for
+ example, QParameter::value), the \a type is the type of the value stored in
+ the QVariant.
+
+ \note The \a callback pointer is expected to stay valid while any
+ associated animators are running.
+ */
+void QChannelMapping::setCallback(int type, QAnimationCallback *callback, QAnimationCallback::Flags flags)
+{
+ Q_D(QChannelMapping);
+ if (d->m_type != type) {
+ d->m_type = type;
+ auto e = Qt3DCore::QPropertyUpdatedChangePtr::create(id());
+ e->setPropertyName("type");
+ e->setValue(QVariant(d->m_type));
+ notifyObservers(e);
+ }
+ if (d->m_callback != callback) {
+ d->m_callback = callback;
+ auto e = Qt3DCore::QPropertyUpdatedChangePtr::create(id());
+ e->setPropertyName("callback");
+ e->setValue(QVariant::fromValue(static_cast<void *>(d->m_callback)));
+ notifyObservers(e);
+ }
+ if (d->m_callbackFlags != flags) {
+ d->m_callbackFlags = flags;
+ auto e = Qt3DCore::QPropertyUpdatedChangePtr::create(id());
+ e->setPropertyName("callbackFlags");
+ e->setValue(QVariant::fromValue(int(d->m_callbackFlags)));
+ notifyObservers(e);
+ }
+}
+
Qt3DCore::QNodeCreatedChangeBasePtr QChannelMapping::createNodeCreationChange() const
{
auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QChannelMappingData>::create(this);
@@ -200,6 +258,8 @@ Qt3DCore::QNodeCreatedChangeBasePtr QChannelMapping::createNodeCreationChange()
data.property = d->m_property;
data.type = d->m_type;
data.propertyName = d->m_propertyName;
+ data.callback = d->m_callback;
+ data.callbackFlags = d->m_callbackFlags;
return creationChange;
}
diff --git a/src/animation/frontend/qchannelmapping.h b/src/animation/frontend/qchannelmapping.h
index d768298fe..d14015b9f 100644
--- a/src/animation/frontend/qchannelmapping.h
+++ b/src/animation/frontend/qchannelmapping.h
@@ -38,6 +38,7 @@
#define QT3DANIMATION_QCHANNELMAPPING_H
#include <Qt3DAnimation/qt3danimation_global.h>
+#include <Qt3DAnimation/qanimationcallback.h>
#include <Qt3DCore/qnode.h>
QT_BEGIN_NAMESPACE
@@ -60,6 +61,9 @@ public:
QString channelName() const;
Qt3DCore::QNode *target() const;
QString property() const;
+ QAnimationCallback *callback() const;
+
+ void setCallback(int type, QAnimationCallback *callback, QAnimationCallback::Flags flags);
public Q_SLOTS:
void setChannelName(const QString &channelName);
diff --git a/src/animation/frontend/qchannelmapping_p.h b/src/animation/frontend/qchannelmapping_p.h
index 2c48b0485..95b300888 100644
--- a/src/animation/frontend/qchannelmapping_p.h
+++ b/src/animation/frontend/qchannelmapping_p.h
@@ -49,6 +49,7 @@
//
#include <Qt3DCore/private/qnode_p.h>
+#include <Qt3DAnimation/qanimationcallback.h>
QT_BEGIN_NAMESPACE
@@ -68,6 +69,8 @@ public:
QString m_property;
const char *m_propertyName;
int m_type;
+ QAnimationCallback *m_callback;
+ QAnimationCallback::Flags m_callbackFlags;
};
struct QChannelMappingData
@@ -77,6 +80,8 @@ struct QChannelMappingData
QString property;
int type;
const char *propertyName;
+ QAnimationCallback *callback;
+ QAnimationCallback::Flags callbackFlags;
};
} // namespace Qt3DAnimation
diff --git a/src/animation/frontend/qclipanimator.cpp b/src/animation/frontend/qclipanimator.cpp
index eb6c80aee..da6534d16 100644
--- a/src/animation/frontend/qclipanimator.cpp
+++ b/src/animation/frontend/qclipanimator.cpp
@@ -41,6 +41,7 @@
#include "qclipanimator_p.h"
#include <Qt3DAnimation/qabstractanimationclip.h>
#include <Qt3DAnimation/qchannelmapper.h>
+#include <Qt3DAnimation/private/qanimationcallbacktrigger_p.h>
QT_BEGIN_NAMESPACE
@@ -165,6 +166,16 @@ Qt3DCore::QNodeCreatedChangeBasePtr QClipAnimator::createNodeCreationChange() co
return creationChange;
}
+/*! \internal */
+void QClipAnimator::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &change)
+{
+ if (change->type() == Qt3DCore::CallbackTriggered) {
+ QAnimationCallbackTriggerPtr callbackTrigger = qSharedPointerCast<Qt3DAnimation::QAnimationCallbackTrigger>(change);
+ if (callbackTrigger->callback())
+ callbackTrigger->callback()->valueChanged(callbackTrigger->value());
+ }
+}
+
} // namespace Qt3DAnimation
QT_END_NAMESPACE
diff --git a/src/animation/frontend/qclipanimator.h b/src/animation/frontend/qclipanimator.h
index 311ac4ab0..7b06f9878 100644
--- a/src/animation/frontend/qclipanimator.h
+++ b/src/animation/frontend/qclipanimator.h
@@ -70,6 +70,7 @@ Q_SIGNALS:
protected:
QClipAnimator(QClipAnimatorPrivate &dd, Qt3DCore::QNode *parent = nullptr);
+ void sceneChangeEvent(const Qt3DCore::QSceneChangePtr &change) override;
private:
Q_DECLARE_PRIVATE(QClipAnimator)