| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 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 https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qskeletonloader.h" |
| 41 | #include "qskeletonloader_p.h" |
| 42 | #include <Qt3DCore/qjoint.h> |
| 43 | #include <Qt3DCore/qpropertyupdatedchange.h> |
| 44 | #include <Qt3DCore/private/qskeletoncreatedchange_p.h> |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | namespace Qt3DCore { |
| 49 | |
| 50 | QSkeletonLoaderPrivate::QSkeletonLoaderPrivate() |
| 51 | : QAbstractSkeletonPrivate() |
| 52 | , m_source() |
| 53 | , m_createJoints(false) |
| 54 | , m_status(QSkeletonLoader::NotReady) |
| 55 | , m_rootJoint(nullptr) |
| 56 | { |
| 57 | m_type = QSkeletonCreatedChangeBase::SkeletonLoader; |
| 58 | } |
| 59 | |
| 60 | void QSkeletonLoaderPrivate::setStatus(QSkeletonLoader::Status status) |
| 61 | { |
| 62 | Q_Q(QSkeletonLoader); |
| 63 | if (status != m_status) { |
| 64 | m_status = status; |
| 65 | const bool blocked = q->blockNotifications(block: true); |
| 66 | emit q->statusChanged(status: m_status); |
| 67 | q->blockNotifications(block: blocked); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void QSkeletonLoaderPrivate::setRootJoint(QJoint *rootJoint) |
| 72 | { |
| 73 | Q_Q(QSkeletonLoader); |
| 74 | if (rootJoint == m_rootJoint) |
| 75 | return; |
| 76 | |
| 77 | if (m_rootJoint) |
| 78 | unregisterDestructionHelper(node: m_rootJoint); |
| 79 | |
| 80 | if (rootJoint && !rootJoint->parent()) |
| 81 | rootJoint->setParent(q); |
| 82 | |
| 83 | m_rootJoint = rootJoint; |
| 84 | |
| 85 | // Ensures proper bookkeeping |
| 86 | if (m_rootJoint) |
| 87 | registerPrivateDestructionHelper(node: m_rootJoint, func: &QSkeletonLoaderPrivate::setRootJoint); |
| 88 | |
| 89 | emit q->rootJointChanged(rootJoint: m_rootJoint); |
| 90 | } |
| 91 | |
| 92 | /*! |
| 93 | \qmltype SkeletonLoader |
| 94 | \inqmlmodule Qt3D.Core |
| 95 | \inherits AbstractSkeleton |
| 96 | \instantiates Qt3DCore::QSkeletonLoader |
| 97 | \since 5.10 |
| 98 | \brief Used to load a skeleton of joints from file. |
| 99 | |
| 100 | Use SkeletonLoader if you wish to load a whole skeleton from file rather |
| 101 | than creating the joints yourself using Skeleton and Joints. Creating a |
| 102 | skeleton and binding the vertices of a mesh to the skeleton is most easily |
| 103 | performed in a 3D digital content creation tool such as Blender. The |
| 104 | resulting skeleton and mesh can then be exported in a suitable format such |
| 105 | as glTF 2 for consumption by Qt 3D. |
| 106 | */ |
| 107 | |
| 108 | /*! |
| 109 | \qmlproperty url SkeletonLoader::source |
| 110 | |
| 111 | Holds the source url from which to load the skeleton. |
| 112 | */ |
| 113 | |
| 114 | /*! |
| 115 | \qmlproperty SkeletonLoader.Status SkeletonLoader::status |
| 116 | |
| 117 | Holds the current status of skeleton loading. |
| 118 | */ |
| 119 | |
| 120 | /*! |
| 121 | \class Qt3DCore::QSkeletonLoader |
| 122 | \inmodule Qt3DCore |
| 123 | \inherits Qt3DCore::QAbstractSkeleton |
| 124 | \since 5.10 |
| 125 | \brief Used to load a skeleton of joints from file. |
| 126 | |
| 127 | Use SkeletonLoader if you wish to load a whole skeleton from file rather |
| 128 | than creating the joints yourself using Skeleton and Joints. Creating a |
| 129 | skeleton and binding the vertices of a mesh to the skeleton is most easily |
| 130 | performed in a 3D digital content creation tool such as Blender. The |
| 131 | resulting skeleton and mesh can then be exported in a suitable format such |
| 132 | as glTF 2 for consumption by Qt 3D. |
| 133 | */ |
| 134 | |
| 135 | /*! |
| 136 | \enum QSkeletonLoader::Status |
| 137 | |
| 138 | This enum identifies the status of skeleton. |
| 139 | |
| 140 | \value NotReady The skeleton has not been loaded yet |
| 141 | \value Ready The skeleton was successfully loaded |
| 142 | \value Error An error occurred while loading the skeleton |
| 143 | */ |
| 144 | /*! |
| 145 | \property Qt3DCore::QSkeletonLoader::createJointsEnabled |
| 146 | |
| 147 | \brief A boolean to indicate whether createJoints is enabled or not. |
| 148 | */ |
| 149 | /*! |
| 150 | Constructs a new QSkeletonLoader with \a parent. |
| 151 | */ |
| 152 | QSkeletonLoader::QSkeletonLoader(Qt3DCore::QNode *parent) |
| 153 | : QAbstractSkeleton(*new QSkeletonLoaderPrivate, parent) |
| 154 | { |
| 155 | } |
| 156 | |
| 157 | /*! |
| 158 | Constructs a new QSkeletonLoader with \a parent and sets the \a source. |
| 159 | */ |
| 160 | QSkeletonLoader::QSkeletonLoader(const QUrl &source, QNode *parent) |
| 161 | : QAbstractSkeleton(*new QSkeletonLoaderPrivate, parent) |
| 162 | { |
| 163 | setSource(source); |
| 164 | } |
| 165 | |
| 166 | /*! \internal */ |
| 167 | QSkeletonLoader::QSkeletonLoader(QSkeletonLoaderPrivate &dd, Qt3DCore::QNode *parent) |
| 168 | : QAbstractSkeleton(dd, parent) |
| 169 | { |
| 170 | } |
| 171 | |
| 172 | /*! \internal */ |
| 173 | QSkeletonLoader::~QSkeletonLoader() |
| 174 | { |
| 175 | } |
| 176 | |
| 177 | /*! |
| 178 | \property Qt3DCore::QSkeletonLoader::source |
| 179 | |
| 180 | Holds the source url from which to load the skeleton. |
| 181 | */ |
| 182 | QUrl QSkeletonLoader::source() const |
| 183 | { |
| 184 | Q_D(const QSkeletonLoader); |
| 185 | return d->m_source; |
| 186 | } |
| 187 | |
| 188 | /*! |
| 189 | \property Qt3DCore::QSkeletonLoader::status |
| 190 | |
| 191 | Holds the current status of skeleton loading. |
| 192 | */ |
| 193 | QSkeletonLoader::Status QSkeletonLoader::status() const |
| 194 | { |
| 195 | Q_D(const QSkeletonLoader); |
| 196 | return d->m_status; |
| 197 | } |
| 198 | |
| 199 | /*! |
| 200 | Returns a boolean indicating whether CreateJoints |
| 201 | is enabled or not. |
| 202 | The default value is \c false. |
| 203 | */ |
| 204 | bool QSkeletonLoader::isCreateJointsEnabled() const |
| 205 | { |
| 206 | Q_D(const QSkeletonLoader); |
| 207 | return d->m_createJoints; |
| 208 | } |
| 209 | /*! |
| 210 | Returns the root joint of the hierarchy of joints forming the skeleton. |
| 211 | */ |
| 212 | Qt3DCore::QJoint *QSkeletonLoader::rootJoint() const |
| 213 | { |
| 214 | Q_D(const QSkeletonLoader); |
| 215 | return d->m_rootJoint; |
| 216 | } |
| 217 | |
| 218 | void QSkeletonLoader::setSource(const QUrl &source) |
| 219 | { |
| 220 | Q_D(QSkeletonLoader); |
| 221 | if (d->m_source == source) |
| 222 | return; |
| 223 | |
| 224 | d->m_source = source; |
| 225 | emit sourceChanged(source); |
| 226 | } |
| 227 | |
| 228 | void QSkeletonLoader::setCreateJointsEnabled(bool createJoints) |
| 229 | { |
| 230 | Q_D(QSkeletonLoader); |
| 231 | if (d->m_createJoints == createJoints) |
| 232 | return; |
| 233 | |
| 234 | d->m_createJoints = createJoints; |
| 235 | emit createJointsEnabledChanged(createJointsEnabled: createJoints); |
| 236 | } |
| 237 | |
| 238 | void QSkeletonLoader::setRootJoint(QJoint *rootJoint) |
| 239 | { |
| 240 | Q_D(QSkeletonLoader); |
| 241 | d->setRootJoint(rootJoint); |
| 242 | } |
| 243 | |
| 244 | /*! \internal */ |
| 245 | void QSkeletonLoader::sceneChangeEvent(const QSceneChangePtr &change) |
| 246 | { |
| 247 | QAbstractSkeleton::sceneChangeEvent(change); |
| 248 | } |
| 249 | |
| 250 | /*! \internal */ |
| 251 | Qt3DCore::QNodeCreatedChangeBasePtr QSkeletonLoader::createNodeCreationChange() const |
| 252 | { |
| 253 | auto creationChange = QSkeletonCreatedChangePtr<QSkeletonLoaderData>::create(arguments: this); |
| 254 | auto &data = creationChange->data; |
| 255 | Q_D(const QSkeletonLoader); |
| 256 | data.source = d->m_source; |
| 257 | data.createJoints = d->m_createJoints; |
| 258 | return creationChange; |
| 259 | } |
| 260 | |
| 261 | } // namespace Qt3DCore |
| 262 | |
| 263 | QT_END_NAMESPACE |
| 264 | |