1 /*
2 * Copyright (c) 2023 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 "animation/rs_interpolating_spring_animation.h"
17
18 #include "animation/rs_animation_common.h"
19 #include "animation/rs_render_interpolating_spring_animation.h"
20 #include "command/rs_animation_command.h"
21 #include "modifier/rs_property.h"
22 #include "platform/common/rs_log.h"
23 #include "transaction/rs_transaction_proxy.h"
24 #include "ui/rs_node.h"
25
26 namespace OHOS {
27 namespace Rosen {
RSInterpolatingSpringAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & byValue)28 RSInterpolatingSpringAnimation::RSInterpolatingSpringAnimation(
29 std::shared_ptr<RSPropertyBase> property, const std::shared_ptr<RSPropertyBase>& byValue)
30 : RSPropertyAnimation(property)
31 {
32 isDelta_ = true;
33 byValue_ = byValue;
34 }
35
RSInterpolatingSpringAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue)36 RSInterpolatingSpringAnimation::RSInterpolatingSpringAnimation(std::shared_ptr<RSPropertyBase> property,
37 const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue)
38 : RSPropertyAnimation(property)
39 {
40 isDelta_ = false;
41 startValue_ = startValue;
42 endValue_ = endValue;
43 }
44
SetTimingCurve(const RSAnimationTimingCurve & timingCurve)45 void RSInterpolatingSpringAnimation::SetTimingCurve(const RSAnimationTimingCurve& timingCurve)
46 {
47 if (timingCurve.type_ != RSAnimationTimingCurve::CurveType::INTERPOLATING_SPRING) {
48 ROSEN_LOGE("RSInterpolatingSpringAnimation::SetTimingCurve: invalid timing curve type");
49 return;
50 }
51 timingCurve_ = timingCurve;
52 }
53
GetTimingCurve() const54 const RSAnimationTimingCurve& RSInterpolatingSpringAnimation::GetTimingCurve() const
55 {
56 return timingCurve_;
57 }
58
SetZeroThreshold(const float zeroThreshold)59 void RSInterpolatingSpringAnimation::SetZeroThreshold(const float zeroThreshold)
60 {
61 constexpr float ZERO = 0.0f;
62 if (zeroThreshold_ < ZERO) {
63 ROSEN_LOGE("RSInterpolatingSpringAnimation::SetZeroThreshold: invalid threshold.");
64 return;
65 }
66 zeroThreshold_ = zeroThreshold;
67 isLogicallyFinishCallback_ = true;
68 }
69
OnStart()70 void RSInterpolatingSpringAnimation::OnStart()
71 {
72 RSPropertyAnimation::OnStart();
73 auto animation = std::make_shared<RSRenderInterpolatingSpringAnimation>(GetId(), GetPropertyId(),
74 originValue_->GetRenderProperty(), startValue_->GetRenderProperty(), endValue_->GetRenderProperty());
75 // 300: placeholder for estimated duration, will be replaced by real duration on animation start.
76 SetDuration(300);
77 UpdateParamToRenderAnimation(animation);
78 if (const auto& springParams = timingCurve_.springParams_) {
79 animation->SetSpringParameters(springParams->response_, springParams->dampingRatio_,
80 springParams->initialVelocity_, springParams->minimumAmplitudeRatio_);
81 }
82 animation->SetAdditive(GetAdditive());
83 if (GetIsLogicallyFinishCallback()) {
84 animation->SetZeroThreshold(zeroThreshold_);
85 }
86 if (isCustom_) {
87 animation->AttachRenderProperty(property_->GetRenderProperty());
88 StartUIAnimation(animation);
89 } else {
90 StartRenderAnimation(animation);
91 }
92 }
93
StartRenderAnimation(const std::shared_ptr<RSRenderInterpolatingSpringAnimation> & animation)94 void RSInterpolatingSpringAnimation::StartRenderAnimation(
95 const std::shared_ptr<RSRenderInterpolatingSpringAnimation>& animation)
96 {
97 auto target = GetTarget().lock();
98 if (target == nullptr) {
99 ROSEN_LOGE("Failed to start interpolating spring animation, target is null!");
100 return;
101 }
102 std::unique_ptr<RSCommand> command =
103 std::make_unique<RSAnimationCreateInterpolatingSpring>(target->GetId(), animation);
104 target->AddCommand(command, target->IsRenderServiceNode(), target->GetFollowType(), target->GetId());
105 if (target->NeedForcedSendToRemote()) {
106 std::unique_ptr<RSCommand> commandForRemote =
107 std::make_unique<RSAnimationCreateInterpolatingSpring>(target->GetId(), animation);
108 target->AddCommand(commandForRemote, true, target->GetFollowType(), target->GetId());
109 }
110 }
111
StartUIAnimation(const std::shared_ptr<RSRenderInterpolatingSpringAnimation> & animation)112 void RSInterpolatingSpringAnimation::StartUIAnimation(
113 const std::shared_ptr<RSRenderInterpolatingSpringAnimation>& animation)
114 {
115 StartCustomAnimation(animation);
116 }
117
GetIsLogicallyFinishCallback() const118 bool RSInterpolatingSpringAnimation::GetIsLogicallyFinishCallback() const
119 {
120 return isLogicallyFinishCallback_;
121 }
122 } // namespace Rosen
123 } // namespace OHOS