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/spring_motion.h"
17
18 #include "base/log/log.h"
19
20 namespace OHOS::Ace {
21 namespace {
22
23 constexpr float UNIT_CONVERT = 1000.0f;
24
25 } // namespace
26
SpringMotion(double start,double end,double velocity,const RefPtr<SpringProperty> & spring)27 SpringMotion::SpringMotion(double start, double end, double velocity, const RefPtr<SpringProperty>& spring)
28 {
29 Reset(start, end, velocity, spring);
30 }
31
GetType()32 SpringModelType SpringMotion::GetType()
33 {
34 return model_->GetType();
35 }
36
GetCurrentPosition()37 double SpringMotion::GetCurrentPosition()
38 {
39 return currentPosition_;
40 }
41
GetCurrentVelocity()42 double SpringMotion::GetCurrentVelocity()
43 {
44 return currentVelocity_;
45 }
46
GetEndValue() const47 double SpringMotion::GetEndValue() const
48 {
49 return endPosition_;
50 }
51
IsCompleted(double value,double velocity) const52 bool SpringMotion::IsCompleted(double value, double velocity) const
53 {
54 return NearZero(value - endPosition_, accuracy_) && NearZero(velocity, accuracy_);
55 }
56
IsCompleted()57 bool SpringMotion::IsCompleted()
58 {
59 return IsCompleted(currentPosition_, currentVelocity_);
60 }
61
SetAccuracy(double accuracy)62 void SpringMotion::SetAccuracy(double accuracy)
63 {
64 accuracy_ = std::abs(accuracy);
65 }
66
Reset(double start,double end,double velocity,const RefPtr<SpringProperty> & spring)67 void SpringMotion::Reset(double start, double end, double velocity, const RefPtr<SpringProperty>& spring)
68 {
69 currentPosition_ = start;
70 currentVelocity_ = velocity;
71 endPosition_ = end;
72 model_ = SpringModel::Build(start - end, velocity, spring);
73 }
74
Move(float offsetTime)75 void SpringMotion::Move(float offsetTime)
76 {
77 // change millisecond to second.
78 float offsetTimeInSecond = offsetTime / UNIT_CONVERT;
79 currentPosition_ = endPosition_ + model_->Position(offsetTimeInSecond);
80 currentVelocity_ = model_->Velocity(offsetTimeInSecond);
81 if (IsCompleted()) {
82 currentPosition_ = endPosition_;
83 currentVelocity_ = 0.0;
84 }
85 }
86
ScrollSpringMotion(double start,double end,double velocity,const RefPtr<SpringProperty> & spring)87 ScrollSpringMotion::ScrollSpringMotion(double start, double end, double velocity, const RefPtr<SpringProperty>& spring)
88 : SpringMotion(start, end, velocity, spring)
89 {}
90
IsCompleted()91 bool ScrollSpringMotion::IsCompleted()
92 {
93 return NearZero(currentPosition_ - endPosition_, accuracy_);
94 }
95
GetCurrentPosition()96 double ScrollSpringMotion::GetCurrentPosition()
97 {
98 return currentPosition_;
99 }
100
Move(float offsetTime)101 void ScrollSpringMotion::Move(float offsetTime)
102 {
103 // change millisecond to second.
104 float offsetTimeInSecond = offsetTime / UNIT_CONVERT;
105 currentPosition_ = endPosition_ + model_->Position(offsetTimeInSecond);
106 if (IsCompleted()) {
107 currentPosition_ = endPosition_;
108 }
109 }
110
111 } // namespace OHOS::Ace