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/simple_spring_adapter.h"
17 
18 namespace OHOS::Ace {
19 
AddNode(RefPtr<SpringNode> node)20 void SimpleSpringAdapter::AddNode(RefPtr<SpringNode> node)
21 {
22     if (!node) {
23         LOGE("AddNode failed. node is null.");
24         return;
25     }
26     nodes_[node->GetIndex()] = node;
27     NotifyNodeAdd(node);
28 }
29 
GetNext(const RefPtr<SpringNode> & node)30 RefPtr<SpringNode> SimpleSpringAdapter::GetNext(const RefPtr<SpringNode>& node)
31 {
32     if (!node) {
33         LOGE("Get next failed. node is null.");
34         return nullptr;
35     }
36     return GetNode(node->GetIndex() + 1);
37 }
38 
GetNode(int32_t index) const39 RefPtr<SpringNode> SimpleSpringAdapter::GetNode(int32_t index) const
40 {
41     auto iter = nodes_.find(index);
42     if (iter == nodes_.end()) {
43         return nullptr;
44     }
45     return iter->second;
46 }
47 
DumpNodes()48 std::string SimpleSpringAdapter::DumpNodes()
49 {
50     int32_t nodeSize = static_cast<int32_t>(nodes_.size());
51     int32_t zeroCount = 0;
52     std::string dumpInfo;
53     for (int32_t index = 0; index < nodeSize; index++) {
54         auto node = AceType::DynamicCast<SimpleSpringNode>(nodes_.at(index));
55         if (!node) {
56             continue;
57         }
58         dumpInfo += std::to_string(node->GetIndex()) + ":" + std::to_string(node->GetValue()) + ", " +
59                     std::to_string(node->GetTransferParams()->Stiffness()) + "; ";
60         if (NearZero(node->GetValue())) {
61             zeroCount++;
62         }
63     }
64     if (zeroCount == nodeSize) {
65         return std::string(" All Zeros");
66     } else {
67         return dumpInfo;
68     }
69 }
70 
FlushAnimation()71 void SimpleSpringAdapter::FlushAnimation()
72 {
73     // flush from control to edge.
74     for (int32_t index = 0; index < GetSize(); index++) {
75         auto node = GetNode(index);
76         if (node) {
77             node->OnAnimation();
78         }
79     }
80 }
81 
82 } // namespace OHOS::Ace