| 1 | // Copyright (C) 2018 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "bmbase_p.h" |
| 5 | |
| 6 | #include <QLoggingCategory> |
| 7 | #include <QRegularExpression> |
| 8 | #include <QRegularExpressionMatch> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | Q_LOGGING_CATEGORY(lcLottieQtBodymovinParser, "qt.lottieqt.bodymovin.parser" ); |
| 13 | Q_LOGGING_CATEGORY(lcLottieQtBodymovinUpdate, "qt.lottieqt.bodymovin.update" ); |
| 14 | Q_LOGGING_CATEGORY(lcLottieQtBodymovinRender, "qt.lottieqt.bodymovin.render" ); |
| 15 | |
| 16 | BMBase::BMBase(const BMBase &other) |
| 17 | { |
| 18 | m_definition = other.m_definition; |
| 19 | m_type = other.m_type; |
| 20 | m_hidden = other.m_hidden; |
| 21 | m_name = other.m_name; |
| 22 | m_autoOrient = other.m_autoOrient; |
| 23 | for (BMBase *child : other.m_children) { |
| 24 | BMBase *clone = child->clone(); |
| 25 | clone->setParent(this); |
| 26 | appendChild(child: clone); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | BMBase::~BMBase() |
| 31 | { |
| 32 | qDeleteAll(c: m_children); |
| 33 | } |
| 34 | |
| 35 | BMBase *BMBase::clone() const |
| 36 | { |
| 37 | return new BMBase(*this); |
| 38 | } |
| 39 | |
| 40 | QString BMBase::name() const |
| 41 | { |
| 42 | return m_name; |
| 43 | } |
| 44 | |
| 45 | void BMBase::setName(const QString &name) |
| 46 | { |
| 47 | m_name = name; |
| 48 | } |
| 49 | |
| 50 | bool BMBase::setProperty(BMLiteral::PropertyType propertyName, QVariant value) |
| 51 | { |
| 52 | for (BMBase *child : std::as_const(t&: m_children)) { |
| 53 | bool changed = child->setProperty(propertyName, value); |
| 54 | if (changed) |
| 55 | return true; |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | int BMBase::type() const |
| 61 | { |
| 62 | return m_type; |
| 63 | } |
| 64 | |
| 65 | void BMBase::setType(int type) |
| 66 | { |
| 67 | m_type = type; |
| 68 | } |
| 69 | |
| 70 | void BMBase::prependChild(BMBase *child) |
| 71 | { |
| 72 | m_children.push_front(t: child); |
| 73 | } |
| 74 | |
| 75 | void BMBase::insertChildBeforeLast(BMBase *child) |
| 76 | { |
| 77 | m_children.insert(i: qMax(a: m_children.size() - 1, b: 0), t: child); |
| 78 | } |
| 79 | |
| 80 | void BMBase::appendChild(BMBase *child) |
| 81 | { |
| 82 | m_children.push_back(t: child); |
| 83 | } |
| 84 | |
| 85 | BMBase *BMBase::findChild(const QString &childName) |
| 86 | { |
| 87 | if (name() == childName) |
| 88 | return this; |
| 89 | |
| 90 | BMBase *found = nullptr; |
| 91 | for (BMBase *child : std::as_const(t&: m_children)) { |
| 92 | found = child->findChild(childName); |
| 93 | if (found) |
| 94 | break; |
| 95 | } |
| 96 | return found; |
| 97 | } |
| 98 | |
| 99 | void BMBase::updateProperties(int frame) |
| 100 | { |
| 101 | if (m_hidden) |
| 102 | return; |
| 103 | |
| 104 | for (BMBase *child : std::as_const(t&: m_children)) |
| 105 | child->updateProperties(frame); |
| 106 | } |
| 107 | |
| 108 | void BMBase::render(LottieRenderer &renderer) const |
| 109 | { |
| 110 | if (m_hidden) |
| 111 | return; |
| 112 | |
| 113 | renderer.saveState(); |
| 114 | for (BMBase *child : std::as_const(t: m_children)) { |
| 115 | if (child->m_hidden) |
| 116 | continue; |
| 117 | child->render(renderer); |
| 118 | } |
| 119 | renderer.restoreState(); |
| 120 | } |
| 121 | |
| 122 | void BMBase::resolveTopRoot() |
| 123 | { |
| 124 | if (!m_topRoot) { |
| 125 | BMBase *p = this; |
| 126 | while (p) { |
| 127 | m_topRoot = p; |
| 128 | p = p->parent(); |
| 129 | } |
| 130 | } |
| 131 | Q_ASSERT(m_topRoot); |
| 132 | } |
| 133 | |
| 134 | BMBase *BMBase::topRoot() const |
| 135 | { |
| 136 | return m_topRoot; |
| 137 | } |
| 138 | |
| 139 | void BMBase::parse(const QJsonObject &definition) |
| 140 | { |
| 141 | qCDebug(lcLottieQtBodymovinParser) << "BMBase::parse()" ; |
| 142 | |
| 143 | m_definition = definition; |
| 144 | |
| 145 | m_hidden = definition.value(key: QLatin1String("hd" )).toBool(defaultValue: false); |
| 146 | m_name = definition.value(key: QLatin1String("nm" )).toString(); |
| 147 | m_matchName = definition.value(key: QLatin1String("mn" )).toString(); |
| 148 | m_autoOrient = definition.value(key: QLatin1String("ao" )).toBool(); |
| 149 | |
| 150 | if (m_autoOrient) |
| 151 | qCWarning(lcLottieQtBodymovinParser) |
| 152 | << "Element has auto-orientation set, but it is not supported" ; |
| 153 | } |
| 154 | |
| 155 | const QJsonObject &BMBase::definition() const |
| 156 | { |
| 157 | return m_definition; |
| 158 | } |
| 159 | |
| 160 | bool BMBase::active(int frame) const |
| 161 | { |
| 162 | Q_UNUSED(frame); |
| 163 | return !m_hidden; |
| 164 | } |
| 165 | |
| 166 | bool BMBase::hidden() const |
| 167 | { |
| 168 | return m_hidden; |
| 169 | } |
| 170 | |
| 171 | void BMBase::setParent(BMBase *parent) |
| 172 | { |
| 173 | m_parent = parent; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | const QJsonObject BMBase::resolveExpression(const QJsonObject &definition) |
| 178 | { |
| 179 | QString expr = definition.value(key: QLatin1String("x" )).toString(); |
| 180 | |
| 181 | // If there is no expression, return the original object definition |
| 182 | if (expr.isEmpty()) |
| 183 | return definition; |
| 184 | |
| 185 | // Find out layer handle |
| 186 | resolveTopRoot(); |
| 187 | |
| 188 | QRegularExpression re(QStringLiteral("effect\\(\\'(.*?)\\'\\)\\(\\'(.*?)\\'\\)" )); |
| 189 | QRegularExpressionMatch match = re.match(subject: expr); |
| 190 | if (!match.hasMatch()) |
| 191 | return definition; |
| 192 | |
| 193 | QString effect = match.captured(nth: 1); |
| 194 | QString elementName = match.captured(nth: 2); |
| 195 | |
| 196 | QJsonObject retVal = definition; |
| 197 | |
| 198 | if (BMBase *source = m_topRoot->findChild(childName: effect)) { |
| 199 | if (source->children().size()) |
| 200 | retVal = source->children().at(i: 0)->definition().value(key: QLatin1String("v" )).toObject(); |
| 201 | else |
| 202 | retVal = source->definition().value(key: QLatin1String("v" )).toObject(); |
| 203 | if (source->children().size() > 1) |
| 204 | qCWarning(lcLottieQtBodymovinParser) << "Effect source points" |
| 205 | "to a group that has" |
| 206 | "many children. The" |
| 207 | "first is be picked" ; |
| 208 | } else { |
| 209 | qCWarning(lcLottieQtBodymovinParser) << "Failed to find specified effect" << effect; |
| 210 | } |
| 211 | |
| 212 | // Let users of the json know that it is originated from expression, |
| 213 | // so they can adjust their behavior accordingly |
| 214 | retVal.insert(key: QLatin1String("fromExpression" ), value: true); |
| 215 | |
| 216 | return retVal; |
| 217 | } |
| 218 | |
| 219 | QT_END_NAMESPACE |
| 220 | |