• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
GetSpaceDelta()54     float GetSpaceDelta() const
55     {
56         return spaceDelta_;
57     }
SetSpaceDelta(float delta)58     void SetSpaceDelta(float delta)
59     {
60         spaceDelta_ = delta;
61     }
62 
63 private:
64     RefPtr<SpringMotion> spring_;
65     RefPtr<SpringProperty> springProperty_;
66     int32_t index_;
67     float space_;
68     float maxSpace_;
69     float minSpace_;
70     float spaceDelta_ = 0.0f;
71     float curPosition_ = 0.0f;
72     float curVelocity_ = 0.0f;
73 };
74 
75 class ChainAnimation : public AceType {
76     DECLARE_ACE_TYPE(ChainAnimation, AceType);
77 
78 public:
79     ChainAnimation(float space, float maxSpace, float minSpace, RefPtr<SpringProperty> springProperty);
80     void SetDelta(float delta, float overOffset);
81     float GetValue(int32_t index);
82     float GetValuePredict(int32_t index, float delta);
83     float SetControlIndex(int32_t index);
GetControlIndex()84     int32_t GetControlIndex()
85     {
86         return controlIndex_;
87     }
SetMaxIndex(int32_t index)88     void SetMaxIndex(int32_t index)
89     {
90         maxIndex_ = index;
91     }
SetAnimationCallback(std::function<void ()> callback)92     void SetAnimationCallback(std::function<void()> callback)
93     {
94         animationCallback_ = std::move(callback);
95     }
SetConductivity(float value)96     void SetConductivity(float value)
97     {
98         conductivity_ = value;
99     }
SetIntensity(float value)100     void SetIntensity(float value)
101     {
102         intensity_ = value;
103     }
SetEdgeEffectIntensity(float value)104     void SetEdgeEffectIntensity(float value)
105     {
106         edgeEffectIntensity_ = value;
107     }
SetEdgeEffect(ChainEdgeEffect edgeEffect)108     void SetEdgeEffect(ChainEdgeEffect edgeEffect)
109     {
110         edgeEffect_ = edgeEffect;
111     }
112     void SetSpace(float space, float maxSpace, float minSpace);
GetSpace()113     float GetSpace() const
114     {
115         return space_;
116     }
117     bool HasSpaceDelta() const;
118     void ResetSpaceDelta();
119 
120     static constexpr float DEFAULT_CONDUCTIVITY = 0.7f;
121     static constexpr float DEFAULT_INTENSITY = 0.3f;
122     static constexpr float DEFAULT_EDGE_EFFECT_INTENSITY = 0.04f;
123 
124 private:
125     void TickAnimation();
126 
127     std::function<void()> animationCallback_;
128     std::map<int32_t, RefPtr<ChainAnimationNode>> nodes_;
129     RefPtr<SpringProperty> springProperty_;
130     RefPtr<Scheduler> scheduler_;
131     uint64_t timestamp_ = 0;
132     float space_;
133     float maxSpace_;
134     float minSpace_;
135     int32_t controlIndex_ = 0;
136     int32_t maxIndex_ = 0;
137     float conductivity_ = DEFAULT_CONDUCTIVITY;
138     float intensity_ = DEFAULT_INTENSITY;
139     float edgeEffectIntensity_ = DEFAULT_EDGE_EFFECT_INTENSITY;
140     ChainEdgeEffect edgeEffect_ = ChainEdgeEffect::DEFAULT;
141 };
142 } // namespace OHOS::Ace
143 #endif