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