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 #include "chain_animation.h"
17
18 #include "core/pipeline/pipeline_base.h"
19
20 namespace OHOS::Ace {
21 namespace {
22 constexpr int64_t NANOS_TO_MILLS = 1000000;
23 constexpr int32_t CHAIN_NODE_NUMBER = 15;
24 constexpr double DEFAULT_CHAIN_VALUE_ACCURACY = 0.5;
25 constexpr double DEFAULT_CHAIN_VALUE_VELOCITY_ACCURACY = 1000.0;
26 } // namespace
27
ChainAnimationNode(int32_t index,float space,float maxSpace,float minSpace,RefPtr<SpringProperty> springProperty)28 ChainAnimationNode::ChainAnimationNode(
29 int32_t index, float space, float maxSpace, float minSpace, RefPtr<SpringProperty> springProperty)
30 : springProperty_(std::move(springProperty)), index_(index), space_(space), maxSpace_(maxSpace),
31 minSpace_(minSpace), curPosition_(space)
32 {
33 spring_ = AceType::MakeRefPtr<SpringMotion>(space, space, 0.0, springProperty_);
34 spring_->SetAccuracy(DEFAULT_CHAIN_VALUE_ACCURACY);
35 spring_->SetVelocityAccuracy(DEFAULT_CHAIN_VALUE_VELOCITY_ACCURACY);
36 }
37
TickAnimation(float duration)38 bool ChainAnimationNode::TickAnimation(float duration)
39 {
40 spring_->OnTimestampChanged(duration, 0.0f, false);
41 curPosition_ = spring_->GetCurrentPosition();
42 curVelocity_ = spring_->GetCurrentVelocity();
43 return spring_->IsCompleted();
44 }
45
SetDelta(float delta,float spaceDelta,float duration)46 void ChainAnimationNode::SetDelta(float delta, float spaceDelta, float duration)
47 {
48 spaceDelta = std::clamp(spaceDelta, minSpace_ - space_, maxSpace_ - space_);
49 spring_->OnTimestampChanged(duration, 0.0f, false);
50 curPosition_ = spring_->GetCurrentPosition();
51 curPosition_ = std::clamp(curPosition_ + delta, minSpace_ - spaceDelta, maxSpace_ - spaceDelta);
52 spring_->Reset(curPosition_, space_, curVelocity_, springProperty_);
53 spaceDelta_ = spaceDelta;
54 }
55
GetDeltaPredict(float delta,float duration)56 float ChainAnimationNode::GetDeltaPredict(float delta, float duration)
57 {
58 spring_->OnTimestampChanged(duration, 0.0f, false);
59 float curPosition = spring_->GetCurrentPosition();
60 curPosition = std::clamp(curPosition + delta, minSpace_, maxSpace_);
61 return curPosition - space_;
62 }
63
GetDelta() const64 float ChainAnimationNode::GetDelta() const
65 {
66 return curPosition_ - space_ + spaceDelta_;
67 }
68
ChainAnimation(float space,float maxSpace,float minSpace,RefPtr<SpringProperty> springProperty)69 ChainAnimation::ChainAnimation(float space, float maxSpace, float minSpace, RefPtr<SpringProperty> springProperty)
70 : springProperty_(springProperty), space_(space), maxSpace_(maxSpace), minSpace_(minSpace)
71 {
72 for (int32_t i = 1; i < CHAIN_NODE_NUMBER; i++) {
73 nodes_.emplace(i, AceType::MakeRefPtr<ChainAnimationNode>(i, space, maxSpace, minSpace, springProperty));
74 nodes_.emplace(-i, AceType::MakeRefPtr<ChainAnimationNode>(-i, space, maxSpace, minSpace, springProperty));
75 }
76 auto&& callback = [weak = AceType::WeakClaim(this)](uint64_t duration) {
77 ACE_SCOPED_TRACE("ChainAnimation");
78 auto chain = weak.Upgrade();
79 CHECK_NULL_VOID(chain);
80 chain->TickAnimation();
81 };
82 scheduler_ = AceType::MakeRefPtr<Scheduler>(callback, PipelineBase::GetCurrentContext());
83 }
84
SetDelta(float delta,float overOffset)85 void ChainAnimation::SetDelta(float delta, float overOffset)
86 {
87 auto context = PipelineBase::GetCurrentContext();
88 CHECK_NULL_VOID(context);
89 auto timestamp = context->GetTimeFromExternalTimer();
90 double duration = static_cast<double>(timestamp - timestamp_) / static_cast<double>(NANOS_TO_MILLS);
91 float factor = (1 - conductivity_) * intensity_;
92 if (edgeEffect_ == ChainEdgeEffect::STRETCH) {
93 float spaceDelta = std::abs(overOffset) * edgeEffectIntensity_;
94 for (int32_t i = 1; i < CHAIN_NODE_NUMBER; i++) {
95 nodes_[i]->SetDelta(delta * factor, spaceDelta, static_cast<float>(duration));
96 nodes_[-i]->SetDelta(-delta * factor, spaceDelta, static_cast<float>(duration));
97 factor *= conductivity_;
98 }
99 } else {
100 float spaceDelta = overOffset * edgeEffectIntensity_;
101 for (int32_t i = 1; i < CHAIN_NODE_NUMBER; i++) {
102 nodes_[i]->SetDelta(delta * factor, -spaceDelta, static_cast<float>(duration));
103 nodes_[-i]->SetDelta(-delta * factor, spaceDelta, static_cast<float>(duration));
104 factor *= conductivity_;
105 spaceDelta *= conductivity_;
106 }
107 }
108 if (!scheduler_->IsActive() && !NearZero(delta)) {
109 scheduler_->Start();
110 }
111 timestamp_ = timestamp;
112 }
113
TickAnimation()114 void ChainAnimation::TickAnimation()
115 {
116 auto context = PipelineBase::GetCurrentContext();
117 CHECK_NULL_VOID(context);
118 auto timestamp = context->GetTimeFromExternalTimer();
119 auto duration = static_cast<double>(timestamp - timestamp_) / static_cast<double>(NANOS_TO_MILLS);
120 auto finish = true;
121 for (int32_t i = 1; i < CHAIN_NODE_NUMBER; i++) {
122 finish = nodes_[i]->TickAnimation(duration) && finish;
123 finish = nodes_[-i]->TickAnimation(duration) && finish;
124 }
125 if (finish) {
126 scheduler_->Stop();
127 }
128 if (animationCallback_) {
129 animationCallback_();
130 }
131 }
132
GetValue(int32_t index)133 float ChainAnimation::GetValue(int32_t index)
134 {
135 float value = 0.0f;
136 if (index > controlIndex_) {
137 for (int32_t i = 1; i <= index - controlIndex_ && i < CHAIN_NODE_NUMBER; i++) {
138 value += nodes_[i]->GetDelta();
139 }
140 } else if (index < controlIndex_) {
141 for (int32_t i = 1; i <= controlIndex_ - index && i < CHAIN_NODE_NUMBER; i++) {
142 value -= nodes_[-i]->GetDelta();
143 }
144 }
145 return value;
146 }
147
GetValuePredict(int32_t index,float delta)148 float ChainAnimation::GetValuePredict(int32_t index, float delta)
149 {
150 auto context = PipelineBase::GetCurrentContext();
151 CHECK_NULL_RETURN(context, 0);
152 auto timestamp = context->GetTimeFromExternalTimer();
153 double duration = static_cast<double>(timestamp - timestamp_) / static_cast<double>(NANOS_TO_MILLS);
154 float value = 0.0f;
155 float factor = (1 - conductivity_) * intensity_;
156 if (index > controlIndex_) {
157 for (int32_t i = 1; i <= index - controlIndex_ && i < CHAIN_NODE_NUMBER; i++) {
158 value += nodes_[i]->GetDeltaPredict(delta * factor, duration);
159 factor *= conductivity_;
160 }
161 } else if (index < controlIndex_) {
162 for (int32_t i = 1; i <= controlIndex_ - index && i < CHAIN_NODE_NUMBER; i++) {
163 value -= nodes_[-i]->GetDeltaPredict(-delta * factor, duration);
164 factor *= conductivity_;
165 }
166 }
167 return value;
168 }
169
SetControlIndex(int32_t index)170 float ChainAnimation::SetControlIndex(int32_t index)
171 {
172 if (index == controlIndex_) {
173 return 0.0f;
174 }
175 float delta = GetValue(index);
176 if (scheduler_->IsActive()) {
177 std::map<int32_t, RefPtr<ChainAnimationNode>> tmpNodes;
178 int32_t dt = index - controlIndex_;
179 for (int32_t i = 1; i < CHAIN_NODE_NUMBER; i++) {
180 auto next = i + dt <= 0 ? i + dt - 1 : i + dt;
181 if (next > -CHAIN_NODE_NUMBER && next < CHAIN_NODE_NUMBER) {
182 tmpNodes[i] = nodes_[next];
183 tmpNodes[i]->SetIndex(i);
184 } else {
185 tmpNodes.emplace(
186 i, AceType::MakeRefPtr<ChainAnimationNode>(i, space_, maxSpace_, minSpace_, springProperty_));
187 }
188 auto prev = dt - i >= 0 ? dt - i + 1 : dt - i;
189 if (prev > -CHAIN_NODE_NUMBER && prev < CHAIN_NODE_NUMBER) {
190 tmpNodes[-i] = nodes_[prev];
191 tmpNodes[-i]->SetIndex(-i);
192 } else {
193 tmpNodes.emplace(
194 -i, AceType::MakeRefPtr<ChainAnimationNode>(-i, space_, maxSpace_, minSpace_, springProperty_));
195 }
196 }
197 nodes_.swap(tmpNodes);
198 }
199 controlIndex_ = index;
200 return delta;
201 }
202
SetSpace(float space,float maxSpace,float minSpace)203 void ChainAnimation::SetSpace(float space, float maxSpace, float minSpace)
204 {
205 space_ = space;
206 maxSpace_ = maxSpace;
207 minSpace_ = minSpace;
208 for (int32_t i = 1; i < CHAIN_NODE_NUMBER; i++) {
209 nodes_[i]->SetSpace(space, maxSpace, minSpace);
210 nodes_[-i]->SetSpace(space, maxSpace, minSpace);
211 }
212 }
213 } // namespace OHOS::Ace
214