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 #include "core/components_ng/token_theme/token_theme.h" 33 #include "core/pipeline_ng/pipeline_context.h" 34 35 namespace OHOS::Ace::NG { 36 37 class CheckBoxModifier : public ContentModifier { 38 DECLARE_ACE_TYPE(CheckBoxModifier, ContentModifier); 39 40 public: 41 CheckBoxModifier(bool isSelect, const Color& boardColor, const Color& checkColor, const Color& borderColor, 42 const Color& shadowColor, const SizeF& size, const OffsetF& offset, float checkStroke, float strokeSize); 43 ~CheckBoxModifier() override = default; 44 onDraw(DrawingContext & context)45 void onDraw(DrawingContext& context) override 46 { 47 if (useContentModifier_->Get()) { 48 return; 49 } 50 RSCanvas& canvas = context.canvas; 51 PaintCheckBox(canvas, offset_->Get(), size_->Get()); 52 } 53 54 void UpdateAnimatableProperty(bool needAnimation, const RefPtr<PipelineContext>& context = nullptr) 55 { 56 switch (touchHoverType_) { 57 case TouchHoverAnimationType::HOVER: 58 SetBoardColor(LinearColor(hoverColor_), hoverDuration_, Curves::FRICTION, context); 59 break; 60 case TouchHoverAnimationType::PRESS_TO_HOVER: 61 SetBoardColor(LinearColor(hoverColor_), hoverToTouchDuration_, Curves::SHARP, context); 62 break; 63 case TouchHoverAnimationType::NONE: 64 SetBoardColor(LinearColor(hoverColor_.BlendOpacity(0)), hoverDuration_, Curves::FRICTION, context); 65 break; 66 case TouchHoverAnimationType::HOVER_TO_PRESS: 67 SetBoardColor(LinearColor(clickEffectColor_), hoverToTouchDuration_, Curves::SHARP, context); 68 break; 69 case TouchHoverAnimationType::PRESS: 70 SetBoardColor(LinearColor(clickEffectColor_), hoverDuration_, Curves::FRICTION, context); 71 break; 72 default: 73 break; 74 } 75 if (!needAnimation) { 76 animatableBoardColor_->Set( 77 isSelect_->Get() ? LinearColor(userActiveColor_) : LinearColor(inactivePointColor_)); 78 animatableCheckColor_->Set( 79 isSelect_->Get() ? LinearColor(pointColor_) : LinearColor(pointColor_.BlendOpacity(0))); 80 animatableBorderColor_->Set( 81 isSelect_->Get() ? LinearColor(Color::TRANSPARENT) : LinearColor(inactiveColor_)); 82 animatableShadowColor_->Set( 83 isSelect_->Get() ? LinearColor(shadowColor_) : LinearColor(shadowColor_.BlendOpacity(0))); 84 return; 85 } 86 AnimationOption option = AnimationOption(); 87 option.SetDuration(colorAnimationDuration_); 88 option.SetCurve(Curves::FAST_OUT_SLOW_IN); 89 AnimationUtils::Animate(option, [&]() { 90 animatableBoardColor_->Set( 91 isSelect_->Get() ? LinearColor(userActiveColor_) : LinearColor(inactivePointColor_)); 92 animatableCheckColor_->Set( 93 isSelect_->Get() ? LinearColor(pointColor_) : LinearColor(pointColor_.BlendOpacity(0))); 94 animatableBorderColor_->Set( 95 isSelect_->Get() ? LinearColor(Color::TRANSPARENT) : LinearColor(inactiveColor_)); 96 animatableShadowColor_->Set( 97 isSelect_->Get() ? LinearColor(shadowColor_) : LinearColor(shadowColor_.BlendOpacity(0))); 98 }, nullptr, nullptr, context); 99 } 100 SetBoardColor(LinearColor color,int32_t duration,const RefPtr<CubicCurve> & curve,const RefPtr<PipelineContext> & context)101 void SetBoardColor( 102 LinearColor color, int32_t duration, const RefPtr<CubicCurve>& curve, const RefPtr<PipelineContext>& context) 103 { 104 if (animateTouchHoverColor_) { 105 AnimationOption option = AnimationOption(); 106 option.SetDuration(duration); 107 option.SetCurve(curve); 108 AnimationUtils::Animate(option, [&]() { animateTouchHoverColor_->Set(color); }, nullptr, nullptr, context); 109 } 110 } 111 112 void InitializeParam(TokenThemeScopeId themeScopeId); 113 void PaintCheckBox(RSCanvas& canvas, const OffsetF& paintOffset, const SizeF& paintSize) const; 114 void DrawFocusBoard(RSCanvas& canvas, const SizeF& contentSize, const OffsetF& offset) const; 115 void DrawTouchAndHoverBoard(RSCanvas& canvas, const SizeF& contentSize, const OffsetF& offset) const; 116 117 void DrawBorder(RSCanvas& canvas, const OffsetF& origin, RSPen& pen, const SizeF& paintSize) const; 118 void DrawBackboard(RSCanvas& canvas, const OffsetF& origin, RSBrush& brush, const SizeF& paintSize) const; 119 void DrawCheck(RSCanvas& canvas, const OffsetF& origin, RSPen& pen, RSPen& shadowPen, const SizeF& paintSize) const; 120 SetUserActiveColor(const Color & color)121 void SetUserActiveColor(const Color& color) 122 { 123 userActiveColor_ = color; 124 } 125 SetInActiveColor(const Color & color)126 void SetInActiveColor(const Color& color) 127 { 128 inactiveColor_ = color; 129 } 130 SetPointColor(const Color & color)131 void SetPointColor(const Color& color) 132 { 133 pointColor_ = color; 134 } 135 SetEnabled(bool enabled)136 void SetEnabled(bool enabled) 137 { 138 if (enabled_) { 139 enabled_->Set(enabled); 140 } 141 } 142 SetIsSelect(bool isSelect)143 void SetIsSelect(bool isSelect) 144 { 145 if (isSelect_) { 146 isSelect_->Set(isSelect); 147 } 148 } 149 SetIsFocused(bool isFocused)150 void SetIsFocused(bool isFocused) 151 { 152 if (isFocused_) { 153 isFocused_->Set(isFocused); 154 } 155 } 156 SetHotZoneOffset(OffsetF & hotZoneOffset)157 void SetHotZoneOffset(OffsetF& hotZoneOffset) 158 { 159 hotZoneOffset_ = hotZoneOffset; 160 } 161 SetHotZoneSize(SizeF & hotZoneSize)162 void SetHotZoneSize(SizeF& hotZoneSize) 163 { 164 hotZoneSize_ = hotZoneSize; 165 } 166 SetOffset(OffsetF & offset)167 void SetOffset(OffsetF& offset) 168 { 169 if (offset_) { 170 offset_->Set(offset); 171 } 172 } 173 SetUseContentModifier(bool useContentModifier)174 void SetUseContentModifier(bool useContentModifier) 175 { 176 if (useContentModifier_) { 177 useContentModifier_->Set(useContentModifier); 178 } 179 } 180 SetSize(SizeF & size)181 void SetSize(SizeF& size) 182 { 183 if (size_) { 184 size_->Set(size); 185 } 186 } 187 SetStrokeWidth(float value)188 void SetStrokeWidth(float value) 189 { 190 if (checkStroke_) { 191 checkStroke_->Set(value); 192 } 193 } 194 SetStrokeSize(float value)195 void SetStrokeSize(float value) 196 { 197 if (strokeSize_) { 198 strokeSize_->Set(value); 199 } 200 } 201 SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType)202 void SetTouchHoverAnimationType(const TouchHoverAnimationType touchHoverType) 203 { 204 touchHoverType_ = touchHoverType; 205 } 206 SetCheckboxStyle(CheckBoxStyle checkBoxStyle)207 void SetCheckboxStyle(CheckBoxStyle checkBoxStyle) 208 { 209 checkBoxStyle_ = checkBoxStyle; 210 CHECK_NULL_VOID(checkBoxShape_); 211 checkBoxShape_->Set(static_cast<int32_t>(checkBoxStyle)); 212 } 213 SetHoverEffectType(HoverEffectType hoverEffectType)214 void SetHoverEffectType(HoverEffectType hoverEffectType) 215 { 216 hoverEffectType_ = hoverEffectType; 217 } 218 SetHasBuilder(bool hasBuilder)219 void SetHasBuilder(bool hasBuilder) 220 { 221 if (hasBuilder_) { 222 hasBuilder_->Set(hasBuilder); 223 } 224 } 225 SetHasUnselectedColor(bool hasUnselectedColor)226 void SetHasUnselectedColor(bool hasUnselectedColor) 227 { 228 hasUnselectedColor_ = hasUnselectedColor; 229 } 230 231 private: 232 void DrawRectOrCircle(RSCanvas& canvas, const RSRoundRect& rrect) const; 233 234 float borderWidth_ = 0.0f; 235 float borderRadius_ = 0.0f; 236 float whiteBorderRadius_ = 0.0f; 237 Color pointColor_; 238 Color activeColor_; 239 Color inactiveColor_; 240 Color shadowColor_; 241 Color clickEffectColor_; 242 Color hoverColor_; 243 Color inactivePointColor_; 244 Color userActiveColor_; 245 Color focusBoardColor_; 246 Color borderFocusedColor_; 247 Color focusedBGColorUnselected_; 248 Dimension hoverRadius_; 249 Dimension hotZoneHorizontalPadding_; 250 Dimension hotZoneVerticalPadding_; 251 Dimension defaultPaddingSize_; 252 Dimension defaultRoundPaddingSize_; 253 Dimension hoverPaddingSize_; 254 Dimension shadowWidth_; 255 Dimension focusBoardSize_; 256 Dimension roundFocusBoardSize_; 257 float hoverDuration_ = 0.0f; 258 float hoverToTouchDuration_ = 0.0f; 259 float touchDuration_ = 0.0f; 260 float colorAnimationDuration_ = 0.0f; 261 bool hasUnselectedColor_ = false; 262 bool showCircleDial_ = false; 263 OffsetF hotZoneOffset_; 264 SizeF hotZoneSize_; 265 TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; 266 CheckBoxStyle checkBoxStyle_ = CheckBoxStyle::CIRCULAR_STYLE; 267 HoverEffectType hoverEffectType_ = HoverEffectType::AUTO; 268 RefPtr<PropertyInt> checkBoxShape_; 269 270 RefPtr<PropertyBool> enabled_; 271 RefPtr<PropertyBool> useContentModifier_; 272 RefPtr<PropertyBool> hasBuilder_; 273 RefPtr<PropertyBool> isFocused_; 274 RefPtr<AnimatablePropertyColor> animatableBoardColor_; 275 RefPtr<AnimatablePropertyColor> animatableCheckColor_; 276 RefPtr<AnimatablePropertyColor> animatableBorderColor_; 277 RefPtr<AnimatablePropertyColor> animatableShadowColor_; 278 RefPtr<AnimatablePropertyColor> animateTouchHoverColor_; 279 RefPtr<AnimatablePropertyFloat> checkStroke_; 280 RefPtr<AnimatablePropertyFloat> strokeSize_; 281 RefPtr<PropertyBool> isSelect_; 282 RefPtr<AnimatablePropertyOffsetF> offset_; 283 RefPtr<AnimatablePropertySizeF> size_; 284 285 ACE_DISALLOW_COPY_AND_MOVE(CheckBoxModifier); 286 }; 287 } // namespace OHOS::Ace::NG 288 289 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOX_CHECKBOX_MODIFIER_H 290