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/native_curve_helper.h"
17 
18 #include "core/animation/cubic_curve.h"
19 #include "core/animation/spring_curve.h"
20 
21 namespace OHOS::Ace {
22 
ToNativeCurve(const RefPtr<Curve> & curve)23 Rosen::RSAnimationTimingCurve NativeCurveHelper::ToNativeCurve(const RefPtr<Curve>& curve)
24 {
25     if (AceType::InstanceOf<LinearCurve>(curve)) {
26         return Rosen::RSAnimationTimingCurve::LINEAR;
27     } else if (auto cubicCurve = AceType::DynamicCast<CubicCurve>(curve)) {
28         return Rosen::RSAnimationTimingCurve::CreateCubicCurve(
29             cubicCurve->x0_, cubicCurve->y0_, cubicCurve->x1_, cubicCurve->y1_);
30     } else if (auto springCurve = AceType::DynamicCast<SpringCurve>(curve)) {
31         return Rosen::RSAnimationTimingCurve::CreateSpringCurve(springCurve->velocity_, springCurve->mass_,
32             springCurve->stiffness_, springCurve->damping_);
33     } else if (auto interpolatingSpringCurve = AceType::DynamicCast<InterpolatingSpring>(curve)) {
34         return Rosen::RSAnimationTimingCurve::CreateInterpolatingSpring(interpolatingSpringCurve->GetMass(),
35             interpolatingSpringCurve->GetStiffness(), interpolatingSpringCurve->GetDamping(),
36             interpolatingSpringCurve->GetVelocity(), interpolatingSpringCurve->GetMinimumAmplitudeRatio());
37     } else if (auto customCurve = AceType::DynamicCast<CustomCurve>(curve)) {
38         return Rosen::RSAnimationTimingCurve::CreateCustomCurve(customCurve->interpolateFunc_);
39     } else if (auto springMotionCurve = AceType::DynamicCast<ResponsiveSpringMotion>(curve)) {
40         return Rosen::RSAnimationTimingCurve::CreateSpring(springMotionCurve->GetResponse(),
41             springMotionCurve->GetDampingRatio(), springMotionCurve->GetBlendDuration());
42     } else if (auto stepsCurve = AceType::DynamicCast<StepsCurve>(curve)) {
43         return Rosen::RSAnimationTimingCurve::CreateStepsCurve(stepsCurve->steps_,
44             static_cast<Rosen::StepsCurvePosition>(stepsCurve->position_));
45     } else {
46         return Rosen::RSAnimationTimingCurve::CreateCustomCurve(
47             [weak = WeakPtr<Curve>(curve)](float fraction) -> float {
48                 auto curve = weak.Upgrade();
49                 if (curve == nullptr) {
50                     return 1.0f;
51                 }
52 
53                 return curve->MoveInternal(fraction);
54             });
55     }
56 }
57 
ToNativeMotionPathOption(const MotionPathOption & option,bool pathNeedOrigin)58 Rosen::RSMotionPathOption NativeCurveHelper::ToNativeMotionPathOption(const MotionPathOption& option,
59     bool pathNeedOrigin)
60 {
61     auto motionOption = Rosen::RSMotionPathOption(option.GetPath());
62     motionOption.SetBeginFraction(option.GetBegin());
63     motionOption.SetEndFraction(option.GetEnd());
64     motionOption.SetRotationMode(
65         option.GetRotate() ? Rosen::RotationMode::ROTATE_AUTO : Rosen::RotationMode::ROTATE_NONE);
66     motionOption.SetPathNeedAddOrigin(pathNeedOrigin);
67     return motionOption;
68 }
69 
70 } // namespace OHOS::Ace
71