1 /* 2 * Copyright (c) 2021 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_PAGE_TRANSITION_PAGE_TRANSITION_INFO_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PAGE_TRANSITION_PAGE_TRANSITION_INFO_H 18 19 #include <memory> 20 #include <optional> 21 22 #include "base/memory/ace_type.h" 23 #include "core/animation/curves.h" 24 25 namespace OHOS::Ace { 26 27 enum class PageTransitionType { 28 ENTER, // current page enter not specify route type. 29 EXIT, // current page exit not specify route type. 30 ENTER_POP, // top page pop from route stack, current page on top. 31 ENTER_PUSH, // current page push into route stack. 32 EXIT_POP, // current page pop from route stack. 33 EXIT_PUSH, // another page push info route stack. 34 }; 35 36 enum class RouteType { 37 NONE, 38 PUSH, 39 POP, 40 }; 41 42 enum class SlideEffect { 43 NONE, 44 LEFT, 45 RIGHT, 46 TOP, 47 BOTTOM, 48 }; 49 50 using PageTransitionEventFunc = std::function<void(RouteType, const float&)>; 51 52 class ACE_EXPORT PageTransition : public AceType { 53 DECLARE_ACE_TYPE(PageTransition, AceType); 54 55 public: ProcessPageTransitionType(const RefPtr<PageTransition> & pageTransition)56 static void ProcessPageTransitionType(const RefPtr<PageTransition>& pageTransition) 57 { 58 if (pageTransition->type_ == PageTransitionType::ENTER) { 59 switch (pageTransition->routeType_) { 60 case RouteType::POP: 61 pageTransition->type_ = PageTransitionType::ENTER_POP; 62 break; 63 case RouteType::PUSH: 64 pageTransition->type_ = PageTransitionType::ENTER_PUSH; 65 break; 66 case RouteType::NONE: 67 default: 68 break; 69 } 70 } else if (pageTransition->type_ == PageTransitionType::EXIT) { 71 switch (pageTransition->routeType_) { 72 case RouteType::POP: 73 pageTransition->type_ = PageTransitionType::EXIT_POP; 74 break; 75 case RouteType::PUSH: 76 pageTransition->type_ = PageTransitionType::EXIT_PUSH; 77 break; 78 case RouteType::NONE: 79 default: 80 break; 81 } 82 } 83 } 84 type_(type)85 PageTransition(PageTransitionType type = PageTransitionType::ENTER) : type_(type) {} 86 virtual ~PageTransition() = default; 87 SetRouteType(RouteType routeType)88 void SetRouteType(RouteType routeType) 89 { 90 routeType_ = routeType; 91 } 92 SetDuration(int32_t duration)93 void SetDuration(int32_t duration) 94 { 95 tweenOption_.SetDuration(duration); 96 } 97 SetDelay(int32_t delay)98 void SetDelay(int32_t delay) 99 { 100 tweenOption_.SetDelay(delay); 101 } 102 SetCurve(const RefPtr<Curve> & curve)103 void SetCurve(const RefPtr<Curve>& curve) 104 { 105 tweenOption_.SetCurve(curve); 106 } 107 SetEffect(SlideEffect effect)108 void SetEffect(SlideEffect effect) 109 { 110 effect_ = effect; 111 } 112 GetType()113 PageTransitionType GetType() const 114 { 115 return type_; 116 } 117 GetDuration()118 int32_t GetDuration() const 119 { 120 return tweenOption_.GetDuration(); 121 } 122 GetDelay()123 int32_t GetDelay() const 124 { 125 return tweenOption_.GetDelay(); 126 } 127 GetCurve()128 const RefPtr<Curve>& GetCurve() const 129 { 130 return tweenOption_.GetCurve(); 131 } 132 GetTweenOption()133 const TweenOption& GetTweenOption() const 134 { 135 return tweenOption_; 136 } 137 GetRouteType()138 RouteType GetRouteType() const 139 { 140 return routeType_; 141 } 142 GetSlideEffect()143 SlideEffect GetSlideEffect() const 144 { 145 return effect_; 146 } 147 GetOnEnterHandler()148 const PageTransitionEventFunc& GetOnEnterHandler() const 149 { 150 return OnEnterHandler_; 151 } 152 GetOnExitHandler()153 const PageTransitionEventFunc& GetOnExitHandler() const 154 { 155 return OnExitHandler_; 156 } 157 AddTranslateAnimation(const Dimension & dx,const Dimension & dy,const Dimension & dz)158 void AddTranslateAnimation(const Dimension& dx, const Dimension& dy, const Dimension& dz) 159 { 160 TransformOperation init; 161 init.type_ = TransformOperationType::TRANSLATE; 162 init.translateOperation_ = TranslateOperation(Dimension {}, Dimension {}, Dimension {}); 163 TransformOperation target; 164 target.type_ = TransformOperationType::TRANSLATE; 165 target.translateOperation_ = TranslateOperation(dx, dy, dz); 166 167 if (type_ == PageTransitionType::ENTER) { 168 std::swap(init, target); 169 } 170 auto animation = AceType::MakeRefPtr<CurveAnimation<TransformOperation>>(init, target, tweenOption_.GetCurve()); 171 tweenOption_.AddTransformAnimation(animation); 172 } 173 AddScaleAnimation(float scaleX,float scaleY,float scaleZ,const Dimension & centerX,const Dimension & centerY)174 void AddScaleAnimation(float scaleX, float scaleY, float scaleZ, const Dimension& centerX, const Dimension& centerY) 175 { 176 TransformOperation init; 177 init.type_ = TransformOperationType::SCALE; 178 init.scaleOperation_ = ScaleOperation(1.0, 1.0, 1.0); 179 180 TransformOperation target; 181 target.type_ = TransformOperationType::SCALE; 182 target.scaleOperation_ = ScaleOperation(scaleX, scaleY, scaleZ); 183 184 if (type_ == PageTransitionType::ENTER) { 185 std::swap(init, target); 186 } 187 auto animation = AceType::MakeRefPtr<CurveAnimation<TransformOperation>>(init, target, tweenOption_.GetCurve()); 188 189 tweenOption_.SetTransformOrigin(centerX, centerY); 190 tweenOption_.AddTransformAnimation(animation); 191 } 192 AddOpacityAnimation(float targetValue)193 void AddOpacityAnimation(float targetValue) 194 { 195 float initValue = 1.0f; 196 if (type_ == PageTransitionType::ENTER) { 197 std::swap(initValue, targetValue); 198 } 199 auto animation = AceType::MakeRefPtr<CurveAnimation<float>>(initValue, targetValue, tweenOption_.GetCurve()); 200 tweenOption_.SetOpacityAnimation(animation); 201 } 202 SetOnEnterHandler(PageTransitionEventFunc && handler)203 void SetOnEnterHandler(PageTransitionEventFunc&& handler) 204 { 205 OnEnterHandler_ = std::move(handler); 206 } 207 SetOnExitHandler(PageTransitionEventFunc && handler)208 void SetOnExitHandler(PageTransitionEventFunc&& handler) 209 { 210 OnExitHandler_ = std::move(handler); 211 } 212 213 private: 214 RouteType routeType_ = RouteType::NONE; 215 PageTransitionType type_ = PageTransitionType::ENTER; 216 SlideEffect effect_ = SlideEffect::NONE; 217 TweenOption tweenOption_; 218 PageTransitionEventFunc OnEnterHandler_; 219 PageTransitionEventFunc OnExitHandler_; 220 }; 221 222 } // namespace OHOS::Ace 223 224 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PAGE_TRANSITION_PAGE_TRANSITION_INFO_H 225