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_PATTERN_SLIDER_SLIDER_CONTENT_MODIFIER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SLIDER_SLIDER_CONTENT_MODIFIER_H 18 19 #include "base/memory/referenced.h" 20 #include "base/utils/utils.h" 21 #include "core/components/common/properties/animation_option.h" 22 #include "core/components/slider/slider_theme.h" 23 #include "core/components_ng/base/modifier.h" 24 #include "core/components_ng/pattern/slider/slider_paint_property.h" 25 #include "core/components_ng/render/animation_utils.h" 26 #include "core/components_ng/render/drawing.h" 27 #include "core/pipeline/pipeline_base.h" 28 29 namespace OHOS::Ace::NG { 30 class SliderContentModifier : public ContentModifier { 31 DECLARE_ACE_TYPE(SliderContentModifier, ContentModifier); 32 33 public: 34 struct Parameters { 35 float trackThickness = 0.0f; 36 SizeF blockSize; 37 float stepRatio = 0.0f; 38 float hotCircleShadowWidth = 0.0f; 39 bool mouseHoverFlag_ = false; 40 bool mousePressedFlag_ = false; 41 PointF selectStart; 42 PointF selectEnd; 43 PointF backStart; 44 PointF backEnd; 45 PointF circleCenter; 46 Color selectColor; 47 Color trackBackgroundColor; 48 Color blockColor; 49 }; 50 51 explicit SliderContentModifier(const Parameters& parameters, std::function<void()> updateImageFunc); 52 ~SliderContentModifier() override = default; 53 54 void onDraw(DrawingContext& context) override; 55 56 void DrawBackground(DrawingContext& context); 57 void DrawStep(DrawingContext& context); 58 void DrawSelect(DrawingContext& context); 59 void DrawDefaultBlock(DrawingContext& context); 60 void DrawHoverOrPress(DrawingContext& context); 61 void DrawShadow(DrawingContext& context); 62 UpdateThemeColor()63 void UpdateThemeColor() 64 { 65 auto pipeline = PipelineBase::GetCurrentContext(); 66 CHECK_NULL_VOID(pipeline); 67 auto sliderTheme = pipeline->GetTheme<SliderTheme>(); 68 CHECK_NULL_VOID(sliderTheme); 69 blockOuterEdgeColor_ = sliderTheme->GetBlockOuterEdgeColor(); 70 blockShadowColor_ = sliderTheme->GetBlockShadowColor(); 71 } 72 73 void UpdateData(const Parameters& parameters); 74 void JudgeNeedAnimate(const RefPtr<SliderPaintProperty>& property); 75 SetTrackThickness(float trackThickness)76 void SetTrackThickness(float trackThickness) 77 { 78 if (trackThickness_) { 79 trackThickness_->Set(trackThickness); 80 } 81 } 82 SetTrackBackgroundColor(Color color)83 void SetTrackBackgroundColor(Color color) 84 { 85 if (trackBackgroundColor_) { 86 trackBackgroundColor_->Set(LinearColor(color)); 87 } 88 } 89 SetSelectColor(Color color)90 void SetSelectColor(Color color) 91 { 92 if (selectColor_) { 93 selectColor_->Set(LinearColor(color)); 94 } 95 } 96 SetBlockColor(Color color)97 void SetBlockColor(Color color) 98 { 99 if (blockColor_) { 100 blockColor_->Set(LinearColor(color)); 101 } 102 } 103 104 void SetBoardColor(); 105 SetBackgroundSize(const PointF & start,const PointF & end)106 void SetBackgroundSize(const PointF& start, const PointF& end) 107 { 108 if (backStart_) { 109 backStart_->Set(start - PointF()); 110 } 111 if (backEnd_) { 112 backEnd_->Set(end - PointF()); 113 } 114 } 115 116 void SetSelectSize(const PointF& start, const PointF& end); 117 118 void SetCircleCenter(const PointF& center); 119 SetStepRatio(float stepRatio)120 void SetStepRatio(float stepRatio) 121 { 122 if (stepRatio_) { 123 stepRatio_->Set(stepRatio); 124 } 125 } 126 SetNotAnimated()127 void SetNotAnimated() 128 { 129 needAnimate_ = false; 130 } 131 SetAnimated()132 void SetAnimated() 133 { 134 needAnimate_ = true; 135 } 136 SetSliderMode(SliderModelNG::SliderMode sliderMode)137 void SetSliderMode(SliderModelNG::SliderMode sliderMode) 138 { 139 if (sliderMode_) { 140 sliderMode_->Set(static_cast<int>(sliderMode)); 141 } 142 } 143 SetTrackBorderRadius(float trackBorderRadius)144 void SetTrackBorderRadius(float trackBorderRadius) 145 { 146 if (trackBorderRadius_) { 147 trackBorderRadius_->Set(trackBorderRadius); 148 } 149 } 150 SetStepSize(float stepSize)151 void SetStepSize(float stepSize) 152 { 153 if (stepSize_) { 154 stepSize_->Set(stepSize); 155 } 156 } 157 SetStepColor(const Color & stepColor)158 void SetStepColor(const Color& stepColor) 159 { 160 if (stepColor_) { 161 stepColor_->Set(LinearColor(stepColor)); 162 } 163 } 164 SetShowSteps(bool showSteps)165 void SetShowSteps(bool showSteps) 166 { 167 if (isShowStep_) { 168 isShowStep_->Set(showSteps); 169 } 170 } 171 SetBlockType(SliderModelNG::BlockStyleType type)172 void SetBlockType(SliderModelNG::BlockStyleType type) 173 { 174 if (blockType_) { 175 blockType_->Set(static_cast<int>(type)); 176 } 177 } 178 SetBlockSize(const SizeF & blockSize)179 void SetBlockSize(const SizeF& blockSize) 180 { 181 if (blockSize_) { 182 blockSize_->Set(blockSize); 183 } 184 } 185 SetBlockBorderColor(const Color & blockBorderColor)186 void SetBlockBorderColor(const Color& blockBorderColor) 187 { 188 if (blockBorderColor_) { 189 blockBorderColor_->Set(LinearColor(blockBorderColor)); 190 } 191 } 192 SetBlockBorderWidth(float blockBorderWidth)193 void SetBlockBorderWidth(float blockBorderWidth) 194 { 195 if (blockBorderWidth_) { 196 blockBorderWidth_->Set(blockBorderWidth); 197 } 198 } 199 200 void SetBlockShape(const RefPtr<BasicShape>& shape); 201 SetDirection(Axis axis)202 void SetDirection(Axis axis) 203 { 204 if (directionAxis_) { 205 directionAxis_->Set(static_cast<int>(axis)); 206 } 207 } 208 GetBlockCenter()209 PointF GetBlockCenter() 210 { 211 auto blockCenterX = blockCenterX_->Get(); 212 auto blockCenterY = blockCenterY_->Get(); 213 auto backStart = backStart_->Get(); 214 auto backEnd = backEnd_->Get(); 215 if (static_cast<Axis>(directionAxis_->Get()) == Axis::HORIZONTAL) { 216 blockCenterX = std::clamp(blockCenterX, backStart.GetX(), backEnd.GetX()); 217 } else { 218 blockCenterY = std::clamp(blockCenterY, backStart.GetY(), backEnd.GetY()); 219 } 220 return { blockCenterX, blockCenterY }; 221 } 222 GetTrackThickness()223 float GetTrackThickness() const 224 { 225 return trackThickness_->Get(); 226 } 227 GetBlockSize()228 SizeF GetBlockSize() const 229 { 230 return blockSize_->Get(); 231 } 232 SetVisible(bool isVisible)233 void SetVisible(bool isVisible) 234 { 235 CHECK_NULL_VOID(isVisible_ != isVisible); 236 isVisible_ = isVisible; 237 LOGD("SliderContentModifier SetVisible %d", isVisible_); 238 } 239 GetVisible()240 bool GetVisible() const 241 { 242 return isVisible_; 243 } 244 245 void UpdateContentDirtyRect(const SizeF& frameSize); 246 private: 247 void InitializeShapeProperty(); 248 RSRect GetTrackRect(); 249 250 void DrawBlock(DrawingContext& context); 251 void DrawBlockShape(DrawingContext& context); 252 void DrawBlockShapeCircle(DrawingContext& context, RefPtr<Circle>& circle); 253 void DrawBlockShapeEllipse(DrawingContext& context, RefPtr<Ellipse>& ellipse); 254 void DrawBlockShapePath(DrawingContext& context, RefPtr<Path>& path); 255 void DrawBlockShapeRect(DrawingContext& context, RefPtr<ShapeRect>& rect); 256 void SetShapeRectRadius(RSRoundRect& roundRect, float borderWidth); 257 void SetBlockClip(DrawingContext& context); 258 void StopSelectAnimation(const PointF& end); 259 void StopCircleCenterAnimation(const PointF& center); 260 261 private: 262 std::function<void()> updateImageFunc_; 263 264 // animatable property 265 RefPtr<AnimatablePropertyOffsetF> selectStart_; 266 RefPtr<AnimatablePropertyOffsetF> selectEnd_; 267 RefPtr<AnimatablePropertyOffsetF> backStart_; 268 RefPtr<AnimatablePropertyOffsetF> backEnd_; 269 RefPtr<AnimatablePropertyFloat> blockCenterX_; 270 RefPtr<AnimatablePropertyFloat> blockCenterY_; 271 RefPtr<AnimatablePropertyFloat> trackThickness_; 272 RefPtr<AnimatablePropertyColor> trackBackgroundColor_; 273 RefPtr<AnimatablePropertyColor> selectColor_; 274 RefPtr<AnimatablePropertyColor> blockColor_; 275 RefPtr<AnimatablePropertyColor> boardColor_; 276 277 RefPtr<AnimatablePropertyFloat> trackBorderRadius_; 278 RefPtr<AnimatablePropertyFloat> stepSize_; 279 RefPtr<AnimatablePropertyColor> stepColor_; 280 RefPtr<AnimatablePropertySizeF> blockSize_; 281 RefPtr<AnimatablePropertyColor> blockBorderColor_; 282 RefPtr<AnimatablePropertyFloat> blockBorderWidth_; 283 RefPtr<AnimatablePropertyFloat> shapeWidth_; 284 RefPtr<AnimatablePropertyFloat> shapeHeight_; 285 RefPtr<AnimatablePropertyFloat> circleRadius_; 286 RefPtr<AnimatablePropertyFloat> ellipseRadiusX_; 287 RefPtr<AnimatablePropertyFloat> ellipseRadiusY_; 288 RefPtr<AnimatablePropertyFloat> rectTopLeftRadiusX_; 289 RefPtr<AnimatablePropertyFloat> rectTopLeftRadiusY_; 290 RefPtr<AnimatablePropertyFloat> rectTopRightRadiusX_; 291 RefPtr<AnimatablePropertyFloat> rectTopRightRadiusY_; 292 RefPtr<AnimatablePropertyFloat> rectBottomLeftRadiusX_; 293 RefPtr<AnimatablePropertyFloat> rectBottomLeftRadiusY_; 294 RefPtr<AnimatablePropertyFloat> rectBottomRightRadiusX_; 295 RefPtr<AnimatablePropertyFloat> rectBottomRightRadiusY_; 296 // non-animatable property 297 RefPtr<PropertyFloat> stepRatio_; 298 RefPtr<PropertyInt> sliderMode_; 299 RefPtr<PropertyInt> directionAxis_; 300 RefPtr<PropertyBool> isShowStep_; 301 RefPtr<PropertyInt> blockType_; 302 303 // others 304 struct MarkerPenAndPath { 305 RSPen pen; 306 RSBrush brush; 307 RSPath path; 308 } markerPenAndPath; 309 310 OffsetF targetSelectEnd_; 311 PointF targetCenter_; 312 bool isVisible_ = true; 313 bool mouseHoverFlag_ = false; 314 bool mousePressedFlag_ = false; 315 bool reverse_ = false; 316 bool needAnimate_ = false; // Translate Animation on-off 317 float hotCircleShadowWidth_ = 0.0f; 318 Color blockOuterEdgeColor_; 319 Color blockShadowColor_; 320 RefPtr<BasicShape> shape_; 321 ACE_DISALLOW_COPY_AND_MOVE(SliderContentModifier); 322 }; 323 324 } // namespace OHOS::Ace::NG 325 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SLIDER_SLIDER_CONTENT_MODIFIER_H 326