1 /* 2 * Copyright (c) 2024 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_ARC_SWIPER_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_ARC_SWIPER_PATTERN_H 18 19 #include "core/components_ng/pattern/swiper/swiper_pattern.h" 20 21 namespace OHOS::Ace::NG { 22 constexpr float TO_LEFT_ARC_SWIPER_PROPORTION = 5.0f / 2.0f; //The critical point of arc_swiper is 2/5 screen 23 constexpr float TO_RIGHT_ARC_SWIPER_PROPORTION = 5.0f / 3.0f; 24 constexpr int32_t MIN_PAGE_VELOCITY = 520; 25 class ArcSwiperPattern : public SwiperPattern { 26 DECLARE_ACE_TYPE(ArcSwiperPattern, SwiperPattern); 27 public: ArcSwiperPattern()28 ArcSwiperPattern() 29 { 30 swiperProportion_ = TO_LEFT_ARC_SWIPER_PROPORTION; 31 newMinTurnPageVelocity_ = MIN_PAGE_VELOCITY; 32 } 33 ~ArcSwiperPattern()34 ~ArcSwiperPattern() {} 35 void SaveCircleDotIndicatorProperty(const RefPtr<FrameNode>& indicatorNode) override; 36 void SetSwiperArcDotParameters(const SwiperArcDotParameters& swiperArcDotParameters) override; 37 std::string GetArcDotIndicatorStyle() const override; 38 std::shared_ptr<SwiperArcDotParameters> GetSwiperArcDotParameters() const override; 39 bool IsLoop() const override; 40 void SetDisableTransitionAnimation(bool isDisable) override; 41 bool GetAndResetDisableFlushFocus() override; 42 43 #ifdef SUPPORT_DIGITAL_CROWN 44 void SetDigitalCrownSensitivity(CrownSensitivity sensitivity) override; 45 void InitOnCrownEventInternal(const RefPtr<FocusHub>& focusHub) override; 46 bool IsCrownSpring() const override; 47 void SetIsCrownSpring(bool isCrownSpring) override; 48 void HandleCrownEvent(const CrownEvent& event, const OffsetF& center, const OffsetF& offset); 49 #endif 50 private: 51 std::string GradientToJson(Gradient colors) const; 52 mutable std::shared_ptr<SwiperArcDotParameters> swiperArcDotParameters_; 53 enum AnimationType { 54 TYPE_SCALE, 55 TYPE_OFFSET, 56 TYPE_BLUR, 57 TYPE_COLOR, 58 TYPE_ALPHA 59 }; 60 61 struct AnimationParam { 62 AnimationOption option; 63 RefPtr<FrameNode> frameNode; 64 AnimationType type; 65 float scaleValue; 66 float opacityValue; 67 float blurValue; 68 Color backColor; 69 OffsetF offset; 70 }; 71 72 enum AnimationFinishType { 73 EXIT_SCALE, 74 EXIT_OFFSET, 75 EXIT_BLUR, 76 EXIT_ALPHA, 77 EXIT_BACKGROUND, 78 79 ENTRY_SCALE, 80 ENTRY_OFFSET, 81 ENTRY_BLUR, 82 ENTRY_ALPHA, 83 ENTRY_BACKGROUND 84 }; 85 86 void ResetAnimationParam() override; 87 void InitialFrameNodePropertyAnimation(const OffsetF& offset, const RefPtr<FrameNode>& frameNode); 88 void UsePropertyAnimation(const OffsetF& offset); 89 void PlayPropertyTranslateAnimation(float translate, int32_t nextIndex, float velocity = 0.0f, 90 bool stopAutoPlay = false, std::optional<float> pixelRoundTargetPos = std::nullopt) override; 91 void PlayScrollAnimation(float currentDelta, float currentIndexOffset) override; 92 void ResetCurrentFrameNodeAnimation() override; 93 void ResetParentNodeColor() override; 94 RefPtr<Curve> GetCurve() const override; 95 int32_t GetDuration() const override; 96 97 int32_t CalcTime(int32_t time); 98 void ResetFrameNodeAnimation(const RefPtr<FrameNode>& frameNode, bool resetBackgroundcolor); 99 void ClearAnimationFinishList(); 100 void HorizontalScrollAnimation(float offset); 101 void PlayHorizontalScrollExitAnimation(float swiperWidth, float startPos, const RefPtr<FrameNode>& frameNode); 102 void PlayHorizontalScrollEntryAnimation(float swiperWidth, float startPos, const RefPtr<FrameNode>& frameNode); 103 std::shared_ptr<Color> GetEntryNodeBackgroundColor(const RefPtr<FrameNode>& frameNode); 104 105 void VerticalScrollAnimation(float offset); 106 void PlayVerticalScrollExitAnimation(float swiperWidth, float startPos, const RefPtr<FrameNode>& frameNode); 107 void PlayVerticalScrollEntryAnimation(float swiperWidth, float startPosj, const RefPtr<FrameNode>& frameNode); 108 109 void PlayScrollBackgroundAnimation(const std::shared_ptr<Color>& color, const std::shared_ptr<Color>& parentColor, 110 const RefPtr<RenderContext>& parentRenderContext, bool exitNodeNeedTransparent); 111 void PlayPropertyTranslateFlipAnimation(const OffsetF& offset); 112 113 void PlayHorizontalEntryAnimation(const OffsetF& offset, const RefPtr<FrameNode>& frameNode, bool rollback); 114 void PlayHorizontalExitAnimation(const OffsetF& offset, const RefPtr<FrameNode>& frameNode, bool rollBack); 115 116 void PlayVerticalExitAnimation(const OffsetF& offset, const RefPtr<FrameNode>& frameNode, bool rollBack); 117 void PlayVerticalEntryAnimation(const OffsetF& offset, const RefPtr<FrameNode>& frameNode, bool rollBack); 118 119 void PlayAnimation(const OffsetF& offset, int32_t index, const RefPtr<FrameNode>& frameNode); 120 void PlayHorizontalAnimation(const OffsetF& offset, int32_t index, const RefPtr<FrameNode>& frameNode, 121 bool rollBack); 122 void PlayVerticalAnimation(const OffsetF& offset, int32_t index, const RefPtr<FrameNode>& frameNode, bool rollBack); 123 124 void AnimationFinish(); 125 void BuildAnimationFinishCallback(bool exit, AnimationParam& param, const RefPtr<RenderContext>& renderContext, 126 FinishCallback& finishCallback); 127 void HandlePropertyTranslateCallback(float translate, int32_t nextIndex, float velocity); 128 void AddFinishAnimation(const AnimationFinishType& animationFinishType, bool vertical = false, 129 bool rollBack = false); 130 void BuildDefaultTranslateAnimationOption(AnimationOption& option, float translate); 131 void PlayPropertyTranslateDefaultAnimation(const OffsetF& offset, float translate); 132 bool IsPreItem(int32_t index, float translate, bool rollback = false); 133 bool IsScrollOverCritical(); 134 bool EnableTransitionAnimation() const; 135 std::shared_ptr<Color> GetBackgroundColorValue(const RefPtr<FrameNode>& frameNode); 136 std::shared_ptr<AnimationUtils::Animation> Animation(bool exit, AnimationParam& param, bool vertical = false, 137 bool rollback = false); 138 bool IsDisableTransitionAnimation() const; 139 void SetBackgroundColor(const RefPtr<RenderContext>& renderContext, const std::shared_ptr<Color>& color); 140 std::shared_ptr<Color> CalcBackgroundByPercent(const std::shared_ptr<Color>& colorA, 141 const std::shared_ptr<Color>& colorB, float percent); 142 143 Color GetVerticalEntryBackgroundValue(bool rollBack); 144 void PlayVerticalExitOffsetAnimation(const OffsetF& offset, const RefPtr<FrameNode>& frameNode, bool rollBack); 145 void PlayVerticalEntryOffsetAnimation(const OffsetF& offset, const RefPtr<FrameNode>& frameNode, bool rollBack); 146 void PlayVerticalScrollEntryBackgroundAnimation(float percent, const RefPtr<FrameNode>& frameNode); 147 void PlayVerticalEntryBlurAnimation(const RefPtr<FrameNode>& frameNode, bool rollBack); 148 void ResetBackgroundColor(const RefPtr<FrameNode>& frameNode); 149 std::shared_ptr<Color> preNodeBackgroundColor_; 150 std::shared_ptr<Color> entryNodeBackgroundColor_; 151 std::shared_ptr<Color> parentNodeBackgroundColor_; 152 OffsetF offset_; 153 std::vector<std::shared_ptr<AnimationUtils::Animation>> animationVector_; 154 std::vector<AnimationFinishType> animationFinishList_; 155 WeakPtr<FrameNode> exitFrameNode_; 156 float horizontalExitNodeScale_ = 0.0f; 157 float horizontalExitNodeBlur_ = 0.0f; 158 float horizontalExitNodeOpacity_ = 0.0f; 159 float horizontalEntryNodeScale_ = 0.0f; 160 float horizontalEntryNodeBlur_ = 0.0f; 161 float horizontalEntryNodeOpacity_ = 0.0f; 162 163 float verticalExitNodeScale_ = 0.0f; 164 float verticalExitNodeBlur_ = 0.0f; 165 float verticalExitNodeOpacity_ = 0.0f; 166 float verticalEntryNodeScale_ = 0.0f; 167 float verticalEntryNodeOpacity_ = 0.0f; 168 float verticalEntryNodeBlur_ = 0.0f; 169 Axis axis_ = Axis::HORIZONTAL; 170 bool disableTransitionAnimation_ = false; 171 #ifdef SUPPORT_DIGITAL_CROWN 172 void HandleCrownActionBegin(double degree, double mainDelta, GestureEvent& info, const OffsetF& offset); 173 void HandleCrownActionUpdate(double degree, double mainDelta, GestureEvent& info, const OffsetF& offset); 174 void HandleCrownActionEnd(double degree, double mainDelta, GestureEvent& info, const OffsetF& offset); 175 void HandleCrownActionCancel(); 176 double GetCrownRotatePx(const CrownEvent& event) const; 177 void UpdateCrownVelocity(double degree, double mainDelta, bool isEnd = false); 178 void StartVibrator(bool isLeft); 179 #endif 180 181 #ifdef SUPPORT_DIGITAL_CROWN 182 CrownSensitivity crownSensitivity_ = CrownSensitivity::MEDIUM; 183 Offset accumulativeCrownPx_; 184 bool isCrownSpring_ = false; 185 double crownVelocity_ = 0.0; 186 double crownTurnVelocity_ = 0.0; 187 bool isHandleCrownActionEnd_ = false; 188 int32_t oldCurrentIndex_ = -1; 189 bool isChanged_ = false; 190 #endif 191 bool isDisableFlushFocus_ = false; 192 bool canChangeDirectionFlag_ = false; 193 bool scrollToLeft_ = false; 194 bool scrollToTop_ = false; 195 }; 196 } // namespace OHOS::Ace::NG 197 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_ARC_SWIPER_PATTERN_H