1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: http://www.qt.io/licensing/ |
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 "qmorphtarget.h" |
38 | #include "Qt3DAnimation/private/qmorphtarget_p.h" |
39 | |
40 | QT_BEGIN_NAMESPACE |
41 | |
42 | namespace Qt3DAnimation { |
43 | |
44 | /*! |
45 | \class Qt3DAnimation::QMorphTarget |
46 | \brief A class providing morph targets to blend-shape animation. |
47 | \inmodule Qt3DAnimation |
48 | \since 5.9 |
49 | \inherits QObject |
50 | |
51 | A Qt3DAnimation::QMorphTarget class is a convenience class, which provides a list |
52 | of \l {Qt3DRender::QAttribute} {QAttributes}, which the QMorphingAnimation uses |
53 | to animate geometry. A QMorphTarget can also be created based on existing |
54 | \l Qt3DRender::QGeometry. |
55 | |
56 | */ |
57 | /*! |
58 | \qmltype MorphTarget |
59 | \brief A type providing morph targets to blend-shape animation. |
60 | \inqmlmodule Qt3D.Animation |
61 | \since 5.9 |
62 | \inherits QtObject |
63 | \instantiates Qt3DAnimation::QMorphTarget |
64 | |
65 | A MorphTarget type is a convenience type, which provides a list |
66 | of \l {Qt3D.Render::Attribute} {Attributes}, which the MorphingAnimation uses |
67 | to animate geometry. A MorphTarget can also be created based on existing |
68 | \l {Qt3D.Render::Geometry}{Geometry}. |
69 | |
70 | */ |
71 | |
72 | /*! |
73 | \property Qt3DAnimation::QMorphTarget::attributeNames |
74 | Holds a list of attribute names contained in the morph target. |
75 | \readonly |
76 | */ |
77 | |
78 | /*! |
79 | \qmlproperty list<string> MorphTarget::attributeNames |
80 | Holds a list of attribute names contained in the morph target. |
81 | \readonly |
82 | */ |
83 | /*! |
84 | \qmlproperty list<Attribute> MorphTarget::attributes |
85 | Holds the list of attributes in the morph target. |
86 | */ |
87 | /*! |
88 | \qmlmethod MorphTarget Qt3D.Animation::MorphTarget::fromGeometry(geometry, stringList) |
89 | Returns a morph target based on the attributes defined by the given \a stringList from |
90 | the given \a geometry. |
91 | */ |
92 | |
93 | QMorphTargetPrivate::QMorphTargetPrivate() |
94 | : QObjectPrivate() |
95 | { |
96 | |
97 | } |
98 | |
99 | void QMorphTargetPrivate::updateAttributeNames() |
100 | { |
101 | m_attributeNames.clear(); |
102 | for (const Qt3DRender::QAttribute *attr : qAsConst(t&: m_targetAttributes)) |
103 | m_attributeNames.push_back(t: attr->name()); |
104 | } |
105 | |
106 | /*! |
107 | Constructs a QMorphTarget with given \a parent. |
108 | */ |
109 | QMorphTarget::QMorphTarget(QObject *parent) |
110 | : QObject(*new QMorphTargetPrivate, parent) |
111 | { |
112 | |
113 | } |
114 | |
115 | /*! |
116 | Returns a list of attributes contained in the morph target. |
117 | */ |
118 | QVector<Qt3DRender::QAttribute *> QMorphTarget::attributeList() const |
119 | { |
120 | Q_D(const QMorphTarget); |
121 | return d->m_targetAttributes; |
122 | } |
123 | |
124 | QStringList QMorphTarget::attributeNames() const |
125 | { |
126 | Q_D(const QMorphTarget); |
127 | return d->m_attributeNames; |
128 | } |
129 | |
130 | /*! |
131 | Sets \a attributes to the morph target. Old attributes are cleared. |
132 | */ |
133 | void QMorphTarget::setAttributes(const QVector<Qt3DRender::QAttribute *> &attributes) |
134 | { |
135 | Q_D(QMorphTarget); |
136 | d->m_targetAttributes = attributes; |
137 | d->m_attributeNames.clear(); |
138 | for (const Qt3DRender::QAttribute *attr : attributes) |
139 | d->m_attributeNames.push_back(t: attr->name()); |
140 | |
141 | emit attributeNamesChanged(attributeNames: d->m_attributeNames); |
142 | } |
143 | |
144 | /*! |
145 | Adds an \a attribute the morph target. An attribute with the same |
146 | name must not have been added previously to the morph target. |
147 | */ |
148 | void QMorphTarget::addAttribute(Qt3DRender::QAttribute *attribute) |
149 | { |
150 | Q_D(QMorphTarget); |
151 | for (const Qt3DRender::QAttribute *attr : qAsConst(t&: d->m_targetAttributes)) { |
152 | if (attr->name() == attribute->name()) |
153 | return; |
154 | } |
155 | d->m_targetAttributes.push_back(t: attribute); |
156 | d->m_attributeNames.push_back(t: attribute->name()); |
157 | emit attributeNamesChanged(attributeNames: d->m_attributeNames); |
158 | } |
159 | |
160 | /*! |
161 | Removes an \a attribute from the morph target. |
162 | */ |
163 | void QMorphTarget::removeAttribute(Qt3DRender::QAttribute *attribute) |
164 | { |
165 | Q_D(QMorphTarget); |
166 | if (d->m_targetAttributes.contains(t: attribute)) { |
167 | d->m_targetAttributes.removeAll(t: attribute); |
168 | d->updateAttributeNames(); |
169 | emit attributeNamesChanged(attributeNames: d->m_attributeNames); |
170 | } |
171 | } |
172 | |
173 | /*! |
174 | Returns a morph target based on the \a attributes in the given \a geometry. |
175 | */ |
176 | QMorphTarget *QMorphTarget::fromGeometry(Qt3DRender::QGeometry *geometry, const QStringList &attributes) |
177 | { |
178 | QMorphTarget *target = new QMorphTarget(); |
179 | const auto geometryAttributes = geometry->attributes(); |
180 | for (Qt3DRender::QAttribute *attr : geometryAttributes) { |
181 | if (attributes.contains(str: attr->name())) |
182 | target->addAttribute(attribute: attr); |
183 | } |
184 | return target; |
185 | } |
186 | |
187 | } // Qt3DAnimation |
188 | |
189 | QT_END_NAMESPACE |
190 | |