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_RENDER_SIDE_BAR_ANIMATION_CONTROLLER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RENDER_SIDE_BAR_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/components/side_bar/side_bar_container_component.h"
23 #include "core/pipeline/base/render_node.h"
24 #include "core/pipeline/pipeline_context.h"
25 
26 namespace OHOS::Ace {
27 using AnimationCallback = std::function<void(double)>;
28 using StopAnimationCallback = std::function<void()>;
29 
30 class SideBarAnimation : public AceType {
31     DECLARE_ACE_TYPE(SideBarAnimation, AceType);
32 
33 public:
34     SideBarAnimation(const WeakPtr<PipelineContext>& context, double start, double end, int delay,
35         int duration, const RefPtr<Curve>& curve, AnimationCallback&& callback);
36     ~SideBarAnimation() override = default;
37 
38     void Play();
39     void Stop();
40 
AddStopListenerCallback(const StopAnimationCallback & callBack)41     void AddStopListenerCallback(const StopAnimationCallback& callBack)
42     {
43         if (controller_) {
44             controller_->AddStopListener(callBack);
45         }
46     }
47 
ClearStopListenerCallback()48     void ClearStopListenerCallback()
49     {
50         if (controller_) {
51             controller_->ClearStopListeners();
52         }
53     }
54 
55 private:
56     double start_ = 0.0;
57     double end_ = 0.0;
58     int delay_ = 0;
59     int duration_ = 0;
60     WeakPtr<PipelineContext> context_;
61     RefPtr<Curve> curve_;
62     AnimationCallback callback_;
63     RefPtr<CurveAnimation<double>> animation_;
64     RefPtr<Animator> controller_;
65 };
66 
67 class SideBarAnimationController : public AceType {
68     DECLARE_ACE_TYPE(SideBarAnimationController, AceType);
69 
70 public:
SideBarAnimationController(const WeakPtr<PipelineContext> & context)71     explicit SideBarAnimationController(const WeakPtr<PipelineContext>& context) : context_(context) {}
72     ~SideBarAnimationController() override = default;
73 
SetAnimationStopCallback(StopAnimationCallback && callback)74     void SetAnimationStopCallback(StopAnimationCallback&& callback)
75     {
76         stopCallback_ = std::move(callback);
77     }
78 
SetRenderSideBarContainer(const WeakPtr<RenderNode> & node)79     void SetRenderSideBarContainer(const WeakPtr<RenderNode>& node)
80     {
81         sidebar_ = node;
82     }
83 
GetRenderSideBarContainer()84     const WeakPtr<RenderNode>& GetRenderSideBarContainer() const
85     {
86         return sidebar_;
87     }
88 
SetSideBarPositon(const SideBarPosition & sidebarposition)89     void SetSideBarPositon(const SideBarPosition& sidebarposition)
90     {
91         sidebarpositon_ = sidebarposition;
92     }
93 
94     void StopAnimation();
95 
96     void PlaySideBarContainerToAnimation(SideStatus status);
97 
98 private:
99     void CreateAnimation();
100     void PlayRightToLeftAnimation();
101     void StopRightToLeftAnimation();
102     void PlayLeftToRightAnimation();
103     void StopLeftToRightAnimation();
104 
105     RefPtr<SideBarAnimation> leftToRightAnimation_;
106     RefPtr<SideBarAnimation> rightToLeftAnimation_;
107 
108     WeakPtr<PipelineContext> context_;
109     StopAnimationCallback stopCallback_;
110     bool isAnimationCreated_ = false;
111     WeakPtr<RenderNode> sidebar_;
112     SideBarPosition sidebarpositon_ = SideBarPosition::START;
113 };
114 
115 }
116 
117 #endif