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 #include "core/animation/bilateral_spring_adapter.h"
17
18 namespace OHOS::Ace {
19
ResetControl(int32_t delta)20 double BilateralSpringAdapter::ResetControl(int32_t delta)
21 {
22 auto controlNode = GetControlNode();
23 if (!controlNode) {
24 return 0.0;
25 }
26 double lastValue = controlNode->GetValue();
27 LOGD("BeforeReset: %{public}s", DumpNodes().c_str());
28 RefPtr<SpringNode> srcNode;
29 int32_t nodeSize = static_cast<int32_t>(nodes_.size());
30 if (delta < 0) {
31 for (int32_t index = nodeSize - 1; index >= 0; index--) {
32 if (index + delta < 0) {
33 srcNode = nodes_.at(0);
34 } else {
35 srcNode = nodes_.at(index + delta);
36 }
37 MoveNode(srcNode, index);
38 }
39 } else {
40 for (int32_t index = 0; index < nodeSize; index++) {
41 if (index + delta >= nodeSize) {
42 srcNode = nodes_.at(nodeSize - 1);
43 } else {
44 srcNode = nodes_.at(index + delta);
45 }
46 MoveNode(srcNode, index);
47 }
48 }
49 double ret = controlNode->GetValue() - lastValue;
50 controlNode->ResetNode(0.0, controlNode->GetVelocity());
51 LOGD("AfterReset: %{public}s", DumpNodes().c_str());
52 return ret;
53 }
54
FlushAnimation()55 void BilateralSpringAdapter::FlushAnimation()
56 {
57 // flush from control to edge.
58 int32_t controlIndex = GetControlIndex();
59 for (int32_t index = 1; index <= controlIndex; index++) {
60 if (controlIndex + index < GetSize()) {
61 auto node = GetNode(controlIndex + index);
62 if (node) {
63 node->OnAnimation();
64 }
65 }
66 if (controlIndex - index >= 0) {
67 auto node = GetNode(controlIndex - index);
68 if (node) {
69 node->OnAnimation();
70 }
71 }
72 }
73 }
74
MoveNode(const RefPtr<SpringNode> & srcNode,int32_t dstIndex)75 void BilateralSpringAdapter::MoveNode(const RefPtr<SpringNode>& srcNode, int32_t dstIndex)
76 {
77 RefPtr<SpringNode> dstNode;
78 if (srcNode) {
79 dstNode = nodes_[dstIndex];
80 if (dstNode) {
81 dstNode->ResetNode(srcNode->GetValue(), srcNode->GetVelocity());
82 }
83 } else {
84 LOGD("Move node. index: %{public}d, control: %{public}d", dstIndex, GetControlIndex());
85 dstNode = nodes_[dstIndex];
86 if (dstNode) {
87 dstNode->ResetNode(0.0, 0.0);
88 }
89 }
90 }
91
SetDeltaValue(double delta)92 void BilateralSpringAdapter::SetDeltaValue(double delta)
93 {
94 int32_t controlIndex = GetControlIndex();
95 RefPtr<SpringNode> node;
96 for (int32_t index = 1; index <= controlIndex; index++) {
97 if (controlIndex + index < GetSize()) {
98 node = GetNode(controlIndex + index);
99 if (node) {
100 node->SetDeltaValue(delta);
101 }
102 }
103 if (controlIndex - index >= 0) {
104 node = GetNode(controlIndex - index);
105 if (node) {
106 node->SetDeltaValue(delta);
107 }
108 }
109 }
110 }
111
112 }; // namespace OHOS::Ace