| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "qlottiepolystar_p.h" |
| 5 | |
| 6 | #include <QJsonObject> |
| 7 | #include <QRectF> |
| 8 | |
| 9 | #include "qlottietrimpath_p.h" |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | QLottiePolyStar::QLottiePolyStar(const QLottiePolyStar &other) |
| 14 | : QLottieShape(other) |
| 15 | { |
| 16 | m_position = other.m_position; |
| 17 | m_pointCount = other.m_pointCount; |
| 18 | m_outerRadius = other.m_outerRadius; |
| 19 | m_innerRadius = other.m_innerRadius; |
| 20 | m_startAngle = other.m_startAngle; |
| 21 | m_polygonMode = other.m_polygonMode; |
| 22 | } |
| 23 | |
| 24 | QLottiePolyStar::QLottiePolyStar(const QJsonObject &definition, QLottieBase *parent) |
| 25 | { |
| 26 | setParent(parent); |
| 27 | construct(definition); |
| 28 | } |
| 29 | |
| 30 | QLottieBase *QLottiePolyStar::clone() const |
| 31 | { |
| 32 | return new QLottiePolyStar(*this); |
| 33 | } |
| 34 | |
| 35 | void QLottiePolyStar::construct(const QJsonObject &definition) |
| 36 | { |
| 37 | QLottieBase::parse(definition); |
| 38 | if (m_hidden) |
| 39 | return; |
| 40 | |
| 41 | qCDebug(lcLottieQtLottieParser) << "QLottiePolyStar::construct():"<< m_name; |
| 42 | |
| 43 | QJsonObject position = definition.value(key: QLatin1String("p")).toObject(); |
| 44 | position = resolveExpression(definition: position); |
| 45 | m_position.construct(definition: position); |
| 46 | |
| 47 | QJsonObject outerRadius = definition.value(key: QLatin1String("or")).toObject(); |
| 48 | outerRadius = resolveExpression(definition: outerRadius); |
| 49 | m_outerRadius.construct(definition: outerRadius); |
| 50 | |
| 51 | QJsonObject innerRadius = definition.value(key: QLatin1String("ir")).toObject(); |
| 52 | innerRadius = resolveExpression(definition: innerRadius); |
| 53 | m_innerRadius.construct(definition: innerRadius); |
| 54 | |
| 55 | QJsonObject startAngle = definition.value(key: QLatin1String("r")).toObject(); |
| 56 | startAngle = resolveExpression(definition: startAngle); |
| 57 | m_startAngle.construct(definition: startAngle); |
| 58 | |
| 59 | QJsonObject pointCount = definition.value(key: QLatin1String("pt")).toObject(); |
| 60 | pointCount = resolveExpression(definition: pointCount); |
| 61 | m_pointCount.construct(definition: pointCount); |
| 62 | |
| 63 | m_polygonMode = (definition.value(key: QLatin1String("sy")).toInt() == 2); |
| 64 | |
| 65 | m_direction = definition.value(key: QLatin1String("d")).toInt(); |
| 66 | } |
| 67 | |
| 68 | bool QLottiePolyStar::acceptsTrim() const |
| 69 | { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | void QLottiePolyStar::updateProperties(int frame) |
| 74 | { |
| 75 | m_position.update(frame); |
| 76 | m_outerRadius.update(frame); |
| 77 | m_innerRadius.update(frame); |
| 78 | m_startAngle.update(frame); |
| 79 | m_pointCount.update(frame); |
| 80 | |
| 81 | m_path.clear(); |
| 82 | |
| 83 | const int numPoints = m_pointCount.value(); |
| 84 | if (numPoints < 1) |
| 85 | return; |
| 86 | const qreal angleStep = -360.0 / numPoints; |
| 87 | const qreal halfAngleStep = angleStep / 2; |
| 88 | qreal angle = 90 - m_startAngle.value(); |
| 89 | |
| 90 | const QPointF pos = m_position.value(); |
| 91 | QLineF outLine(pos, QPointF(pos.x(), pos.y() - m_outerRadius.value())); |
| 92 | QLineF inLine(pos, QPointF(pos.x(), pos.y() - m_innerRadius.value())); |
| 93 | |
| 94 | outLine.setAngle(angle); |
| 95 | const QPointF startPt = outLine.p2(); |
| 96 | m_path.moveTo(p: startPt); |
| 97 | for (int i = 0; i < numPoints; i++) { |
| 98 | if (!m_polygonMode) { |
| 99 | inLine.setAngle(angle + halfAngleStep); |
| 100 | m_path.lineTo(p: inLine.p2()); |
| 101 | } |
| 102 | angle += angleStep; |
| 103 | outLine.setAngle(angle); |
| 104 | m_path.lineTo(p: outLine.p2()); |
| 105 | } |
| 106 | // Fix potential accuracy errors, ensuring path is closed: |
| 107 | m_path.setElementPositionAt(i: m_path.elementCount() - 1, x: startPt.x(), y: startPt.y()); |
| 108 | |
| 109 | if (hasReversedDirection()) |
| 110 | m_path = m_path.toReversed(); |
| 111 | } |
| 112 | |
| 113 | void QLottiePolyStar::render(QLottieRenderer &renderer) const |
| 114 | { |
| 115 | renderer.render(star: *this); |
| 116 | } |
| 117 | |
| 118 | QPointF QLottiePolyStar::position() const |
| 119 | { |
| 120 | return m_position.value(); |
| 121 | } |
| 122 | |
| 123 | qreal QLottiePolyStar::outerRadius() const |
| 124 | { |
| 125 | return m_outerRadius.value(); |
| 126 | } |
| 127 | |
| 128 | qreal QLottiePolyStar::innerRadius() const |
| 129 | { |
| 130 | return m_innerRadius.value(); |
| 131 | } |
| 132 | |
| 133 | qreal QLottiePolyStar::startAngle() const |
| 134 | { |
| 135 | return m_startAngle.value(); |
| 136 | } |
| 137 | |
| 138 | int QLottiePolyStar::pointCount() const |
| 139 | { |
| 140 | return m_pointCount.value(); |
| 141 | } |
| 142 | |
| 143 | bool QLottiePolyStar::isPolygonModeEnabled() const |
| 144 | { |
| 145 | return m_polygonMode; |
| 146 | } |
| 147 | |
| 148 | QT_END_NAMESPACE |
| 149 |
