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) : 72 context_(context) {} 73 ~SideBarAnimationController() override = default; 74 SetAnimationStopCallback(StopAnimationCallback && callback)75 void SetAnimationStopCallback(StopAnimationCallback&& callback) 76 { 77 stopCallback_ = std::move(callback); 78 } 79 SetRenderSideBarContainer(const WeakPtr<RenderNode> & node)80 void SetRenderSideBarContainer(const WeakPtr<RenderNode>& node) 81 { 82 sidebar_ = node; 83 } 84 GetRenderSideBarContainer()85 const WeakPtr<RenderNode>& GetRenderSideBarContainer() const 86 { 87 return sidebar_; 88 } 89 SetSideBarPositon(const SideBarPosition & sidebarposition)90 void SetSideBarPositon(const SideBarPosition& sidebarposition) 91 { 92 sidebarpositon_ = sidebarposition; 93 } 94 95 void StopAnimation(); 96 97 void PlaySideBarContainerToAnimation(SideStatus status); 98 99 private: 100 void CreateAnimation(); 101 void PlayRightToLeftAnimation(); 102 void StopRightToLeftAnimation(); 103 void PlayLeftToRightAnimation(); 104 void StopLeftToRightAnimation(); 105 106 RefPtr<SideBarAnimation> leftToRightAnimation_; 107 RefPtr<SideBarAnimation> rightToLeftAnimation_; 108 109 WeakPtr<PipelineContext> context_; 110 StopAnimationCallback stopCallback_; 111 bool isAnimationCreated_ = false; 112 WeakPtr<RenderNode> sidebar_; 113 SideBarPosition sidebarpositon_ = SideBarPosition::START; 114 }; 115 116 } 117 118 #endif