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 #include "animation/rs_curve_animation.h"
17 
18 #include "animation/rs_animation_common.h"
19 #include "animation/rs_render_curve_animation.h"
20 #include "command/rs_animation_command.h"
21 #include "platform/common/rs_log.h"
22 #include "transaction/rs_transaction_proxy.h"
23 #include "ui/rs_node.h"
24 #include "modifier/rs_property.h"
25 
26 namespace OHOS {
27 namespace Rosen {
RSCurveAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & byValue)28 RSCurveAnimation::RSCurveAnimation(std::shared_ptr<RSPropertyBase> property,
29     const std::shared_ptr<RSPropertyBase>& byValue) : RSPropertyAnimation(property)
30 {
31     isDelta_ = true;
32     byValue_ = byValue;
33 }
34 
RSCurveAnimation(std::shared_ptr<RSPropertyBase> property,const std::shared_ptr<RSPropertyBase> & startValue,const std::shared_ptr<RSPropertyBase> & endValue)35 RSCurveAnimation::RSCurveAnimation(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 RSCurveAnimation::SetTimingCurve(const RSAnimationTimingCurve& timingCurve)
45 {
46     if (timingCurve.type_ != RSAnimationTimingCurve::CurveType::INTERPOLATING) {
47         ROSEN_LOGE("RSCurveAnimation::SetTimingCurve: invalid timing curve type");
48         return;
49     }
50     timingCurve_ = timingCurve;
51 }
52 
GetTimingCurve() const53 const RSAnimationTimingCurve& RSCurveAnimation::GetTimingCurve() const
54 {
55     return timingCurve_;
56 }
57 
StartRenderAnimation(const std::shared_ptr<RSRenderCurveAnimation> & animation)58 void RSCurveAnimation::StartRenderAnimation(const std::shared_ptr<RSRenderCurveAnimation>& animation)
59 {
60     auto target = GetTarget().lock();
61     if (target == nullptr) {
62         ROSEN_LOGE("Failed to start curve animation, target is null!");
63         return;
64     }
65     auto transactionProxy = RSTransactionProxy::GetInstance();
66     if (transactionProxy == nullptr) {
67         ROSEN_LOGE("Failed to start curve animation, transaction proxy is null!");
68         return;
69     }
70 
71     std::unique_ptr<RSCommand> command = std::make_unique<RSAnimationCreateCurve>(target->GetId(), animation);
72     transactionProxy->AddCommand(command, target->IsRenderServiceNode(), target->GetFollowType(), target->GetId());
73 }
74 
StartUIAnimation(const std::shared_ptr<RSRenderCurveAnimation> & animation)75 void RSCurveAnimation::StartUIAnimation(const std::shared_ptr<RSRenderCurveAnimation>& animation)
76 {
77     StartCustomAnimation(animation);
78 }
79 
OnStart()80 void RSCurveAnimation::OnStart()
81 {
82     RSPropertyAnimation::OnStart();
83     auto interpolator = timingCurve_.GetInterpolator(GetDuration());
84     auto animation = std::make_shared<RSRenderCurveAnimation>(GetId(), GetPropertyId(),
85         originValue_->GetRenderProperty(), startValue_->GetRenderProperty(), endValue_->GetRenderProperty());
86     animation->SetInterpolator(interpolator);
87     animation->SetAdditive(GetAdditive());
88     UpdateParamToRenderAnimation(animation);
89     if (isCustom_) {
90         animation->AttachRenderProperty(property_->GetRenderProperty());
91         StartUIAnimation(animation);
92     } else {
93         StartRenderAnimation(animation);
94     }
95 }
96 
IsSupportInteractiveAnimator()97 bool RSCurveAnimation::IsSupportInteractiveAnimator()
98 {
99     auto interpolator = timingCurve_.GetInterpolator(GetDuration());
100     if (interpolator->GetType() == InterpolatorType::CUSTOM || interpolator->GetType() == InterpolatorType::STEPS) {
101         return false;
102     }
103     return true;
104 }
105 } // namespace Rosen
106 } // namespace OHOS
107