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_CHECKBOX_CHECKBOX_MODIFIER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOX_CHECKBOX_MODIFIER_H 18 19 #include <vector> 20 21 #include "base/geometry/ng/offset_t.h" 22 #include "core/animation/spring_curve.h" 23 #include "core/common/container.h" 24 #include "core/components_ng/base/modifier.h" 25 #include "core/components_ng/pattern/checkbox/checkbox_model_ng.h" 26 #include "core/components_ng/pattern/radio/radio_modifier.h" 27 #include "core/components_ng/property/property.h" 28 #include "core/components_ng/render/animation_utils.h" 29 #include "core/components_ng/render/canvas_image.h" 30 #include "core/components_ng/render/drawing_forward.h" 31 #include "core/components_ng/render/paint_wrapper.h" 32 33 namespace OHOS::Ace::NG { 34 35 class CheckBoxModifier : public ContentModifier { 36 DECLARE_ACE_TYPE(CheckBoxModifier, ContentModifier); 37 38 public: 39 CheckBoxModifier(bool isSelect, const Color& boardColor, const Color& checkColor, const Color& borderColor, 40 const Color& shadowColor, const SizeF& size, const OffsetF& offset, float checkStroke, float strokeSize); 41 ~CheckBoxModifier() override = default; 42 onDraw(DrawingContext & context)43 void onDraw(DrawingContext& context) override 44 { 45 if (useContentModifier_->Get()) { 46 return; 47 } 48 RSCanvas& canvas = context.canvas; 49 PaintCheckBox(canvas, offset_->Get(), size_->Get()); 50 } 51 UpdateAnimatableProperty()52 void UpdateAnimatableProperty() 53 { 54 switch (touchHoverType_) { 55 case TouchHoverAnimationType::HOVER: 56 SetBoardColor(LinearColor(hoverColor_), hoverDuration_, Curves::FRICTION); 57 break; 58 case TouchHoverAnimationType::PRESS_TO_HOVER: 59 SetBoardColor(LinearColor(hoverColor_), hoverToTouchDuration_, Curves::SHARP); 60 break; 61 case TouchHoverAnimationType::NONE: 62 SetBoardColor(LinearColor(hoverColor_.BlendOpacity(0)), hoverDuration_, Curves::FRICTION); 63 break; 64 case TouchHoverAnimationType::HOVER_TO_PRESS: 65 SetBoardColor(LinearColor(clickEffectColor_), hoverToTouchDuration_, Curves::SHARP); 66 break; 67 case TouchHoverAnimationType::PRESS: 68 SetBoardColor(LinearColor(clickEffectColor_), hoverDuration_, Curves::FRICTION); 69 break; 70 default: 71 break; 72 } 73 AnimationOption option = AnimationOption(); 74 option.SetDuration(colorAnimationDuration_); 75 option.SetCurve(Curves::FAST_OUT_SLOW_IN); 76 AnimationUtils::Animate(option, [&]() { 77 animatableBoardColor_->Set( 78 isSelect_->Get() ? LinearColor(userActiveColor_) : LinearColor(inactivePointColor_)); 79 animatableCheckColor_->Set( 80 isSelect_->Get() ? LinearColor(pointColor_) : LinearColor(pointColor_.BlendOpacity(0))); 81 animatableBorderColor_->Set( 82 isSelect_->Get() ? LinearColor(Color::TRANSPARENT) : LinearColor(inactiveColor_)); 83 animatableShadowColor_->Set( 84 isSelect_->Get() ? LinearColor(shadowColor_) : LinearColor(shadowColor_.BlendOpacity(0))); 85 }); 86 } 87 SetBoardColor(LinearColor color,int32_t duratuion,const RefPtr<CubicCurve> & curve)88 void SetBoardColor(LinearColor color, int32_t duratuion, const RefPtr<CubicCurve>& curve) 89 { 90 if (animateTouchHoverColor_) { 91 AnimationOption option = AnimationOption(); 92 option.SetDuration(duratuion); 93 option.SetCurve(curve); 94 AnimationUtils::Animate(option, [&]() { animateTouchHoverColor_->Set(color); }); 95 } 96 } 97 98 void InitializeParam(); 99 void PaintCheckBox(RSCanvas& canvas, const OffsetF& paintOffset, const SizeF& paintSize) const; 100 void DrawTouchAndHoverBoard(RSCanvas& canvas, const SizeF& contentSize, const OffsetF& offset) const; 101 102 void DrawBorder(RSCanvas& canvas, const OffsetF& origin, RSPen& pen, const SizeF& paintSize) const; 103 void DrawBackboard(RSCanvas& canvas, const OffsetF& origin, RSBrush& brush, const SizeF& paintSize) const; 104 void DrawCheck(RSCanvas& canvas, const OffsetF& origin, RSPen& pen, RSPen& shadowPen, const SizeF& paintSize) const; 105 SetUserActiveColor(const Color & color)106 void SetUserActiveColor(const Color& color) 107 { 108 userActiveColor_ = color; 109 } 110 SetInActiveColor(const Color & color)111 void SetInActiveColor(const Color& color) 112 { 113 inactiveColor_ = color; 114 } 115 SetPointColor(const Color & color)116 void SetPointColor(const Color& color) 117 { 118 pointColor_ = color; 119 } 120 SetEnabled(bool enabled)121 void SetEnabled(bool enabled) 122 { 123 if (enabled_) { 124 enabled_->Set(enabled); 125 } 126 } 127 SetIsSelect(bool isSelect)128 void SetIsSelect(bool isSelect) 129 { 130 if (isSelect_) { 131 isSelect_->Set(isSelect); 132 } 133 } 134 SetHotZoneOffset(OffsetF & hotZoneOffset)135 void SetHotZoneOffset(OffsetF& hotZoneOffset) 136 { 137 hotZoneOffset_ = hotZoneOffset; 138 } 139 SetHotZoneSize(SizeF & hotZoneSize)140 void SetHotZoneSize(SizeF& hotZoneSize) 141 { 142 hotZoneSize_ = hotZoneSize; 143 } 144 SetOffset(OffsetF & offset)145 void SetOffset(OffsetF& offset) 146 { 147 if (offset_) { 148 offset_->Set(offset); 149 } 150 } 151 SetUseContentModifier(bool useContentModifier)152 void SetUseContentModifier(bool useContentModifier) 153 { 154 if (useContentModifier_) { 155 useContentModifier_->Set(useContentModifier); 156 } 157 } 158 SetSize(SizeF & size)159 void SetSize(SizeF& size) 160 { 161 if (size_) { 162 size_->Set(size); 163 } 164 } 165 SetStrokeWidth(float value)166 void SetStrokeWidth(float value) 167 { 168 if (checkStroke_) { 169 checkStroke_->Set(value); 170 } 171 } 172 SetStrokeSize(float value)173 void SetStrokeSize(float value) 174 { 175 if (strokeSize_) { 176 strokeSize_->Set(value); 177 } 178 } 179 SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)180 void SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType) 181 { 182 touchHoverType_ = touchHoverType; 183 } 184 SetCheckboxStyle(CheckBoxStyle checkBoxStyle)185 void SetCheckboxStyle(CheckBoxStyle checkBoxStyle) 186 { 187 checkBoxStyle_ = checkBoxStyle; 188 CHECK_NULL_VOID(checkBoxShape_); 189 checkBoxShape_->Set(static_cast<int32_t>(checkBoxStyle)); 190 } 191 SetHoverEffectType(HoverEffectType hoverEffectType)192 void SetHoverEffectType(HoverEffectType hoverEffectType) 193 { 194 hoverEffectType_ = hoverEffectType; 195 } 196 SetHasBuilder(bool hasBuilder)197 void SetHasBuilder(bool hasBuilder) 198 { 199 if (hasBuilder_) { 200 hasBuilder_->Set(hasBuilder); 201 } 202 } 203 204 private: 205 float borderWidth_ = 0.0f; 206 float borderRadius_ = 0.0f; 207 Color pointColor_; 208 Color activeColor_; 209 Color inactiveColor_; 210 Color shadowColor_; 211 Color clickEffectColor_; 212 Color hoverColor_; 213 Color inactivePointColor_; 214 Color userActiveColor_; 215 Dimension hoverRadius_; 216 Dimension hotZoneHorizontalPadding_; 217 Dimension hotZoneVerticalPadding_; 218 Dimension defaultPaddingSize_; 219 Dimension shadowWidth_; 220 float hoverDuration_ = 0.0f; 221 float hoverToTouchDuration_ = 0.0f; 222 float touchDuration_ = 0.0f; 223 float colorAnimationDuration_ = 0.0f; 224 OffsetF hotZoneOffset_; 225 SizeF hotZoneSize_; 226 TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; 227 CheckBoxStyle checkBoxStyle_ = CheckBoxStyle::CIRCULAR_STYLE; 228 HoverEffectType hoverEffectType_ = HoverEffectType::AUTO; 229 RefPtr<PropertyInt> checkBoxShape_; 230 231 RefPtr<PropertyBool> enabled_; 232 RefPtr<PropertyBool> useContentModifier_; 233 RefPtr<PropertyBool> hasBuilder_; 234 RefPtr<AnimatablePropertyColor> animatableBoardColor_; 235 RefPtr<AnimatablePropertyColor> animatableCheckColor_; 236 RefPtr<AnimatablePropertyColor> animatableBorderColor_; 237 RefPtr<AnimatablePropertyColor> animatableShadowColor_; 238 RefPtr<AnimatablePropertyColor> animateTouchHoverColor_; 239 RefPtr<AnimatablePropertyFloat> checkStroke_; 240 RefPtr<AnimatablePropertyFloat> strokeSize_; 241 RefPtr<PropertyBool> isSelect_; 242 RefPtr<AnimatablePropertyOffsetF> offset_; 243 RefPtr<AnimatablePropertySizeF> size_; 244 245 ACE_DISALLOW_COPY_AND_MOVE(CheckBoxModifier); 246 }; 247 } // namespace OHOS::Ace::NG 248 249 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOX_CHECKBOX_MODIFIER_H 250