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 springMotionCurve->GetMinimumAmplitudeRatio());
43 } else if (auto stepsCurve = AceType::DynamicCast<StepsCurve>(curve)) {
44 return Rosen::RSAnimationTimingCurve::CreateStepsCurve(stepsCurve->steps_,
45 static_cast<Rosen::StepsCurvePosition>(stepsCurve->position_));
46 } else {
47 return Rosen::RSAnimationTimingCurve::CreateCustomCurve(
48 [weak = WeakPtr<Curve>(curve)](float fraction) -> float {
49 auto curve = weak.Upgrade();
50 if (curve == nullptr) {
51 return 1.0f;
52 }
53
54 return curve->MoveInternal(fraction);
55 });
56 }
57 }
58
ToNativeMotionPathOption(const MotionPathOption & option,bool pathNeedOrigin)59 Rosen::RSMotionPathOption NativeCurveHelper::ToNativeMotionPathOption(const MotionPathOption& option,
60 bool pathNeedOrigin)
61 {
62 auto motionOption = Rosen::RSMotionPathOption(option.GetPath());
63 motionOption.SetBeginFraction(option.GetBegin());
64 motionOption.SetEndFraction(option.GetEnd());
65 motionOption.SetRotationMode(
66 option.GetRotate() ? Rosen::RotationMode::ROTATE_AUTO : Rosen::RotationMode::ROTATE_NONE);
67 motionOption.SetPathNeedAddOrigin(pathNeedOrigin);
68 return motionOption;
69 }
70
71 } // namespace OHOS::Ace
72