1 /*
2  * Copyright (c) 2022 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_NG_PATTERN_CIRCLE_PAINT_METHOD_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CIRCLE_PAINT_METHOD_H
18 
19 #include "base/geometry/ng/size_t.h"
20 #include "core/components_ng/base/geometry_node.h"
21 #include "core/components_ng/pattern/shape/shape_paint_method.h"
22 #include "core/components_ng/pattern/shape/shape_overlay_modifier.h"
23 #include "core/components_ng/pattern/shape/shape_paint_property.h"
24 #include "core/components_ng/render/circle_painter.h"
25 #include "core/components_ng/render/node_paint_method.h"
26 
27 namespace OHOS::Ace::NG {
28 
29 class ACE_EXPORT CirclePaintMethod : public ShapePaintMethod {
30     DECLARE_ACE_TYPE(CirclePaintMethod, ShapePaintMethod)
31 public:
32     CirclePaintMethod() = default;
CirclePaintMethod(const RefPtr<ShapePaintProperty> & shapePaintProperty,const RefPtr<ShapeOverlayModifier> & shapeOverlayModifier)33     CirclePaintMethod(
34         const RefPtr<ShapePaintProperty>& shapePaintProperty,
35         const RefPtr<ShapeOverlayModifier>& shapeOverlayModifier)
36         : ShapePaintMethod(shapePaintProperty, shapeOverlayModifier)
37     {}
38     ~CirclePaintMethod() override = default;
39 
GetContentDrawFunction(PaintWrapper * paintWrapper)40     CanvasDrawFunction GetContentDrawFunction(PaintWrapper* paintWrapper) override
41     {
42         CHECK_NULL_RETURN(paintWrapper, nullptr);
43         auto shapePaintProperty = DynamicCast<ShapePaintProperty>(paintWrapper->GetPaintProperty()->Clone());
44         CHECK_NULL_RETURN(shapePaintProperty, nullptr);
45 
46         if (propertiesFromAncestor_) {
47             if (!shapePaintProperty->HasFill() && propertiesFromAncestor_->HasFill()) {
48                 auto renderContext = paintWrapper->GetRenderContext();
49                 renderContext->UpdateForegroundColor(propertiesFromAncestor_->GetFillValue());
50                 renderContext->ResetForegroundColorStrategy();
51             }
52             shapePaintProperty->UpdateShapeProperty(propertiesFromAncestor_);
53         }
54         if (paintWrapper->HasForegroundColor()) {
55             shapePaintProperty->UpdateFill(paintWrapper->GetForegroundColor());
56         } else if (paintWrapper->HasForegroundColorStrategy()) {
57             shapePaintProperty->UpdateFill(Color::FOREGROUND);
58             shapePaintProperty->ResetFillOpacity();
59         }
60         float height = paintWrapper->GetContentSize().Height();
61         float width = paintWrapper->GetContentSize().Width();
62         float radius = (width > height ? height : width) * 0.5;
63         return
64             [radiusValue = radius, offsetValue = paintWrapper->GetContentOffset(), shapePaintProperty, paintWrapper](
65                 RSCanvas& canvas) {
66                     CirclePainter::DrawCircle(canvas, radiusValue, offsetValue, *shapePaintProperty);
67                     if (paintWrapper) {
68                         paintWrapper->FlushOverlayModifier();
69                     }
70                 };
71     }
72 
GetOverlayModifier(PaintWrapper * paintWrapper)73     RefPtr<Modifier> GetOverlayModifier(PaintWrapper* paintWrapper) override
74     {
75         CHECK_NULL_RETURN(paintWrapper, nullptr);
76         CHECK_NULL_RETURN(shapeOverlayModifier_, nullptr);
77         auto paintProperty = paintWrapper->GetPaintProperty();
78         CHECK_NULL_RETURN(paintProperty, nullptr);
79         auto shapePaintProperty = DynamicCast<ShapePaintProperty>(paintProperty->Clone());
80         CHECK_NULL_RETURN(shapePaintProperty, nullptr);
81 
82         if (propertiesFromAncestor_) {
83             shapePaintProperty->UpdateShapeProperty(propertiesFromAncestor_);
84         }
85         float height = paintWrapper->GetContentSize().Height();
86         float width = paintWrapper->GetContentSize().Width();
87         auto offset = paintWrapper->GetContentOffset();
88         float strokeWidth =
89             shapePaintProperty->GetStrokeWidthValue(ShapePaintProperty::STROKE_WIDTH_DEFAULT).ConvertToPx();
90         RectF boundsRect = { (offset.GetX() - strokeWidth), (offset.GetY() - strokeWidth), (width + strokeWidth * 2),
91             (height + strokeWidth * 2) };
92         shapeOverlayModifier_->SetBoundsRect(boundsRect);
93         return shapeOverlayModifier_;
94     }
95 
96     ACE_DISALLOW_COPY_AND_MOVE(CirclePaintMethod);
97 };
98 
99 } // namespace OHOS::Ace::NG
100 
101 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CIRCLE_PAINT_METHOD_H
102