1 /*
2  * Copyright (c) 2023 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_CHAIN_ANIMATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_CHAIN_ANIMATION_H
18 
19 #include <functional>
20 #include <map>
21 #include <utility>
22 
23 #include "base/memory/ace_type.h"
24 #include "base/memory/referenced.h"
25 #include "core/animation/scheduler.h"
26 #include "core/animation/spring_motion.h"
27 
28 namespace OHOS::Ace {
29 enum class ChainEdgeEffect {
30     DEFAULT,
31     STRETCH,
32 };
33 
34 class ChainAnimationNode : public AceType {
35     DECLARE_ACE_TYPE(ChainAnimationNode, AceType);
36 
37 public:
38     ChainAnimationNode(
39         int32_t index, float space, float maxSpace, float minSpace, RefPtr<SpringProperty> springProperty);
40     void SetDelta(float delta, float spaceDelta, float duration);
41     float GetDelta() const;
42     float GetDeltaPredict(float delta, float duration);
43     bool TickAnimation(float duration);
SetIndex(int32_t index)44     void SetIndex(int32_t index)
45     {
46         index_ = index;
47     }
SetSpace(float space,float maxSpace,float minSpace)48     void SetSpace(float space, float maxSpace, float minSpace)
49     {
50         space_ = space;
51         maxSpace_ = maxSpace;
52         minSpace_ = minSpace;
53     }
54 
55 private:
56     RefPtr<SpringMotion> spring_;
57     RefPtr<SpringProperty> springProperty_;
58     int32_t index_;
59     float space_;
60     float maxSpace_;
61     float minSpace_;
62     float spaceDelta_ = 0.0f;
63     float curPosition_ = 0.0f;
64     float curVelocity_ = 0.0f;
65 };
66 
67 class ChainAnimation : public AceType {
68     DECLARE_ACE_TYPE(ChainAnimation, AceType);
69 
70 public:
71     ChainAnimation(float space, float maxSpace, float minSpace, RefPtr<SpringProperty> springProperty);
72     void SetDelta(float delta, float overOffset);
73     float GetValue(int32_t index);
74     float GetValuePredict(int32_t index, float delta);
75     float SetControlIndex(int32_t index);
GetControlIndex()76     int32_t GetControlIndex()
77     {
78         return controlIndex_;
79     }
SetMaxIndex(int32_t index)80     void SetMaxIndex(int32_t index)
81     {
82         maxIndex_ = index;
83     }
SetAnimationCallback(std::function<void ()> callback)84     void SetAnimationCallback(std::function<void()> callback)
85     {
86         animationCallback_ = std::move(callback);
87     }
SetConductivity(float value)88     void SetConductivity(float value)
89     {
90         conductivity_ = value;
91     }
SetIntensity(float value)92     void SetIntensity(float value)
93     {
94         intensity_ = value;
95     }
SetEdgeEffectIntensity(float value)96     void SetEdgeEffectIntensity(float value)
97     {
98         edgeEffectIntensity_ = value;
99     }
SetEdgeEffect(ChainEdgeEffect edgeEffect)100     void SetEdgeEffect(ChainEdgeEffect edgeEffect)
101     {
102         edgeEffect_ = edgeEffect;
103     }
104     void SetSpace(float space, float maxSpace, float minSpace);
GetSpace()105     float GetSpace() const
106     {
107         return space_;
108     }
109 
110     static constexpr float DEFAULT_CONDUCTIVITY = 0.7f;
111     static constexpr float DEFAULT_INTENSITY = 0.3f;
112     static constexpr float DEFAULT_EDGE_EFFECT_INTENSITY = 0.04f;
113 
114 private:
115     void TickAnimation();
116 
117     std::function<void()> animationCallback_;
118     std::map<int32_t, RefPtr<ChainAnimationNode>> nodes_;
119     RefPtr<SpringProperty> springProperty_;
120     RefPtr<Scheduler> scheduler_;
121     uint64_t timestamp_ = 0;
122     float space_;
123     float maxSpace_;
124     float minSpace_;
125     int32_t controlIndex_ = 0;
126     int32_t maxIndex_ = 0;
127     float conductivity_ = DEFAULT_CONDUCTIVITY;
128     float intensity_ = DEFAULT_INTENSITY;
129     float edgeEffectIntensity_ = DEFAULT_EDGE_EFFECT_INTENSITY;
130     ChainEdgeEffect edgeEffect_ = ChainEdgeEffect::DEFAULT;
131 };
132 } // namespace OHOS::Ace
133 #endif