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