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