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