1 /* 2 * Copyright (c) 2024 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_OVER_INDICATOR_SWIPER_INDICATOR_MODIFIER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_OVER_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/pattern/swiper_indicator/dot_indicator/dot_indicator_modifier.h" 24 namespace OHOS::Ace::NG { 25 constexpr float ONE_IN_TWO = 0.5f; 26 constexpr float THREE_QUARTERS = 0.75f; 27 class OverlengthDotIndicatorModifier : public DotIndicatorModifier { 28 DECLARE_ACE_TYPE(OverlengthDotIndicatorModifier, DotIndicatorModifier); 29 30 public: OverlengthDotIndicatorModifier()31 OverlengthDotIndicatorModifier() 32 : DotIndicatorModifier(), 33 firstPointOpacity_(AceType::MakeRefPtr<AnimatablePropertyUint8>(0)), 34 newPointOpacity_(AceType::MakeRefPtr<AnimatablePropertyUint8>(0)), 35 unselectedIndicatorWidth_(AceType::MakeRefPtr<AnimatablePropertyVectorFloat>(LinearVector<float>(0))), 36 unselectedIndicatorHeight_(AceType::MakeRefPtr<AnimatablePropertyVectorFloat>(LinearVector<float>(0))) 37 { 38 AttachProperty(firstPointOpacity_); 39 AttachProperty(newPointOpacity_); 40 AttachProperty(unselectedIndicatorWidth_); 41 AttachProperty(unselectedIndicatorHeight_); 42 } 43 ~OverlengthDotIndicatorModifier() override = default; 44 45 void onDraw(DrawingContext& context) override; 46 // paint 47 void PaintBackground(DrawingContext& context, const ContentProperty& contentProperty, int32_t maxDisplayCount, 48 bool isBindIndicator); 49 void PaintContent(DrawingContext& context, ContentProperty& contentProperty) override; 50 void PaintBlackPoint(DrawingContext& context, ContentProperty& contentProperty); 51 void PaintUnselectedIndicator( 52 RSCanvas& canvas, const OffsetF& center, float width, float height, const LinearColor& indicatorColor); 53 void UpdateShrinkPaintProperty(const OffsetF& margin, const LinearVector<float>& normalItemHalfSizes, 54 const std::pair<float, float>& longPointCenterX); 55 56 void UpdateNormalPaintProperty(const OffsetF& margin, const LinearVector<float>& normalItemHalfSizes, 57 const std::pair<float, float>& longPointCenterX); SetRealItemCount(int32_t realItemCount)58 void SetRealItemCount(int32_t realItemCount) 59 { 60 realItemCount_ = realItemCount; 61 } 62 SetMaxDisplayCount(int32_t maxDisplayCount)63 void SetMaxDisplayCount(int32_t maxDisplayCount) 64 { 65 maxDisplayCount_ = maxDisplayCount; 66 } 67 68 void PlayIndicatorAnimation(const OffsetF& margin, const LinearVector<float>& itemHalfSizes, 69 GestureState gestureState, TouchBottomTypeLoop touchBottomTypeLoop); 70 SetMoveDirection(OverlongIndicatorMove moveDirection)71 void SetMoveDirection(OverlongIndicatorMove moveDirection) 72 { 73 moveDirection_ = moveDirection; 74 } 75 SetAnimationStartCenterX(const LinearVector<float> & animationStartCenterX)76 void SetAnimationStartCenterX(const LinearVector<float>& animationStartCenterX) 77 { 78 animationStartCenterX_ = animationStartCenterX; 79 } 80 SetAnimationEndCenterX(const LinearVector<float> & animationEndCenterX)81 void SetAnimationEndCenterX(const LinearVector<float>& animationEndCenterX) 82 { 83 animationEndCenterX_ = animationEndCenterX; 84 } 85 SetAnimationStartIndicatorWidth(const LinearVector<float> & animationStartIndicatorWidth)86 void SetAnimationStartIndicatorWidth(const LinearVector<float>& animationStartIndicatorWidth) 87 { 88 animationStartIndicatorWidth_ = animationStartIndicatorWidth; 89 } 90 SetAnimationEndIndicatorWidth(const LinearVector<float> & animationEndIndicatorWidth)91 void SetAnimationEndIndicatorWidth(const LinearVector<float>& animationEndIndicatorWidth) 92 { 93 animationEndIndicatorWidth_ = animationEndIndicatorWidth; 94 } 95 SetAnimationStartIndicatorHeight(const LinearVector<float> & animationStartIndicatorHeight)96 void SetAnimationStartIndicatorHeight(const LinearVector<float>& animationStartIndicatorHeight) 97 { 98 animationStartIndicatorHeight_ = animationStartIndicatorHeight; 99 } 100 SetAnimationEndIndicatorHeight(const LinearVector<float> & animationEndIndicatorHeight)101 void SetAnimationEndIndicatorHeight(const LinearVector<float>& animationEndIndicatorHeight) 102 { 103 animationEndIndicatorHeight_ = animationEndIndicatorHeight; 104 } 105 GetCurrentOverlongType()106 OverlongType GetCurrentOverlongType() const 107 { 108 return currentOverlongType_; 109 } 110 SetCurrentOverlongType(OverlongType currentOverlongType)111 void SetCurrentOverlongType(OverlongType currentOverlongType) 112 { 113 currentOverlongType_ = currentOverlongType; 114 } 115 SetAnimationStartIndex(int32_t animationStartIndex)116 void SetAnimationStartIndex(int32_t animationStartIndex) 117 { 118 animationStartIndex_ = animationStartIndex; 119 } 120 SetAnimationEndIndex(int32_t animationEndIndex)121 void SetAnimationEndIndex(int32_t animationEndIndex) 122 { 123 animationEndIndex_ = animationEndIndex; 124 } 125 GetAnimationEndIndex()126 int32_t GetAnimationEndIndex() const 127 { 128 return animationEndIndex_; 129 } 130 SetTurnPageRate(float turnPageRate)131 void SetTurnPageRate(float turnPageRate) 132 { 133 turnPageRate_ = turnPageRate; 134 } 135 SetForceStopPageRate(float forceStopPageRate)136 void SetForceStopPageRate(float forceStopPageRate) 137 { 138 forceStopPageRate_ = forceStopPageRate; 139 } 140 SetBlackPointCenterMoveRate(float blackPointCenterMoveRate)141 void SetBlackPointCenterMoveRate(float blackPointCenterMoveRate) 142 { 143 blackPointCenterMoveRate_ = blackPointCenterMoveRate; 144 } 145 SetLongPointLeftCenterMoveRate(float longPointLeftCenterMoveRate)146 void SetLongPointLeftCenterMoveRate(float longPointLeftCenterMoveRate) 147 { 148 longPointLeftCenterMoveRate_ = longPointLeftCenterMoveRate; 149 } 150 SetLongPointRightCenterMoveRate(float longPointRightCenterMoveRate)151 void SetLongPointRightCenterMoveRate(float longPointRightCenterMoveRate) 152 { 153 longPointRightCenterMoveRate_ = longPointRightCenterMoveRate; 154 } 155 SetGestureState(GestureState gestureState)156 void SetGestureState(GestureState gestureState) 157 { 158 gestureState_ = gestureState; 159 } 160 SetIsCustomSizeValue(bool isCustomSizeValue)161 void SetIsCustomSizeValue(bool isCustomSizeValue) 162 { 163 isCustomSizeValue_ = isCustomSizeValue; 164 } 165 SetTouchBottomTypeLoop(TouchBottomTypeLoop touchBottomTypeLoop)166 void SetTouchBottomTypeLoop(TouchBottomTypeLoop touchBottomTypeLoop) 167 { 168 touchBottomTypeLoop_ = touchBottomTypeLoop; 169 } 170 SetIsSwiperTouchDown(bool isSwiperTouchDown)171 void SetIsSwiperTouchDown(bool isSwiperTouchDown) 172 { 173 isSwiperTouchDown_ = isSwiperTouchDown; 174 } 175 SetIsHorizontalAndRTL(bool isHorizontalAndRTL)176 void SetIsHorizontalAndRTL(bool isHorizontalAndRTL) 177 { 178 isHorizontalAndRTL_ = isHorizontalAndRTL; 179 } 180 SetKeepStatus(bool keepStatus)181 void SetKeepStatus(bool keepStatus) 182 { 183 keepStatus_ = keepStatus; 184 } 185 UpdateCurrentStatus()186 void UpdateCurrentStatus() 187 { 188 currentSelectedIndex_ = targetSelectedIndex_; 189 currentOverlongType_ = targetOverlongType_; 190 } 191 SetNeedUpdate(bool needUpdate)192 void SetNeedUpdate(bool needUpdate) 193 { 194 needUpdate_ = needUpdate; 195 } 196 SetIsAutoPlay(bool isAutoPlay)197 void SetIsAutoPlay(bool isAutoPlay) 198 { 199 isAutoPlay_ = isAutoPlay; 200 } 201 SetIsBindIndicator(bool isBindIndicator)202 void SetIsBindIndicator(bool isBindIndicator) 203 { 204 isBindIndicator_ = isBindIndicator; 205 } 206 SetIsLoop(bool isLoop)207 void SetIsLoop(bool isLoop) 208 { 209 isLoop_ = isLoop; 210 } 211 212 void InitOverlongStatus(int32_t pageIndex); 213 void InitOverlongSelectedIndex(int32_t pageIndex); 214 void CalcTargetSelectedIndex(int32_t currentPageIndex, int32_t targetPageIndex); 215 void CalcTargetSelectedIndexOnForward(int32_t currentPageIndex, int32_t targetPageIndex); 216 void CalcTargetSelectedIndexOnBackward(int32_t currentPageIndex, int32_t targetPageIndex); 217 void CalcTargetOverlongStatus(int32_t currentPageIndex, int32_t targetPageIndex); 218 void StopAnimation(bool ifImmediately) override; 219 220 private: 221 void PlayBlackPointsAnimation(const LinearVector<float>& itemHalfSizes); 222 void CalcAnimationEndCenterX(const LinearVector<float>& itemHalfSizes); 223 void CalcTargetStatusOnLongPointMove(const LinearVector<float>& itemHalfSizes); 224 void CalcTargetStatusOnAllPointMoveForward(const LinearVector<float>& itemHalfSizes); 225 void CalcTargetStatusOnAllPointMoveBackward(const LinearVector<float>& itemHalfSizes); 226 std::pair<LinearVector<float>, std::pair<float, float>> CalcIndicatorCenterX( 227 const LinearVector<float>& itemHalfSizes, int32_t selectedIndex, OverlongType overlongType); 228 LinearVector<float> CalcIndicatorSize( 229 const LinearVector<float>& itemHalfSizes, OverlongType overlongType, bool isWidth); 230 void UpdateSelectedCenterXOnDrag(const LinearVector<float>& itemHalfSizes); 231 void UpdateUnselectedCenterXOnDrag(); 232 int32_t CalcTargetIndexOnDrag() const; 233 std::pair<float, float> CalcLongPointEndCenterXWithBlack(size_t index, const LinearVector<float>& itemHalfSizes); 234 float GetMoveRateOnAllMove() const; 235 int32_t GetBlackPointsAnimationDuration() const; 236 void AdjustTargetStatus(int32_t targetPageIndex); 237 std::pair<float, float> GetTouchBottomCenterX(ContentProperty& contentProperty); 238 OverlongType RevertOverlongType(OverlongType overlongType) const; 239 void StopBlackAnimation(); 240 bool NeedUpdateWhenAnimationFinish() const; 241 float CalcRealPadding( 242 float unselectedIndicatorRadius, float selectedIndicatorRadius, OverlongType overlongType) const; 243 244 RefPtr<AnimatablePropertyUint8> firstPointOpacity_; 245 RefPtr<AnimatablePropertyUint8> newPointOpacity_; 246 RefPtr<AnimatablePropertyVectorFloat> unselectedIndicatorWidth_; 247 RefPtr<AnimatablePropertyVectorFloat> unselectedIndicatorHeight_; 248 int32_t maxDisplayCount_ = 0; 249 int32_t realItemCount_ = 0; 250 OverlongIndicatorMove moveDirection_ = OverlongIndicatorMove::NONE; 251 LinearVector<float> animationStartCenterX_ = {}; 252 LinearVector<float> animationEndCenterX_ = {}; 253 LinearVector<float> animationStartIndicatorWidth_ = {}; 254 LinearVector<float> animationEndIndicatorWidth_ = {}; 255 LinearVector<float> animationStartIndicatorHeight_ = {}; 256 LinearVector<float> animationEndIndicatorHeight_ = {}; 257 std::pair<float, float> overlongSelectedStartCenterX_ = { 0.0f, 0.0f}; 258 std::pair<float, float> overlongSelectedEndCenterX_ = { 0.0f, 0.0f}; 259 OffsetF normalMargin_ = { 0, 0 }; 260 261 int32_t animationStartIndex_ = 0; 262 int32_t animationEndIndex_ = 0; 263 int32_t currentSelectedIndex_ = 0; 264 int32_t targetSelectedIndex_ = 0; 265 OverlongType currentOverlongType_ = OverlongType::NONE; 266 OverlongType targetOverlongType_ = OverlongType::NONE; 267 float turnPageRate_ = 0.0f; 268 float forceStopPageRate_ = FLT_MAX; 269 float blackPointCenterMoveRate_ = 0.0f; 270 float longPointLeftCenterMoveRate_ = 0.0f; 271 float longPointRightCenterMoveRate_ = 0.0f; 272 GestureState gestureState_ = GestureState::GESTURE_STATE_INIT; 273 TouchBottomTypeLoop touchBottomTypeLoop_ = TouchBottomTypeLoop::TOUCH_BOTTOM_TYPE_LOOP_NONE; 274 bool isCustomSizeValue_ = false; 275 bool isSwiperTouchDown_ = false; 276 bool keepStatus_ = false; 277 bool blackPointsAnimEnd_ = true; 278 bool isHorizontalAndRTL_ = false; 279 bool needUpdate_ = true; 280 bool isAutoPlay_ = false; 281 bool isBindIndicator_ = false; 282 bool isLoop_ = true; 283 ACE_DISALLOW_COPY_AND_MOVE(OverlengthDotIndicatorModifier); 284 }; 285 } // namespace OHOS::Ace::NG 286 287 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_OVER_INDICATOR_SWIPER_INDICATOR_MODIFIER_H 288