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_SELECT_OVERLAY_CONTENT_MODIFIER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SELECT_OVERLAY_CONTENT_MODIFIER_H
18 
19 #include <optional>
20 #include <vector>
21 
22 #include "base/geometry/dimension.h"
23 #include "base/geometry/ng/vector.h"
24 #include "base/memory/ace_type.h"
25 #include "core/components/common/properties/color.h"
26 #include "core/components_ng/base/frame_node.h"
27 #include "core/components_ng/base/modifier.h"
28 #include "core/components_ng/pattern/select_overlay/select_overlay_property.h"
29 #include "core/components_ng/render/node_paint_method.h"
30 #include "core/pipeline_ng/pipeline_context.h"
31 
32 namespace OHOS::Ace::NG {
33 
34 struct HandleDrawInfo {
35     OffsetF startPoint;
36     OffsetF endPoint;
37     OffsetF centerOffset;
38     float handleWidth = 0.0f;
39     bool isHandleLineShow = true;
40     bool isCircleShow = true;
41 };
42 
43 struct PaintHandleParams {
44     bool isHandleLineShow = true;
45     bool isCircleShow = true;
46     bool isDragging = false;
47 };
48 
49 class SelectOverlayContentModifier : public ContentModifier {
50     DECLARE_ACE_TYPE(SelectOverlayContentModifier, ContentModifier)
51 
52 public:
53     SelectOverlayContentModifier(const WeakPtr<Pattern>& pattern);
54 
55     ~SelectOverlayContentModifier() override = default;
56 
57     void onDraw(DrawingContext& drawingContext) override;
58 
SetInShowArea(bool inShowArea)59     void SetInShowArea(bool inShowArea)
60     {
61         CHECK_NULL_VOID(inShowArea_);
62         inShowArea_->Set(inShowArea);
63     }
64 
SetHandleReverse(bool reverse)65     void SetHandleReverse(bool reverse)
66     {
67         CHECK_NULL_VOID(handleReverse_);
68         handleReverse_->Set(reverse);
69     }
70 
SetIsSingleHandle(bool isSingle)71     void SetIsSingleHandle(bool isSingle)
72     {
73         CHECK_NULL_VOID(isSingleHandle_);
74         isSingleHandle_->Set(isSingle);
75     }
76 
SetFirstHandleIsShow(bool isShow)77     void SetFirstHandleIsShow(bool isShow)
78     {
79         CHECK_NULL_VOID(firstHandleIsShow_);
80         firstHandleIsShow_->Set(isShow);
81     }
82 
SetFirstCircleIsShow(bool isShow)83     void SetFirstCircleIsShow(bool isShow)
84     {
85         CHECK_NULL_VOID(firstCircleIsShow_);
86         firstCircleIsShow_->Set(isShow);
87     }
88 
SetSecondHandleIsShow(bool isShow)89     void SetSecondHandleIsShow(bool isShow)
90     {
91         CHECK_NULL_VOID(secondHandleIsShow_);
92         secondHandleIsShow_->Set(isShow);
93     }
94 
SetSecondCircleIsShow(bool isShow)95     void SetSecondCircleIsShow(bool isShow)
96     {
97         CHECK_NULL_VOID(secondCircleIsShow_);
98         secondCircleIsShow_->Set(isShow);
99     }
100 
SetIsHiddenHandle(bool isHidden)101     void SetIsHiddenHandle(bool isHidden)
102     {
103         CHECK_NULL_VOID(isHiddenHandle_);
104         isHiddenHandle_->Set(isHidden);
105     }
106 
SetIsHandleLineShow(bool isShow)107     void SetIsHandleLineShow(bool isShow)
108     {
109         CHECK_NULL_VOID(isHandleLineShow_);
110         isHandleLineShow_->Set(isShow);
111     }
112 
SetViewPort(const RectF & viewPort)113     void SetViewPort(const RectF& viewPort)
114     {
115         CHECK_NULL_VOID(viewPort_);
116         viewPort_->Set(viewPort);
117     }
118 
SetFirstHandle(const RectF & handle)119     void SetFirstHandle(const RectF& handle)
120     {
121         CHECK_NULL_VOID(firstHandle_);
122         firstHandle_->Set(handle);
123     }
124 
SetSecondHandle(const RectF & handle)125     void SetSecondHandle(const RectF& handle)
126     {
127         CHECK_NULL_VOID(secondHandle_);
128         secondHandle_->Set(handle);
129     }
130 
SetHandleColor(const Color & color)131     void SetHandleColor(const Color& color)
132     {
133         CHECK_NULL_VOID(handleColor_);
134         handleColor_->Set(color);
135     }
136 
SetInnerHandleColor(const Color & color)137     void SetInnerHandleColor(const Color& color)
138     {
139         CHECK_NULL_VOID(innerHandleColor_);
140         innerHandleColor_->Set(color);
141     }
142 
SetHandleRadius(float radius)143     void SetHandleRadius(float radius)
144     {
145         CHECK_NULL_VOID(handleRadius_);
146         handleRadius_->Set(radius);
147     }
148 
SetHandleStrokeWidth(float width)149     void SetHandleStrokeWidth(float width)
150     {
151         CHECK_NULL_VOID(handleStrokeWidth_);
152         handleStrokeWidth_->Set(width);
153     }
154 
SetInnerHandleRadius(float radius)155     void SetInnerHandleRadius(float radius)
156     {
157         CHECK_NULL_VOID(innerHandleRadius_);
158         innerHandleRadius_->Set(radius);
159     }
160 
SetHandleOpacity(float opacity)161     void SetHandleOpacity(float opacity)
162     {
163         CHECK_NULL_VOID(handleOpacity_);
164         handleOpacity_->Set(opacity);
165     }
166 
SetIsUsingMouse(bool value)167     void SetIsUsingMouse(bool value)
168     {
169         isUsingMouse_ = value;
170     }
171 
SetSecondHandlePaintInfo(const SelectHandlePaintInfo & paintInfo)172     void SetSecondHandlePaintInfo(const SelectHandlePaintInfo& paintInfo)
173     {
174         secondHandlePaintInfo_ = paintInfo;
175     }
176 
SetFirstHandlePaintInfo(const SelectHandlePaintInfo & paintInfo)177     void SetFirstHandlePaintInfo(const SelectHandlePaintInfo& paintInfo)
178     {
179         firstHandlePaintInfo_ = paintInfo;
180     }
181 
SetPaintHandleUsePoints(bool isPaintHandleUsePoints)182     void SetPaintHandleUsePoints(bool isPaintHandleUsePoints)
183     {
184         isPaintHandleUsePoints_ = isPaintHandleUsePoints;
185     }
186 
187     static OffsetF CalculateCenterPoint(const OffsetF& start, const OffsetF& end, float radius, bool handleOnTop);
188 
SetIsOverlayMode(bool isOverlayMode)189     void SetIsOverlayMode(bool isOverlayMode)
190     {
191         isOverlayMode_ = isOverlayMode;
192     }
193 
SetScale(const VectorF & scale)194     void SetScale(const VectorF& scale)
195     {
196         scale_ = scale;
197     }
198 
SetClipHandleDrawRect(bool isClipHandleDrawRect)199     void SetClipHandleDrawRect(bool isClipHandleDrawRect)
200     {
201         isClipHandleDrawRect_ = isClipHandleDrawRect;
202     }
203 
204 private:
205     void PaintHandle(RSCanvas& canvas, const RectF& handleRect, bool handleOnTop, const PaintHandleParams& params);
206     void PaintHandle(RSCanvas& canvas, const HandleDrawInfo& handleInfo);
207 
208     void PaintSingleHandle(RSCanvas& canvas);
209     bool PaintSingleHandleWithPoints(RSCanvas& canvas);
210     void PaintSingleHandleWithRect(RSCanvas& canvas);
211 
212     bool PaintDoubleHandleWithPoint(RSCanvas& canvas);
213     void PaintDoubleHandleWithRect(RSCanvas& canvas);
214     void PaintDoubleHandle(RSCanvas& canvas);
215 
216     void ClipViewPort(RSCanvas& canvas);
217     void ClipHandleDrawRect(RSCanvas& canvas, const RectF& handleRect, bool handleOnTop, bool isDragging);
218     RectF ConvertPointsToRect(const SelectHandlePaintInfo& paintInfo) const;
219     RectF GetFirstPaintRect() const;
220     RectF GetSecondPaintRect() const;
221 
GetDrawHandleRadius()222     float GetDrawHandleRadius()
223     {
224         return handleRadius_->Get() + handleStrokeWidth_->Get() / 2.0f;
225     }
226     bool CheckHandleCircleIsShow(const RectF& handlRect);
227     bool IsDraggingHandle(bool isFirst);
228 
229     RefPtr<PropertyBool> inShowArea_;
230     RefPtr<PropertyBool> handleReverse_;
231     RefPtr<PropertyBool> isSingleHandle_;
232     RefPtr<PropertyBool> firstHandleIsShow_;
233     RefPtr<PropertyBool> firstCircleIsShow_;
234     RefPtr<PropertyBool> secondHandleIsShow_;
235     RefPtr<PropertyBool> secondCircleIsShow_;
236     RefPtr<PropertyBool> isHiddenHandle_;
237     RefPtr<PropertyBool> isHandleLineShow_;
238     RefPtr<PropertyRectF> viewPort_;
239     RefPtr<PropertyRectF> firstHandle_;
240     RefPtr<PropertyRectF> secondHandle_;
241     RefPtr<PropertyColor> handleColor_;
242     RefPtr<PropertyColor> innerHandleColor_;
243     RefPtr<PropertyFloat> handleRadius_;
244     RefPtr<PropertyFloat> handleStrokeWidth_;
245     RefPtr<PropertyFloat> innerHandleRadius_;
246     RefPtr<AnimatablePropertyFloat> handleOpacity_;
247     bool isUsingMouse_ = false;
248     bool isPaintHandleUsePoints_ = false;
249     SelectHandlePaintInfo firstHandlePaintInfo_;
250     SelectHandlePaintInfo secondHandlePaintInfo_;
251     bool isOverlayMode_ = true;
252     bool isClipHandleDrawRect_ = false;
253     VectorF scale_ = VectorF(1.0f, 1.0f);
254     WeakPtr<Pattern> pattern_;
255 
256     ACE_DISALLOW_COPY_AND_MOVE(SelectOverlayContentModifier);
257 };
258 } // namespace OHOS::Ace::NG
259 
260 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SELECT_OVERLAY_CONTENT_MODIFIER_H