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