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 class ACE_EXPORT DotIndicatorPaintMethod : public NodePaintMethod { DECLARE_ACE_TYPE(DotIndicatorPaintMethod,NodePaintMethod)29 DECLARE_ACE_TYPE(DotIndicatorPaintMethod, NodePaintMethod) 30 public: 31 explicit DotIndicatorPaintMethod(const RefPtr<DotIndicatorModifier>& dotIndicatorModifier) 32 : dotIndicatorModifier_(dotIndicatorModifier) 33 {} 34 ~DotIndicatorPaintMethod() override = default; 35 GetContentModifier(PaintWrapper * paintWrapper)36 RefPtr<Modifier> GetContentModifier(PaintWrapper* paintWrapper) override 37 { 38 CHECK_NULL_RETURN(dotIndicatorModifier_, nullptr); 39 return dotIndicatorModifier_; 40 } 41 42 void UpdateContentModifier(PaintWrapper* paintWrapper) override; 43 void PaintNormalIndicator(const PaintWrapper* paintWrapper); 44 void PaintHoverIndicator(const PaintWrapper* paintWrapper); 45 void PaintPressIndicator(const PaintWrapper* paintWrapper); 46 void CalculateNormalMargin(const LinearVector<float>& itemHalfSizes, const SizeF& frameSize); 47 void CalculatePointCenterX(const LinearVector<float>& itemHalfSizes, float margin, 48 float padding, float space, int32_t index); 49 void CalculateHoverIndex(const LinearVector<float>& itemHalfSizes); 50 bool isHoverPoint(const PointF& hoverPoint, const OffsetF& leftCenter, 51 const OffsetF& rightCenter, const LinearVector<float>& itemHalfSizes); 52 53 void UpdateBackground(const PaintWrapper* paintWrapper); SetCurrentIndex(int32_t index)54 void SetCurrentIndex(int32_t index) 55 { 56 currentIndex_ = index; 57 } 58 SetItemCount(int32_t itemCount)59 void SetItemCount(int32_t itemCount) 60 { 61 itemCount_ = itemCount; 62 } 63 SetDisplayCount(int32_t displayCount)64 void SetDisplayCount(int32_t displayCount) 65 { 66 displayCount_ = displayCount; 67 } 68 SetAxis(Axis axis)69 void SetAxis(Axis axis) 70 { 71 axis_ = axis; 72 } 73 GetAxis()74 Axis GetAxis() const 75 { 76 return axis_; 77 } 78 SetIsLoop(bool isLoop)79 void SetIsLoop(bool isLoop) 80 { 81 isLoop_ = isLoop; 82 } 83 SetIsHover(bool isHover)84 void SetIsHover(bool isHover) 85 { 86 isHover_ = isHover; 87 } 88 SetIsPressed(bool isPressed)89 void SetIsPressed(bool isPressed) 90 { 91 isPressed_ = isPressed; 92 } 93 SetHoverPoint(const PointF & hoverPoint)94 void SetHoverPoint(const PointF& hoverPoint) 95 { 96 hoverPoint_ = hoverPoint; 97 } 98 SetTurnPageRate(float turnPageRate)99 void SetTurnPageRate(float turnPageRate) 100 { 101 turnPageRate_ = turnPageRate; 102 } 103 SetTouchBottomRate(float touchBottomRate)104 void SetTouchBottomRate(float touchBottomRate) 105 { 106 touchBottomRate_ = touchBottomRate; 107 } 108 SetMouseClickIndex(const std::optional<int32_t> & mouseClickIndex)109 void SetMouseClickIndex(const std::optional<int32_t>& mouseClickIndex) 110 { 111 mouseClickIndex_ = mouseClickIndex; 112 } 113 SetIsTouchBottom(TouchBottomType touchBottomType)114 void SetIsTouchBottom(TouchBottomType touchBottomType) 115 { 116 touchBottomType_ = touchBottomType; 117 } 118 private: 119 struct StarAndEndPointCenter { 120 float startLongPointLeftCenterX = 0.0f; 121 float endLongPointLeftCenterX = 0.0f; 122 float startLongPointRightCenterX = 0.0f; 123 float endLongPointRightCenterX = 0.0f; 124 }; 125 void CalculatePointCenterX(const StarAndEndPointCenter& starAndEndPointCenter, 126 const LinearVector<float>& startVectorBlackPointCenterX, const LinearVector<float>& endVectorBlackPointCenterX); 127 void ForwardCalculation( 128 const LinearVector<float>& itemHalfSizes, float startCenterX, float endCenterX, float space, int32_t index); 129 void BackwardCalculation( 130 const LinearVector<float>& itemHalfSizes, float startCenterX, float endCenterX, float space, int32_t index); GetSwiperIndicatorTheme()131 static RefPtr<OHOS::Ace::SwiperIndicatorTheme> GetSwiperIndicatorTheme() 132 { 133 auto pipelineContext = PipelineBase::GetCurrentContext(); 134 CHECK_NULL_RETURN(pipelineContext, nullptr); 135 auto swiperTheme = pipelineContext->GetTheme<SwiperIndicatorTheme>(); 136 CHECK_NULL_RETURN(swiperTheme, nullptr); 137 return swiperTheme; 138 } 139 140 RefPtr<DotIndicatorModifier> dotIndicatorModifier_; 141 PointF hoverPoint_; 142 std::optional<int32_t> hoverIndex_ = std::nullopt; 143 std::optional<int32_t> mouseClickIndex_ = std::nullopt; 144 Axis axis_ = Axis::HORIZONTAL; 145 int32_t currentIndex_ = 0; 146 int32_t itemCount_ = 0; 147 int32_t displayCount_ = 1; 148 float turnPageRate_ = 0.0f; 149 float touchBottomRate_ = 0.0f; 150 bool isLoop_ = true; 151 bool isHover_ = false; 152 bool isPressed_ = false; 153 bool longPointIsHover_ = false; 154 bool IsCustomSizeValue_ = false; 155 // Animatable properties for updating Modifier 156 LinearVector<float> vectorBlackPointCenterX_ = {}; 157 std::pair<float, float> longPointCenterX_ = { 0, 0 }; 158 OffsetF normalMargin_ = { 0, 0 }; 159 float centerY_ = 0.0f; 160 TouchBottomType touchBottomType_ = TouchBottomType::NONE; 161 ACE_DISALLOW_COPY_AND_MOVE(DotIndicatorPaintMethod); 162 }; 163 } // namespace OHOS::Ace::NG 164 165 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_SWIPER_INDICATOR_DOT_INDICATOR_PAINT_METHOD_H