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