• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (model_ == nullptr) {
35         LOGE("SpringModel is nullptr.");
36         return SpringModelType::CRITICAL_DAMPED;
37     }
38     return model_->GetType();
39 }
40 
GetCurrentPosition()41 double SpringMotion::GetCurrentPosition()
42 {
43     return currentPosition_;
44 }
45 
GetCurrentVelocity()46 double SpringMotion::GetCurrentVelocity()
47 {
48     return currentVelocity_;
49 }
50 
GetEndValue() const51 double SpringMotion::GetEndValue() const
52 {
53     return endPosition_;
54 }
55 
IsCompleted(double value,double velocity) const56 bool SpringMotion::IsCompleted(double value, double velocity) const
57 {
58     return NearZero(value - endPosition_, accuracy_) && NearZero(velocity, accuracy_);
59 }
60 
IsCompleted()61 bool SpringMotion::IsCompleted()
62 {
63     return IsCompleted(currentPosition_, currentVelocity_);
64 }
65 
SetAccuracy(double accuracy)66 void SpringMotion::SetAccuracy(double accuracy)
67 {
68     accuracy_ = std::abs(accuracy);
69 }
70 
Reset(double start,double end,double velocity,const RefPtr<SpringProperty> & spring)71 void SpringMotion::Reset(double start, double end, double velocity, const RefPtr<SpringProperty>& spring)
72 {
73     currentPosition_ = start;
74     currentVelocity_ = velocity;
75     endPosition_ = end;
76     model_ = SpringModel::Build(start - end, velocity, spring);
77 }
78 
Move(float offsetTime)79 void SpringMotion::Move(float offsetTime)
80 {
81     if (model_ == nullptr) {
82         LOGE("SpringModel is nullptr.");
83         return;
84     }
85     // change millisecond to second.
86     float offsetTimeInSecond = offsetTime / UNIT_CONVERT;
87     currentPosition_ = endPosition_ + model_->Position(offsetTimeInSecond);
88     currentVelocity_ = model_->Velocity(offsetTimeInSecond);
89     if (IsCompleted()) {
90         currentPosition_ = endPosition_;
91         currentVelocity_ = 0.0;
92     }
93 }
94 
ScrollSpringMotion(double start,double end,double velocity,const RefPtr<SpringProperty> & spring)95 ScrollSpringMotion::ScrollSpringMotion(double start, double end, double velocity, const RefPtr<SpringProperty>& spring)
96     : SpringMotion(start, end, velocity, spring)
97 {}
98 
IsCompleted()99 bool ScrollSpringMotion::IsCompleted()
100 {
101     return NearZero(currentPosition_ - endPosition_, accuracy_);
102 }
103 
GetCurrentPosition()104 double ScrollSpringMotion::GetCurrentPosition()
105 {
106     return currentPosition_;
107 }
108 
Move(float offsetTime)109 void ScrollSpringMotion::Move(float offsetTime)
110 {
111     if (model_ == nullptr) {
112         LOGE("SpringModel is nullptr.");
113         return;
114     }
115     // change millisecond to second.
116     float offsetTimeInSecond = offsetTime / UNIT_CONVERT;
117     currentPosition_ = endPosition_ + model_->Position(offsetTimeInSecond);
118     if (IsCompleted()) {
119         currentPosition_ = endPosition_;
120     }
121 }
122 
GetMotionType() const123 std::string SpringMotion::GetMotionType() const
124 {
125     return "spring";
126 }
127 
128 } // namespace OHOS::Ace