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_SWIPER_INDICATOR_SWIPER_INDICATOR_MODIFIER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_INDICATOR_SWIPER_INDICATOR_MODIFIER_H 18 19 #include <optional> 20 21 #include "core/components/swiper/swiper_indicator_theme.h" 22 #include "core/components_ng/base/modifier.h" 23 #include "core/components_ng/render/animation_utils.h" 24 #include "core/components_ng/render/drawing_prop_convertor.h" 25 26 namespace OHOS::Ace::NG { 27 enum class TouchBottomType { 28 NONE = 0, 29 START, 30 END, 31 }; 32 class DotIndicatorModifier : public ContentModifier { 33 DECLARE_ACE_TYPE(DotIndicatorModifier, ContentModifier); 34 public: DotIndicatorModifier()35 DotIndicatorModifier() 36 : backgroundColor_(AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor::TRANSPARENT)), 37 vectorBlackPointCenterX_(AceType::MakeRefPtr<AnimatablePropertyVectorFloat>(LinearVector<float>(0))), 38 longPointLeftCenterX_(AceType::MakeRefPtr<AnimatablePropertyFloat>(0)), 39 longPointRightCenterX_(AceType::MakeRefPtr<AnimatablePropertyFloat>(0)), 40 normalToHoverPointDilateRatio_(AceType::MakeRefPtr<AnimatablePropertyFloat>(1)), 41 hoverToNormalPointDilateRatio_(AceType::MakeRefPtr<AnimatablePropertyFloat>(1)), 42 longPointDilateRatio_(AceType::MakeRefPtr<AnimatablePropertyFloat>(1)), 43 indicatorPadding_(AceType::MakeRefPtr<AnimatablePropertyFloat>(0)), 44 indicatorMargin_(AceType::MakeRefPtr<AnimatablePropertyOffsetF>(OffsetF(0, 0))), 45 itemHalfSizes_(AceType::MakeRefPtr<AnimatablePropertyVectorFloat>(LinearVector<float>(4))), 46 backgroundWidthDilateRatio_(AceType::MakeRefPtr<AnimatablePropertyFloat>(1)), 47 backgroundHeightDilateRatio_(AceType::MakeRefPtr<AnimatablePropertyFloat>(1)), 48 unselectedColor_(AceType::MakeRefPtr<PropertyColor>(Color::TRANSPARENT)), 49 selectedColor_(AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor::TRANSPARENT)), 50 touchBottomPointColor_(AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor::TRANSPARENT)) 51 { 52 AttachProperty(vectorBlackPointCenterX_); 53 AttachProperty(longPointLeftCenterX_); 54 AttachProperty(longPointRightCenterX_); 55 AttachProperty(normalToHoverPointDilateRatio_); 56 AttachProperty(hoverToNormalPointDilateRatio_); 57 AttachProperty(longPointDilateRatio_); 58 AttachProperty(backgroundColor_); 59 AttachProperty(indicatorPadding_); 60 AttachProperty(indicatorMargin_); 61 AttachProperty(itemHalfSizes_); 62 AttachProperty(unselectedColor_); 63 AttachProperty(selectedColor_); 64 AttachProperty(backgroundWidthDilateRatio_); 65 AttachProperty(backgroundHeightDilateRatio_); 66 AttachProperty(touchBottomPointColor_); 67 } 68 ~DotIndicatorModifier() override = default; 69 70 struct ContentProperty { 71 Color backgroundColor = Color::TRANSPARENT; 72 LinearVector<float> vectorBlackPointCenterX; 73 LinearVector<float> itemHalfSizes = {}; 74 float longPointLeftCenterX = 0; 75 float longPointRightCenterX = 0; 76 float normalToHoverPointDilateRatio = 1; 77 float hoverToNormalPointDilateRatio = 1; 78 float longPointDilateRatio = 0; 79 float indicatorPadding = 0; 80 OffsetF indicatorMargin = { 0, 0 }; 81 }; 82 83 void onDraw(DrawingContext& context) override; 84 // paint 85 void PaintContent(DrawingContext& context, ContentProperty& contentProperty); 86 void PaintUnselectedIndicator(RSCanvas& canvas, const OffsetF& center, const LinearVector<float>& itemHalfSizes, 87 bool currentIndexFlag, const LinearColor& indicatorColor); 88 void PaintSelectedIndicator(RSCanvas& canvas, const OffsetF& center, const OffsetF& leftCenter, 89 const OffsetF& rightCenter, const LinearVector<float>& itemHalfSizes); 90 void PaintMask(DrawingContext& context); 91 void PaintBackground(DrawingContext& context, const ContentProperty& contentProperty); 92 LinearVector<float> GetItemHalfSizes(size_t index, ContentProperty& contentProperty); 93 // Update property 94 void UpdateShrinkPaintProperty(const OffsetF& margin, const LinearVector<float>& normalItemHalfSizes, 95 const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX); 96 void UpdateDilatePaintProperty(const LinearVector<float>& hoverItemHalfSizes, 97 const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX); 98 void UpdateBackgroundColor(const Color& backgroundColor); 99 100 void UpdateNormalPaintProperty(const OffsetF& margin, const LinearVector<float>& normalItemHalfSizes, 101 const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX); 102 void UpdateHoverPaintProperty(const LinearVector<float>& hoverItemHalfSizes, 103 const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX); 104 void UpdatePressPaintProperty(const LinearVector<float>& hoverItemHalfSizes, 105 const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX); 106 // Update 107 void UpdateNormalToHoverPaintProperty(const LinearVector<float>& hoverItemHalfSizes, 108 const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX); 109 void UpdateHoverToNormalPaintProperty(const OffsetF& margin, const LinearVector<float>& normalItemHalfSizes, 110 const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX); 111 void UpdateNormalToPressPaintProperty(const LinearVector<float>& hoverItemHalfSizes, 112 const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX); 113 void UpdatePressToNormalPaintProperty(const OffsetF& margin, const LinearVector<float>& normalItemHalfSizes, 114 const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX); 115 void UpdateHoverAndPressConversionPaintProperty(); 116 117 // Point dilate ratio 118 void UpdateNormalToHoverPointDilateRatio(); 119 void UpdateHoverToNormalPointDilateRatio(); 120 void UpdateLongPointDilateRatio(); 121 122 void UpdateAllPointCenterXAnimation(GestureState gestureState, const LinearVector<float>& vectorBlackPointCenterX, 123 const std::pair<float, float>& longPointCenterX); 124 125 // Touch bottom animation 126 void UpdateTouchBottomAnimation(TouchBottomType touchBottomType, const LinearVector<float>& vectorBlackPointCenterX, 127 const std::pair<float, float>& longPointCenterX, float touchBottomRate); 128 SetAxis(Axis axis)129 void SetAxis(Axis axis) 130 { 131 axis_ = axis; 132 } 133 SetUnselectedColor(const Color & unselectedColor)134 void SetUnselectedColor(const Color& unselectedColor) 135 { 136 if (unselectedColor_) { 137 unselectedColor_->Set(unselectedColor); 138 } 139 } 140 SetSelectedColor(const Color & selectedColor)141 void SetSelectedColor(const Color& selectedColor) 142 { 143 if (selectedColor_ && isSelectedColorAnimEnd_) { 144 selectedColor_->Set(LinearColor(selectedColor)); 145 } 146 if (touchBottomPointColor_ && isSelectedColorAnimEnd_) { 147 touchBottomPointColor_->Set(LinearColor(selectedColor)); 148 } 149 } 150 SetCurrentIndex(int32_t index)151 void SetCurrentIndex(int32_t index) 152 { 153 currentIndex_ = index; 154 } 155 SetNormalToHoverIndex(const std::optional<int32_t> & normalToHoverIndex)156 void SetNormalToHoverIndex(const std::optional<int32_t>& normalToHoverIndex) 157 { 158 normalToHoverIndex_ = normalToHoverIndex; 159 } 160 SetHoverToNormalIndex(const std::optional<int32_t> & hoverToNormalIndex)161 void SetHoverToNormalIndex(const std::optional<int32_t>& hoverToNormalIndex) 162 { 163 hoverToNormalIndex_ = hoverToNormalIndex; 164 } 165 GetNormalToHoverIndex()166 std::optional<int32_t> GetNormalToHoverIndex() 167 { 168 return normalToHoverIndex_; 169 } 170 GetHoverToNormalIndex()171 std::optional<int32_t> GetHoverToNormalIndex() 172 { 173 return hoverToNormalIndex_; 174 } 175 SetIndicatorMask(bool indicatorMask)176 void SetIndicatorMask(bool indicatorMask) 177 { 178 indicatorMask_ = indicatorMask; 179 } 180 SetOffset(const OffsetF & offset)181 void SetOffset(const OffsetF& offset) 182 { 183 offset_ = offset; 184 } 185 SetCenterY(const float & centerY)186 void SetCenterY(const float& centerY) 187 { 188 centerY_ = centerY; 189 } 190 SetIsHover(bool isHover)191 void SetIsHover(bool isHover) 192 { 193 isHover_ = isHover; 194 } 195 GetIsHover()196 bool GetIsHover() const 197 { 198 return isHover_; 199 } 200 SetIsPressed(bool isPressed)201 void SetIsPressed(bool isPressed) 202 { 203 isPressed_ = isPressed; 204 } 205 GetIsPressed()206 bool GetIsPressed() const 207 { 208 return isPressed_; 209 } 210 SetLongPointIsHover(bool isHover)211 void SetLongPointIsHover(bool isHover) 212 { 213 longPointIsHover_ = isHover; 214 } 215 GetLongPointIsHover()216 bool GetLongPointIsHover() const 217 { 218 return longPointIsHover_; 219 } 220 SetItemWidth(const float itemWidth)221 void SetItemWidth(const float itemWidth) 222 { 223 itemWidth_ = itemWidth; 224 } 225 SetItemHeight(const float itemHeight)226 void SetItemHeight(const float itemHeight) 227 { 228 itemHeight_ = itemHeight; 229 } 230 SetSelectedItemWidth(const float selectedItemWidth)231 void SetSelectedItemWidth(const float selectedItemWidth) 232 { 233 selectedItemWidth_ = selectedItemWidth; 234 } 235 SetSelectedItemHeight(const float selectedItemHeight)236 void SetSelectedItemHeight(const float selectedItemHeight) 237 { 238 selectedItemHeight_ = selectedItemHeight; 239 } 240 SetIsIndicatorCustomSize(bool isCustomSize)241 void SetIsIndicatorCustomSize(bool isCustomSize) 242 { 243 isCustomSize_ = isCustomSize; 244 } 245 SetAnimationDuration(int32_t duration)246 void SetAnimationDuration(int32_t duration) 247 { 248 animationDuration_ = duration; 249 } 250 void PlayIndicatorAnimation(const LinearVector<float>& vectorBlackPointCenterX, 251 const std::vector<std::pair<float, float>>& longPointCenterX, GestureState gestureState, 252 TouchBottomTypeLoop touchBottomTypeLoop); 253 void StopAnimation(); SetLongPointHeadCurve(RefPtr<Curve> curve,float motionVelocity)254 void SetLongPointHeadCurve(RefPtr<Curve> curve, float motionVelocity) 255 { 256 headCurve_ = curve; 257 motionVelocity_ = motionVelocity; 258 } 259 UpdateVectorBlackPointCenterX(const LinearVector<float> & value)260 inline void UpdateVectorBlackPointCenterX(const LinearVector<float>& value) 261 { 262 vectorBlackPointCenterX_->Set(value); 263 } 264 UpdateLongPointCenterX(const std::pair<float,float> & value)265 inline void UpdateLongPointCenterX(const std::pair<float, float>& value) 266 { 267 longPointLeftCenterX_->Set(value.first); 268 longPointRightCenterX_->Set(value.second); 269 } 270 271 private: GetSwiperIndicatorTheme()272 static RefPtr<OHOS::Ace::SwiperIndicatorTheme> GetSwiperIndicatorTheme() 273 { 274 auto pipelineContext = PipelineBase::GetCurrentContext(); 275 CHECK_NULL_RETURN(pipelineContext, nullptr); 276 auto swiperTheme = pipelineContext->GetTheme<SwiperIndicatorTheme>(); 277 CHECK_NULL_RETURN(swiperTheme, nullptr); 278 return swiperTheme; 279 } 280 281 void PlayBlackPointsAnimation(const LinearVector<float>& vectorBlackPointCenterX); 282 void PlayLongPointAnimation(const std::vector<std::pair<float, float>>& longPointCenterX, 283 GestureState gestureState, TouchBottomTypeLoop touchBottomTypeLoop, 284 const LinearVector<float>& vectorBlackPointCenterX); 285 void PlayTouchBottomAnimation(const std::vector<std::pair<float, float>>& longPointCenterX, 286 TouchBottomTypeLoop touchBottomTypeLoop, const LinearVector<float>& vectorBlackPointCenterX); 287 void PlayOpacityAnimation(); 288 289 RefPtr<AnimatablePropertyColor> backgroundColor_; 290 RefPtr<AnimatablePropertyVectorFloat> vectorBlackPointCenterX_; 291 RefPtr<AnimatablePropertyFloat> longPointLeftCenterX_; 292 RefPtr<AnimatablePropertyFloat> longPointRightCenterX_; 293 RefPtr<AnimatablePropertyFloat> normalToHoverPointDilateRatio_; 294 RefPtr<AnimatablePropertyFloat> hoverToNormalPointDilateRatio_; 295 RefPtr<AnimatablePropertyFloat> longPointDilateRatio_; 296 RefPtr<AnimatablePropertyFloat> indicatorPadding_; 297 RefPtr<AnimatablePropertyOffsetF> indicatorMargin_; 298 RefPtr<AnimatablePropertyVectorFloat> itemHalfSizes_; 299 RefPtr<AnimatablePropertyFloat> backgroundWidthDilateRatio_; 300 RefPtr<AnimatablePropertyFloat> backgroundHeightDilateRatio_; 301 302 std::shared_ptr<AnimationUtils::Animation> blackPointsAnimation_; 303 std::shared_ptr<AnimationUtils::Animation> longPointLeftAnimation_; 304 std::shared_ptr<AnimationUtils::Animation> longPointRightAnimation_; 305 RefPtr<Curve> headCurve_; 306 float motionVelocity_ = 0; 307 308 float centerY_ = 0; 309 Axis axis_ = Axis::HORIZONTAL; 310 RefPtr<PropertyColor> unselectedColor_; 311 RefPtr<AnimatablePropertyColor> selectedColor_; 312 bool isSelectedColorAnimEnd_ = true; 313 RefPtr<AnimatablePropertyColor> touchBottomPointColor_; 314 bool isTouchBottomLoop_ = false; 315 bool ifNeedFinishCallback_ = false; 316 std::optional<int32_t> normalToHoverIndex_ = std::nullopt; 317 std::optional<int32_t> hoverToNormalIndex_ = std::nullopt; 318 bool longPointIsHover_ = false; 319 bool isHover_ = false; 320 bool isPressed_ = false; 321 bool longPointLeftAnimEnd_ = true; 322 bool longPointRightAnimEnd_ = true; 323 324 bool indicatorMask_ = false; 325 bool isCustomSize_ = false; 326 int32_t currentIndex_ = 0; 327 int32_t animationDuration_ = 0; 328 OffsetF offset_; 329 float itemWidth_ = 0.0f; 330 float itemHeight_ = 0.0f; 331 float selectedItemWidth_ = 0.0f; 332 float selectedItemHeight_ = 0.0f; 333 TouchBottomType touchBottomType_ = TouchBottomType::NONE; 334 ACE_DISALLOW_COPY_AND_MOVE(DotIndicatorModifier); 335 }; 336 } // namespace OHOS::Ace::NG 337 338 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_INDICATOR_SWIPER_INDICATOR_MODIFIER_H 339