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