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_SPRING_NODE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_NODE_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/animation/spring_adapter.h"
21 #include "core/animation/spring_node_listenable.h"
22 
23 namespace OHOS::Ace {
24 
25 class SpringNode : public AceType, public SpringNodeListenable {
26     DECLARE_ACE_TYPE(SpringNode, AceType);
27 
28 public:
SpringNode(int32_t index)29     explicit SpringNode(int32_t index)
30     {
31         index_ = index;
32     }
33 
34     ~SpringNode() override = default;
35 
36     static constexpr int32_t UNSET_DISTANCE = -1;
37 
GetIndex()38     int32_t GetIndex() const
39     {
40         return index_;
41     }
42 
SetIndex(int32_t index)43     void SetIndex(int32_t index)
44     {
45         index_ = index;
46     }
47 
GetFrameDelta()48     int32_t GetFrameDelta() const
49     {
50         return frameDelta_;
51     }
52 
SetFrameDelta(int32_t frameDelta)53     void SetFrameDelta(int32_t frameDelta)
54     {
55         frameDelta_ = frameDelta;
56     }
57 
SetAdapter(const RefPtr<SpringAdapter> & adapter)58     void SetAdapter(const RefPtr<SpringAdapter>& adapter)
59     {
60         if (adapter) {
61             adapter_ = adapter;
62         }
63     }
64 
SetMinDecoration(double minDecoration)65     void SetMinDecoration(double minDecoration)
66     {
67         if (GreatOrEqual(minDecoration, 0.0)) {
68             minDecoration_ = minDecoration;
69         }
70     }
71 
SetMaxDecoration(double maxDecoration)72     void SetMaxDecoration(double maxDecoration)
73     {
74         if (GreatOrEqual(maxDecoration, 0.0)) {
75             maxDecoration_ = maxDecoration;
76         }
77     }
78 
SetDecoration(double decoration)79     void SetDecoration(double decoration)
80     {
81         if (GreatOrEqual(decoration, 0.0)) {
82             decoration_ = decoration;
83         }
84     }
85 
GetAdapter()86     const RefPtr<SpringAdapter>& GetAdapter() const
87     {
88         return adapter_;
89     }
90 
91     // Set control node's value
SetValue(double value)92     virtual void SetValue(double value)
93     {
94         isAnimateToEnd_ = false;
95     }
96 
EndToValue(double endValue,double velocity)97     virtual void EndToValue(double endValue, double velocity)
98     {
99         isAnimateToEnd_ = true;
100     }
101 
102     virtual void Cancel() = 0;
103 
104     virtual void TransferParams(double stiffness, double damping) = 0;
105 
106     virtual double GetValue() const = 0;
107     virtual double GetVelocity() const = 0;
108 
109     virtual void OnAnimation() = 0;
110 
OnUpdate(double value,double velocity)111     void OnUpdate(double value, double velocity) {}
112 
OnEnd(double value)113     void OnEnd(double value) {}
114 
SetDeltaValue(double delta)115     virtual void SetDeltaValue(double delta) {}
116 
ResetNode(double value,double velocity)117     virtual void ResetNode(double value, double velocity) {}
118 
IsRunning()119     bool IsRunning() const
120     {
121         return isRunning_;
122     }
123 
IsAnimateToEnd()124     bool IsAnimateToEnd() const
125     {
126         return isAnimateToEnd_;
127     }
128 
GetDistance()129     int32_t GetDistance() const
130     {
131         return distance_;
132     }
133 
SetDistance(int32_t distance)134     void SetDistance(int32_t distance)
135     {
136         distance_ = distance;
137     }
138 
139 protected:
140     uint64_t startTime_ = 0;     // The start time when the node starts to move
141     bool isRunning_ = false; // Whether the node is moving
142     RefPtr<SpringAdapter> adapter_;
143     int32_t index_ = 0; // Node's index
144     double decoration_ = 0.0;
145     double minDecoration_ = 0.0;
146     double maxDecoration_ = 0.0;
147 
148 private:
149     int32_t frameDelta_ = 1;
150     bool isAnimateToEnd_ = false;
151     int32_t distance_ = UNSET_DISTANCE; // Distance to control point
152 };
153 
154 } // namespace OHOS::Ace
155 
156 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_NODE_H
157