Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Polystar.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
23#include "src/utils/SkJSON.h"
24
25#include <array>
26#include <cmath>
27#include <cstddef>
28
29namespace sksg {
30class GeometryNode;
31}
32
33namespace skottie {
34namespace internal {
35
36namespace {
37
38class PolystarGeometryAdapter final :
39 public DiscardableAdapterBase<PolystarGeometryAdapter, sksg::Path> {
40public:
41 enum class Type {
42 kStar, kPoly,
43 };
44
45 PolystarGeometryAdapter(const skjson::ObjectValue& jstar,
46 const AnimationBuilder* abuilder, Type t)
47 : fType(t) {
48 this->bind(*abuilder, jstar["pt"], fPointCount );
49 this->bind(*abuilder, jstar["p" ], fPosition );
50 this->bind(*abuilder, jstar["r" ], fRotation );
51 this->bind(*abuilder, jstar["ir"], fInnerRadius );
52 this->bind(*abuilder, jstar["or"], fOuterRadius );
53 this->bind(*abuilder, jstar["is"], fInnerRoundness);
54 this->bind(*abuilder, jstar["os"], fOuterRoundness);
55 }
56
57private:
58 void onSync() override {
59 static constexpr int kMaxPointCount = 100000;
60 const auto count = SkToUInt(SkTPin(SkScalarRoundToInt(fPointCount), 0, kMaxPointCount));
61 const auto arc = sk_ieee_float_divide(SK_ScalarPI * 2, count);
62
63 const auto pt_on_circle = [](const SkV2& c, SkScalar r, SkScalar a) {
64 return SkPoint::Make(c.x + r * std::cos(a),
65 c.y + r * std::sin(a));
66 };
67
68 // TODO: inner/outer "roundness"?
69
70 SkPathBuilder poly;
71
72 auto angle = SkDegreesToRadians(fRotation - 90);
73 poly.moveTo(pt_on_circle(fPosition, fOuterRadius, angle));
74 poly.incReserve(fType == Type::kStar ? count * 2 : count);
75
76 for (unsigned i = 0; i < count; ++i) {
77 if (fType == Type::kStar) {
78 poly.lineTo(pt_on_circle(fPosition, fInnerRadius, angle + arc * 0.5f));
79 }
80 angle += arc;
81 poly.lineTo(pt_on_circle(fPosition, fOuterRadius, angle));
82 }
83
84 poly.close();
85 this->node()->setPath(poly.detach());
86 }
87
88 const Type fType;
89
90 Vec2Value fPosition = {0,0};
91 ScalarValue fPointCount = 0,
92 fRotation = 0,
93 fInnerRadius = 0,
94 fOuterRadius = 0,
95 fInnerRoundness = 0,
96 fOuterRoundness = 0;
97};
98
99} // namespace
100
102 const AnimationBuilder* abuilder) {
103 static constexpr PolystarGeometryAdapter::Type gTypes[] = {
104 PolystarGeometryAdapter::Type::kStar, // "sy": 1
105 PolystarGeometryAdapter::Type::kPoly, // "sy": 2
106 };
107
108 const auto type = ParseDefault<size_t>(jstar["sy"], 0) - 1;
109 if (type >= std::size(gTypes)) {
110 abuilder->log(Logger::Level::kError, &jstar, "Unknown polystar type.");
111 return nullptr;
112 }
113
114 return abuilder->attachDiscardableAdapter<PolystarGeometryAdapter>
115 (jstar, abuilder, gTypes[type]);
116}
117
118} // namespace internal
119} // namespace skottie
int count
static constexpr float sk_ieee_float_divide(float numer, float denom)
#define SkDegreesToRadians(degrees)
Definition SkScalar.h:77
#define SkScalarRoundToInt(x)
Definition SkScalar.h:37
#define SK_ScalarPI
Definition SkScalar.h:21
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition SkTPin.h:19
constexpr unsigned SkToUInt(S x)
Definition SkTo.h:30
SkPathBuilder & close()
SkPathBuilder & lineTo(SkPoint pt)
void incReserve(int extraPtCount, int extraVerbCount)
SkPathBuilder & moveTo(SkPoint pt)
void attachDiscardableAdapter(sk_sp< T > adapter) const
void log(Logger::Level, const skjson::Value *, const char fmt[],...) const SK_PRINTF_LIKE(4
Definition Skottie.cpp:71
static sk_sp< sksg::GeometryNode > AttachPolystarGeometry(const skjson::ObjectValue &, const AnimationBuilder *)
Definition Polystar.cpp:101
float SkScalar
Definition extension.cpp:12
struct MyStruct a[10]
SkScalar ScalarValue
SkV2 Vec2Value
Definition Skottie.h:32
@ kStar
the * key
static constexpr SkPoint Make(float x, float y)
Definition SkM44.h:19
float x
Definition SkM44.h:20
float y
Definition SkM44.h:20