• 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 #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         CheckBoxGroupPaintProperty::SelectStatus status;
53     };
54 
55     explicit CheckBoxGroupModifier(const Parameters& parameters);
56     ~CheckBoxGroupModifier() override = default;
onDraw(DrawingContext & context)57     void onDraw(DrawingContext& context) override
58     {
59         PaintCheckBox(context, offset_->Get(), size_->Get());
60     }
61 
UpdateAnimatableProperty()62     void UpdateAnimatableProperty()
63     {
64         switch (touchHoverType_) {
65             case TouchHoverAnimationType::HOVER:
66                 SetBoardColor(LinearColor(hoverColor_), hoverDuration_, Curves::FRICTION);
67                 break;
68             case TouchHoverAnimationType::PRESS_TO_HOVER:
69                 SetBoardColor(LinearColor(hoverColor_), hoverToTouchDuration_, Curves::SHARP);
70                 break;
71             case TouchHoverAnimationType::NONE:
72                 SetBoardColor(LinearColor(hoverColor_.BlendOpacity(0)), hoverDuration_, Curves::FRICTION);
73                 break;
74             case TouchHoverAnimationType::HOVER_TO_PRESS:
75                 SetBoardColor(LinearColor(clickEffectColor_), hoverToTouchDuration_, Curves::SHARP);
76                 break;
77             case TouchHoverAnimationType::PRESS:
78                 SetBoardColor(LinearColor(clickEffectColor_), hoverDuration_, Curves::FRICTION);
79                 break;
80             default:
81                 break;
82         }
83     }
84 
SetBoardColor(LinearColor color,int32_t duratuion,const RefPtr<CubicCurve> & curve)85     void SetBoardColor(LinearColor color, int32_t duratuion, const RefPtr<CubicCurve>& curve)
86     {
87         if (animateTouchHoverColor_) {
88             AnimationOption option = AnimationOption();
89             option.SetDuration(duratuion);
90             option.SetCurve(curve);
91             AnimationUtils::Animate(option, [&]() { animateTouchHoverColor_->Set(color); });
92         }
93     }
94 
95     void PaintCheckBox(DrawingContext& context, const OffsetF& paintOffset, const SizeF& contentSize) const;
96     void PaintCheckBoxGroupPartStatus(
97         RSCanvas& canvas, const OffsetF& paintOffset, RSBrush& brush, RSPen& pen, const SizeF& paintSize) const;
98     void DrawCheck(RSCanvas& canvas, const OffsetF& origin, RSPen& pen, RSPen& shadowPen, const SizeF& paintSize) const;
99     void DrawUnselected(RSCanvas& canvas, const OffsetF& origin, RSPen& pen, const SizeF& paintSize) const;
100     void DrawActiveBorder(RSCanvas& canvas, const OffsetF& paintOffset, RSBrush& brush, const SizeF& paintSize) const;
101     void DrawUnselectedBorder(
102         RSCanvas& canvas, const OffsetF& paintOffset, RSBrush& brush, const SizeF& paintSize) const;
103     void DrawPart(RSCanvas& canvas, const OffsetF& origin, RSPen& pen, const SizeF& paintSize) const;
104     void DrawTouchAndHoverBoard(RSCanvas& canvas, const SizeF& contentSize, const OffsetF& offset) const;
105 
SetBorderWidth(float borderWidth)106     void SetBorderWidth(float borderWidth)
107     {
108         borderWidth_ = borderWidth;
109     }
110 
SetBorderRadius(float borderRadius)111     void SetBorderRadius(float borderRadius)
112     {
113         borderRadius_ = borderRadius;
114     }
115 
SetCheckStroke(float checkStroke)116     void SetCheckStroke(float checkStroke)
117     {
118         checkStroke_->Set(checkStroke);
119     }
120 
SetCheckMarkPaintSize(float checkMarkPaintSize)121     void SetCheckMarkPaintSize(float checkMarkPaintSize)
122     {
123         checkMarkPaintSize_->Set(checkMarkPaintSize);
124     }
125 
SetPointColor(Color pointColor)126     void SetPointColor(Color pointColor)
127     {
128         pointColor_->Set(LinearColor(pointColor));
129     }
130 
SetActiveColor(Color activeColor)131     void SetActiveColor(Color activeColor)
132     {
133         activeColor_->Set(LinearColor(activeColor));
134     }
135 
SetInactiveColor(Color inactiveColor)136     void SetInactiveColor(Color inactiveColor)
137     {
138         inactiveColor_->Set(LinearColor(inactiveColor));
139     }
140 
SetShadowColor(Color shadowColor)141     void SetShadowColor(Color shadowColor)
142     {
143         shadowColor_ = shadowColor;
144     }
145 
SetClickEffectColor(Color clickEffectColor)146     void SetClickEffectColor(Color clickEffectColor)
147     {
148         clickEffectColor_ = clickEffectColor;
149     }
150 
SetHoverColor(Color hoverColor)151     void SetHoverColor(Color hoverColor)
152     {
153         hoverColor_ = hoverColor;
154     }
155 
SetInactivePointColor(Color inactivePointColor)156     void SetInactivePointColor(Color inactivePointColor)
157     {
158         inactivePointColor_ = inactivePointColor;
159     }
160 
SetHoverRadius(Dimension hoverRadius)161     void SetHoverRadius(Dimension hoverRadius)
162     {
163         hoverRadius_ = hoverRadius;
164     }
165 
SetHotZoneHorizontalPadding(Dimension hotZoneHorizontalPadding)166     void SetHotZoneHorizontalPadding(Dimension hotZoneHorizontalPadding)
167     {
168         hotZoneHorizontalPadding_ = hotZoneHorizontalPadding;
169     }
170 
SetHotZoneVerticalPadding(Dimension hotZoneVerticalPadding)171     void SetHotZoneVerticalPadding(Dimension hotZoneVerticalPadding)
172     {
173         hotZoneVerticalPadding_ = hotZoneVerticalPadding;
174     }
175 
SetShadowWidth(Dimension shadowWidth)176     void SetShadowWidth(Dimension shadowWidth)
177     {
178         shadowWidth_ = shadowWidth;
179     }
180 
SetEnabled(bool value)181     void SetEnabled(bool value)
182     {
183         enabled_->Set(value);
184     }
185 
SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)186     void SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)
187     {
188         touchHoverType_ = touchHoverType;
189     }
190 
SetUiStatus(UIStatus uiStatus)191     void SetUiStatus(UIStatus uiStatus)
192     {
193         if (uiStatus_) {
194             uiStatus_->Set(static_cast<int>(uiStatus));
195         }
196     }
197 
SetSelectStatus(CheckBoxGroupPaintProperty::SelectStatus status)198     void SetSelectStatus(CheckBoxGroupPaintProperty::SelectStatus status)
199     {
200         if (status_) {
201             status_->Set(static_cast<int>(status));
202         }
203     }
204 
GetBorderWidth()205     float GetBorderWidth() const
206     {
207         return borderWidth_;
208     }
209 
SetOffset(OffsetF & offset)210     void SetOffset(OffsetF& offset)
211     {
212         if (offset_) {
213             offset_->Set(offset);
214         }
215     }
216 
SetSize(SizeF & size)217     void SetSize(SizeF& size)
218     {
219         if (size_) {
220             size_->Set(size);
221         }
222     }
223 
224 private:
225     RefPtr<AnimatablePropertyColor> activeColor_;
226     RefPtr<AnimatablePropertyColor> pointColor_;
227     RefPtr<AnimatablePropertyColor> inactiveColor_;
228     RefPtr<AnimatablePropertyFloat> checkMarkPaintSize_;
229     RefPtr<AnimatablePropertyFloat> checkStroke_;
230     RefPtr<AnimatablePropertyColor> animateTouchHoverColor_;
231     TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
232     RefPtr<PropertyBool> enabled_;
233     RefPtr<PropertyInt> uiStatus_;
234     RefPtr<PropertyInt> status_;
235     RefPtr<PropertyOffsetF> offset_;
236     RefPtr<PropertySizeF> size_;
237     float borderWidth_;
238     float borderRadius_;
239     Color shadowColor_;
240     Color clickEffectColor_;
241     Color hoverColor_;
242     Color inactivePointColor_;
243     Dimension hoverRadius_;
244     Dimension hotZoneHorizontalPadding_;
245     Dimension hotZoneVerticalPadding_;
246     Dimension shadowWidth_;
247     float hoverDuration_ = 0.0f;
248     float hoverToTouchDuration_ = 0.0f;
249     ACE_DISALLOW_COPY_AND_MOVE(CheckBoxGroupModifier);
250 };
251 } // namespace OHOS::Ace::NG
252 
253 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOXGROUP_CHECKBOXGROUP_MODIFIER_H
254