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_ANIMATION_PROPERTY_ANIMATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_PROPERTY_ANIMATION_H
18 
19 #include <functional>
20 #include <list>
21 
22 #include "core/animation/curve.h"
23 #include "core/animation/animatable.h"
24 #include "core/animation/interpolator.h"
25 
26 namespace OHOS::Ace {
27 
28 class PropertyAnimation : public Interpolator {
29     DECLARE_ACE_TYPE(PropertyAnimation, Interpolator);
30 
31 public:
32     using PropCallback = std::function<void(const RefPtr<Animatable>&)>;
33     explicit PropertyAnimation(AnimatableType type);
34     PropertyAnimation() = delete;
35     ~PropertyAnimation() override = default;
36     void SetStart(const RefPtr<Animatable>& animatable);
37     void AddAnimatable(const RefPtr<Animatable>& animatable);
GetAnimatable()38     const std::list<RefPtr<Animatable>>& GetAnimatable() const
39     {
40         return animatables_;
41     }
42     void SetCurve(const RefPtr<Curve>& curve);
AddListener(const PropCallback & callback)43     void AddListener(const PropCallback& callback)
44     {
45         animateTo_ = callback;
46     }
GetInit()47     const RefPtr<Animatable>& GetInit() const
48     {
49         return init_;
50     }
SetInit(const RefPtr<Animatable> & init)51     void SetInit(const RefPtr<Animatable>& init)
52     {
53         init_ = init;
54     }
55 
56 private:
57     void OnNormalizedTimestampChanged(float normalized, bool reverse) override;
58     void OnInitNotify(float normalizedTime, bool reverse) override;
59     RefPtr<Curve> GetCurve() override;
60     void Calculate(float keyTime);
61     void TriggerFrame(const RefPtr<Animatable>& start, const RefPtr<Animatable>& end, float time);
62 
63     template<class T>
64     void Next(const RefPtr<Animatable>& start, const RefPtr<Animatable>& end, float time);
65 
66     AnimatableType type_;
67     std::list<RefPtr<Animatable>> animatables_;
68     PropCallback animateTo_ = nullptr;
69     RefPtr<Animatable> init_;
70 };
71 
72 using PropAnimationMap = std::map<AnimatableType, RefPtr<PropertyAnimation>>;
73 
74 } // namespace OHOS::Ace
75 
76 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_PROPERTY_ANIMATION_H