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 #ifdef NEW_SKIA
46 if (!isAnimating_) {
47 canvas.DrawPath(*pattern->GetBackgroundPath());
48 } else {
49 canvas.DrawPath(*pattern->GenerateBackgroundPath(backgroundOffset_->Get()));
50 }
51 canvas.ClipPath(*pattern->GetClipPath(), RSClipOp::INTERSECT, true);
52 #else
53 if (!isAnimating_) {
54 canvas.DrawPath(*pattern->GetBackgroundPath());
55 } else {
56 canvas.DrawPath(*pattern->GenerateBackgroundPath(backgroundOffset_->Get()));
57 }
58 canvas.ClipPath(*pattern->GetClipPath(), RSClipOp::INTERSECT, true);
59 #endif
60 auto&& paragraph = pattern->GetParagraph();
61 if (std::holds_alternative<RefPtr<Paragraph>>(paragraph)) {
62 auto paragraphPtr = std::get<RefPtr<Paragraph>>(paragraph);
63 paragraphPtr->Paint(canvas, pattern->GetTextRect().GetX(), pattern->GetTextRect().GetY());
64 } else {
65 auto rsParagraph = std::get<std::shared_ptr<RSParagraph>>(paragraph);
66 rsParagraph->Paint(&canvas, pattern->GetTextRect().GetX(), pattern->GetTextRect().GetY());
67 }
68
69 size_t index = 0;
70 auto contentOffset = pattern->GetContentOffset();
71 auto imageChildren = pattern->GetImageChildren();
72 auto rectsForPlaceholders = pattern->GetRectsForPlaceholders();
73 for (const auto& child : imageChildren) {
74 auto rect = rectsForPlaceholders.at(index);
75 auto offset = OffsetF(rect.Left(), rect.Top()) - contentOffset;
76 auto imageChild = DynamicCast<ImagePattern>(child->GetPattern());
77 if (imageChild) {
78 RectF imageRect(offset.GetX(), offset.GetY(), rect.Width(), rect.Height());
79 auto canvasImage = imageChild->GetCanvasImage();
80 CHECK_NULL_VOID(canvasImage);
81 auto pixelMapImage = DynamicCast<PixelMapImage>(canvasImage);
82 CHECK_NULL_VOID(pixelMapImage);
83 pixelMapImage->DrawRect(canvas, ToRSRect(imageRect));
84 }
85 ++index;
86 }
87 }
88
StartAnimate()89 void TextDragOverlayModifier::StartAnimate()
90 {
91 isAnimating_ = true;
92 backgroundOffset_->Set(0);
93 AnimationOption option;
94 option.SetDuration(TEXT_ANIMATION_DURATION);
95 option.SetCurve(Curves::EASE_OUT);
96 option.SetDelay(0);
97 option.SetOnFinishEvent([this]() { isAnimating_ = false; });
98 AnimationUtils::Animate(
99 option, [this]() { SetBackgroundOffset(TEXT_DRAG_OFFSET.ConvertToPx()); }, option.GetOnFinishEvent());
100 }
101
SetBackgroundOffset(float offset)102 void TextDragOverlayModifier::SetBackgroundOffset(float offset)
103 {
104 backgroundOffset_->Set(offset);
105 }
106 } // namespace OHOS::Ace::NG
107