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 { 32 DECLARE_ACE_TYPE(DotIndicatorPaintMethod, NodePaintMethod); 33 public: DotIndicatorPaintMethod(const RefPtr<DotIndicatorModifier> & dotIndicatorModifier)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 SetIsAutoLinear(bool isAutoLinear)72 void SetIsAutoLinear(bool isAutoLinear) 73 { 74 isAutoLinear_ = isAutoLinear; 75 } 76 SetTotalItemCount(int32_t totalItemCount)77 void SetTotalItemCount(int32_t totalItemCount) 78 { 79 totalItemCount_ = totalItemCount; 80 } 81 SetDisplayCount(int32_t displayCount)82 void SetDisplayCount(int32_t displayCount) 83 { 84 displayCount_ = displayCount; 85 } 86 SetSwipeByGroup(bool isSwipeByGroup)87 void SetSwipeByGroup(bool isSwipeByGroup) 88 { 89 isSwipeByGroup_ = isSwipeByGroup; 90 } 91 SetAxis(Axis axis)92 void SetAxis(Axis axis) 93 { 94 axis_ = axis; 95 } 96 GetAxis()97 Axis GetAxis() const 98 { 99 return axis_; 100 } 101 SetIsLoop(bool isLoop)102 void SetIsLoop(bool isLoop) 103 { 104 isLoop_ = isLoop; 105 } 106 SetIsHover(bool isHover)107 void SetIsHover(bool isHover) 108 { 109 isHover_ = isHover; 110 } 111 SetIsPressed(bool isPressed)112 void SetIsPressed(bool isPressed) 113 { 114 isPressed_ = isPressed; 115 } 116 SetHoverPoint(const PointF & hoverPoint)117 void SetHoverPoint(const PointF& hoverPoint) 118 { 119 hoverPoint_ = hoverPoint; 120 } 121 SetTurnPageRate(float turnPageRate)122 void SetTurnPageRate(float turnPageRate) 123 { 124 turnPageRate_ = turnPageRate; 125 } 126 SetGroupTurnPageRate(float groupTurnPageRate)127 void SetGroupTurnPageRate(float groupTurnPageRate) 128 { 129 groupTurnPageRate_ = groupTurnPageRate; 130 } 131 GetGestureState()132 GestureState GetGestureState() 133 { 134 return gestureState_; 135 } 136 SetGestureState(GestureState gestureState)137 void SetGestureState(GestureState gestureState) 138 { 139 gestureState_ = gestureState; 140 } 141 SetTouchBottomTypeLoop(TouchBottomTypeLoop touchBottomTypeLoop)142 void SetTouchBottomTypeLoop(TouchBottomTypeLoop touchBottomTypeLoop) 143 { 144 touchBottomTypeLoop_ = touchBottomTypeLoop; 145 } 146 SetTouchBottomRate(float touchBottomRate)147 void SetTouchBottomRate(float touchBottomRate) 148 { 149 touchBottomRate_ = touchBottomRate; 150 } 151 SetTouchBottomPageRate(float touchBottomPageRate)152 void SetTouchBottomPageRate(float touchBottomPageRate) 153 { 154 touchBottomPageRate_ = touchBottomPageRate; 155 } 156 SetMouseClickIndex(const std::optional<int32_t> & mouseClickIndex)157 void SetMouseClickIndex(const std::optional<int32_t>& mouseClickIndex) 158 { 159 mouseClickIndex_ = mouseClickIndex; 160 } 161 SetIsTouchBottom(TouchBottomType touchBottomType)162 void SetIsTouchBottom(TouchBottomType touchBottomType) 163 { 164 touchBottomType_ = touchBottomType; 165 } 166 SetPointAnimationStage(PointAnimationStage pointAnimationStage)167 void SetPointAnimationStage(PointAnimationStage pointAnimationStage) 168 { 169 pointAnimationStage_ = pointAnimationStage; 170 } 171 SetCurrentIndexActual(int32_t currentIndexActual)172 void SetCurrentIndexActual(int32_t currentIndexActual) 173 { 174 currentIndexActual_ = currentIndexActual; 175 } 176 SetNextValidIndex(int32_t nextValidIndex)177 void SetNextValidIndex(int32_t nextValidIndex) 178 { 179 nextValidIndex_ = nextValidIndex; 180 } 181 SetHorizontalAndRightToLeft(TextDirection textDirection)182 void SetHorizontalAndRightToLeft(TextDirection textDirection) 183 { 184 isHorizontalAndRightToLeft_ = axis_ == Axis::HORIZONTAL && textDirection == TextDirection::RTL; 185 } 186 SetFirstIndex(int32_t index)187 void SetFirstIndex(int32_t index) 188 { 189 firstIndex_ = index; 190 } 191 SetTargetIndex(const std::optional<int32_t> & targetIndex)192 void SetTargetIndex(const std::optional<int32_t>& targetIndex) 193 { 194 targetIndex_ = targetIndex; 195 } 196 197 protected: 198 struct StarAndEndPointCenter { 199 float startLongPointLeftCenterX = 0.0f; 200 float endLongPointLeftCenterX = 0.0f; 201 float startLongPointRightCenterX = 0.0f; 202 float endLongPointRightCenterX = 0.0f; 203 }; 204 virtual std::pair<float, float> CalculatePointCenterX(const StarAndEndPointCenter& starAndEndPointCenter, 205 const LinearVector<float>& startVectorBlackPointCenterX, 206 const LinearVector<float>& endVectorBlackPointCenterX); 207 virtual std::tuple<std::pair<float, float>, LinearVector<float>> CalculateLongPointCenterX( 208 const PaintWrapper* paintWrapper); 209 virtual std::pair<float, float> ForwardCalculation( 210 const LinearVector<float>& itemHalfSizes, float startCenterX, float endCenterX, float space, int32_t index); 211 std::pair<float, float> BackwardCalculation( 212 const LinearVector<float>& itemHalfSizes, float startCenterX, float endCenterX, float space, int32_t index); GetSwiperIndicatorTheme()213 static RefPtr<OHOS::Ace::SwiperIndicatorTheme> GetSwiperIndicatorTheme() 214 { 215 auto pipelineContext = PipelineBase::GetCurrentContext(); 216 CHECK_NULL_RETURN(pipelineContext, nullptr); 217 auto swiperTheme = pipelineContext->GetTheme<SwiperIndicatorTheme>(); 218 CHECK_NULL_RETURN(swiperTheme, nullptr); 219 return swiperTheme; 220 } 221 virtual void UpdateNormalIndicator(LinearVector<float>& itemHalfSizes, const PaintWrapper* paintWrapper); 222 std::pair<int32_t, int32_t> GetStartAndEndIndex(int32_t index); 223 void GetLongPointAnimationStateSecondCenter( 224 const PaintWrapper* paintWrapper, std::vector<std::pair<float, float>>& pointCenterX); 225 std::tuple<float, float, float> GetMoveRate(); 226 void AdjustPointCenterXForTouchBottom(StarAndEndPointCenter& pointCenter, 227 LinearVector<float>& endVectorBlackPointCenterX, int32_t startCurrentIndex, int32_t endCurrentIndex, 228 float selectedItemWidth, int32_t index); 229 bool AdjustPointCenterXForTouchBottomNew(StarAndEndPointCenter& pointCenter, 230 LinearVector<float>& endVectorBlackPointCenterX, int32_t endCurrentIndex, float selectedItemWidth); 231 std::pair<int32_t, int32_t> GetIndex(int32_t index); 232 std::pair<int32_t, int32_t> GetIndexOnRTL(int32_t index); 233 bool NeedBottomAnimation() const; 234 int32_t CalculateMouseClickIndexOnRTL(); 235 std::pair<int32_t, int32_t> CalCurrentIndex(); 236 237 RefPtr<DotIndicatorModifier> dotIndicatorModifier_; 238 PointF hoverPoint_; 239 std::optional<int32_t> hoverIndex_ = std::nullopt; 240 std::optional<int32_t> mouseClickIndex_ = std::nullopt; 241 std::optional<int32_t> targetIndex_ = std::nullopt; 242 Axis axis_ = Axis::HORIZONTAL; 243 int32_t currentIndex_ = 0; 244 int32_t currentIndexActual_ = 0; 245 int32_t firstIndex_ = 0; 246 int32_t nextValidIndex_ = 0; 247 int32_t itemCount_ = 0; 248 int32_t totalItemCount_ = 0; 249 int32_t displayCount_ = 1; 250 bool isAutoLinear_ = false; 251 float turnPageRate_ = 0.0f; 252 float groupTurnPageRate_ = 0.0f; 253 GestureState gestureState_ = GestureState::GESTURE_STATE_INIT; 254 TouchBottomTypeLoop touchBottomTypeLoop_ = TouchBottomTypeLoop::TOUCH_BOTTOM_TYPE_LOOP_NONE; 255 PointAnimationStage pointAnimationStage_ = PointAnimationStage::STATE_SHRINKT_TO_BLACK_POINT; 256 float touchBottomRate_ = 0.0f; 257 float touchBottomPageRate_ = 0.0f; 258 bool isHorizontalAndRightToLeft_ = false; 259 bool isLoop_ = true; 260 bool isHover_ = false; 261 bool isPressed_ = false; 262 bool longPointIsHover_ = false; 263 bool IsCustomSizeValue_ = false; 264 bool isSwipeByGroup_ = false; 265 // Animatable properties for updating Modifier 266 LinearVector<float> vectorBlackPointCenterX_ = {}; 267 std::pair<float, float> longPointCenterX_ = { 0, 0 }; 268 OffsetF normalMargin_ = { 0, 0 }; 269 float centerY_ = 0.0f; 270 TouchBottomType touchBottomType_ = TouchBottomType::NONE; 271 ACE_DISALLOW_COPY_AND_MOVE(DotIndicatorPaintMethod); 272 }; 273 } // namespace OHOS::Ace::NG 274 275 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SWIPER_INDICATOR_DOT_INDICATOR_PAINT_METHOD_H