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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_DRAG_RICH_EDITOR_DRAG_OVERLAY_MODIFIER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_DRAG_RICH_EDITOR_DRAG_OVERLAY_MODIFIER_H
18 
19 #include "base/memory/ace_type.h"
20 #include "base/memory/referenced.h"
21 #include "core/components/common/properties/color.h"
22 #include "core/components_ng/base/modifier.h"
23 #include "core/components_ng/pattern/pattern.h"
24 #include "core/components_ng/pattern/rich_editor/rich_editor_pattern.h"
25 #include "core/components_ng/pattern/text_drag/text_drag_overlay_modifier.h"
26 #include "core/components_ng/pattern/text_drag/text_drag_pattern.h"
27 
28 namespace OHOS::Ace::NG {
29 class TextDragPattern;
30 
31 class RichEditorDragOverlayModifier : public TextDragOverlayModifier {
32     DECLARE_ACE_TYPE(RichEditorDragOverlayModifier, TextDragOverlayModifier);
33 
34 public:
RichEditorDragOverlayModifier(const WeakPtr<OHOS::Ace::NG::Pattern> & pattern,const WeakPtr<TextPattern> & hostPattern)35     RichEditorDragOverlayModifier(
36         const WeakPtr<OHOS::Ace::NG::Pattern>& pattern, const WeakPtr<TextPattern>& hostPattern)
37         : TextDragOverlayModifier(pattern), hostPattern_(hostPattern)
38     {
39         firstHandle_ = AceType::MakeRefPtr<PropertyRectF>(RectF(0, 0, 0, 0));
40         secondHandle_ = AceType::MakeRefPtr<PropertyRectF>(RectF(0, 0, 0, 0));
41         handleRadius_ = AceType::MakeRefPtr<PropertyFloat>(0.0);
42         handleColor_ = AceType::MakeRefPtr<PropertyColor>(Color::BLACK);
43         innerHandleRadius_ = AceType::MakeRefPtr<PropertyFloat>(0.0);
44         innerHandleColor_ = AceType::MakeRefPtr<PropertyColor>(Color::BLACK);
45         handleOpacity_ = AceType::MakeRefPtr<AnimatablePropertyFloat>(0.0);
46         selectedColor_ = AceType::MakeRefPtr<PropertyInt>(0);
47         shadowOpacity_ = AceType::MakeRefPtr<AnimatablePropertyFloat>(0.0);
48         AttachProperty(firstHandle_);
49         AttachProperty(secondHandle_);
50         AttachProperty(handleRadius_);
51         AttachProperty(handleColor_);
52         AttachProperty(innerHandleRadius_);
53         AttachProperty(innerHandleColor_);
54         AttachProperty(handleOpacity_);
55         AttachProperty(selectedColor_);
56         AttachProperty(shadowOpacity_);
57     }
58 
59     ~RichEditorDragOverlayModifier() override = default;
60 
61     void onDraw(DrawingContext& context) override;
62     void StartFloatingAnimate() override;
63     void StartFloatingSelBackgroundAnimate();
64 
SetFirstHandle(const RectF & handle)65     void SetFirstHandle(const RectF& handle)
66     {
67         CHECK_NULL_VOID(firstHandle_);
68         firstHandle_->Set(handle);
69     }
70 
SetSecondHandle(const RectF & handle)71     void SetSecondHandle(const RectF& handle)
72     {
73         CHECK_NULL_VOID(secondHandle_);
74         secondHandle_->Set(handle);
75     }
76 
SetHandleRadius(float radius)77     void SetHandleRadius(float radius)
78     {
79         CHECK_NULL_VOID(handleRadius_);
80         handleRadius_->Set(radius);
81     }
82 
SetHandleColor(const Color & color)83     void SetHandleColor(const Color& color)
84     {
85         CHECK_NULL_VOID(handleColor_);
86         handleColor_->Set(color);
87     }
88 
SetInnerHandleRadius(float radius)89     void SetInnerHandleRadius(float radius)
90     {
91         CHECK_NULL_VOID(innerHandleRadius_);
92         innerHandleRadius_->Set(radius);
93     }
94 
SetInnerHandleColor(const Color & color)95     void SetInnerHandleColor(const Color& color)
96     {
97         CHECK_NULL_VOID(innerHandleColor_);
98         innerHandleColor_->Set(color);
99     }
100 
SetHandleOpacity(float opacity)101     void SetHandleOpacity(float opacity)
102     {
103         CHECK_NULL_VOID(handleOpacity_);
104         handleOpacity_->Set(opacity);
105     }
106 
SetSelectedColor(uint32_t selectedColor)107     void SetSelectedColor(uint32_t selectedColor)
108     {
109         CHECK_NULL_VOID(selectedColor_);
110         selectedColor_->Set(static_cast<int32_t>(selectedColor));
111     }
112 
SetShadowOpacity(float opacity)113     void SetShadowOpacity(float opacity)
114     {
115         CHECK_NULL_VOID(shadowOpacity_);
116         shadowOpacity_->Set(opacity);
117     }
118 
119 private:
120     void PaintImage(DrawingContext& context);
121     void PaintImageNode(DrawingContext& context, const RefPtr<FrameNode>& imageNode,
122         const RefPtr<ImagePattern>& pattern, const OffsetF& offset);
123     void PaintFrameNode(DrawingContext& context, const RefPtr<FrameNode>& frameNode,
124         const RefPtr<Pattern>& pattern, const OffsetF& offset);
125     void PaintBackground(const RSPath& path, RSCanvas& canvas, RefPtr<TextDragPattern> textDragPattern,
126         RefPtr<RichEditorPattern> richEditorPattern);
127     void PaintSelBackground(RSCanvas& canvas, RefPtr<TextDragPattern> textDragPattern,
128         RefPtr<RichEditorPattern> richEditorPattern);
129     void PaintHandle(RSCanvas& canvas, const RectF& handleRect, bool isFirstHandle, float startX, float startY);
130     void PaintHandleRing(RSCanvas& canvas);
131     void PaintHandleHold(RSCanvas& canvas, const RectF& handleRect, const OffsetF& startPoint,
132         const OffsetF& endPoint);
133     void PaintShadow(const RSPath& path, const Shadow& shadow, RSCanvas& canvas);
134     Color GetDragBackgroundColor(const Color& defaultColor);
135 
136     DragAnimType type_ = DragAnimType::DEFAULT;
137     const WeakPtr<TextPattern> hostPattern_;
138     RefPtr<PropertyRectF> firstHandle_;
139     RefPtr<PropertyRectF> secondHandle_;
140     RefPtr<PropertyFloat> handleRadius_;
141     RefPtr<PropertyColor> handleColor_;
142     RefPtr<PropertyFloat> innerHandleRadius_;
143     RefPtr<PropertyColor> innerHandleColor_;
144     RefPtr<AnimatablePropertyFloat> handleOpacity_;
145     RefPtr<AnimatablePropertyFloat> shadowOpacity_;
146     RefPtr<PropertyInt> selectedColor_;
147     ACE_DISALLOW_COPY_AND_MOVE(RichEditorDragOverlayModifier);
148 };
149 } // namespace OHOS::Ace::NG
150 
151 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_DRAG_RICH_EDITOR_DRAG_OVERLAY_MODIFIER_H
152