1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SHAPE_RENDER_SHAPE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SHAPE_RENDER_SHAPE_H
18 
19 #include "base/json/json_util.h"
20 #include "core/components/shape/shape_component.h"
21 #include "core/pipeline/base/render_node.h"
22 
23 namespace OHOS::Ace {
24 
25 class RenderShape : public RenderNode {
26     DECLARE_ACE_TYPE(RenderShape, RenderNode);
27 
28 public:
29     static RefPtr<RenderNode> Create();
30     void Update(const RefPtr<Component>& component) override;
31     void PerformLayout() override;
32 
OnAttachContext()33     void OnAttachContext() override
34     {
35         const std::function<void()> callback = [weak = WeakClaim(this)] {
36             auto renderShape = weak.Upgrade();
37             if (renderShape) {
38                 renderShape->OnAnimationCallback();
39             }
40         };
41         strokeState_.SetContextAndCallback(context_, callback);
42         fillState_.SetContextAndCallback(context_, callback);
43         width_.SetContextAndCallback(context_, callback);
44         height_.SetContextAndCallback(context_, callback);
45         topLeftRadius_.SetContextAndCallback(context_, callback);
46         topRightRadius_.SetContextAndCallback(context_, callback);
47         bottomRightRadius_.SetContextAndCallback(context_, callback);
48         bottomLeftRadius_.SetContextAndCallback(context_, callback);
49         pathCmd_.SetContextAndCallback(context_, callback);
50     }
51 
52     virtual Size CalcSize() = 0;
53     void OnAnimationCallback();
54 
GetShapeType()55     ShapeType GetShapeType() const
56     {
57         return shapeType_;
58     }
59 
GetTopLeftRadius()60     Radius GetTopLeftRadius() const
61     {
62         return topLeftRadius_;
63     }
64 
GetTopRightRadius()65     Radius GetTopRightRadius() const
66     {
67         return topRightRadius_;
68     }
69 
GetBottomLeftRadius()70     Radius GetBottomLeftRadius() const
71     {
72         return bottomLeftRadius_;
73     }
74 
GetBottomRightRadius()75     Radius GetBottomRightRadius() const
76     {
77         return bottomRightRadius_;
78     }
79 
GetStartPoint()80     ShapePoint GetStartPoint() const
81     {
82         return start_;
83     }
84 
GetEndPoint()85     ShapePoint GetEndPoint() const
86     {
87         return end_;
88     }
89 
GetPoints()90     std::vector<ShapePoint> GetPoints() const
91     {
92         return points_;
93     }
94 
GetPathCmd()95     std::string GetPathCmd() const
96     {
97         return pathCmd_.GetValue();
98     }
99 
GetFillState()100     FillState GetFillState() const
101     {
102         return fillState_;
103     }
104 
GetStrokeState()105     StrokeState GetStrokeState() const
106     {
107         return strokeState_;
108     }
109 
GetAntiAlias()110     bool GetAntiAlias() const
111     {
112         return antiAlias_.first && antiAlias_.second;
113     }
114 
GetWidthDimension()115     Dimension GetWidthDimension() const
116     {
117         return static_cast<Dimension>(width_);
118     }
119 
GetHeightDimension()120     Dimension GetHeightDimension() const
121     {
122         return static_cast<Dimension>(height_);
123     }
124 
GetShapeComponent()125     RefPtr<ShapeComponent> GetShapeComponent() const
126     {
127         return component_;
128     }
129 
130     void NormalToPxOfShape(AnimatableDimension sizeFromComponent, AnimatableDimension& sizeOfCurrent);
131     void NotifySizeTransition(const AnimationOption& option, Size fromSize, Size toSize, int32_t nodeId) override;
132 
133 protected:
134     AnimatableDimension width_ = AnimatableDimension(-1.0, DimensionUnit::PX);  // exclude margin
135     AnimatableDimension height_ = AnimatableDimension(-1.0, DimensionUnit::PX); // exclude margin
136     ShapeType shapeType_ = ShapeType::RECT;
137     Radius topLeftRadius_;
138     Radius topRightRadius_;
139     Radius bottomRightRadius_;
140     Radius bottomLeftRadius_;
141     ShapePoint start_;
142     ShapePoint end_;
143     std::vector<ShapePoint> points_;
144     AnimatablePath pathCmd_;
145     FillState fillState_;
146     StrokeState strokeState_;
147     std::pair<bool, bool> antiAlias_ = std::make_pair(false, true);
148     RefPtr<ShapeComponent> component_;
149 };
150 
151 } // namespace OHOS::Ace
152 
153 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SHAPE_RENDER_SHAPE_H
154