• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_PATTERNS_SWIPER_INDICATOR_SWIPER_INDICATOR_MODIFIER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_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/render/drawing_prop_convertor.h"
24 
25 namespace OHOS::Ace::NG {
26 enum class TouchBottomType {
27     NONE = 0,
28     START,
29     END,
30 };
31 class DotIndicatorModifier : public ContentModifier {
32     DECLARE_ACE_TYPE(DotIndicatorModifier, ContentModifier);
33 public:
DotIndicatorModifier()34     DotIndicatorModifier()
35         : backgroundColor_(AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor::TRANSPARENT)),
36           vectorBlackPointCenterX_(AceType::MakeRefPtr<AnimatablePropertyVectorFloat>(LinearVector<float>(0))),
37           longPointLeftCenterX_(AceType::MakeRefPtr<AnimatablePropertyFloat>(0)),
38           longPointRightCenterX_(AceType::MakeRefPtr<AnimatablePropertyFloat>(0)),
39           normalToHoverPointDilateRatio_(AceType::MakeRefPtr<AnimatablePropertyFloat>(1)),
40           hoverToNormalPointDilateRatio_(AceType::MakeRefPtr<AnimatablePropertyFloat>(1)),
41           longPointDilateRatio_(AceType::MakeRefPtr<AnimatablePropertyFloat>(1)),
42           indicatorPadding_(AceType::MakeRefPtr<AnimatablePropertyFloat>(0)),
43           indicatorMargin_(AceType::MakeRefPtr<AnimatablePropertyOffsetF>(OffsetF(0, 0))),
44           itemHalfSizes_(AceType::MakeRefPtr<AnimatablePropertyVectorFloat>(LinearVector<float>(4))),
45           backgroundWidthDilateRatio_(AceType::MakeRefPtr<AnimatablePropertyFloat>(1)),
46           backgroundHeightDilateRatio_(AceType::MakeRefPtr<AnimatablePropertyFloat>(1)),
47           unselectedColor_(AceType::MakeRefPtr<PropertyColor>(Color::TRANSPARENT)),
48           selectedColor_(AceType::MakeRefPtr<PropertyColor>(Color::TRANSPARENT))
49     {
50         AttachProperty(vectorBlackPointCenterX_);
51         AttachProperty(longPointLeftCenterX_);
52         AttachProperty(longPointRightCenterX_);
53         AttachProperty(normalToHoverPointDilateRatio_);
54         AttachProperty(hoverToNormalPointDilateRatio_);
55         AttachProperty(longPointDilateRatio_);
56         AttachProperty(backgroundColor_);
57         AttachProperty(indicatorPadding_);
58         AttachProperty(indicatorMargin_);
59         AttachProperty(itemHalfSizes_);
60         AttachProperty(unselectedColor_);
61         AttachProperty(selectedColor_);
62         AttachProperty(backgroundWidthDilateRatio_);
63         AttachProperty(backgroundHeightDilateRatio_);
64     }
65     ~DotIndicatorModifier() override = default;
66 
67     struct ContentProperty {
68         Color backgroundColor = Color::TRANSPARENT;
69         LinearVector<float> vectorBlackPointCenterX;
70         LinearVector<float> itemHalfSizes = {};
71         float longPointLeftCenterX = 0;
72         float longPointRightCenterX = 0;
73         float normalToHoverPointDilateRatio = 1;
74         float hoverToNormalPointDilateRatio = 1;
75         float longPointDilateRatio = 0;
76         float indicatorPadding = 0;
77         OffsetF indicatorMargin = { 0, 0 };
78     };
79 
80     void onDraw(DrawingContext& context) override;
81     // paint
82     void PaintContent(DrawingContext& context, ContentProperty& contentProperty);
83     void PaintUnselectedIndicator(
84         RSCanvas& canvas, const OffsetF& center, const LinearVector<float>& itemHalfSizes, bool currentIndexFlag);
85     void PaintSelectedIndicator(RSCanvas& canvas, const OffsetF& center, const OffsetF& leftCenter,
86         const OffsetF& rightCenter, const LinearVector<float>& itemHalfSizes);
87     void PaintMask(DrawingContext& context);
88     void PaintBackground(DrawingContext& context, const ContentProperty& contentProperty);
89     LinearVector<float> GetItemHalfSizes(size_t index, ContentProperty& contentProperty);
90     // Update property
91     void UpdateShrinkPaintProperty(const OffsetF& margin, const LinearVector<float>& normalItemHalfSizes,
92         const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX);
93     void UpdateDilatePaintProperty(const LinearVector<float>& hoverItemHalfSizes,
94         const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX);
95     void UpdateBackgroundColor(const Color& backgroundColor);
96 
97     void UpdateNormalPaintProperty(const OffsetF& margin, const LinearVector<float>& normalItemHalfSizes,
98         const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX);
99     void UpdateHoverPaintProperty(const LinearVector<float>& hoverItemHalfSizes,
100         const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX);
101     void UpdatePressPaintProperty(const LinearVector<float>& hoverItemHalfSizes,
102         const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX);
103     // Update
104     void UpdateNormalToHoverPaintProperty(const LinearVector<float>& hoverItemHalfSizes,
105         const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX);
106     void UpdateHoverToNormalPaintProperty(const OffsetF& margin, const LinearVector<float>& normalItemHalfSizes,
107         const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX);
108     void UpdateNormalToPressPaintProperty(const LinearVector<float>& hoverItemHalfSizes,
109         const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX);
110     void UpdatePressToNormalPaintProperty(const OffsetF& margin, const LinearVector<float>& normalItemHalfSizes,
111         const LinearVector<float>& vectorBlackPointCenterX, const std::pair<float, float>& longPointCenterX);
112     void UpdateHoverAndPressConversionPaintProperty();
113 
114     // Point dilate ratio
115     void UpdateNormalToHoverPointDilateRatio();
116     void UpdateHoverToNormalPointDilateRatio();
117     void UpdateLongPointDilateRatio();
118 
119     void UpdateAllPointCenterXAnimation(bool isForward, const LinearVector<float>& vectorBlackPointCenterX,
120         const std::pair<float, float>& longPointCenterX);
121 
122     void UpdateLongPointLeftCenterX(float longPointLeftCenterX, bool isAnimation);
123     void UpdateLongPointRightCenterX(float longPointRightCenterX, bool isAnimation);
124 
125     // Touch bottom animation
126     void UpdateTouchBottomAnimation(TouchBottomType touchBottomType, const LinearVector<float>& vectorBlackPointCenterX,
127         const std::pair<float, float>& longPointCenterX, float touchBottomRate);
128 
SetAxis(Axis axis)129     void SetAxis(Axis axis)
130     {
131         axis_ = axis;
132     }
133 
SetUnselectedColor(const Color & unselectedColor)134     void SetUnselectedColor(const Color& unselectedColor)
135     {
136         if (unselectedColor_) {
137             unselectedColor_->Set(unselectedColor);
138         }
139     }
140 
SetSelectedColor(const Color & selectedColor)141     void SetSelectedColor(const Color& selectedColor)
142     {
143         if (selectedColor_) {
144             selectedColor_->Set(selectedColor);
145         }
146     }
147 
SetCurrentIndex(int32_t index)148     void SetCurrentIndex(int32_t index)
149     {
150         currentIndex_ = index;
151     }
152 
SetNormalToHoverIndex(const std::optional<int32_t> & normalToHoverIndex)153     void SetNormalToHoverIndex(const std::optional<int32_t>& normalToHoverIndex)
154     {
155         normalToHoverIndex_ = normalToHoverIndex;
156     }
157 
SetHoverToNormalIndex(const std::optional<int32_t> & hoverToNormalIndex)158     void SetHoverToNormalIndex(const std::optional<int32_t>& hoverToNormalIndex)
159     {
160         hoverToNormalIndex_ = hoverToNormalIndex;
161     }
162 
GetNormalToHoverIndex()163     std::optional<int32_t> GetNormalToHoverIndex()
164     {
165         return normalToHoverIndex_;
166     }
167 
GetHoverToNormalIndex()168     std::optional<int32_t> GetHoverToNormalIndex()
169     {
170         return hoverToNormalIndex_;
171     }
172 
SetIndicatorMask(bool indicatorMask)173     void SetIndicatorMask(bool indicatorMask)
174     {
175         indicatorMask_ = indicatorMask;
176     }
177 
SetOffset(const OffsetF & offset)178     void SetOffset(const OffsetF& offset)
179     {
180         offset_ = offset;
181     }
182 
SetCenterY(const float & centerY)183     void SetCenterY(const float& centerY)
184     {
185         centerY_ = centerY;
186     }
187 
SetIsHover(bool isHover)188     void SetIsHover(bool isHover)
189     {
190         isHover_ = isHover;
191     }
192 
GetIsHover()193     bool GetIsHover() const
194     {
195         return isHover_;
196     }
197 
SetIsPressed(bool isPressed)198     void SetIsPressed(bool isPressed)
199     {
200         isPressed_ = isPressed;
201     }
202 
GetIsPressed()203     bool GetIsPressed() const
204     {
205         return isPressed_;
206     }
207 
SetLongPointIsHover(bool isHover)208     void SetLongPointIsHover(bool isHover)
209     {
210         longPointIsHover_ = isHover;
211     }
212 
GetLongPointIsHover()213     bool GetLongPointIsHover() const
214     {
215         return longPointIsHover_;
216     }
217 
SetItemWidth(const float itemWidth)218     void SetItemWidth(const float itemWidth)
219     {
220         itemWidth_ = itemWidth;
221     }
222 
SetItemHeight(const float itemHeight)223     void SetItemHeight(const float itemHeight)
224     {
225         itemHeight_ = itemHeight;
226     }
227 
SetSelectedItemWidth(const float selectedItemWidth)228     void SetSelectedItemWidth(const float selectedItemWidth)
229     {
230         selectedItemWidth_ = selectedItemWidth;
231     }
232 
SetSelectedItemHeight(const float selectedItemHeight)233     void SetSelectedItemHeight(const float selectedItemHeight)
234     {
235         selectedItemHeight_ = selectedItemHeight;
236     }
237 
SetIsIndicatorCustomSize(bool isCustomSize)238     void SetIsIndicatorCustomSize(bool isCustomSize)
239     {
240         isCustomSize_ = isCustomSize;
241     }
242 
243 private:
GetSwiperIndicatorTheme()244     static RefPtr<OHOS::Ace::SwiperIndicatorTheme> GetSwiperIndicatorTheme()
245     {
246         auto pipelineContext = PipelineBase::GetCurrentContext();
247         CHECK_NULL_RETURN(pipelineContext, nullptr);
248         auto swiperTheme = pipelineContext->GetTheme<SwiperIndicatorTheme>();
249         CHECK_NULL_RETURN(swiperTheme, nullptr);
250         return swiperTheme;
251     }
252 
253     RefPtr<AnimatablePropertyColor> backgroundColor_;
254     RefPtr<AnimatablePropertyVectorFloat> vectorBlackPointCenterX_;
255     RefPtr<AnimatablePropertyFloat> longPointLeftCenterX_;
256     RefPtr<AnimatablePropertyFloat> longPointRightCenterX_;
257     RefPtr<AnimatablePropertyFloat> normalToHoverPointDilateRatio_;
258     RefPtr<AnimatablePropertyFloat> hoverToNormalPointDilateRatio_;
259     RefPtr<AnimatablePropertyFloat> longPointDilateRatio_;
260     RefPtr<AnimatablePropertyFloat> indicatorPadding_;
261     RefPtr<AnimatablePropertyOffsetF> indicatorMargin_;
262     RefPtr<AnimatablePropertyVectorFloat> itemHalfSizes_;
263     RefPtr<AnimatablePropertyFloat> backgroundWidthDilateRatio_;
264     RefPtr<AnimatablePropertyFloat> backgroundHeightDilateRatio_;
265 
266     float centerY_ = 0;
267     Axis axis_ = Axis::HORIZONTAL;
268     RefPtr<PropertyColor> unselectedColor_;
269     RefPtr<PropertyColor> selectedColor_;
270     std::optional<int32_t> normalToHoverIndex_ = std::nullopt;
271     std::optional<int32_t> hoverToNormalIndex_ = std::nullopt;
272     bool longPointIsHover_ = false;
273     bool isHover_ = false;
274     bool isPressed_ = false;
275     bool longPointLeftAnimEnd_ = true;
276     bool longPointRightAnimEnd_ = true;
277 
278     bool indicatorMask_ = false;
279     bool isCustomSize_ = false;
280     int32_t currentIndex_ = 0;
281     OffsetF offset_;
282     float itemWidth_ = 0.0f;
283     float itemHeight_ = 0.0f;
284     float selectedItemWidth_ = 0.0f;
285     float selectedItemHeight_ = 0.0f;
286     TouchBottomType touchBottomType_ = TouchBottomType::NONE;
287     ACE_DISALLOW_COPY_AND_MOVE(DotIndicatorModifier);
288 };
289 } // namespace OHOS::Ace::NG
290 
291 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_INDICATOR_SWIPER_INDICATOR_MODIFIER_H
292