| 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 "qlerpclipblend.h" |
| 38 | #include "qlerpclipblend_p.h" |
| 39 | #include <Qt3DAnimation/qclipblendnodecreatedchange.h> |
| 40 | |
| 41 | QT_BEGIN_NAMESPACE |
| 42 | |
| 43 | namespace Qt3DAnimation { |
| 44 | |
| 45 | /*! |
| 46 | \qmltype LerpClipBlend |
| 47 | \instantiates Qt3DAnimation::QLerpClipBlend |
| 48 | \inqmlmodule Qt3D.Animation |
| 49 | |
| 50 | \brief Performs a linear interpolation of two animation clips based on a |
| 51 | normalized factor. |
| 52 | |
| 53 | \since 5.9 |
| 54 | |
| 55 | LerpClipBlend can be useful to create advanced animation effects based on |
| 56 | individual animation clips. For instance, given a player character,, lerp |
| 57 | blending could be used to combine a walking animation clip with an injured |
| 58 | animation clip based on a blend factor that increases the more the player |
| 59 | gets injured. This would then allow with blend factor == 0 to have a non |
| 60 | injured walking player, with blend factor == 1 a fully injured player, with |
| 61 | blend factor == 0.5 a partially walking and injured player. |
| 62 | |
| 63 | \sa BlendedClipAnimator |
| 64 | */ |
| 65 | |
| 66 | /*! |
| 67 | \class Qt3DAnimation::QLerpClipBlend |
| 68 | \inmodule Qt3DAnimation |
| 69 | \inherits Qt3DAnimation::QAbstractClipBlendNode |
| 70 | |
| 71 | \brief Performs a linear interpolation of two animation clips based on a |
| 72 | normalized factor. |
| 73 | |
| 74 | \since 5.9 |
| 75 | |
| 76 | QLerpClipBlend can be useful to create advanced animation effects based on |
| 77 | individual animation clips. For instance, given a player character, lerp |
| 78 | blending could be used to combine a walking animation clip with an injured |
| 79 | animation clip based on a blend factor that increases the more the player |
| 80 | gets injured. This would then allow with blend factor == 0 to have a non |
| 81 | injured walking player, with blend factor == 1 a fully injured player, with |
| 82 | blend factor == 0.5 a partially walking and injured player. |
| 83 | |
| 84 | \sa QBlendedClipAnimator |
| 85 | */ |
| 86 | |
| 87 | QLerpClipBlendPrivate::QLerpClipBlendPrivate() |
| 88 | : QAbstractClipBlendNodePrivate() |
| 89 | , m_startClip(nullptr) |
| 90 | , m_endClip(nullptr) |
| 91 | , m_blendFactor(0.0f) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | QLerpClipBlend::QLerpClipBlend(Qt3DCore::QNode *parent) |
| 96 | : QAbstractClipBlendNode(*new QLerpClipBlendPrivate(), parent) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | QLerpClipBlend::QLerpClipBlend(QLerpClipBlendPrivate &dd, Qt3DCore::QNode *parent) |
| 101 | : QAbstractClipBlendNode(dd, parent) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | QLerpClipBlend::~QLerpClipBlend() |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | Qt3DCore::QNodeCreatedChangeBasePtr QLerpClipBlend::createNodeCreationChange() const |
| 110 | { |
| 111 | Q_D(const QLerpClipBlend); |
| 112 | auto creationChange = QClipBlendNodeCreatedChangePtr<QLerpClipBlendData>::create(arguments: this); |
| 113 | QLerpClipBlendData &data = creationChange->data; |
| 114 | data.startClipId = Qt3DCore::qIdForNode(node: d->m_startClip); |
| 115 | data.endClipId = Qt3DCore::qIdForNode(node: d->m_endClip); |
| 116 | data.blendFactor = d->m_blendFactor; |
| 117 | return creationChange; |
| 118 | } |
| 119 | |
| 120 | /*! |
| 121 | \qmlproperty real LerpClipBlend::blendFactor |
| 122 | |
| 123 | Specifies the blending factor between 0 and 1 to control the blending of |
| 124 | two animation clips. |
| 125 | */ |
| 126 | /*! |
| 127 | \property QLerpClipBlend::blendFactor |
| 128 | |
| 129 | Specifies the blending factor between 0 and 1 to control the blending of |
| 130 | two animation clips. |
| 131 | */ |
| 132 | float QLerpClipBlend::blendFactor() const |
| 133 | { |
| 134 | Q_D(const QLerpClipBlend); |
| 135 | return d->m_blendFactor; |
| 136 | } |
| 137 | |
| 138 | /*! |
| 139 | \qmlproperty AbstractClipBlendNode LerpClipBlend::startClip |
| 140 | |
| 141 | Holds the sub-tree that should be used as the start clip for this |
| 142 | lerp blend node. That is, the clip returned by this blend node when |
| 143 | the blendFactor is set to a value of 0. |
| 144 | */ |
| 145 | /*! |
| 146 | \property QLerpClipBlend::startClip |
| 147 | |
| 148 | Holds the sub-tree that should be used as the start clip for this |
| 149 | lerp blend node. That is, the clip returned by this blend node when |
| 150 | the blendFactor is set to a value of 0. |
| 151 | */ |
| 152 | Qt3DAnimation::QAbstractClipBlendNode *QLerpClipBlend::startClip() const |
| 153 | { |
| 154 | Q_D(const QLerpClipBlend); |
| 155 | return d->m_startClip; |
| 156 | } |
| 157 | |
| 158 | /*! |
| 159 | \qmlproperty AbstractClipBlendNode LerpClipBlend::endClip |
| 160 | |
| 161 | Holds the sub-tree that should be used as the end clip for this |
| 162 | lerp blend node. That is, the clip returned by this blend node when |
| 163 | the blendFactor is set to a value of 1. |
| 164 | */ |
| 165 | /*! |
| 166 | \property QLerpClipBlend::endClip |
| 167 | |
| 168 | Holds the sub-tree that should be used as the start clip for this |
| 169 | lerp blend node. That is, the clip returned by this blend node when |
| 170 | the blendFactor is set to a value of 1. |
| 171 | */ |
| 172 | Qt3DAnimation::QAbstractClipBlendNode *QLerpClipBlend::endClip() const |
| 173 | { |
| 174 | Q_D(const QLerpClipBlend); |
| 175 | return d->m_endClip; |
| 176 | } |
| 177 | |
| 178 | void QLerpClipBlend::setBlendFactor(float blendFactor) |
| 179 | { |
| 180 | Q_D(QLerpClipBlend); |
| 181 | |
| 182 | if (d->m_blendFactor == blendFactor) |
| 183 | return; |
| 184 | |
| 185 | d->m_blendFactor = blendFactor; |
| 186 | emit blendFactorChanged(blendFactor); |
| 187 | } |
| 188 | |
| 189 | void QLerpClipBlend::setStartClip(Qt3DAnimation::QAbstractClipBlendNode *startClip) |
| 190 | { |
| 191 | Q_D(QLerpClipBlend); |
| 192 | if (d->m_startClip == startClip) |
| 193 | return; |
| 194 | |
| 195 | if (d->m_startClip) |
| 196 | d->unregisterDestructionHelper(node: d->m_startClip); |
| 197 | |
| 198 | if (startClip && !startClip->parent()) |
| 199 | startClip->setParent(this); |
| 200 | d->m_startClip = startClip; |
| 201 | |
| 202 | // Ensures proper bookkeeping |
| 203 | if (d->m_startClip) |
| 204 | d->registerDestructionHelper(node: d->m_startClip, func: &QLerpClipBlend::setStartClip, d->m_startClip); |
| 205 | emit startClipChanged(startClip); |
| 206 | } |
| 207 | |
| 208 | void QLerpClipBlend::setEndClip(Qt3DAnimation::QAbstractClipBlendNode *endClip) |
| 209 | { |
| 210 | Q_D(QLerpClipBlend); |
| 211 | if (d->m_endClip == endClip) |
| 212 | return; |
| 213 | |
| 214 | if (d->m_endClip) |
| 215 | d->unregisterDestructionHelper(node: d->m_endClip); |
| 216 | |
| 217 | if (endClip && !endClip->parent()) |
| 218 | endClip->setParent(this); |
| 219 | d->m_endClip = endClip; |
| 220 | |
| 221 | // Ensures proper bookkeeping |
| 222 | if (d->m_endClip) |
| 223 | d->registerDestructionHelper(node: d->m_endClip, func: &QLerpClipBlend::setEndClip, d->m_endClip); |
| 224 | emit endClipChanged(endClip); |
| 225 | } |
| 226 | |
| 227 | } // Qt3DAnimation |
| 228 | |
| 229 | QT_END_NAMESPACE |
| 230 | |