| 1 | // Copyright (C) 2018 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "bmbasictransform_p.h" |
| 5 | |
| 6 | #include <QJsonObject> |
| 7 | |
| 8 | #include "bmconstants_p.h" |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | BMBasicTransform::BMBasicTransform(const BMBasicTransform &other) |
| 13 | : BMShape(other) |
| 14 | { |
| 15 | m_direction = other.m_direction; |
| 16 | m_anchorPoint = other.m_anchorPoint; |
| 17 | m_splitPosition = other.m_splitPosition; |
| 18 | m_position = other.m_position; |
| 19 | m_xPos = other.m_xPos; |
| 20 | m_yPos = other.m_yPos; |
| 21 | m_scale = other.m_scale; |
| 22 | m_rotation = other.m_rotation; |
| 23 | m_opacity = other.m_opacity; |
| 24 | } |
| 25 | |
| 26 | BMBasicTransform::BMBasicTransform(const QJsonObject &definition, const QVersionNumber &version, |
| 27 | BMBase *parent) |
| 28 | { |
| 29 | setParent(parent); |
| 30 | construct(definition, version); |
| 31 | } |
| 32 | |
| 33 | BMBase *BMBasicTransform::clone() const |
| 34 | { |
| 35 | return new BMBasicTransform(*this); |
| 36 | } |
| 37 | |
| 38 | void BMBasicTransform::construct(const QJsonObject &definition, const QVersionNumber &version) |
| 39 | { |
| 40 | BMBase::parse(definition); |
| 41 | |
| 42 | qCDebug(lcLottieQtBodymovinParser) |
| 43 | << "BMBasicTransform::construct():" << m_name; |
| 44 | |
| 45 | QJsonObject anchors = definition.value(key: QLatin1String("a" )).toObject(); |
| 46 | anchors = resolveExpression(definition: anchors); |
| 47 | m_anchorPoint.construct(definition: anchors, version); |
| 48 | |
| 49 | if (definition.value(key: QLatin1String("p" )).toObject().contains(key: QLatin1String("s" ))) { |
| 50 | QJsonObject posX = definition.value(key: QLatin1String("p" )).toObject().value(key: QLatin1String("x" )).toObject(); |
| 51 | posX = resolveExpression(definition: posX); |
| 52 | m_xPos.construct(definition: posX, version); |
| 53 | |
| 54 | QJsonObject posY = definition.value(key: QLatin1String("p" )).toObject().value(key: QLatin1String("y" )).toObject(); |
| 55 | posY = resolveExpression(definition: posY); |
| 56 | m_yPos.construct(definition: posY, version); |
| 57 | |
| 58 | m_splitPosition = true; |
| 59 | } else { |
| 60 | QJsonObject position = definition.value(key: QLatin1String("p" )).toObject(); |
| 61 | position = resolveExpression(definition: position); |
| 62 | m_position.construct(definition: position, version); |
| 63 | } |
| 64 | |
| 65 | QJsonObject scale = definition.value(key: QLatin1String("s" )).toObject(); |
| 66 | scale = resolveExpression(definition: scale); |
| 67 | m_scale.construct(definition: scale, version); |
| 68 | |
| 69 | QJsonObject rotation = definition.value(key: QLatin1String("r" )).toObject(); |
| 70 | rotation = resolveExpression(definition: rotation); |
| 71 | m_rotation.construct(definition: rotation, version); |
| 72 | |
| 73 | // If this is the base class for BMRepeaterTransform, |
| 74 | // opacity is not present |
| 75 | if (definition.contains(key: QLatin1String("o" ))) { |
| 76 | QJsonObject opacity = definition.value(key: QLatin1String("o" )).toObject(); |
| 77 | opacity = resolveExpression(definition: opacity); |
| 78 | m_opacity.construct(definition: opacity, version); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void BMBasicTransform::updateProperties(int frame) |
| 83 | { |
| 84 | if (m_splitPosition) { |
| 85 | m_xPos.update(frame); |
| 86 | m_yPos.update(frame); |
| 87 | } else |
| 88 | m_position.update(frame); |
| 89 | m_anchorPoint.update(frame); |
| 90 | m_scale.update(frame); |
| 91 | m_rotation.update(frame); |
| 92 | m_opacity.update(frame); |
| 93 | } |
| 94 | |
| 95 | void BMBasicTransform::render(LottieRenderer &renderer) const |
| 96 | { |
| 97 | renderer.render(trans: *this); |
| 98 | } |
| 99 | |
| 100 | QPointF BMBasicTransform::anchorPoint() const |
| 101 | { |
| 102 | return m_anchorPoint.value(); |
| 103 | } |
| 104 | |
| 105 | QPointF BMBasicTransform::position() const |
| 106 | { |
| 107 | if (m_splitPosition) |
| 108 | return QPointF(m_xPos.value(), m_yPos.value()); |
| 109 | else |
| 110 | return m_position.value(); |
| 111 | } |
| 112 | |
| 113 | QPointF BMBasicTransform::scale() const |
| 114 | { |
| 115 | // Scale the value to 0..1 to be suitable for Qt |
| 116 | return m_scale.value() / 100.0; |
| 117 | } |
| 118 | |
| 119 | qreal BMBasicTransform::rotation() const |
| 120 | { |
| 121 | return m_rotation.value(); |
| 122 | } |
| 123 | |
| 124 | qreal BMBasicTransform::opacity() const |
| 125 | { |
| 126 | // Scale the value to 0..1 to be suitable for Qt |
| 127 | return m_opacity.value() / 100.0; |
| 128 | } |
| 129 | |
| 130 | QT_END_NAMESPACE |
| 131 | |