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_SIMPLE_SPRING_CHAIN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SIMPLE_SPRING_CHAIN_H
18 
19 #include "base/geometry/dimension.h"
20 #include "core/animation/param_transfer.h"
21 #include "core/animation/simple_spring_adapter.h"
22 
23 namespace OHOS::Ace {
24 
25 class SpringChainProperty {
26 public:
SetControlStiffness(double stiffness)27     void SetControlStiffness(double stiffness)
28     {
29         controlStiffness_ = stiffness;
30     }
31 
ControlStiffness()32     double ControlStiffness() const
33     {
34         return controlStiffness_;
35     }
36 
SetControlDamping(double damping)37     void SetControlDamping(double damping)
38     {
39         controlDamping_ = damping;
40     }
41 
ControlDamping()42     double ControlDamping() const
43     {
44         return controlDamping_;
45     }
46 
47     static const RefPtr<SpringProperty>& GetDefaultOverSpringProperty();
48 
49     static double GetDefaultFriction();
50 
SetStiffnessTransfer(bool useCurve)51     void SetStiffnessTransfer(bool useCurve)
52     {
53         stiffnessTransfer_ = useCurve;
54     }
55 
StiffnessTransfer()56     bool StiffnessTransfer() const
57     {
58         return stiffnessTransfer_;
59     }
60 
SetDampingTransfer(bool useCurve)61     void SetDampingTransfer(bool useCurve)
62     {
63         dampingTransfer_ = useCurve;
64     }
65 
DampingTransfer()66     bool DampingTransfer() const
67     {
68         return dampingTransfer_;
69     }
70 
SetStiffnessCoefficient(double stiffnessCoefficient)71     void SetStiffnessCoefficient(double stiffnessCoefficient)
72     {
73         stiffnessCoefficient_ = stiffnessCoefficient;
74     }
75 
StiffnessCoefficient()76     double StiffnessCoefficient() const
77     {
78         return stiffnessCoefficient_;
79     }
80 
SetDampingCoefficient(double dampingCoefficient)81     void SetDampingCoefficient(double dampingCoefficient)
82     {
83         dampingCoefficient_ = dampingCoefficient;
84     }
85 
DampingCoefficient()86     double DampingCoefficient() const
87     {
88         return dampingCoefficient_;
89     }
90 
SetInterval(const Dimension & interval)91     void SetInterval(const Dimension& interval)
92     {
93         interval_ = interval;
94     }
95 
Interval()96     const Dimension& Interval() const
97     {
98         return interval_;
99     }
100 
SetMinInterval(const Dimension & minInterval)101     void SetMinInterval(const Dimension& minInterval)
102     {
103         minInterval_ = minInterval;
104     }
105 
MinInterval()106     const Dimension& MinInterval() const
107     {
108         return minInterval_;
109     }
110 
SetMaxInterval(const Dimension & maxInterval)111     void SetMaxInterval(const Dimension& maxInterval)
112     {
113         maxInterval_ = maxInterval;
114     }
115 
MaxInterval()116     const Dimension& MaxInterval() const
117     {
118         return maxInterval_;
119     }
120 
SetFrameDelay(int32_t frameDelay)121     void SetFrameDelay(int32_t frameDelay)
122     {
123         frameDelay_ = frameDelay;
124     }
125 
FrameDelay()126     int32_t FrameDelay() const
127     {
128         return frameDelay_;
129     }
130 
131 private:
132     static constexpr double CHAIN_CONTROL_DAMPING = 30.0;
133     static constexpr double CHAIN_CONTROL_STIFFNESS = 228.0;
134     static constexpr Dimension CHAIN_INTERVAL_DEFAULT = 20.0_vp;
135     static constexpr Dimension CHAIN_MIN_INTERVAL_DEFAULT = 10.0_vp;
136     static constexpr Dimension CHAIN_MAX_INTERVAL_DEFAULT = 40.0_vp;
137 
138     double controlStiffness_ { CHAIN_CONTROL_STIFFNESS };
139     double controlDamping_ { CHAIN_CONTROL_DAMPING };
140     bool stiffnessTransfer_ { true }; // true for curve
141     bool dampingTransfer_ { true };
142     double dampingCoefficient_ { 0.0 };
143     double stiffnessCoefficient_ { 1.0 };
144     Dimension interval_ { CHAIN_INTERVAL_DEFAULT };
145     Dimension minInterval_ { CHAIN_MIN_INTERVAL_DEFAULT };
146     Dimension maxInterval_ { CHAIN_MAX_INTERVAL_DEFAULT };
147     int32_t frameDelay_ { 1 };
148 };
149 
150 class SimpleSpringChain : public SpringAdapter::AdapterObserve {
151     DECLARE_ACE_TYPE(SimpleSpringChain, SpringAdapter::AdapterObserve);
152 
153 public:
154     explicit SimpleSpringChain(const RefPtr<SpringAdapter>& springAdapter);
155     ~SimpleSpringChain() override = default;
156 
OnControlNodeChange()157     void OnControlNodeChange() override
158     {
159         TransferParamsInternal();
160     }
161 
162     void OnNodeAdd(RefPtr<SpringNode>& node) override;
163 
164     void OnNodeDelete(RefPtr<SpringNode>& node) override;
165 
166     void SetDeltaValue(double delta);
167 
168     void SetValue(double value);
169 
170     void FlushAnimation();
171 
172     void EndToPosition(double value, double velocity);
173 
174     void Cancel();
175 
GetControlNode()176     RefPtr<SpringNode> GetControlNode() const
177     {
178         return springAdapter_->GetControlNode();
179     }
180 
GetSize()181     int32_t GetSize() const
182     {
183         return springAdapter_->GetSize();
184     }
185 
GetControlStiffness()186     double GetControlStiffness() const
187     {
188         return controlStiffness_;
189     }
190 
SetControlStiffness(double controlStiffness)191     void SetControlStiffness(double controlStiffness)
192     {
193         if (controlStiffness > 0.0) {
194             controlStiffness_ = controlStiffness;
195         }
196     }
197 
GetControlDamping()198     double GetControlDamping() const
199     {
200         return controlDamping_;
201     }
202 
SetControlDamping(double controlDamping)203     void SetControlDamping(double controlDamping)
204     {
205         if (controlDamping > 0.0) {
206             controlDamping_ = controlDamping;
207         }
208     }
209 
GetSpringAdapter()210     const RefPtr<SpringAdapter>& GetSpringAdapter() const
211     {
212         return springAdapter_;
213     }
214 
SetSpringAdapter(const RefPtr<SpringAdapter> & springAdapter)215     void SetSpringAdapter(const RefPtr<SpringAdapter>& springAdapter)
216     {
217         if (springAdapter) {
218             springAdapter_ = springAdapter;
219         }
220     }
221 
GetFrameDelta()222     int32_t GetFrameDelta() const
223     {
224         return frameDelta_;
225     }
226 
SetFrameDelta(int32_t frameDelta)227     void SetFrameDelta(int32_t frameDelta)
228     {
229         frameDelta_ = frameDelta;
230     }
231 
SetDampingTransfer(const RefPtr<ParamTransfer> & dampingTransfer)232     void SetDampingTransfer(const RefPtr<ParamTransfer>& dampingTransfer)
233     {
234         if (dampingTransfer) {
235             dampingTransfer_ = dampingTransfer;
236         }
237     }
238 
SetStiffnessTransfer(const RefPtr<ParamTransfer> & stiffnessTransfer)239     void SetStiffnessTransfer(const RefPtr<ParamTransfer>& stiffnessTransfer)
240     {
241         if (stiffnessTransfer) {
242             stiffnessTransfer_ = stiffnessTransfer;
243         }
244     }
245 
SetDecoration(double decoration)246     void SetDecoration(double decoration)
247     {
248         decoration_ = decoration;
249     }
250 
SetMinDecoration(double minDecoration)251     void SetMinDecoration(double minDecoration)
252     {
253         minDecoration_ = minDecoration;
254     }
255 
SetMaxDecoration(double maxDecoration)256     void SetMaxDecoration(double maxDecoration)
257     {
258         maxDecoration_ = maxDecoration;
259     }
260 
261 protected:
262     RefPtr<SpringAdapter> springAdapter_;
263 
264 private:
265     void TransferParamsInternal();
266     void SetParams(RefPtr<SpringNode>& node);
267 
268     double controlStiffness_ = 0.0;
269     double controlDamping_ = 0.0;
270     double decoration_ = 0.0;
271     double minDecoration_ = 0.0;
272     double maxDecoration_ = 0.0;
273     int32_t frameDelta_ = 0;
274     RefPtr<ParamTransfer> stiffnessTransfer_;
275     RefPtr<ParamTransfer> dampingTransfer_;
276 };
277 
278 } // namespace OHOS::Ace
279 
280 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SIMPLE_SPRING_CHAIN_H
281