• 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_MOTION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_MOTION_H
18 
19 #include <cmath>
20 
21 #include "base/utils/utils.h"
22 #include "core/animation/motion.h"
23 #include "core/animation/spring_model.h"
24 
25 namespace OHOS::Ace {
26 
27 class ACE_EXPORT SpringMotion : public Motion {
28     DECLARE_ACE_TYPE(SpringMotion, Motion);
29 
30 public:
31     SpringMotion(double start, double end, double velocity, const RefPtr<SpringProperty>& spring);
32 
33     ~SpringMotion() override = default;
34 
35     void Move(float offsetTime) override;
36 
37     SpringModelType GetType();
38 
39     double GetCurrentPosition() override;
40 
41     double GetCurrentVelocity() override;
42 
43     double GetEndValue() const;
44 
45     bool IsCompleted(double value, double velocity) const;
46 
47     bool IsCompleted() override;
48 
49     void SetAccuracy(double accuracy);
50 
51     void Reset(double start, double end, double velocity, const RefPtr<SpringProperty>& spring);
52 
53 protected:
54     double endPosition_ = 0.0;
55     double currentPosition_ = 0.0;
56     double currentVelocity_ = 0.0;
57     double accuracy_ = NEAR_ZERO;
58     RefPtr<SpringModel> model_;
59 
60 private:
61     // the threshold close to zero
62     static constexpr double NEAR_ZERO = 0.001;
63 };
64 
65 class ScrollSpringMotion : public SpringMotion {
66     DECLARE_ACE_TYPE(ScrollSpringMotion, SpringMotion);
67 
68 public:
69     ScrollSpringMotion(double start, double end, double velocity, const RefPtr<SpringProperty>& spring);
70 
71     ~ScrollSpringMotion() override = default;
72 
73     void Move(float offsetTime) override;
74 
75     bool IsCompleted() override;
76 
77     double GetCurrentPosition() override;
78 };
79 
80 } // namespace OHOS::Ace
81 
82 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_MOTION_H
83