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