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/SkSGGeometryEffect.h" |
14 | |
15 | namespace skottie::internal { |
16 | |
17 | namespace { |
18 | |
19 | class OffsetPathsAdapter final : public DiscardableAdapterBase<OffsetPathsAdapter, |
20 | sksg::OffsetEffect> { |
21 | public: |
22 | OffsetPathsAdapter(const skjson::ObjectValue& joffset, |
23 | const AnimationBuilder& abuilder, |
24 | sk_sp<sksg::GeometryNode> child) |
25 | : INHERITED(sksg::OffsetEffect::Make(std::move(child))) { |
26 | static constexpr SkPaint::Join gJoinMap[] = { |
27 | SkPaint::kMiter_Join, // 'lj': 1 |
28 | SkPaint::kRound_Join, // 'lj': 2 |
29 | SkPaint::kBevel_Join, // 'lj': 3 |
30 | }; |
31 | |
32 | const auto join = ParseDefault<int>(joffset["lj" ], 1) - 1; |
33 | this->node()->setJoin(gJoinMap[SkTPin<int>(join, 0, SK_ARRAY_COUNT(gJoinMap) - 1)]); |
34 | |
35 | this->bind(abuilder, joffset["a" ], fAmount); |
36 | this->bind(abuilder, joffset["ml" ], fMiterLimit); |
37 | } |
38 | |
39 | private: |
40 | void onSync() override { |
41 | this->node()->setOffset(fAmount); |
42 | this->node()->setMiterLimit(fMiterLimit); |
43 | } |
44 | |
45 | ScalarValue fAmount = 0, |
46 | fMiterLimit = 0; |
47 | |
48 | using INHERITED = DiscardableAdapterBase<OffsetPathsAdapter, sksg::OffsetEffect>; |
49 | }; |
50 | |
51 | } // namespace |
52 | |
53 | std::vector<sk_sp<sksg::GeometryNode>> ShapeBuilder::AttachOffsetGeometryEffect( |
54 | const skjson::ObjectValue& jround, const AnimationBuilder* abuilder, |
55 | std::vector<sk_sp<sksg::GeometryNode>>&& geos) { |
56 | std::vector<sk_sp<sksg::GeometryNode>> offsetted; |
57 | offsetted.reserve(geos.size()); |
58 | |
59 | for (auto& g : geos) { |
60 | offsetted.push_back(abuilder->attachDiscardableAdapter<OffsetPathsAdapter> |
61 | (jround, *abuilder, std::move(g))); |
62 | } |
63 | |
64 | return offsetted; |
65 | } |
66 | |
67 | } // namespace skottie::internal |
68 | |