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