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 
SetBlackPointCenterMoveRate(float blackPointCenterMoveRate)134     void SetBlackPointCenterMoveRate(float blackPointCenterMoveRate)
135     {
136         blackPointCenterMoveRate_ = blackPointCenterMoveRate;
137     }
138 
SetLongPointLeftCenterMoveRate(float longPointLeftCenterMoveRate)139     void SetLongPointLeftCenterMoveRate(float longPointLeftCenterMoveRate)
140     {
141         longPointLeftCenterMoveRate_ = longPointLeftCenterMoveRate;
142     }
143 
SetLongPointRightCenterMoveRate(float longPointRightCenterMoveRate)144     void SetLongPointRightCenterMoveRate(float longPointRightCenterMoveRate)
145     {
146         longPointRightCenterMoveRate_ = longPointRightCenterMoveRate;
147     }
148 
SetGestureState(GestureState gestureState)149     void SetGestureState(GestureState gestureState)
150     {
151         gestureState_ = gestureState;
152     }
153 
SetIsCustomSizeValue(bool isCustomSizeValue)154     void SetIsCustomSizeValue(bool isCustomSizeValue)
155     {
156         isCustomSizeValue_ = isCustomSizeValue;
157     }
158 
SetTouchBottomTypeLoop(TouchBottomTypeLoop touchBottomTypeLoop)159     void SetTouchBottomTypeLoop(TouchBottomTypeLoop touchBottomTypeLoop)
160     {
161         touchBottomTypeLoop_ = touchBottomTypeLoop;
162     }
163 
SetIsSwiperTouchDown(bool isSwiperTouchDown)164     void SetIsSwiperTouchDown(bool isSwiperTouchDown)
165     {
166         isSwiperTouchDown_ = isSwiperTouchDown;
167     }
168 
SetIsHorizontalAndRTL(bool isHorizontalAndRTL)169     void SetIsHorizontalAndRTL(bool isHorizontalAndRTL)
170     {
171         isHorizontalAndRTL_ = isHorizontalAndRTL;
172     }
173 
SetKeepStatus(bool keepStatus)174     void SetKeepStatus(bool keepStatus)
175     {
176         keepStatus_ = keepStatus;
177     }
178 
UpdateCurrentStatus()179     void UpdateCurrentStatus()
180     {
181         currentSelectedIndex_ = targetSelectedIndex_;
182         currentOverlongType_ = targetOverlongType_;
183     }
184 
185     void InitOverlongStatus(int32_t pageIndex);
186     void InitOverlongSelectedIndex(int32_t pageIndex);
187     void CalcTargetSelectedIndex(int32_t currentPageIndex, int32_t targetPageIndex);
188     void CalcTargetSelectedIndexOnForward(int32_t currentPageIndex, int32_t targetPageIndex);
189     void CalcTargetSelectedIndexOnBackward(int32_t currentPageIndex, int32_t targetPageIndex);
190     void CalcTargetOverlongStatus(int32_t currentPageIndex, int32_t targetPageIndex);
191     void StopAnimation(bool ifImmediately) override;
192 
193 private:
194     void PlayBlackPointsAnimation(const LinearVector<float>& itemHalfSizes);
195     void CalcAnimationEndCenterX(const LinearVector<float>& itemHalfSizes);
196     void CalcTargetStatusOnLongPointMove(const LinearVector<float>& itemHalfSizes);
197     void CalcTargetStatusOnAllPointMoveForward(const LinearVector<float>& itemHalfSizes);
198     void CalcTargetStatusOnAllPointMoveBackward(const LinearVector<float>& itemHalfSizes);
199     std::pair<LinearVector<float>, std::pair<float, float>> CalcIndicatorCenterX(
200         const LinearVector<float>& itemHalfSizes, int32_t selectedIndex, OverlongType overlongType);
201     LinearVector<float> CalcIndicatorSize(
202         const LinearVector<float>& itemHalfSizes, OverlongType overlongType, bool isWidth);
203     void UpdateSelectedCenterXOnDrag(const LinearVector<float>& itemHalfSizes);
204     void UpdateUnselectedCenterXOnDrag();
205     int32_t CalcTargetIndexOnDrag() const;
206     std::pair<float, float> CalcLongPointEndCenterXWithBlack(int32_t index, const LinearVector<float>& itemHalfSizes);
207     float GetMoveRateOnAllMove() const;
208     int32_t GetBlackPointsAnimationDuration() const;
209     void AdjustTargetStatus(int32_t targetPageIndex);
210     std::pair<float, float> GetTouchBottomCenterX(ContentProperty& contentProperty);
211     OverlongType RevertOverlongType(OverlongType overlongType) const;
212 
213     RefPtr<AnimatablePropertyUint8> firstPointOpacity_;
214     RefPtr<AnimatablePropertyUint8> newPointOpacity_;
215     RefPtr<AnimatablePropertyVectorFloat> unselectedIndicatorWidth_;
216     RefPtr<AnimatablePropertyVectorFloat> unselectedIndicatorHeight_;
217     int32_t maxDisplayCount_ = 0;
218     int32_t realItemCount_ = 0;
219     OverlongIndicatorMove moveDirection_ = OverlongIndicatorMove::NONE;
220     LinearVector<float> animationStartCenterX_ = {};
221     LinearVector<float> animationEndCenterX_ = {};
222     LinearVector<float> animationStartIndicatorWidth_ = {};
223     LinearVector<float> animationEndIndicatorWidth_ = {};
224     LinearVector<float> animationStartIndicatorHeight_ = {};
225     LinearVector<float> animationEndIndicatorHeight_ = {};
226     std::pair<float, float> overlongSelectedStartCenterX_ = { 0.0f, 0.0f};
227     std::pair<float, float> overlongSelectedEndCenterX_ = { 0.0f, 0.0f};
228     OffsetF normalMargin_ = { 0, 0 };
229 
230     int32_t animationStartIndex_ = 0;
231     int32_t animationEndIndex_ = 0;
232     int32_t currentSelectedIndex_ = 0;
233     int32_t targetSelectedIndex_ = 0;
234     OverlongType currentOverlongType_ = OverlongType::NONE;
235     OverlongType targetOverlongType_ = OverlongType::NONE;
236     float turnPageRate_ = 0.0f;
237     float blackPointCenterMoveRate_ = 0.0f;
238     float longPointLeftCenterMoveRate_ = 0.0f;
239     float longPointRightCenterMoveRate_ = 0.0f;
240     GestureState gestureState_ = GestureState::GESTURE_STATE_INIT;
241     TouchBottomTypeLoop touchBottomTypeLoop_ = TouchBottomTypeLoop::TOUCH_BOTTOM_TYPE_LOOP_NONE;
242     bool isCustomSizeValue_ = false;
243     bool isSwiperTouchDown_ = false;
244     bool keepStatus_ = false;
245     bool blackPointsAnimEnd_ = true;
246     bool isHorizontalAndRTL_ = false;
247     ACE_DISALLOW_COPY_AND_MOVE(OverlengthDotIndicatorModifier);
248 };
249 } // namespace OHOS::Ace::NG
250 
251 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_OVER_INDICATOR_SWIPER_INDICATOR_MODIFIER_H
252