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