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