• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BASE_GEOMETRY_ANIMATABLE_DIMENSION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_ANIMATABLE_DIMENSION_H
18 
19 #include "base/geometry/calc_dimension.h"
20 #include "base/geometry/dimension.h"
21 #include "base/memory/referenced.h"
22 #include "core/animation/evaluator.h"
23 #include "core/components/common/properties/animation_option.h"
24 
25 namespace OHOS::Ace {
26 
27 using RenderNodeAnimationCallback = std::function<void()>;
28 
29 class Animator;
30 
31 enum class AnimatorStatus {
32     IDLE,    // when animation not start or been cancel.
33     RUNNING, // play in reverse / forward direction.
34     PAUSED,  // paused by call Pause API.
35     STOPPED, // stopped by call Finish/Stop API or has played to the end.
36 };
37 
38 /*
39  * AnimatableDimension is a Dimension with AnimationOption and Animator.
40  */
41 class ACE_FORCE_EXPORT AnimatableDimension : public CalcDimension {
42 public:
43     AnimatableDimension();
44     ~AnimatableDimension();
45 
46     explicit AnimatableDimension(
47         double value, DimensionUnit unit = DimensionUnit::PX, const AnimationOption& option = AnimationOption());
48 
49     explicit AnimatableDimension(const std::string& value, DimensionUnit unit = DimensionUnit::CALC,
50         const AnimationOption& option = AnimationOption());
51 
52     explicit AnimatableDimension(const Dimension& dimension, const AnimationOption& option = AnimationOption());
53 
54     explicit AnimatableDimension(const CalcDimension& dimension, const AnimationOption& option = AnimationOption());
55 
56     AnimatableDimension(const AnimatableDimension& other);
SetContextAndCallback(const WeakPtr<PipelineBase> & context,const RenderNodeAnimationCallback & callback)57     void SetContextAndCallback(const WeakPtr<PipelineBase>& context, const RenderNodeAnimationCallback& callback)
58     {
59         context_ = context;
60         animationCallback_ = callback;
61     }
62 
SetContextAndCallbackAfterFirstAssign(const WeakPtr<PipelineBase> & context,const RenderNodeAnimationCallback & callback)63     void SetContextAndCallbackAfterFirstAssign(
64         const WeakPtr<PipelineBase>& context, const RenderNodeAnimationCallback& callback)
65     {
66         context_ = context;
67         animationCallback_ = callback;
68         isFirstAssign_ = false;
69     }
70 
GetAnimationOption()71     const AnimationOption& GetAnimationOption() const
72     {
73         return animationOption_;
74     }
75 
SetAnimationOption(const AnimationOption & option)76     void SetAnimationOption(const AnimationOption& option)
77     {
78         animationOption_ = option;
79     }
80 
SetAnimationStopCallback(const RenderNodeAnimationCallback & callback)81     void SetAnimationStopCallback(const RenderNodeAnimationCallback& callback)
82     {
83         stopCallback_ = callback;
84     }
85 
86     AnimatorStatus GetAnimationStatus() const;
87 
SetEvaluator(const RefPtr<Evaluator<double>> & evaluator)88     void SetEvaluator(const RefPtr<Evaluator<double>>& evaluator)
89     {
90         evaluator_ = evaluator;
91     }
92 
93     AnimatableDimension& operator=(const Dimension& newDimension);
94 
95     AnimatableDimension& operator=(const CalcDimension& newDimension);
96 
97     AnimatableDimension& operator=(const AnimatableDimension& newDimension);
98 
99     void MoveTo(double target);
100 
101 private:
102     void AnimateTo(double endValue);
103     void ResetController();
104     void OnAnimationCallback(double value);
105     void ResetAnimatableDimension();
106 
107 private:
108     bool isFirstAssign_ = true;
109     AnimationOption animationOption_;
110     RefPtr<Animator> animationController_;
111     WeakPtr<PipelineBase> context_;
112     RenderNodeAnimationCallback animationCallback_;
113     RenderNodeAnimationCallback stopCallback_;
114     RefPtr<Evaluator<double>> evaluator_;
115 };
116 
117 } // namespace OHOS::Ace
118 
119 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_ANIMATABLE_DIMENSION_H
120