• 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/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         LOGW("construct scroll motion error. empty or invalid spring property, use default spring property.");
37         spring_ =
38             AceType::MakeRefPtr<SpringProperty>(DEFAULT_SPRING_MASS, DEFAULT_SPRING_STIFFNESS, DEFAULT_SPRING_DAMPING);
39     }
40 
41     if (position > initExtent.Trailing()) {
42         springMotion_ = MakeOverScrollMotion(position, velocity);
43     } else if (position < initExtent.Leading()) {
44         springMotion_ = MakeUnderScrollMotion(position, velocity);
45     } else {
46         LOGW("No available motion.");
47     }
48 }
49 
IsValid() const50 bool ScrollMotion::IsValid() const
51 {
52     return springMotion_ != nullptr;
53 }
54 
Move(float offsetTime)55 void ScrollMotion::Move(float offsetTime)
56 {
57     // SpringMotion is null when not OverScroll & UnderScroll.
58     if (!springMotion_) {
59         return;
60     }
61     springMotion_->Move(offsetTime);
62 }
63 
GetCurrentPosition()64 double ScrollMotion::GetCurrentPosition()
65 {
66     if (!springMotion_) {
67         // do not move yet, return init position.
68         return position_;
69     }
70     return springMotion_->GetCurrentPosition();
71 }
72 
GetCurrentVelocity()73 double ScrollMotion::GetCurrentVelocity()
74 {
75     if (!springMotion_) {
76         // do not move yet, return init velocity.
77         return velocity_;
78     }
79     return springMotion_->GetCurrentVelocity();
80 }
81 
IsCompleted()82 bool ScrollMotion::IsCompleted()
83 {
84     if (!springMotion_) {
85         // do not move yet
86         return false;
87     }
88     return springMotion_->IsCompleted();
89 }
90 
91 } // namespace OHOS::Ace