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_FORCE_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 SetVelocityAccuracy(double velocityAccuracy); 52 53 void Reset(double start, double end, double velocity, const RefPtr<SpringProperty>& spring); 54 55 std::string GetMotionType() const override; 56 57 protected: 58 double endPosition_ = 0.0; 59 double currentPosition_ = 0.0; 60 double currentVelocity_ = 0.0; 61 double accuracy_ = NEAR_ZERO; 62 double velocityAccuracy_ = NEAR_ZERO; 63 RefPtr<SpringModel> model_; 64 65 private: 66 // the threshold close to zero 67 static constexpr double NEAR_ZERO = 0.1; 68 }; 69 70 class ScrollSpringMotion : public SpringMotion { 71 DECLARE_ACE_TYPE(ScrollSpringMotion, SpringMotion); 72 73 public: 74 ScrollSpringMotion(double start, double end, double velocity, const RefPtr<SpringProperty>& spring); 75 76 ~ScrollSpringMotion() override = default; 77 78 void Move(float offsetTime) override; 79 80 bool IsCompleted() override; 81 82 double GetCurrentPosition() override; 83 }; 84 85 } // namespace OHOS::Ace 86 87 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_MOTION_H 88