| 1 | // Copyright (C) 2018 The Qt Company Ltd. | 
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only | 
| 3 | |
| 4 | #include "bmshapelayer_p.h" | 
| 5 | |
| 6 | #include <QJsonObject> | 
| 7 | #include <QJsonArray> | 
| 8 | |
| 9 | |
| 10 | #include "bmconstants_p.h" | 
| 11 | #include "bmbase_p.h" | 
| 12 | #include "bmshape_p.h" | 
| 13 | #include "bmtrimpath_p.h" | 
| 14 | #include "bmbasictransform_p.h" | 
| 15 | #include "lottierenderer_p.h" | 
| 16 | |
| 17 | QT_BEGIN_NAMESPACE | 
| 18 | |
| 19 | BMShapeLayer::BMShapeLayer(const BMShapeLayer &other) | 
| 20 | : BMLayer(other) | 
| 21 | { | 
| 22 | m_maskProperties = other.m_maskProperties; | 
| 23 | m_layerTransform = new BMBasicTransform(*other.m_layerTransform); | 
| 24 | m_layerTransform->setParent(this); | 
| 25 | m_appliedTrim = other.m_appliedTrim; | 
| 26 | } | 
| 27 | |
| 28 | BMShapeLayer::BMShapeLayer(const QJsonObject &definition, const QVersionNumber &version) | 
| 29 | { | 
| 30 | m_type = BM_LAYER_SHAPE_IX; | 
| 31 | m_version = version; | 
| 32 | |
| 33 | BMLayer::parse(definition); | 
| 34 | if (m_hidden) | 
| 35 | return; | 
| 36 | |
| 37 |     qCDebug(lcLottieQtBodymovinParser) << "BMShapeLayer::BMShapeLayer()"  | 
| 38 | << m_name; | 
| 39 | |
| 40 |     QJsonArray maskProps = definition.value(key: QLatin1String("maskProperties")).toArray();  | 
| 41 | QJsonArray::const_iterator propIt = maskProps.constBegin(); | 
| 42 | while (propIt != maskProps.constEnd()) { | 
| 43 | m_maskProperties.append(t: (*propIt).toVariant().toInt()); | 
| 44 | ++propIt; | 
| 45 | } | 
| 46 | |
| 47 |     QJsonObject trans = definition.value(key: QLatin1String("ks")).toObject();  | 
| 48 | m_layerTransform = new BMBasicTransform(trans, version, this); | 
| 49 | |
| 50 |     QJsonArray items = definition.value(key: QLatin1String("shapes")).toArray();  | 
| 51 | QJsonArray::const_iterator itemIt = items.constEnd(); | 
| 52 | while (itemIt != items.constBegin()) { | 
| 53 | itemIt--; | 
| 54 | BMShape *shape = BMShape::construct(definition: (*itemIt).toObject(), version, parent: this); | 
| 55 | if (shape) | 
| 56 | appendChild(child: shape); | 
| 57 | } | 
| 58 | |
| 59 | if (m_maskProperties.size()) | 
| 60 | qCWarning(lcLottieQtBodymovinParser) | 
| 61 |             << "BM Shape Layer: mask properties found, but not supported"  | 
| 62 | << m_maskProperties; | 
| 63 | } | 
| 64 | |
| 65 | BMShapeLayer::~BMShapeLayer() | 
| 66 | { | 
| 67 | if (m_layerTransform) | 
| 68 | delete m_layerTransform; | 
| 69 | } | 
| 70 | |
| 71 | BMBase *BMShapeLayer::clone() const | 
| 72 | { | 
| 73 | return new BMShapeLayer(*this); | 
| 74 | } | 
| 75 | |
| 76 | void BMShapeLayer::updateProperties(int frame) | 
| 77 | { | 
| 78 | BMLayer::updateProperties(frame); | 
| 79 | |
| 80 | m_layerTransform->updateProperties(frame); | 
| 81 | |
| 82 | for (BMBase *child : children()) { | 
| 83 | if (child->hidden()) | 
| 84 | continue; | 
| 85 | |
| 86 | BMShape *shape = dynamic_cast<BMShape*>(child); | 
| 87 | |
| 88 | if (!shape) | 
| 89 | continue; | 
| 90 | |
| 91 | if (shape->type() == BM_SHAPE_TRIM_IX) { | 
| 92 | BMTrimPath *trim = static_cast<BMTrimPath*>(shape); | 
| 93 | if (m_appliedTrim) | 
| 94 | m_appliedTrim->applyTrim(trimmer: *trim); | 
| 95 | else | 
| 96 | m_appliedTrim = trim; | 
| 97 | } else if (m_appliedTrim) { | 
| 98 | if (shape->acceptsTrim()) | 
| 99 | shape->applyTrim(trimmer: *m_appliedTrim); | 
| 100 | } | 
| 101 | } | 
| 102 | } | 
| 103 | |
| 104 | void BMShapeLayer::render(LottieRenderer &renderer) const | 
| 105 | { | 
| 106 | renderer.saveState(); | 
| 107 | |
| 108 | renderEffects(renderer); | 
| 109 | |
| 110 | // In case there is a linked layer, apply its transform first | 
| 111 | // as it affects tranforms of this layer too | 
| 112 | if (BMLayer *ll = linkedLayer()) | 
| 113 | renderer.render(trans: *ll->transform()); | 
| 114 | |
| 115 | renderer.render(layer: *this); | 
| 116 | |
| 117 | m_layerTransform->render(renderer); | 
| 118 | |
| 119 | for (BMBase *child : children()) { | 
| 120 | if (child->hidden()) | 
| 121 | continue; | 
| 122 | child->render(renderer); | 
| 123 | } | 
| 124 | |
| 125 | if (m_appliedTrim && !m_appliedTrim->hidden()) | 
| 126 | m_appliedTrim->render(renderer); | 
| 127 | |
| 128 | renderer.restoreState(); | 
| 129 | } | 
| 130 | |
| 131 | QT_END_NAMESPACE | 
| 132 | 
