1 /*
2  * Copyright (c) 2024 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 #include "core/components_ng/pattern/image/image_overlay_modifier.h"
17 
18 #include "base/utils/utils.h"
19 #include "core/common/container.h"
20 #include "core/components_ng/render/drawing.h"
21 #include "core/components_ng/render/drawing_prop_convertor.h"
22 #include "core/pipeline_ng/pipeline_context.h"
23 
24 namespace OHOS::Ace::NG {
ImageOverlayModifier(const Color & selectedColor)25 ImageOverlayModifier::ImageOverlayModifier(const Color& selectedColor) : selectedColor_(selectedColor)
26 {
27     offset_ = AceType::MakeRefPtr<PropertyOffsetF>(OffsetF());
28     AttachProperty(offset_);
29     isSelected_ = AceType::MakeRefPtr<PropertyBool>(false);
30     AttachProperty(isSelected_);
31     size_ = AceType::MakeRefPtr<PropertySizeF>(SizeF());
32     AttachProperty(size_);
33 }
34 
onDraw(DrawingContext & drawingContext)35 void ImageOverlayModifier::onDraw(DrawingContext& drawingContext)
36 {
37     CHECK_NULL_VOID(isSelected_->Get());
38     auto& canvas = drawingContext.canvas;
39     canvas.Save();
40     RSBrush brush;
41     brush.SetAntiAlias(true);
42     brush.SetColor(selectedColor_.GetValue());
43     canvas.AttachBrush(brush);
44 
45     auto offset = offset_->Get();
46     auto size = size_->Get();
47 
48     canvas.DrawRect(
49         RSRect(offset.GetX(), offset.GetY(), offset.GetX() + size.Width(), offset.GetY() + size.Height()));
50 
51     canvas.DetachBrush();
52     canvas.Restore();
53 }
54 
55 } // namespace OHOS::Ace::NG