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_CHECKBOXGROUP_CHECKBOXGROUP_MODIFIER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOXGROUP_CHECKBOXGROUP_MODIFIER_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components_ng/base/modifier.h"
22 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_paint_property.h"
23 #include "core/components_ng/pattern/radio/radio_paint_method.h"
24 #include "core/components_ng/property/property.h"
25 #include "core/components_ng/render/animation_utils.h"
26 #include "core/components_ng/render/drawing_forward.h"
27 
28 namespace OHOS::Ace::NG {
29 class CheckBoxGroupModifier : public ContentModifier {
30     DECLARE_ACE_TYPE(CheckBoxGroupModifier, ContentModifier);
31 
32 public:
33     struct Parameters {
34         float borderWidth;
35         float borderRadius;
36         float checkStroke;
37         float checkMarkPaintSize;
38         float hoverDuration;
39         float hoverToTouchDuration;
40         Color pointColor;
41         Color activeColor;
42         Color inactiveColor;
43         Color shadowColor;
44         Color clickEffectColor;
45         Color hoverColor;
46         Color inactivePointColor;
47         Dimension hoverRadius;
48         Dimension hotZoneHorizontalPadding;
49         Dimension hotZoneVerticalPadding;
50         Dimension shadowWidth;
51         UIStatus uiStatus;
52         Dimension defaultPaddingSize;
53         CheckBoxGroupPaintProperty::SelectStatus status;
54     };
55 
56     explicit CheckBoxGroupModifier(const Parameters& parameters);
57     ~CheckBoxGroupModifier() override = default;
onDraw(DrawingContext & context)58     void onDraw(DrawingContext& context) override
59     {
60         PaintCheckBox(context, offset_->Get(), size_->Get());
61     }
62 
UpdateAnimatableProperty()63     void UpdateAnimatableProperty()
64     {
65         switch (touchHoverType_) {
66             case TouchHoverAnimationType::HOVER:
67                 SetBoardColor(LinearColor(hoverColor_), hoverDuration_, Curves::FRICTION);
68                 break;
69             case TouchHoverAnimationType::PRESS_TO_HOVER:
70                 SetBoardColor(LinearColor(hoverColor_), hoverToTouchDuration_, Curves::SHARP);
71                 break;
72             case TouchHoverAnimationType::NONE:
73                 SetBoardColor(LinearColor(hoverColor_.BlendOpacity(0)), hoverDuration_, Curves::FRICTION);
74                 break;
75             case TouchHoverAnimationType::HOVER_TO_PRESS:
76                 SetBoardColor(LinearColor(clickEffectColor_), hoverToTouchDuration_, Curves::SHARP);
77                 break;
78             case TouchHoverAnimationType::PRESS:
79                 SetBoardColor(LinearColor(clickEffectColor_), hoverDuration_, Curves::FRICTION);
80                 break;
81             default:
82                 break;
83         }
84     }
85 
SetBoardColor(LinearColor color,int32_t duratuion,const RefPtr<CubicCurve> & curve)86     void SetBoardColor(LinearColor color, int32_t duratuion, const RefPtr<CubicCurve>& curve)
87     {
88         if (animateTouchHoverColor_) {
89             AnimationOption option = AnimationOption();
90             option.SetDuration(duratuion);
91             option.SetCurve(curve);
92             AnimationUtils::Animate(option, [&]() { animateTouchHoverColor_->Set(color); });
93         }
94     }
95 
96     void PaintCheckBox(DrawingContext& context, const OffsetF& paintOffset, const SizeF& contentSize) const;
97     void PaintCheckBoxGroupPartStatus(
98         RSCanvas& canvas, const OffsetF& paintOffset, RSBrush& brush, RSPen& pen, const SizeF& paintSize) const;
99     void DrawCheck(RSCanvas& canvas, const OffsetF& origin, RSPen& pen, RSPen& shadowPen, const SizeF& paintSize) const;
100     void DrawUnselected(RSCanvas& canvas, const OffsetF& origin, RSPen& pen, const SizeF& paintSize) const;
101     void DrawActiveBorder(RSCanvas& canvas, const OffsetF& paintOffset, RSBrush& brush, const SizeF& paintSize) const;
102     void DrawUnselectedBorder(
103         RSCanvas& canvas, const OffsetF& paintOffset, RSBrush& brush, const SizeF& paintSize) const;
104     void DrawPart(RSCanvas& canvas, const OffsetF& origin, RSPen& pen, const SizeF& paintSize) const;
105     void DrawTouchAndHoverBoard(RSCanvas& canvas, const SizeF& contentSize, const OffsetF& offset) const;
106     void DrawRectOrCircle(RSCanvas& canvas, const RSRoundRect& rrect) const;
107 
SetBorderWidth(float borderWidth)108     void SetBorderWidth(float borderWidth)
109     {
110         borderWidth_ = borderWidth;
111     }
112 
SetBorderRadius(float borderRadius)113     void SetBorderRadius(float borderRadius)
114     {
115         borderRadius_ = borderRadius;
116     }
117 
SetCheckStroke(float checkStroke)118     void SetCheckStroke(float checkStroke)
119     {
120         checkStroke_->Set(checkStroke);
121     }
122 
SetCheckMarkPaintSize(float checkMarkPaintSize)123     void SetCheckMarkPaintSize(float checkMarkPaintSize)
124     {
125         checkMarkPaintSize_->Set(checkMarkPaintSize);
126     }
127 
SetPointColor(Color pointColor)128     void SetPointColor(Color pointColor)
129     {
130         pointColor_->Set(LinearColor(pointColor));
131     }
132 
SetActiveColor(Color activeColor)133     void SetActiveColor(Color activeColor)
134     {
135         activeColor_->Set(LinearColor(activeColor));
136     }
137 
SetInactiveColor(Color inactiveColor)138     void SetInactiveColor(Color inactiveColor)
139     {
140         inactiveColor_->Set(LinearColor(inactiveColor));
141     }
142 
SetShadowColor(Color shadowColor)143     void SetShadowColor(Color shadowColor)
144     {
145         shadowColor_ = shadowColor;
146     }
147 
SetClickEffectColor(Color clickEffectColor)148     void SetClickEffectColor(Color clickEffectColor)
149     {
150         clickEffectColor_ = clickEffectColor;
151     }
152 
SetHoverColor(Color hoverColor)153     void SetHoverColor(Color hoverColor)
154     {
155         hoverColor_ = hoverColor;
156     }
157 
SetInactivePointColor(Color inactivePointColor)158     void SetInactivePointColor(Color inactivePointColor)
159     {
160         inactivePointColor_ = inactivePointColor;
161     }
162 
SetHoverRadius(Dimension hoverRadius)163     void SetHoverRadius(Dimension hoverRadius)
164     {
165         hoverRadius_ = hoverRadius;
166     }
167 
SetHotZoneHorizontalPadding(Dimension hotZoneHorizontalPadding)168     void SetHotZoneHorizontalPadding(Dimension hotZoneHorizontalPadding)
169     {
170         hotZoneHorizontalPadding_ = hotZoneHorizontalPadding;
171     }
172 
SetHotZoneVerticalPadding(Dimension hotZoneVerticalPadding)173     void SetHotZoneVerticalPadding(Dimension hotZoneVerticalPadding)
174     {
175         hotZoneVerticalPadding_ = hotZoneVerticalPadding;
176     }
177 
SetShadowWidth(Dimension shadowWidth)178     void SetShadowWidth(Dimension shadowWidth)
179     {
180         shadowWidth_ = shadowWidth;
181     }
182 
SetEnabled(bool value)183     void SetEnabled(bool value)
184     {
185         enabled_->Set(value);
186     }
187 
SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)188     void SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)
189     {
190         touchHoverType_ = touchHoverType;
191     }
192 
SetUiStatus(UIStatus uiStatus)193     void SetUiStatus(UIStatus uiStatus)
194     {
195         if (uiStatus_) {
196             uiStatus_->Set(static_cast<int>(uiStatus));
197         }
198     }
199 
SetSelectStatus(CheckBoxGroupPaintProperty::SelectStatus status)200     void SetSelectStatus(CheckBoxGroupPaintProperty::SelectStatus status)
201     {
202         if (status_) {
203             status_->Set(static_cast<int>(status));
204         }
205     }
206 
GetBorderWidth()207     float GetBorderWidth() const
208     {
209         return borderWidth_;
210     }
211 
SetOffset(OffsetF & offset)212     void SetOffset(OffsetF& offset)
213     {
214         if (offset_) {
215             offset_->Set(offset);
216         }
217     }
218 
SetSize(SizeF & size)219     void SetSize(SizeF& size)
220     {
221         if (size_) {
222             size_->Set(size);
223         }
224     }
225 
SetHoverEffectType(HoverEffectType hoverEffectType)226     void SetHoverEffectType(HoverEffectType hoverEffectType)
227     {
228         hoverEffectType_ = hoverEffectType;
229     }
230 
SetCheckboxGroupStyle(CheckBoxStyle checkBoxGroupStyle)231     void SetCheckboxGroupStyle(CheckBoxStyle checkBoxGroupStyle)
232     {
233         checkBoxGroupStyle_ = checkBoxGroupStyle;
234         CHECK_NULL_VOID(checkBoxGroupShape_);
235         checkBoxGroupShape_->Set(static_cast<int32_t>(checkBoxGroupStyle));
236     }
237 
238 private:
239     RefPtr<AnimatablePropertyColor> activeColor_;
240     RefPtr<AnimatablePropertyColor> pointColor_;
241     RefPtr<AnimatablePropertyColor> inactiveColor_;
242     RefPtr<AnimatablePropertyFloat> checkMarkPaintSize_;
243     RefPtr<AnimatablePropertyFloat> checkStroke_;
244     RefPtr<AnimatablePropertyColor> animateTouchHoverColor_;
245     TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
246     HoverEffectType hoverEffectType_ = HoverEffectType::AUTO;
247     RefPtr<PropertyBool> enabled_;
248     RefPtr<PropertyInt> uiStatus_;
249     RefPtr<PropertyInt> status_;
250     RefPtr<PropertyOffsetF> offset_;
251     RefPtr<PropertySizeF> size_;
252     RefPtr<PropertyInt> checkBoxGroupShape_;
253     CheckBoxStyle checkBoxGroupStyle_ = CheckBoxStyle::CIRCULAR_STYLE;
254     float borderWidth_;
255     float borderRadius_;
256     Color shadowColor_;
257     Color clickEffectColor_;
258     Color hoverColor_;
259     Color inactivePointColor_;
260     Dimension hoverRadius_;
261     Dimension hotZoneHorizontalPadding_;
262     Dimension hotZoneVerticalPadding_;
263     Dimension shadowWidth_;
264     float hoverDuration_ = 0.0f;
265     float hoverToTouchDuration_ = 0.0f;
266     Dimension defaultPaddingSize_;
267     ACE_DISALLOW_COPY_AND_MOVE(CheckBoxGroupModifier);
268 };
269 } // namespace OHOS::Ace::NG
270 
271 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOXGROUP_CHECKBOXGROUP_MODIFIER_H
272