1 /*
2 * Copyright (c) 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 #include "animation/rs_spring_animation.h"
17
18 #include "animation/rs_animation_common.h"
19 #include "animation/rs_render_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 {
RSSpringAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & byValue)28 RSSpringAnimation::RSSpringAnimation(std::shared_ptr<RSPropertyBase> property,
29 const std::shared_ptr<RSPropertyBase>& byValue) : RSPropertyAnimation(property)
30 {
31 isDelta_ = true;
32 byValue_ = byValue;
33 }
34
RSSpringAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue)35 RSSpringAnimation::RSSpringAnimation(std::shared_ptr<RSPropertyBase> property,
36 const std::shared_ptr<RSPropertyBase>& startValue, const std::shared_ptr<RSPropertyBase>& endValue)
37 : RSPropertyAnimation(property)
38 {
39 isDelta_ = false;
40 startValue_ = startValue;
41 endValue_ = endValue;
42 }
43
SetTimingCurve(const RSAnimationTimingCurve & timingCurve)44 void RSSpringAnimation::SetTimingCurve(const RSAnimationTimingCurve& timingCurve)
45 {
46 if (timingCurve.type_ != RSAnimationTimingCurve::CurveType::SPRING) {
47 ROSEN_LOGE("RSSpringAnimation::SetTimingCurve: invalid timing curve type");
48 return;
49 }
50 timingCurve_ = timingCurve;
51 }
52
GetTimingCurve() const53 const RSAnimationTimingCurve& RSSpringAnimation::GetTimingCurve() const
54 {
55 return timingCurve_;
56 }
57
OnStart()58 void RSSpringAnimation::OnStart()
59 {
60 RSPropertyAnimation::OnStart();
61 auto animation = std::make_shared<RSRenderSpringAnimation>(GetId(), GetPropertyId(),
62 originValue_->CreateRenderProperty(), startValue_->CreateRenderProperty(), endValue_->CreateRenderProperty());
63 // 300: placeholder for estimated duration, will be replaced by real duration on animation start.
64 SetDuration(300);
65 UpdateParamToRenderAnimation(animation);
66 animation->SetSpringParameters(timingCurve_.response_, timingCurve_.dampingRatio_);
67 animation->SetAdditive(GetAdditive());
68 if (isCustom_) {
69 StartUIAnimation(animation);
70 } else {
71 StartRenderAnimation(animation);
72 }
73 }
74
StartRenderAnimation(const std::shared_ptr<RSRenderSpringAnimation> & animation)75 void RSSpringAnimation::StartRenderAnimation(const std::shared_ptr<RSRenderSpringAnimation>& animation)
76 {
77 auto target = GetTarget().lock();
78 if (target == nullptr) {
79 ROSEN_LOGE("Failed to start spring animation, target is null!");
80 return;
81 }
82 auto transactionProxy = RSTransactionProxy::GetInstance();
83 if (transactionProxy == nullptr) {
84 return;
85 }
86
87 std::unique_ptr<RSCommand> command = std::make_unique<RSAnimationCreateSpring>(target->GetId(), animation);
88 transactionProxy->AddCommand(command, target->IsRenderServiceNode(), target->GetFollowType(), target->GetId());
89 if (target->NeedForcedSendToRemote()) {
90 std::unique_ptr<RSCommand> commandForRemote =
91 std::make_unique<RSAnimationCreateSpring>(target->GetId(), animation);
92 transactionProxy->AddCommand(commandForRemote, true, target->GetFollowType(), target->GetId());
93 }
94 if (target->NeedSendExtraCommand()) {
95 std::unique_ptr<RSCommand> extraCommand = std::make_unique<RSAnimationCreateSpring>(target->GetId(), animation);
96 transactionProxy->AddCommand(
97 extraCommand, !target->IsRenderServiceNode(), target->GetFollowType(), target->GetId());
98 }
99 }
100
StartUIAnimation(const std::shared_ptr<RSRenderSpringAnimation> & animation)101 void RSSpringAnimation::StartUIAnimation(const std::shared_ptr<RSRenderSpringAnimation>& animation)
102 {
103 StartCustomPropertyAnimation(animation);
104 }
105 } // namespace Rosen
106 } // namespace OHOS
107