• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SHAPE_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SHAPE_SHAPE_COMPONENT_H
18 
19 #include "core/pipeline/base/measurable.h"
20 #include "frameworks/core/components/common/properties/animatable_path.h"
21 #include "frameworks/core/components/common/properties/radius.h"
22 #include "frameworks/core/components/common/properties/svg_paint_state.h"
23 #include "frameworks/core/pipeline/base/render_component.h"
24 
25 namespace OHOS::Ace {
26 
27 using ShapePoint = std::pair<Dimension, Dimension>;
28 using ShapePoints = std::vector<ShapePoint>;
29 
30 enum class ShapeType {
31     RECT = 0,
32     CIRCLE,
33     ELLIPSE,
34     LINE,
35     POLYGON,
36     POLYLINE,
37     PATH,
38 };
39 
40 // keep same as the doc
41 static const char* SHAPE_TYPE_STRINGS[] = { "Rect", "Circle", "Ellipse", "Line", "Polygon", "Polyline", "Path"};
42 
43 class ACE_EXPORT ShapeComponent : public RenderComponent, public Measurable {
44     DECLARE_ACE_TYPE(ShapeComponent, RenderComponent, Measurable);
45 
46 public:
ShapeComponent(ShapeType shapeType)47     explicit ShapeComponent(ShapeType shapeType) : RenderComponent(), shapeType_(shapeType) {}
48     ~ShapeComponent() override = default;
49     RefPtr<RenderNode> CreateRenderNode() override;
50     RefPtr<Element> CreateElement() override;
51 
SetTopLeftRadius(const Radius & topLeftRadius)52     void SetTopLeftRadius(const Radius& topLeftRadius)
53     {
54         topLeftRadius_ = topLeftRadius;
55     }
56 
SetTopRightRadius(const Radius & topRightRadius)57     void SetTopRightRadius(const Radius& topRightRadius)
58     {
59         topRightRadius_ = topRightRadius;
60     }
61 
SetBottomRightRadius(const Radius & bottomRightRadius)62     void SetBottomRightRadius(const Radius& bottomRightRadius)
63     {
64         bottomRightRadius_ = bottomRightRadius;
65     }
66 
SetBottomLeftRadius(const Radius & bottomLeftRadius)67     void SetBottomLeftRadius(const Radius& bottomLeftRadius)
68     {
69         bottomLeftRadius_ = bottomLeftRadius;
70     }
71 
SetStart(const ShapePoint & start)72     void SetStart(const ShapePoint& start)
73     {
74         start_ = start;
75     }
76 
SetEnd(const ShapePoint & end)77     void SetEnd(const ShapePoint& end)
78     {
79         end_ = end;
80     }
81 
SetPoints(const ShapePoints & points)82     void SetPoints(const ShapePoints& points)
83     {
84         points_ = points;
85     }
86 
87     void SetPathCmd(const std::string& pathCmd, const AnimationOption& option = AnimationOption())
88     {
89         pathCmd_ = AnimatablePath(pathCmd, option);
90     }
91 
GetShapeType()92     ShapeType GetShapeType() const
93     {
94         return shapeType_;
95     }
96 
GetTopLeftRadius()97     Radius GetTopLeftRadius() const
98     {
99         return topLeftRadius_;
100     }
101 
GetTopRightRadius()102     Radius GetTopRightRadius() const
103     {
104         return topRightRadius_;
105     }
106 
GetBottomRightRadius()107     Radius GetBottomRightRadius() const
108     {
109         return bottomRightRadius_;
110     }
111 
GetBottomLeftRadius()112     Radius GetBottomLeftRadius() const
113     {
114         return bottomLeftRadius_;
115     }
116 
117     void SetRadiusWidth(const Dimension& value, const AnimationOption& option = AnimationOption())
118     {
119         topLeftRadius_.SetX(value, option);
120         topRightRadius_.SetX(value, option);
121         bottomRightRadius_.SetX(value, option);
122         bottomLeftRadius_.SetX(value, option);
123     }
124 
125     void SetRadiusHeight(const Dimension& value, const AnimationOption& option = AnimationOption())
126     {
127         topLeftRadius_.SetY(value, option);
128         topRightRadius_.SetY(value, option);
129         bottomRightRadius_.SetY(value, option);
130         bottomLeftRadius_.SetY(value, option);
131     }
132 
GetStart()133     ShapePoint GetStart() const
134     {
135         return start_;
136     }
137 
GetEnd()138     ShapePoint GetEnd() const
139     {
140         return end_;
141     }
142 
GetPathCmd()143     const AnimatablePath& GetPathCmd() const
144     {
145         return pathCmd_;
146     }
147 
GetPoints()148     const std::vector<ShapePoint>& GetPoints() const
149     {
150         return points_;
151     }
152 
GetFillState()153     FillState GetFillState() const
154     {
155         return fillState_;
156     }
157 
GetStrokeState()158     StrokeState GetStrokeState() const
159     {
160         return strokeState_;
161     }
162 
SetAntiAlias(bool antiAlias)163     void SetAntiAlias(bool antiAlias)
164     {
165         antiAlias_.first = true;
166         antiAlias_.second = antiAlias;
167     }
168 
GetAntiAlias()169     const std::pair<bool, bool>& GetAntiAlias() const
170     {
171         return antiAlias_;
172     }
173 
174     void SetStroke(const Color& color, const AnimationOption& option = AnimationOption())
175     {
176         strokeState_.SetColor(color, true, option);
177     }
178 
179     void SetFill(const Color& color, const AnimationOption& option = AnimationOption())
180     {
181         fillState_.SetColor(color, true, option);
182     }
183 
184     void SetStrokeDashOffset(const Dimension& dashOffset, const AnimationOption& option = AnimationOption())
185     {
186         strokeState_.SetStrokeDashOffset(dashOffset, true, option);
187     }
188 
SetStrokeLineCap(LineCapStyle lineCapStyle)189     void SetStrokeLineCap(LineCapStyle lineCapStyle)
190     {
191         strokeState_.SetLineCap(lineCapStyle);
192     }
193 
SetStrokeLineJoin(LineJoinStyle lineJoinStyle)194     void SetStrokeLineJoin(LineJoinStyle lineJoinStyle)
195     {
196         strokeState_.SetLineJoin(lineJoinStyle);
197     }
198 
SetStrokeMiterLimit(double miterLimit)199     void SetStrokeMiterLimit(double miterLimit)
200     {
201         strokeState_.SetMiterLimit(miterLimit);
202     }
203 
204     void SetStrokeOpacity(double opacity, const AnimationOption& option = AnimationOption())
205     {
206         strokeState_.SetOpacity(std::clamp(opacity, 0.0, 1.0), true, option);
207     }
208 
209     void SetFillOpacity(double opacity, const AnimationOption& option = AnimationOption())
210     {
211         fillState_.SetOpacity(std::clamp(opacity, 0.0, 1.0), true, option);
212     }
213 
214     void SetStrokeWidth(const Dimension& lineWidth, const AnimationOption& option = AnimationOption())
215     {
216         strokeState_.SetLineWidth(lineWidth, true, option);
217     }
218 
SetStrokeDashArray(const std::vector<Dimension> & segments)219     void SetStrokeDashArray(const std::vector<Dimension>& segments)
220     {
221         strokeState_.SetStrokeDashArray(segments);
222     }
223 
224     void InheritShapeStyle(const FillState& fillState, const StrokeState& strokeState, bool antiAlias);
225 
226 private:
227     ShapeType shapeType_ = ShapeType::RECT;
228     Radius topLeftRadius_ = Radius(-1);
229     Radius topRightRadius_ = Radius(-1);
230     Radius bottomRightRadius_ = Radius(-1);
231     Radius bottomLeftRadius_ = Radius(-1);
232     ShapePoint start_;
233     ShapePoint end_;
234     std::vector<ShapePoint> points_;
235     AnimatablePath pathCmd_;
236     FillState fillState_;
237     StrokeState strokeState_;
238     std::pair<bool, bool> antiAlias_ = std::make_pair(false, true);
239 };
240 
241 } // namespace OHOS::Ace
242 
243 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SHAPE_SHAPE_COMPONENT_H
244