| 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 | |
| 8 | #include "modules/skottie/src/Adapter.h" |
| 9 | #include "modules/skottie/src/SkottieJson.h" |
| 10 | #include "modules/skottie/src/SkottiePriv.h" |
| 11 | #include "modules/skottie/src/SkottieValue.h" |
| 12 | #include "modules/skottie/src/layers/shapelayer/ShapeLayer.h" |
| 13 | #include "modules/sksg/include/SkSGPath.h" |
| 14 | |
| 15 | namespace skottie { |
| 16 | namespace internal { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | class PolystarGeometryAdapter final : |
| 21 | public DiscardableAdapterBase<PolystarGeometryAdapter, sksg::Path> { |
| 22 | public: |
| 23 | enum class Type { |
| 24 | kStar, kPoly, |
| 25 | }; |
| 26 | |
| 27 | PolystarGeometryAdapter(const skjson::ObjectValue& jstar, |
| 28 | const AnimationBuilder* abuilder, Type t) |
| 29 | : fType(t) { |
| 30 | this->bind(*abuilder, jstar["pt" ], fPointCount ); |
| 31 | this->bind(*abuilder, jstar["p" ], fPosition ); |
| 32 | this->bind(*abuilder, jstar["r" ], fRotation ); |
| 33 | this->bind(*abuilder, jstar["ir" ], fInnerRadius ); |
| 34 | this->bind(*abuilder, jstar["or" ], fOuterRadius ); |
| 35 | this->bind(*abuilder, jstar["is" ], fInnerRoundness); |
| 36 | this->bind(*abuilder, jstar["os" ], fOuterRoundness); |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | void onSync() override { |
| 41 | static constexpr int kMaxPointCount = 100000; |
| 42 | const auto count = SkToUInt(SkTPin(SkScalarRoundToInt(fPointCount), 0, kMaxPointCount)); |
| 43 | const auto arc = sk_ieee_float_divide(SK_ScalarPI * 2, count); |
| 44 | |
| 45 | const auto pt_on_circle = [](const SkV2& c, SkScalar r, SkScalar a) { |
| 46 | return SkPoint::Make(c.x + r * std::cos(a), |
| 47 | c.y + r * std::sin(a)); |
| 48 | }; |
| 49 | |
| 50 | // TODO: inner/outer "roundness"? |
| 51 | |
| 52 | SkPath poly; |
| 53 | |
| 54 | auto angle = SkDegreesToRadians(fRotation - 90); |
| 55 | poly.moveTo(pt_on_circle(fPosition, fOuterRadius, angle)); |
| 56 | poly.incReserve(fType == Type::kStar ? count * 2 : count); |
| 57 | |
| 58 | for (unsigned i = 0; i < count; ++i) { |
| 59 | if (fType == Type::kStar) { |
| 60 | poly.lineTo(pt_on_circle(fPosition, fInnerRadius, angle + arc * 0.5f)); |
| 61 | } |
| 62 | angle += arc; |
| 63 | poly.lineTo(pt_on_circle(fPosition, fOuterRadius, angle)); |
| 64 | } |
| 65 | |
| 66 | poly.close(); |
| 67 | this->node()->setPath(poly); |
| 68 | } |
| 69 | |
| 70 | const Type fType; |
| 71 | |
| 72 | Vec2Value fPosition = {0,0}; |
| 73 | ScalarValue fPointCount = 0, |
| 74 | fRotation = 0, |
| 75 | fInnerRadius = 0, |
| 76 | fOuterRadius = 0, |
| 77 | fInnerRoundness = 0, |
| 78 | fOuterRoundness = 0; |
| 79 | }; |
| 80 | |
| 81 | } // namespace |
| 82 | |
| 83 | sk_sp<sksg::GeometryNode> ShapeBuilder::AttachPolystarGeometry(const skjson::ObjectValue& jstar, |
| 84 | const AnimationBuilder* abuilder) { |
| 85 | static constexpr PolystarGeometryAdapter::Type gTypes[] = { |
| 86 | PolystarGeometryAdapter::Type::kStar, // "sy": 1 |
| 87 | PolystarGeometryAdapter::Type::kPoly, // "sy": 2 |
| 88 | }; |
| 89 | |
| 90 | const auto type = ParseDefault<size_t>(jstar["sy" ], 0) - 1; |
| 91 | if (type >= SK_ARRAY_COUNT(gTypes)) { |
| 92 | abuilder->log(Logger::Level::kError, &jstar, "Unknown polystar type." ); |
| 93 | return nullptr; |
| 94 | } |
| 95 | |
| 96 | return abuilder->attachDiscardableAdapter<PolystarGeometryAdapter> |
| 97 | (jstar, abuilder, gTypes[type]); |
| 98 | } |
| 99 | |
| 100 | } // namespace internal |
| 101 | } // namespace skottie |
| 102 | |