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 customCurve = AceType::DynamicCast<CustomCurve>(curve)) {
34 return Rosen::RSAnimationTimingCurve::CreateCustomCurve(customCurve->interpolateFunc_);
35 } else if (auto springMotionCurve = AceType::DynamicCast<ResponsiveSpringMotion>(curve)) {
36 return Rosen::RSAnimationTimingCurve::CreateSpring(springMotionCurve->GetResponse(),
37 springMotionCurve->GetDampingRatio(), springMotionCurve->GetBlendDuration());
38 } else if (auto stepsCurve = AceType::DynamicCast<StepsCurve>(curve)) {
39 return Rosen::RSAnimationTimingCurve::CreateStepsCurve(stepsCurve->steps_,
40 static_cast<Rosen::StepsCurvePosition>(stepsCurve->position_));
41 } else {
42 return Rosen::RSAnimationTimingCurve::CreateCustomCurve(
43 [weak = WeakPtr<Curve>(curve)](float fraction) -> float {
44 auto curve = weak.Upgrade();
45 if (curve == nullptr) {
46 LOGE("transform to native curve failed, curve is null!");
47 return 1.0f;
48 }
49
50 return curve->MoveInternal(fraction);
51 });
52 }
53 }
54
ToNativeMotionPathOption(const MotionPathOption & option,bool pathNeedOrigin)55 Rosen::RSMotionPathOption NativeCurveHelper::ToNativeMotionPathOption(const MotionPathOption& option,
56 bool pathNeedOrigin)
57 {
58 auto motionOption = Rosen::RSMotionPathOption(option.GetPath());
59 motionOption.SetBeginFraction(option.GetBegin());
60 motionOption.SetEndFraction(option.GetEnd());
61 motionOption.SetRotationMode(
62 option.GetRotate() ? Rosen::RotationMode::ROTATE_AUTO : Rosen::RotationMode::ROTATE_NONE);
63 motionOption.SetPathNeedAddOrigin(pathNeedOrigin);
64 return motionOption;
65 }
66
67 } // namespace OHOS::Ace
68