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 animation->SetSpringParameters(timingCurve_.response_, timingCurve_.dampingRatio_, timingCurve_.initialVelocity_,
79 timingCurve_.minimumAmplitudeRatio_);
80 animation->SetAdditive(GetAdditive());
81 if (GetIsLogicallyFinishCallback()) {
82 animation->SetZeroThreshold(zeroThreshold_);
83 }
84 if (isCustom_) {
85 animation->AttachRenderProperty(property_->GetRenderProperty());
86 StartUIAnimation(animation);
87 } else {
88 StartRenderAnimation(animation);
89 }
90 }
91
StartRenderAnimation(const std::shared_ptr<RSRenderInterpolatingSpringAnimation> & animation)92 void RSInterpolatingSpringAnimation::StartRenderAnimation(
93 const std::shared_ptr<RSRenderInterpolatingSpringAnimation>& animation)
94 {
95 auto target = GetTarget().lock();
96 if (target == nullptr) {
97 ROSEN_LOGE("Failed to start interpolating spring animation, target is null!");
98 return;
99 }
100 auto transactionProxy = RSTransactionProxy::GetInstance();
101 if (transactionProxy == nullptr) {
102 ROSEN_LOGE("Failed to start interpolating spring animation, transaction proxy is null!");
103 return;
104 }
105
106 std::unique_ptr<RSCommand> command =
107 std::make_unique<RSAnimationCreateInterpolatingSpring>(target->GetId(), animation);
108 transactionProxy->AddCommand(command, target->IsRenderServiceNode(), target->GetFollowType(), target->GetId());
109 if (target->NeedForcedSendToRemote()) {
110 std::unique_ptr<RSCommand> commandForRemote =
111 std::make_unique<RSAnimationCreateInterpolatingSpring>(target->GetId(), animation);
112 transactionProxy->AddCommand(commandForRemote, true, target->GetFollowType(), target->GetId());
113 }
114 }
115
StartUIAnimation(const std::shared_ptr<RSRenderInterpolatingSpringAnimation> & animation)116 void RSInterpolatingSpringAnimation::StartUIAnimation(
117 const std::shared_ptr<RSRenderInterpolatingSpringAnimation>& animation)
118 {
119 StartCustomAnimation(animation);
120 }
121
GetIsLogicallyFinishCallback() const122 bool RSInterpolatingSpringAnimation::GetIsLogicallyFinishCallback() const
123 {
124 return isLogicallyFinishCallback_;
125 }
126 } // namespace Rosen
127 } // namespace OHOS