• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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/text_drag/text_drag_overlay_modifier.h"
17 
18 #include <variant>
19 
20 #include "base/geometry/rect.h"
21 #include "base/utils/utils.h"
22 #include "core/components_ng/pattern/image/image_pattern.h"
23 #include "core/components_ng/pattern/text_drag/text_drag_pattern.h"
24 #include "core/components_ng/render/adapter/pixelmap_image.h"
25 #include "core/components_ng/render/drawing_prop_convertor.h"
26 
27 namespace OHOS::Ace::NG {
28 constexpr int32_t TEXT_ANIMATION_DURATION = 300;
TextDragOverlayModifier(const WeakPtr<OHOS::Ace::NG::Pattern> & pattern)29 TextDragOverlayModifier::TextDragOverlayModifier(const WeakPtr<OHOS::Ace::NG::Pattern>& pattern) : pattern_(pattern)
30 {
31     backgroundOffset_ = AceType::MakeRefPtr<AnimatablePropertyFloat>(TEXT_DRAG_OFFSET.ConvertToPx());
32     AttachProperty(backgroundOffset_);
33 }
34 
onDraw(DrawingContext & context)35 void TextDragOverlayModifier::onDraw(DrawingContext& context)
36 {
37     auto pattern = DynamicCast<TextDragPattern>(pattern_.Upgrade());
38     CHECK_NULL_VOID(pattern);
39     auto& canvas = context.canvas;
40     Color color(TEXT_DRAG_COLOR_BG);
41     RSBrush brush;
42     brush.SetColor(ToRSColor(color));
43     brush.SetAntiAlias(true);
44     canvas.AttachBrush(brush);
45     if (!isAnimating_) {
46         canvas.DrawPath(*pattern->GetBackgroundPath());
47     } else {
48         canvas.DrawPath(*pattern->GenerateBackgroundPath(backgroundOffset_->Get()));
49     }
50     canvas.DetachBrush();
51     canvas.ClipPath(*pattern->GetClipPath(), RSClipOp::INTERSECT, true);
52     auto&& paragraph = pattern->GetParagraph();
53     if (std::holds_alternative<RefPtr<Paragraph>>(paragraph)) {
54         auto paragraphPtr = std::get<RefPtr<Paragraph>>(paragraph);
55         paragraphPtr->Paint(canvas, pattern->GetTextRect().GetX(), pattern->GetTextRect().GetY());
56     } else {
57         auto rsParagraph = std::get<std::shared_ptr<RSParagraph>>(paragraph);
58         rsParagraph->Paint(&canvas, pattern->GetTextRect().GetX(), pattern->GetTextRect().GetY());
59     }
60 
61     size_t index = 0;
62     auto contentOffset = pattern->GetContentOffset();
63     auto imageChildren = pattern->GetImageChildren();
64     auto rectsForPlaceholders = pattern->GetRectsForPlaceholders();
65     for (const auto& child : imageChildren) {
66         auto rect = rectsForPlaceholders.at(index);
67         auto offset = OffsetF(rect.Left(), rect.Top()) - contentOffset;
68         auto imageChild = DynamicCast<ImagePattern>(child->GetPattern());
69         if (imageChild) {
70             RectF imageRect(offset.GetX(), offset.GetY(), rect.Width(), rect.Height());
71             auto canvasImage = imageChild->GetCanvasImage();
72             CHECK_NULL_VOID(canvasImage);
73             auto pixelMapImage = DynamicCast<PixelMapImage>(canvasImage);
74             CHECK_NULL_VOID(pixelMapImage);
75             pixelMapImage->DrawRect(canvas, ToRSRect(imageRect));
76         }
77         ++index;
78     }
79 }
80 
StartAnimate()81 void TextDragOverlayModifier::StartAnimate()
82 {
83     isAnimating_ = true;
84     backgroundOffset_->Set(0);
85     AnimationOption option;
86     option.SetDuration(TEXT_ANIMATION_DURATION);
87     option.SetCurve(Curves::EASE_OUT);
88     option.SetDelay(0);
89     option.SetOnFinishEvent([this]() { isAnimating_ = false; });
90     AnimationUtils::Animate(
91         option, [this]() { SetBackgroundOffset(TEXT_DRAG_OFFSET.ConvertToPx()); }, option.GetOnFinishEvent());
92 }
93 
SetBackgroundOffset(float offset)94 void TextDragOverlayModifier::SetBackgroundOffset(float offset)
95 {
96     backgroundOffset_->Set(offset);
97 }
98 } // namespace OHOS::Ace::NG
99