1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "bmrect_p.h"
5
6#include <QJsonObject>
7#include <QJsonArray>
8#include <QLoggingCategory>
9
10#include <QDebug>
11
12#include "bmtrimpath_p.h"
13
14QT_BEGIN_NAMESPACE
15
16BMRect::BMRect(const BMRect &other)
17 : BMShape(other)
18{
19 m_position = other.m_position;
20 m_size = other.m_size;
21 m_roundness = other.m_roundness;
22}
23
24BMRect::BMRect(const QJsonObject &definition, const QVersionNumber &version, BMBase *parent)
25{
26 setParent(parent);
27 BMBase::parse(definition);
28 if (m_hidden)
29 return;
30
31 qCDebug(lcLottieQtBodymovinParser) << "BMRect::BMRect():" << m_name;
32
33 QJsonObject position = definition.value(key: QLatin1String("p")).toObject();
34 position = resolveExpression(definition: position);
35 m_position.construct(definition: position, version);
36
37 QJsonObject size = definition.value(key: QLatin1String("s")).toObject();
38 size = resolveExpression(definition: size);
39 m_size.construct(definition: size, version);
40
41 QJsonObject roundness = definition.value(key: QLatin1String("r")).toObject();
42 roundness = resolveExpression(definition: roundness);
43 m_roundness.construct(definition: roundness, version);
44
45 m_direction = definition.value(key: QLatin1String("d")).toInt();
46}
47
48
49BMBase *BMRect::clone() const
50{
51 return new BMRect(*this);
52}
53
54bool BMRect::setProperty(BMLiteral::PropertyType propertyType, QVariant value)
55{
56 switch (propertyType) {
57 case BMLiteral::RectPosition:
58 qCDebug(lcLottieQtBodymovinParser) << "Set position" << value.toPointF();
59 m_position.setValue(value.toPointF());
60 break;
61 default:
62 return false;
63 }
64 return true;
65}
66
67void BMRect::updateProperties(int frame)
68{
69 m_size.update(frame);
70 m_position.update(frame);
71 m_roundness.update(frame);
72
73 // AE uses center of a shape as it's position,
74 // in Qt a translation is needed
75 QPointF pos = QPointF(m_position.value().x() - m_size.value().width() / 2,
76 m_position.value().y() - m_size.value().height() / 2);
77
78 m_path = QPainterPath();
79 m_path.addRoundedRect(rect: QRectF(pos, m_size.value()),
80 xRadius: m_roundness.value(), yRadius: m_roundness.value());
81
82 if (m_direction)
83 m_path = m_path.toReversed();
84}
85
86void BMRect::render(LottieRenderer &renderer) const
87{
88 renderer.render(rect: *this);
89}
90
91bool BMRect::acceptsTrim() const
92{
93 return true;
94}
95
96QPointF BMRect::position() const
97{
98 return m_position.value();
99}
100
101QSizeF BMRect::size() const
102{
103 return m_size.value();
104}
105
106qreal BMRect::roundness() const
107{
108 return m_roundness.value();
109}
110
111QT_END_NAMESPACE
112

source code of qtlottie/src/bodymovin/bmrect.cpp