| 1 | // Copyright (C) 2018 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "qlottietrimpath_p.h" |
| 5 | |
| 6 | #include <QtGlobal> |
| 7 | #include <private/qpainterpath_p.h> |
| 8 | #include <private/qbezier_p.h> |
| 9 | |
| 10 | #include "qlottieconstants_p.h" |
| 11 | |
| 12 | QLottieTrimPath::QLottieTrimPath() |
| 13 | { |
| 14 | m_appliedTrim = this; |
| 15 | } |
| 16 | |
| 17 | QLottieTrimPath::QLottieTrimPath(const QJsonObject &definition, QLottieBase *parent) |
| 18 | { |
| 19 | m_appliedTrim = this; |
| 20 | |
| 21 | setParent(parent); |
| 22 | construct(definition); |
| 23 | } |
| 24 | |
| 25 | QLottieTrimPath::QLottieTrimPath(const QLottieTrimPath &other) |
| 26 | : QLottieShape(other) |
| 27 | { |
| 28 | m_start = other.m_start; |
| 29 | m_end = other.m_end; |
| 30 | m_offset = other.m_offset; |
| 31 | m_isParallel = other.m_isParallel; |
| 32 | } |
| 33 | |
| 34 | QLottieBase *QLottieTrimPath::clone() const |
| 35 | { |
| 36 | return new QLottieTrimPath(*this); |
| 37 | } |
| 38 | |
| 39 | void QLottieTrimPath::construct(const QJsonObject &definition) |
| 40 | { |
| 41 | QLottieBase::parse(definition); |
| 42 | if (m_hidden) |
| 43 | return; |
| 44 | |
| 45 | qCDebug(lcLottieQtLottieParser) << "QLottieTrimPath::construct():"<< m_name; |
| 46 | |
| 47 | QJsonObject start = definition.value(key: QLatin1String("s")).toObject(); |
| 48 | start = resolveExpression(definition: start); |
| 49 | m_start.construct(definition: start); |
| 50 | |
| 51 | QJsonObject end = definition.value(key: QLatin1String("e")).toObject(); |
| 52 | end = resolveExpression(definition: end); |
| 53 | m_end.construct(definition: end); |
| 54 | |
| 55 | QJsonObject offset = definition.value(key: QLatin1String("o")).toObject(); |
| 56 | offset = resolveExpression(definition: offset); |
| 57 | m_offset.construct(definition: offset); |
| 58 | |
| 59 | int multiMode = 1; |
| 60 | if (definition.contains(key: QLatin1String("m"))) { |
| 61 | multiMode = definition.value(key: QLatin1String("m")).toInt(); |
| 62 | } |
| 63 | m_isParallel = (multiMode == 1); |
| 64 | |
| 65 | if (strcmp(s1: qgetenv(varName: "QLOTTIE_FORCE_TRIM_MODE"), s2: "sequential") == 0) { |
| 66 | qCDebug(lcLottieQtLottieRender) << "Forcing trim mode to Sequential"; |
| 67 | m_isParallel = true; |
| 68 | } else if (strcmp(s1: qgetenv(varName: "QLOTTIE_FORCE_TRIM_MODE"), s2: "parallel") == 0) { |
| 69 | qCDebug(lcLottieQtLottieRender) << "Forcing trim mode to Parallel"; |
| 70 | m_isParallel = false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void QLottieTrimPath::updateProperties(int frame) |
| 75 | { |
| 76 | m_start.update(frame); |
| 77 | m_end.update(frame); |
| 78 | m_offset.update(frame); |
| 79 | |
| 80 | qCDebug(lcLottieQtLottieUpdate) << name() << frame << m_start.value() |
| 81 | << m_end.value() << m_offset.value(); |
| 82 | |
| 83 | QLottieShape::updateProperties(frame); |
| 84 | } |
| 85 | |
| 86 | void QLottieTrimPath::render(QLottieRenderer &renderer) const |
| 87 | { |
| 88 | if (m_appliedTrim) { |
| 89 | if (m_appliedTrim->isParallel()) |
| 90 | renderer.setTrimmingState(QLottieRenderer::Parallel); |
| 91 | else |
| 92 | renderer.setTrimmingState(QLottieRenderer::Sequential); |
| 93 | } else |
| 94 | renderer.setTrimmingState(QLottieRenderer::Off); |
| 95 | |
| 96 | renderer.render(trans: *this); |
| 97 | } |
| 98 | |
| 99 | bool QLottieTrimPath::acceptsTrim() const |
| 100 | { |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | void QLottieTrimPath::applyTrim(const QLottieTrimPath &other) |
| 105 | { |
| 106 | qCDebug(lcLottieQtLottieUpdate) << "Join trim paths:" |
| 107 | << other.name() << "into:"<< name(); |
| 108 | |
| 109 | m_name = m_name + QStringLiteral(" & ") + other.name(); |
| 110 | qreal newStart = other.start() + (m_start.value() / 100.0) * |
| 111 | (other.end() - other.start()); |
| 112 | qreal newEnd = other.start() + (m_end.value() / 100.0) * |
| 113 | (other.end() - other.start()); |
| 114 | |
| 115 | m_start.setValue(newStart); |
| 116 | m_end.setValue(newEnd); |
| 117 | m_offset.setValue(m_offset.value() + other.offset()); |
| 118 | } |
| 119 | |
| 120 | qreal QLottieTrimPath::start() const |
| 121 | { |
| 122 | return m_start.value(); |
| 123 | } |
| 124 | |
| 125 | qreal QLottieTrimPath::end() const |
| 126 | { |
| 127 | return m_end.value(); |
| 128 | } |
| 129 | |
| 130 | qreal QLottieTrimPath::offset() const |
| 131 | { |
| 132 | return m_offset.value(); |
| 133 | } |
| 134 | |
| 135 | bool QLottieTrimPath::isParallel() const |
| 136 | { |
| 137 | return m_isParallel; |
| 138 | } |
| 139 | |
| 140 | QPainterPath QLottieTrimPath::trim(const QPainterPath &path) const |
| 141 | { |
| 142 | if (isStructureDumping()) |
| 143 | return path; |
| 144 | qreal offset = m_offset.value() / 360.0; |
| 145 | qreal start = m_start.value() / 100.0; |
| 146 | qreal end = m_end.value() / 100.0; |
| 147 | return path.trimmed(fromFraction: start, toFraction: end, offset); |
| 148 | } |
| 149 |
