1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qmorphtarget.h"
5#include "Qt3DAnimation/private/qmorphtarget_p.h"
6
7QT_BEGIN_NAMESPACE
8
9namespace Qt3DAnimation {
10
11/*!
12 \class Qt3DAnimation::QMorphTarget
13 \brief A class providing morph targets to blend-shape animation.
14 \inmodule Qt3DAnimation
15 \since 5.9
16 \inherits QObject
17
18 A Qt3DAnimation::QMorphTarget class is a convenience class, which provides a list
19 of \l {Qt3DCore::QAttribute} {QAttributes}, which the QMorphingAnimation uses
20 to animate geometry. A QMorphTarget can also be created based on existing
21 \l Qt3DCore::QGeometry.
22
23*/
24/*!
25 \qmltype MorphTarget
26 \brief A type providing morph targets to blend-shape animation.
27 \inqmlmodule Qt3D.Animation
28 \since 5.9
29 \inherits QtObject
30 \instantiates Qt3DAnimation::QMorphTarget
31
32 A MorphTarget type is a convenience type, which provides a list
33 of \l {Qt3D.Core::Attribute} {Attributes}, which the MorphingAnimation uses
34 to animate geometry. A MorphTarget can also be created based on existing
35 \l {Qt3D.Core::Geometry}{Geometry}.
36
37*/
38
39/*!
40 \property Qt3DAnimation::QMorphTarget::attributeNames
41 Holds a list of attribute names contained in the morph target.
42 \readonly
43*/
44
45/*!
46 \qmlproperty list<string> MorphTarget::attributeNames
47 Holds a list of attribute names contained in the morph target.
48 \readonly
49*/
50/*!
51 \qmlproperty list<Attribute> MorphTarget::attributes
52 Holds the list of attributes in the morph target.
53*/
54/*!
55 \qmlmethod MorphTarget Qt3D.Animation::MorphTarget::fromGeometry(geometry, stringList)
56 Returns a morph target based on the attributes defined by the given \a stringList from
57 the given \a geometry.
58*/
59
60QMorphTargetPrivate::QMorphTargetPrivate()
61 : QObjectPrivate()
62{
63
64}
65
66void QMorphTargetPrivate::updateAttributeNames()
67{
68 m_attributeNames.clear();
69 for (const Qt3DCore::QAttribute *attr : std::as_const(t&: m_targetAttributes))
70 m_attributeNames.push_back(t: attr->name());
71}
72
73/*!
74 Constructs a QMorphTarget with given \a parent.
75*/
76QMorphTarget::QMorphTarget(QObject *parent)
77 : QObject(*new QMorphTargetPrivate, parent)
78{
79
80}
81
82/*!
83 Returns a list of attributes contained in the morph target.
84*/
85QList<Qt3DCore::QAttribute *> QMorphTarget::attributeList() const
86{
87 Q_D(const QMorphTarget);
88 return d->m_targetAttributes;
89}
90
91QStringList QMorphTarget::attributeNames() const
92{
93 Q_D(const QMorphTarget);
94 return d->m_attributeNames;
95}
96
97/*!
98 Sets \a attributes to the morph target. Old attributes are cleared.
99*/
100void QMorphTarget::setAttributes(const QList<Qt3DCore::QAttribute *> &attributes)
101{
102 Q_D(QMorphTarget);
103 d->m_targetAttributes = attributes;
104 d->m_attributeNames.clear();
105 for (const Qt3DCore::QAttribute *attr : attributes)
106 d->m_attributeNames.push_back(t: attr->name());
107
108 emit attributeNamesChanged(attributeNames: d->m_attributeNames);
109}
110
111/*!
112 Adds an \a attribute the morph target. An attribute with the same
113 name must not have been added previously to the morph target.
114*/
115void QMorphTarget::addAttribute(Qt3DCore::QAttribute *attribute)
116{
117 Q_D(QMorphTarget);
118 for (const Qt3DCore::QAttribute *attr : std::as_const(t&: d->m_targetAttributes)) {
119 if (attr->name() == attribute->name())
120 return;
121 }
122 d->m_targetAttributes.push_back(t: attribute);
123 d->m_attributeNames.push_back(t: attribute->name());
124 emit attributeNamesChanged(attributeNames: d->m_attributeNames);
125}
126
127/*!
128 Removes an \a attribute from the morph target.
129*/
130void QMorphTarget::removeAttribute(Qt3DCore::QAttribute *attribute)
131{
132 Q_D(QMorphTarget);
133 if (d->m_targetAttributes.contains(t: attribute)) {
134 d->m_targetAttributes.removeAll(t: attribute);
135 d->updateAttributeNames();
136 emit attributeNamesChanged(attributeNames: d->m_attributeNames);
137 }
138}
139
140/*!
141 Returns a morph target based on the \a attributes in the given \a geometry.
142*/
143QMorphTarget *QMorphTarget::fromGeometry(Qt3DCore::QGeometry *geometry, const QStringList &attributes)
144{
145 QMorphTarget *target = new QMorphTarget();
146 const auto geometryAttributes = geometry->attributes();
147 for (Qt3DCore::QAttribute *attr : geometryAttributes) {
148 if (attributes.contains(str: attr->name()))
149 target->addAttribute(attribute: attr);
150 }
151 return target;
152}
153
154} // Qt3DAnimation
155
156QT_END_NAMESPACE
157
158#include "moc_qmorphtarget.cpp"
159

source code of qt3d/src/animation/frontend/qmorphtarget.cpp