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/scroll_motion.h"
17 
18 namespace OHOS::Ace {
19 namespace {
20 
21 constexpr double DEFAULT_SPRING_MASS = 1.0;
22 constexpr double DEFAULT_SPRING_STIFFNESS = 500.0;
23 constexpr double DEFAULT_SPRING_DAMPING = 45.0;
24 
25 } // namespace
26 
ScrollMotion(double position,double velocity,const ExtentPair & extent,const ExtentPair & initExtent,const RefPtr<SpringProperty> & spring)27 ScrollMotion::ScrollMotion(double position, double velocity, const ExtentPair& extent,
28     const ExtentPair& initExtent, const RefPtr<SpringProperty>& spring)
29     : position_(position), velocity_(velocity), leadingExtent_(extent.Leading()), trailingExtent_(extent.Trailing())
30 {
31     if (spring && spring->IsValid()) {
32         spring_ = spring;
33     } else {
34         spring_ =
35             AceType::MakeRefPtr<SpringProperty>(DEFAULT_SPRING_MASS, DEFAULT_SPRING_STIFFNESS, DEFAULT_SPRING_DAMPING);
36     }
37 
38     if (position > initExtent.Trailing()) {
39         springMotion_ = MakeOverScrollMotion(position, velocity);
40     } else if (position < initExtent.Leading()) {
41         springMotion_ = MakeUnderScrollMotion(position, velocity);
42     }
43 }
44 
IsValid() const45 bool ScrollMotion::IsValid() const
46 {
47     return springMotion_ != nullptr;
48 }
49 
Move(float offsetTime)50 void ScrollMotion::Move(float offsetTime)
51 {
52     // SpringMotion is null when not OverScroll & UnderScroll.
53     if (!springMotion_) {
54         return;
55     }
56     springMotion_->Move(offsetTime);
57 }
58 
GetCurrentPosition()59 double ScrollMotion::GetCurrentPosition()
60 {
61     if (!springMotion_) {
62         // do not move yet, return init position.
63         return position_;
64     }
65     return springMotion_->GetCurrentPosition();
66 }
67 
GetCurrentVelocity()68 double ScrollMotion::GetCurrentVelocity()
69 {
70     if (!springMotion_) {
71         // do not move yet, return init velocity.
72         return velocity_;
73     }
74     return springMotion_->GetCurrentVelocity();
75 }
76 
IsCompleted()77 bool ScrollMotion::IsCompleted()
78 {
79     if (!springMotion_) {
80         // do not move yet
81         return false;
82     }
83     return springMotion_->IsCompleted();
84 }
85 
86 } // namespace OHOS::Ace