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_ARC_ARC_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_ARC_ARC_COMPONENT_H
18 
19 #include "base/geometry/dimension.h"
20 #include "base/log/log.h"
21 #include "core/components/common/properties/color.h"
22 #include "core/pipeline/base/render_component.h"
23 
24 namespace OHOS::Ace {
25 
26 enum class ArcFlex {
27     FLEX_X,
28     FLEX_Y,
29     FLEX_XY,
30 };
31 
32 class ArcComponent : public RenderComponent {
33     DECLARE_ACE_TYPE(ArcComponent, RenderComponent);
34 
35 public:
36     ArcComponent() = default;
37     ~ArcComponent() override = default;
38     RefPtr<Element> CreateElement() override;
39     RefPtr<RenderNode> CreateRenderNode() override;
40 
ArcComponent(double outerRadius,double width)41     ArcComponent(double outerRadius, double width)
42     {
43         SetOuterRadius(outerRadius);
44         SetWidth(width);
45     }
46 
ArcComponent(double outerRadius,double width,double startAngle,double sweepAngle)47     ArcComponent(double outerRadius, double width, double startAngle, double sweepAngle)
48     {
49         SetOuterRadius(outerRadius);
50         SetWidth(width);
51         startAngle_ = startAngle;
52         sweepAngle_ = sweepAngle;
53     }
54 
ArcComponent(double outerRadius,double width,double startAngle,double sweepAngle,const Color & color)55     ArcComponent(double outerRadius, double width, double startAngle, double sweepAngle, const Color& color)
56     {
57         SetOuterRadius(outerRadius);
58         SetWidth(width);
59         startAngle_ = startAngle;
60         sweepAngle_ = sweepAngle;
61         color_ = color;
62     }
63 
GetOuterRadius()64     const Dimension& GetOuterRadius() const
65     {
66         return outerRadius_;
67     }
68 
SetOuterRadius(const Dimension & outerRadius)69     void SetOuterRadius(const Dimension& outerRadius)
70     {
71         if (!outerRadius.IsValid()) {
72             LOGE("Invalid outerRadius:%{public}lf", outerRadius.Value());
73             return;
74         }
75         outerRadius_ = outerRadius;
76     }
77 
SetOuterRadius(double outerRadius)78     void SetOuterRadius(double outerRadius)
79     {
80         if (outerRadius < 0.0) {
81             LOGE("Invalid outerRadius:%{public}lf", outerRadius);
82             return;
83         }
84         outerRadius_ = Dimension(outerRadius);
85     }
86 
GetWidth()87     const Dimension& GetWidth() const
88     {
89         return width_;
90     }
91 
SetWidth(const Dimension & width)92     void SetWidth(const Dimension& width)
93     {
94         if (!width.IsValid()) {
95             LOGE("Invalid width:%{public}lf", width.Value());
96             return;
97         }
98         width_ = width;
99     }
100 
SetWidth(double width)101     void SetWidth(double width)
102     {
103         if (width < 0.0) {
104             LOGE("Invalid width:%{public}lf", width);
105             return;
106         }
107         width_ = Dimension(width);
108     }
109 
GetStartAngle()110     double GetStartAngle() const
111     {
112         return startAngle_;
113     }
114 
SetStartAngle(double startAngle)115     void SetStartAngle(double startAngle)
116     {
117         startAngle_ = startAngle;
118     }
119 
GetSweepAngle()120     double GetSweepAngle() const
121     {
122         return sweepAngle_;
123     }
124 
SetSweepAngle(double sweepAngle)125     void SetSweepAngle(double sweepAngle)
126     {
127         sweepAngle_ = sweepAngle;
128     }
129 
GetColor()130     const Color& GetColor() const
131     {
132         return color_;
133     }
134 
SetColor(const Color & color)135     void SetColor(const Color& color)
136     {
137         color_ = color;
138     }
139 
GetArcFlex()140     ArcFlex GetArcFlex() const
141     {
142         return flex_;
143     }
144 
SetArcFlex(ArcFlex flex)145     void SetArcFlex(ArcFlex flex)
146     {
147         flex_ = flex;
148     }
149 
GetShadowWidth()150     const Dimension& GetShadowWidth() const
151     {
152         return arcShadowWidth_;
153     }
154 
SetShadowWidth(const Dimension & width)155     void SetShadowWidth(const Dimension& width)
156     {
157         arcShadowWidth_ = width;
158     }
159 
IsAcrValid()160     bool IsAcrValid() const
161     {
162         return !NearZero(sweepAngle_);
163     }
164 
IsArcShow()165     bool IsArcShow() const
166     {
167         return color_.GetAlpha() != 0;
168     }
169 
170 private:
171     Dimension outerRadius_;
172     Dimension width_;
173     Dimension arcShadowWidth_;
174     ArcFlex flex_ = ArcFlex::FLEX_X;
175     Color color_ = Color::BLACK;
176     double startAngle_ = 0.0;
177     double sweepAngle_ = 0.0;
178 };
179 
180 } // namespace OHOS::Ace
181 
182 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_ARC_ARC_COMPONENT_H
183