1 /*
2  * Copyright (c) 2024 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_VELOCITY_MOTION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_VELOCITY_MOTION_H
18 #include "core/animation/motion.h"
19 
20 namespace OHOS::Ace {
21 using MotionCompleteCallback = std::function<bool(float)>;
22 constexpr float OFFSET_UNIT_CONVERT = 1000.0f;
23 
24 class ACE_EXPORT VelocityMotion : public Motion {
25     DECLARE_ACE_TYPE(VelocityMotion, Motion);
26 
27 public:
VelocityMotion(MotionCompleteCallback && complete)28     VelocityMotion(MotionCompleteCallback&& complete)
29         : complete_(std::move(complete))
30     {
31         lastOffsetTime_ = 0.0f;
32     }
33 
34     /**
35      * @description: Get the roll distance
36      * @return The scroll distance notified to the listener
37      */
GetCurrentPosition()38     double GetCurrentPosition() override
39     {
40         return scrollOffset_;
41     }
42 
GetCurrentVelocity()43     double GetCurrentVelocity() override
44     {
45         return velocity_;
46     }
47     /**
48      * @description: By adding a callback, it is up to the listener to decide whether to stop the motion
49      * @return True stop
50      */
IsCompleted()51     bool IsCompleted() override
52     {
53         if (complete_) {
54             return complete_(scrollOffset_);
55         }
56         return true;
57     }
58 
GetMotionType()59     std::string GetMotionType() const override
60     {
61         return "velocity";
62     }
63 
64     /**
65      * @description: Each subclass of scrollable component should override this method to perform motion in each
66      * timestamp. This function is called in motion's OnTimestampChanged function, where inform the listener of the
67      * distance of rolling
68      * @param {float} offsetTime Time offset, continuously growing, with irregular intervals
69      * @return None
70      */
Move(float offsetTime)71     void Move(float offsetTime) override
72     {
73         scrollOffset_ = velocity_ * (offsetTime - lastOffsetTime_) / OFFSET_UNIT_CONVERT;
74         lastOffsetTime_ = offsetTime;
75     }
76 
77     /**
78      * @description:  Set velocity
79      * @param {float} velocity
80      * @return None
81      */
SetVelocity(const float velocity)82     void SetVelocity(const float velocity)
83     {
84         velocity_ = velocity;
85     }
86 
87     /**
88      * @description:  Init scroll status
89      * @return None
90      */
Init()91     void Init()
92     {
93         scrollOffset_ = 0.0f;
94         lastOffsetTime_ = 0.0f;
95     }
96 
97 private:
98     // Tells the listener how far they need to roll, in px
99     float scrollOffset_ = 0.0f;
100     MotionCompleteCallback complete_;
101     // It is used to record the current speed, and different speeds can be passed in through the reset function, so that
102     // the speed can be changed
103     float velocity_ = 0.0f;
104     // Record the last time given by the move function
105     float lastOffsetTime_ = 0.0f;
106 };
107 } // namespace OHOS::Ace
108 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_VELOCITY_MOTION_H