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 #include "core/animation/spring_animation.h"
17 
18 namespace OHOS::Ace {
19 namespace {
20 
21 constexpr float DEFAULT_VALUE_THRESHOLD = 0.001f;
22 constexpr float VELOCITY_THRESHOLD_RATIO = 25.0f;
23 constexpr float DEFAULT_START_POSITION = 0.0f;
24 constexpr float DEFAULT_END_POSITION = 1.0f;
25 constexpr float DEFAULT_START_VELOCITY = 0.0f;
26 constexpr int32_t DEFAULT_ESTIMATE_STEPS = 100;
27 
28 } // namespace
29 
SpringAnimation(const RefPtr<SpringProperty> & property)30 SpringAnimation::SpringAnimation(const RefPtr<SpringProperty>& property)
31 {
32     property_ = property;
33     valueThreshold_ = (DEFAULT_END_POSITION - DEFAULT_START_POSITION) * DEFAULT_VALUE_THRESHOLD;
34     SetEndPosition(DEFAULT_END_POSITION, DEFAULT_START_VELOCITY);
35 }
36 
SpringAnimation(const RefPtr<SpringProperty> & property,float velocity)37 SpringAnimation::SpringAnimation(const RefPtr<SpringProperty>& property, float velocity)
38 {
39     property_ = property;
40     valueThreshold_ = (DEFAULT_END_POSITION - DEFAULT_START_POSITION) * DEFAULT_VALUE_THRESHOLD;
41     SetEndPosition(DEFAULT_END_POSITION, velocity);
42 }
43 
SpringAnimation(const RefPtr<SpringProperty> & property,float velocity,float valueThreshold)44 SpringAnimation::SpringAnimation(const RefPtr<SpringProperty>& property, float velocity, float valueThreshold)
45 {
46     property_ = property;
47     valueThreshold_ = valueThreshold;
48     SetEndPosition(DEFAULT_END_POSITION, velocity);
49 }
50 
SetEndPosition(float endPosition,float startVelocity)51 void SpringAnimation::SetEndPosition(float endPosition, float startVelocity)
52 {
53     startPosition_ = 0.0f;
54     endPosition_ = endPosition;
55     currentVelocity_ = startVelocity;
56     currentPosition_ = startPosition_;
57     velocityThreshold_ = valueThreshold_ * VELOCITY_THRESHOLD_RATIO;
58     solution_ = SpringModel::Build(endPosition_, startVelocity, property_);
59     InitEstimateDuration();
60 }
61 
UpdatePosition(float normalized)62 void SpringAnimation::UpdatePosition(float normalized)
63 {
64     currentPosition_ = endPosition_ - solution_->Position(normalized * estimateDuration_);
65     currentVelocity_ = solution_->Velocity(normalized * estimateDuration_);
66     if (NearEqual(currentPosition_, endPosition_, valueThreshold_) &&
67         NearZero(currentVelocity_, velocityThreshold_)) {
68         currentPosition_ = endPosition_;
69         currentVelocity_ = 0.0f;
70     }
71 }
72 
InitEstimateDuration()73 void SpringAnimation::InitEstimateDuration()
74 {
75     float position = 0.0f;
76     float velocity = 0.0f;
77     float time = 1.0f / DEFAULT_ESTIMATE_STEPS;
78     for (int32_t i = 1; i < DEFAULT_ESTIMATE_STEPS; ++i) {
79         position = endPosition_ - solution_->Position(time * i);
80         velocity = solution_->Velocity(time * i);
81         if (NearEqual(position, endPosition_, valueThreshold_) && NearZero(velocity, velocityThreshold_)) {
82             estimateDuration_ = time * i;
83             break;
84         }
85     }
86 }
87 
GetCurve()88 RefPtr<Curve> SpringAnimation::GetCurve()
89 {
90     return MakeRefPtr<CustomCurve>([weak = AceType::WeakClaim(this)](float fraction) -> float {
91         auto animation = weak.Upgrade();
92         if (animation == nullptr) {
93             LOGE("create spring curve failed, animation is null!");
94             return NORMALIZED_DURATION_MAX;
95         }
96 
97         animation->UpdatePosition(fraction);
98         return animation->currentPosition_;
99     });
100 }
101 
102 } // namespace OHOS::Ace
103