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_STEPPER_STEPPER_ANIMATION_CONTROLLER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_STEPPER_STEPPER_ANIMATION_CONTROLLER_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/animation/animator.h"
21 #include "core/animation/curve_animation.h"
22 #include "core/pipeline/base/render_node.h"
23 #include "core/pipeline/pipeline_context.h"
24 namespace OHOS::Ace {
25 
26 using AnimationStopCallback = std::function<void()>;
27 using AnimationCallback = std::function<void(double)>;
28 
29 class StepperAnimation : public AceType {
30     DECLARE_ACE_TYPE(StepperAnimation, AceType);
31 
32 public:
33     StepperAnimation(const WeakPtr<PipelineContext>& context, double start, double end, int delay,
34         int duration, const RefPtr<Curve>& curve, const AnimationCallback& callback);
35     ~StepperAnimation() override = default;
36     void Play();
37     void Stop();
38 
AddStopListenerCallback(const AnimationStopCallback & callBack)39     void AddStopListenerCallback(const AnimationStopCallback& callBack)
40     {
41         if (controller_) {
42             controller_->AddStopListener(callBack);
43         }
44     }
45 
ClearStopListenerCallback()46     void ClearStopListenerCallback()
47     {
48         if (controller_) {
49             controller_->ClearStopListeners();
50         }
51     }
52 
53 private:
54     double start_ = 0.0;
55     double end_ = 0.0;
56     int delay_ = 0;
57     int duration_ = 0;
58     WeakPtr<PipelineContext> context_;
59     RefPtr<Curve> curve_;
60     AnimationCallback callback_;
61     RefPtr<CurveAnimation<double>> animation_;
62     RefPtr<Animator> controller_;
63 };
64 
65 class StepperAnimationController : public AceType {
66     DECLARE_ACE_TYPE(StepperAnimationController, AceType);
67 
68 public:
StepperAnimationController(const WeakPtr<PipelineContext> & context)69     explicit StepperAnimationController(const WeakPtr<PipelineContext>& context) : context_(context) {}
70     ~StepperAnimationController() override = default;
71 
SetAnimationStopCallback(const AnimationStopCallback & callback)72     void SetAnimationStopCallback(const AnimationStopCallback& callback)
73     {
74         animationStopCallback_ = callback;
75     }
76 
SetRenderStepper(const WeakPtr<RenderNode> & node)77     void SetRenderStepper(const WeakPtr<RenderNode>& node)
78     {
79         stepper_ = node;
80     }
81 
GetRenderStepper()82     const WeakPtr<RenderNode>& GetRenderStepper() const
83     {
84         return stepper_;
85     }
86 
87     void PlayStepperToAnimation(int32_t fromIndex, int32_t toIndex, bool reverse);
88 
89 private:
90     void CreateAnimation();
91     void CreateOpacityAnimation();
92     void CreateTranslateAnimation();
93     void PlayNextAnimation();
94     void StopNextAnimation();
95     void PlayBackAnimation();
96     void StopBackAnimation();
97 
98     RefPtr<StepperAnimation> fromIndexOpacityAnimation_;
99     RefPtr<StepperAnimation> toIndexOpacityAnimation_;
100     RefPtr<StepperAnimation> fromIndexTranslateAnimationNext_;
101     RefPtr<StepperAnimation> toIndexTranslateAnimationNext_;
102     RefPtr<StepperAnimation> fromIndexTranslateAnimationBack_;
103     RefPtr<StepperAnimation> toIndexTranslateAnimationBack_;
104     WeakPtr<PipelineContext> context_;
105     WeakPtr<RenderNode> stepper_;
106     bool isAnimationCreated_ = false;
107     AnimationStopCallback animationStopCallback_;
108 };
109 
110 } // namespace OHOS::Ace
111 
112 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_STEPPER_STEPPER_ANIMATION_CONTROLLER_H
113