1 /* 2 * Copyright (c) 2021-2022 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_CURVE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_CURVE_H 18 19 #include "core/animation/curve.h" 20 #include "core/animation/spring_motion.h" 21 22 namespace OHOS::Ace { 23 24 class ACE_EXPORT SpringCurve : public Curve { 25 DECLARE_ACE_TYPE(SpringCurve, Curve); 26 27 public: 28 SpringCurve(float velocity, float mass, float stiffness, float damping); 29 ~SpringCurve() override = default; 30 31 float MoveInternal(float time) override; 32 const std::string ToString() override; 33 UpdateVelocity(float velocity)34 void UpdateVelocity(float velocity) 35 { 36 velocity_ = velocity; 37 SetEndPosition(1.0f, velocity_); 38 } 39 GetCurrentVelocity()40 float GetCurrentVelocity() const 41 { 42 return velocity_; 43 } 44 GetMass()45 float GetMass() const 46 { 47 return mass_; 48 } 49 GetStiffness()50 float GetStiffness() const 51 { 52 return stiffness_; 53 } 54 GetDamping()55 float GetDamping() const 56 { 57 return damping_; 58 } IsEqual(const RefPtr<Curve> & curve)59 bool IsEqual(const RefPtr<Curve>& curve) const override 60 { 61 auto other = DynamicCast<SpringCurve>(curve); 62 if (!other) { 63 return false; 64 } 65 return NearEqual(other->GetCurrentVelocity(), velocity_) && NearEqual(other->GetMass(), mass_) && 66 NearEqual(other->GetStiffness(), stiffness_) && NearEqual(other->GetDamping(), damping_); 67 } 68 69 private: 70 void SetEndPosition(float endPosition, float startVelocity); 71 void InitEstimateDuration(); 72 73 float estimateDuration_ = 1.0f; 74 float startPosition_ = 0.0f; 75 float endPosition_ = 0.0f; 76 float currentVelocity_ = 0.0f; 77 float currentPosition_ = 0.0f; 78 float velocityThreshold_ = 0.001f; 79 float valueThreshold_ = 0.001f; 80 float velocity_ = 0.0f; 81 float mass_ = 0.0f; 82 float stiffness_ = 0.0f; 83 float damping_ = 0.0f; 84 RefPtr<SpringProperty> property_; // Contain: mass & stiffness & damping 85 RefPtr<SpringModel> solution_; // Maybe: CriticalDamped or Overdamped or Underdamped 86 87 friend class NativeCurveHelper; 88 }; 89 90 } // namespace OHOS::Ace 91 92 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_CURVE_H