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/SkPathTypes.h"
9 #include "include/core/SkRRect.h"
10 #include "include/core/SkRect.h"
11 #include "include/core/SkRefCnt.h"
12 #include "modules/skottie/src/Adapter.h"
13 #include "modules/skottie/src/SkottieJson.h"
14 #include "modules/skottie/src/SkottiePriv.h"
15 #include "modules/skottie/src/SkottieValue.h"
16 #include "modules/skottie/src/layers/shapelayer/ShapeLayer.h"
17 #include "modules/sksg/include/SkSGRect.h"
18 #include "src/utils/SkJSON.h"
19
20 namespace sksg {
21 class GeometryNode;
22 }
23
24 namespace skottie {
25 namespace internal {
26
27 namespace {
28
29 class EllipseGeometryAdapter final :
30 public DiscardableAdapterBase<EllipseGeometryAdapter, sksg::RRect> {
31 public:
EllipseGeometryAdapter(const skjson::ObjectValue & jellipse,const AnimationBuilder * abuilder)32 EllipseGeometryAdapter(const skjson::ObjectValue& jellipse,
33 const AnimationBuilder* abuilder) {
34 this->node()->setDirection(ParseDefault(jellipse["d"], -1) == 3 ? SkPathDirection::kCCW
35 : SkPathDirection::kCW);
36 this->node()->setInitialPointIndex(1); // starting point: (Center, Top)
37
38 this->bind(*abuilder, jellipse["s"], fSize);
39 this->bind(*abuilder, jellipse["p"], fPosition);
40 }
41
42 private:
onSync()43 void onSync() override {
44 const auto bounds = SkRect::MakeXYWH(fPosition.x - fSize.x / 2,
45 fPosition.y - fSize.y / 2,
46 fSize.x, fSize.y);
47
48 this->node()->setRRect(SkRRect::MakeOval(bounds));
49 }
50
51 Vec2Value fSize = {0,0},
52 fPosition = {0,0}; // center
53 };
54
55 } // namespace
56
AttachEllipseGeometry(const skjson::ObjectValue & jellipse,const AnimationBuilder * abuilder)57 sk_sp<sksg::GeometryNode> ShapeBuilder::AttachEllipseGeometry(const skjson::ObjectValue& jellipse,
58 const AnimationBuilder* abuilder) {
59 return abuilder->attachDiscardableAdapter<EllipseGeometryAdapter>(jellipse, abuilder);
60 }
61
62 } // namespace internal
63 } // namespace skottie
64