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_FRICTION_MOTION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_FRICTION_MOTION_H
18 
19 #include "core/animation/motion.h"
20 
21 namespace OHOS::Ace {
22 
23 constexpr float DEFAULT_MULTIPLIER = 60.0f;
24 
25 class ACE_EXPORT FrictionMotion : public Motion {
26 public:
27     FrictionMotion(double friction, double initPosition, double initVelocity);
28     ~FrictionMotion() override = default;
29 
30     // Called when do a new friction motion
31     void Reset(double friction, double initPosition, double initVelocity, double threshold = DEFAULT_MULTIPLIER);
32 
33     double GetCurrentPosition() override;
34     double GetCurrentVelocity() override;
35     bool IsCompleted() override;
36 
37     double GetVelocityByFinalPosition(double final, double threshold = DEFAULT_MULTIPLIER) const;
38     // Get the Position and Velocity by offsetTime(Unit:second).
39     double GetPosition(float offsetTime) const;
40     double GetVelocity(float offsetTime) const;
41 
42     // Trigger motion in each vsync timestamp(Unit:millisecond).
43     void Move(float offsetTime) override;
44 
45     double GetFinalPosition() const;
46     bool GetTimeByPosition(double position, double& time) const;
47 
48     std::string GetMotionType() const override;
49 
50 private:
51     bool IsValid(double friction) const;
52 
53     double friction_ { 1.0 };
54     double initVelocity_ { 0.0 };
55     double signum_ { 0.0 }; // Rules: V < 0 [-1], V = 0 [0], V > 0 [1].
56     double currentTime_ { 0.0 };
57     double valueThreshold_ { 0.0 };
58     double velocityThreshold_ { 0.0 };
59     double finalTime_ { 0.0 };
60     double finalPosition_ { 0.0 };
61     double initPosition_ { 0.0 };
62 };
63 
64 } // namespace OHOS::Ace
65 
66 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_FRICTION_MOTION_H
67