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.h" 30 #include "core/components_ng/render/paint_wrapper.h" 31 namespace OHOS::Ace::NG { 32 class CheckBoxModifier : public ContentModifier { 33 DECLARE_ACE_TYPE(CheckBoxModifier, ContentModifier); 34 35 public: 36 CheckBoxModifier(bool isSelect, const Color& boardColor, const Color& checkColor, const Color& borderColor, 37 const Color& shadowColor); 38 ~CheckBoxModifier() override = default; 39 onDraw(DrawingContext & context)40 void onDraw(DrawingContext& context) override 41 { 42 RSCanvas canvas = context.canvas; 43 PaintCheckBox(canvas, offset_->Get(), size_->Get()); 44 } 45 UpdateAnimatableProperty()46 void UpdateAnimatableProperty() 47 { 48 AnimationOption option = AnimationOption(); 49 option.SetDuration(hoverDuration_); 50 option.SetCurve(Curves::FRICTION); 51 AnimationUtils::Animate(option, [&]() { 52 animateHoverColor_->Set(isHover_->Get() ? LinearColor(hoverColor_) : LinearColor(Color::TRANSPARENT)); 53 }); 54 55 option.SetDuration(colorAnimationDuration_); 56 option.SetCurve(Curves::FAST_OUT_SLOW_IN); 57 AnimationUtils::Animate(option, [&]() { 58 animatableBoardColor_->Set( 59 isSelect_->Get() ? LinearColor(userActiveColor_) : LinearColor(inactivePointColor_)); 60 animatableCheckColor_->Set( 61 isSelect_->Get() ? LinearColor(pointColor_) : LinearColor(pointColor_.BlendOpacity(0))); 62 animatableBorderColor_->Set( 63 isSelect_->Get() ? LinearColor(Color::TRANSPARENT) : LinearColor(inactiveColor_)); 64 animatableShadowColor_->Set( 65 isSelect_->Get() ? LinearColor(shadowColor_) : LinearColor(shadowColor_.BlendOpacity(0))); 66 }); 67 } 68 69 void InitializeParam(); 70 void PaintCheckBox(RSCanvas& canvas, const OffsetF& paintOffset, const SizeF& paintSize) const; 71 void DrawHoverBoard(RSCanvas& canvas, const SizeF& contentSize, const OffsetF& offset) const; 72 73 void DrawBorder(RSCanvas& canvas, const OffsetF& origin, RSPen& pen, const SizeF& paintSize) const; 74 void DrawBackboard(RSCanvas& canvas, const OffsetF& origin, RSBrush& brush, const SizeF& paintSize) const; 75 void DrawCheck(RSCanvas& canvas, const OffsetF& origin, RSPen& pen, RSPen& shadowPen, const SizeF& paintSize) const; 76 SetUserActiveColor(const Color & color)77 void SetUserActiveColor(const Color& color) 78 { 79 userActiveColor_ = color; 80 } 81 SetEnabled(bool enabled)82 void SetEnabled(bool enabled) 83 { 84 if (enabled_) { 85 enabled_->Set(enabled); 86 } 87 } 88 SetIsSelect(bool isSelect)89 void SetIsSelect(bool isSelect) 90 { 91 if (isSelect_) { 92 isSelect_->Set(isSelect); 93 } 94 } 95 SetHotZoneOffset(OffsetF & hotZoneOffset)96 void SetHotZoneOffset(OffsetF& hotZoneOffset) 97 { 98 hotZoneOffset_ = hotZoneOffset; 99 } 100 SetHotZoneSize(SizeF & hotZoneSize)101 void SetHotZoneSize(SizeF& hotZoneSize) 102 { 103 hotZoneSize_ = hotZoneSize; 104 } 105 SetOffset(OffsetF & offset)106 void SetOffset(OffsetF& offset) 107 { 108 if (offset_) { 109 offset_->Set(offset); 110 } 111 } 112 SetSize(SizeF & size)113 void SetSize(SizeF& size) 114 { 115 if (size_) { 116 size_->Set(size); 117 } 118 } 119 SetIsHover(bool isHover)120 void SetIsHover(bool isHover) 121 { 122 if (isHover_) { 123 isHover_->Set(isHover); 124 } 125 } 126 127 private: 128 float borderWidth_ = 0.0f; 129 float borderRadius_ = 0.0f; 130 float checkStroke_ = 0.0f; 131 Color pointColor_; 132 Color activeColor_; 133 Color inactiveColor_; 134 Color shadowColor_; 135 Color clickEffectColor_; 136 Color hoverColor_; 137 Color inactivePointColor_; 138 Color userActiveColor_; 139 Dimension hoverRadius_; 140 Dimension hotZoneHorizontalPadding_; 141 Dimension hotZoneVerticalPadding_; 142 Dimension shadowWidth_; 143 float hoverDuration_ = 0.0f; 144 float hoverToTouchDuration_ = 0.0f; 145 float touchDuration_ = 0.0f; 146 float colorAnimationDuration_ = 0.0f; 147 148 OffsetF hotZoneOffset_; 149 SizeF hotZoneSize_; 150 151 RefPtr<PropertyBool> enabled_; 152 RefPtr<AnimatablePropertyColor> animatableBoardColor_; 153 RefPtr<AnimatablePropertyColor> animatableCheckColor_; 154 RefPtr<AnimatablePropertyColor> animatableBorderColor_; 155 RefPtr<AnimatablePropertyColor> animatableShadowColor_; 156 RefPtr<AnimatablePropertyColor> animateHoverColor_; 157 RefPtr<PropertyBool> isSelect_; 158 RefPtr<PropertyBool> isHover_; 159 RefPtr<PropertyOffsetF> offset_; 160 RefPtr<PropertySizeF> size_; 161 162 ACE_DISALLOW_COPY_AND_MOVE(CheckBoxModifier); 163 }; 164 } // namespace OHOS::Ace::NG 165 166 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOX_CHECKBOX_MODIFIER_H