• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_SWIPER_INDICATOR_DOT_INDICATOR_PAINT_METHOD_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SWIPER_INDICATOR_DOT_INDICATOR_PAINT_METHOD_H
18 
19 #include "core/components/common/properties/swiper_indicator.h"
20 #include "core/components_ng/pattern/swiper_indicator/dot_indicator/dot_indicator_modifier.h"
21 #include "core/components_ng/pattern/swiper_indicator/dot_indicator/dot_indicator_paint_property.h"
22 #include "core/components_ng/render/canvas_image.h"
23 #include "core/components_ng/render/node_paint_method.h"
24 #include "core/components_ng/render/paint_wrapper.h"
25 #include "core/components_ng/render/render_context.h"
26 
27 namespace OHOS::Ace::NG {
28 enum class PointAnimationStage { STATE_SHRINKT_TO_BLACK_POINT, STATE_EXPAND_TO_LONG_POINT };
29 
30 class ACE_EXPORT DotIndicatorPaintMethod : public NodePaintMethod {
DECLARE_ACE_TYPE(DotIndicatorPaintMethod,NodePaintMethod)31     DECLARE_ACE_TYPE(DotIndicatorPaintMethod, NodePaintMethod)
32 public:
33     explicit DotIndicatorPaintMethod(const RefPtr<DotIndicatorModifier>& dotIndicatorModifier)
34         : dotIndicatorModifier_(dotIndicatorModifier)
35     {}
36     ~DotIndicatorPaintMethod() override = default;
37 
GetContentModifier(PaintWrapper * paintWrapper)38     RefPtr<Modifier> GetContentModifier(PaintWrapper* paintWrapper) override
39     {
40         CHECK_NULL_RETURN(dotIndicatorModifier_, nullptr);
41         return dotIndicatorModifier_;
42     }
43 
44     void UpdateContentModifier(PaintWrapper* paintWrapper) override;
45     void PaintNormalIndicator(const PaintWrapper* paintWrapper);
46     void PaintHoverIndicator(const PaintWrapper* paintWrapper);
47     void PaintPressIndicator(const PaintWrapper* paintWrapper);
48     void CalculateNormalMargin(const LinearVector<float>& itemHalfSizes, const SizeF& frameSize);
49     std::pair<float, float> CalculatePointCenterX(
50         const LinearVector<float>& itemHalfSizes, float margin, float padding, float space, int32_t index);
51     void CalculateHoverIndex(const LinearVector<float>& itemHalfSizes);
52     bool isHoverPoint(const PointF& hoverPoint, const OffsetF& leftCenter,
53         const OffsetF& rightCenter, const LinearVector<float>& itemHalfSizes);
54 
55     void UpdateBackground(const PaintWrapper* paintWrapper);
SetCurrentIndex(int32_t index)56     void SetCurrentIndex(int32_t index)
57     {
58         currentIndex_ = index;
59     }
60 
SetItemCount(int32_t itemCount)61     void SetItemCount(int32_t itemCount)
62     {
63         itemCount_ = itemCount;
64     }
65 
SetDisplayCount(int32_t displayCount)66     void SetDisplayCount(int32_t displayCount)
67     {
68         displayCount_ = displayCount;
69     }
70 
SetAxis(Axis axis)71     void SetAxis(Axis axis)
72     {
73         axis_ = axis;
74     }
75 
GetAxis()76     Axis GetAxis() const
77     {
78         return axis_;
79     }
80 
SetIsLoop(bool isLoop)81     void SetIsLoop(bool isLoop)
82     {
83         isLoop_ = isLoop;
84     }
85 
SetIsHover(bool isHover)86     void SetIsHover(bool isHover)
87     {
88         isHover_ = isHover;
89     }
90 
SetIsPressed(bool isPressed)91     void SetIsPressed(bool isPressed)
92     {
93         isPressed_ = isPressed;
94     }
95 
SetHoverPoint(const PointF & hoverPoint)96     void SetHoverPoint(const PointF& hoverPoint)
97     {
98         hoverPoint_ = hoverPoint;
99     }
100 
SetTurnPageRate(float turnPageRate)101     void SetTurnPageRate(float turnPageRate)
102     {
103         turnPageRate_ = turnPageRate;
104     }
105 
SetGestureState(GestureState gestureState)106     void SetGestureState(GestureState gestureState)
107     {
108         gestureState_ = gestureState;
109     }
110 
SetTouchBottomTypeLoop(TouchBottomTypeLoop touchBottomTypeLoop)111     void SetTouchBottomTypeLoop(TouchBottomTypeLoop touchBottomTypeLoop)
112     {
113         touchBottomTypeLoop_ = touchBottomTypeLoop;
114     }
115 
SetTouchBottomRate(float touchBottomRate)116     void SetTouchBottomRate(float touchBottomRate)
117     {
118         touchBottomRate_ = touchBottomRate;
119     }
120 
SetMouseClickIndex(const std::optional<int32_t> & mouseClickIndex)121     void SetMouseClickIndex(const std::optional<int32_t>& mouseClickIndex)
122     {
123         mouseClickIndex_ = mouseClickIndex;
124     }
125 
SetIsTouchBottom(TouchBottomType touchBottomType)126     void SetIsTouchBottom(TouchBottomType touchBottomType)
127     {
128         touchBottomType_ = touchBottomType;
129     }
130 
SetPointAnimationStage(PointAnimationStage pointAnimationStage)131     void SetPointAnimationStage(PointAnimationStage pointAnimationStage)
132     {
133         pointAnimationStage_ = pointAnimationStage;
134     }
135 
SetCurrentIndexActual(int32_t currentIndexActual)136     void SetCurrentIndexActual(int32_t currentIndexActual)
137     {
138         currentIndexActual_ = currentIndexActual;
139     }
140 
SetNextValidIndex(int32_t nextValidIndex)141     void SetNextValidIndex(int32_t nextValidIndex)
142     {
143         nextValidIndex_ = nextValidIndex;
144     }
145 
146 private:
147     struct StarAndEndPointCenter {
148         float startLongPointLeftCenterX = 0.0f;
149         float endLongPointLeftCenterX = 0.0f;
150         float startLongPointRightCenterX = 0.0f;
151         float endLongPointRightCenterX = 0.0f;
152     };
153     std::pair<float, float> CalculatePointCenterX(const StarAndEndPointCenter& starAndEndPointCenter,
154         const LinearVector<float>& startVectorBlackPointCenterX, const LinearVector<float>& endVectorBlackPointCenterX);
155     std::tuple<std::pair<float, float>, LinearVector<float>> CalculateLongPointCenterX(
156         const PaintWrapper* paintWrapper);
157     std::pair<float, float> ForwardCalculation(
158         const LinearVector<float>& itemHalfSizes, float startCenterX, float endCenterX, float space, int32_t index);
159     std::pair<float, float> BackwardCalculation(
160         const LinearVector<float>& itemHalfSizes, float startCenterX, float endCenterX, float space, int32_t index);
GetSwiperIndicatorTheme()161     static RefPtr<OHOS::Ace::SwiperIndicatorTheme> GetSwiperIndicatorTheme()
162     {
163         auto pipelineContext = PipelineBase::GetCurrentContext();
164         CHECK_NULL_RETURN(pipelineContext, nullptr);
165         auto swiperTheme = pipelineContext->GetTheme<SwiperIndicatorTheme>();
166         CHECK_NULL_RETURN(swiperTheme, nullptr);
167         return swiperTheme;
168     }
169     void UpdateNormalIndicator(LinearVector<float>& itemHalfSizes, const PaintWrapper* paintWrapper);
170     std::pair<int32_t, int32_t> GetStartAndEndIndex(int32_t index);
171     void GetLongPointAnimationStateSecondCenter(
172         const PaintWrapper* paintWrapper, std::vector<std::pair<float, float>>& pointCenterX);
173     std::tuple<float, float, float> GetMoveRate();
174     void AdjustPointCenterXForTouchBottom(StarAndEndPointCenter& pointCenter,
175         LinearVector<float>& endVectorBlackPointCenterX, int32_t startCurrentIndex, int32_t endCurrentIndex,
176         float selectedItemWidth, int32_t index);
177     std::pair<int32_t, int32_t> GetIndex(int32_t index);
178 
179     RefPtr<DotIndicatorModifier> dotIndicatorModifier_;
180     PointF hoverPoint_;
181     std::optional<int32_t> hoverIndex_ = std::nullopt;
182     std::optional<int32_t> mouseClickIndex_ = std::nullopt;
183     Axis axis_ = Axis::HORIZONTAL;
184     int32_t currentIndex_ = 0;
185     int32_t currentIndexActual_ = 0;
186     int32_t nextValidIndex_ = 0;
187     int32_t itemCount_ = 0;
188     int32_t displayCount_ = 1;
189     float turnPageRate_ = 0.0f;
190     GestureState gestureState_ = GestureState::GESTURE_STATE_INIT;
191     TouchBottomTypeLoop touchBottomTypeLoop_ = TouchBottomTypeLoop::TOUCH_BOTTOM_TYPE_LOOP_NONE;
192     PointAnimationStage pointAnimationStage_ = PointAnimationStage::STATE_SHRINKT_TO_BLACK_POINT;
193     float touchBottomRate_ = 0.0f;
194     bool isLoop_ = true;
195     bool isHover_ = false;
196     bool isPressed_ = false;
197     bool longPointIsHover_ = false;
198     bool IsCustomSizeValue_ = false;
199     // Animatable properties for updating Modifier
200     LinearVector<float> vectorBlackPointCenterX_ = {};
201     std::pair<float, float> longPointCenterX_ = { 0, 0 };
202     OffsetF normalMargin_ = { 0, 0 };
203     float centerY_ = 0.0f;
204     TouchBottomType touchBottomType_ = TouchBottomType::NONE;
205     ACE_DISALLOW_COPY_AND_MOVE(DotIndicatorPaintMethod);
206 };
207 } // namespace OHOS::Ace::NG
208 
209 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SWIPER_INDICATOR_DOT_INDICATOR_PAINT_METHOD_H