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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SCROLL_MOTION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SCROLL_MOTION_H
18 
19 #include "core/animation/friction_motion.h"
20 #include "core/animation/motion.h"
21 #include "core/animation/spring_motion.h"
22 
23 namespace OHOS::Ace {
24 
25 class ACE_EXPORT ExtentPair {
26 public:
ExtentPair(double leading,double trailing)27     ExtentPair(double leading, double trailing) : leading_(leading), trailing_(trailing) {}
28     ExtentPair() = delete;
29     ~ExtentPair() = default;
30 
Leading()31     double Leading() const
32     {
33         return leading_;
34     }
35 
Trailing()36     double Trailing() const
37     {
38         return trailing_;
39     }
40 
41 private:
42     double leading_ = 0.0;
43     double trailing_ = 0.0;
44 };
45 
46 class ACE_EXPORT ScrollMotion : public Motion {
47 public:
48     ScrollMotion(double position, double velocity, const ExtentPair& extent, const ExtentPair& initExtent,
49         const RefPtr<SpringProperty>& spring);
50     ~ScrollMotion() override = default;
51 
52     double GetCurrentPosition() override;
53     double GetCurrentVelocity() override;
54     bool IsCompleted() override;
55 
56     // Perform motion in each timestamp
57     void Move(float offsetTime) override;
58 
59     bool IsValid() const;
60 
61 private:
MakeUnderScrollMotion(double position,double velocity)62     RefPtr<ScrollSpringMotion> MakeUnderScrollMotion(double position, double velocity)
63     {
64         return AceType::MakeRefPtr<ScrollSpringMotion>(position, leadingExtent_, velocity, spring_);
65     }
66 
MakeOverScrollMotion(double position,double velocity)67     RefPtr<ScrollSpringMotion> MakeOverScrollMotion(double position, double velocity)
68     {
69         return AceType::MakeRefPtr<ScrollSpringMotion>(position, trailingExtent_, velocity, spring_);
70     }
71 
72     // init position
73     const double position_ { 0.0 };
74     // init velocity
75     const double velocity_ { 0.0 };
76     double leadingExtent_ { 0.0 };
77     double trailingExtent_ { 0.0 };
78     RefPtr<SpringProperty> spring_;
79     RefPtr<ScrollSpringMotion> springMotion_;
80 };
81 
82 } // namespace OHOS::Ace
83 
84 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SCROLL_MOTION_H
85