1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci#include "animation_curve.h"
17f857971dSopenharmony_ci
18f857971dSopenharmony_ci#include "devicestatus_define.h"
19f857971dSopenharmony_ci
20f857971dSopenharmony_ci#undef LOG_TAG
21f857971dSopenharmony_ci#define LOG_TAG "AnimationCurve"
22f857971dSopenharmony_ci
23f857971dSopenharmony_ciconstexpr int32_t CUBIC_PARAM_LIMIT { 4 };
24f857971dSopenharmony_ciconstexpr int32_t SPRING_PARAM_LIMIT { 4 };
25f857971dSopenharmony_ciconstexpr int32_t INTERPOLATING_SPRING_PARAM_LIMIT { 4 };
26f857971dSopenharmony_ciconstexpr int32_t RESPONSE_SPRING_PARAM_LIMIT { 3 };
27f857971dSopenharmony_ciconstexpr int32_t STEPS_PARAM_LIMIT { 2 };
28f857971dSopenharmony_ci
29f857971dSopenharmony_ciconstexpr int32_t ARG_0 { 0 };
30f857971dSopenharmony_ciconstexpr int32_t ARG_1 { 1 };
31f857971dSopenharmony_ciconstexpr int32_t ARG_2 { 2 };
32f857971dSopenharmony_ciconstexpr int32_t ARG_3 { 3 };
33f857971dSopenharmony_ci
34f857971dSopenharmony_cinamespace OHOS {
35f857971dSopenharmony_cinamespace Msdp {
36f857971dSopenharmony_cinamespace DeviceStatus {
37f857971dSopenharmony_cinamespace {
38f857971dSopenharmony_cistatic const RosenCurveType EASE_CURVE = Rosen::RSAnimationTimingCurve::EASE;
39f857971dSopenharmony_ci} // namespace
40f857971dSopenharmony_ci
41f857971dSopenharmony_cistd::unordered_map<std::string, RosenCurveType> AnimationCurve::specialCurveMap_ = {
42f857971dSopenharmony_ci    { "ease", Rosen::RSAnimationTimingCurve::EASE },
43f857971dSopenharmony_ci    { "ease-in", Rosen::RSAnimationTimingCurve::EASE_IN },
44f857971dSopenharmony_ci    { "ease-out", Rosen::RSAnimationTimingCurve::EASE_OUT },
45f857971dSopenharmony_ci    { "ease-in-out", Rosen::RSAnimationTimingCurve::EASE_IN_OUT },
46f857971dSopenharmony_ci    { "linear", Rosen::RSAnimationTimingCurve::LINEAR }
47f857971dSopenharmony_ci};
48f857971dSopenharmony_ci
49f857971dSopenharmony_cistd::unordered_map<std::string, AnimationCurve::CurveCreator> AnimationCurve::curveMap_ = {
50f857971dSopenharmony_ci    { "cubic-bezier", [](const std::vector<float> &curve) {
51f857971dSopenharmony_ci        return AnimationCurve::CreateCubicCurve(curve);
52f857971dSopenharmony_ci    } },
53f857971dSopenharmony_ci    { "spring", [](const std::vector<float> &curve) {
54f857971dSopenharmony_ci        return AnimationCurve::CreateSpringCurve(curve);
55f857971dSopenharmony_ci    } },
56f857971dSopenharmony_ci    { "interpolating-spring", [](const std::vector<float> &curve) {
57f857971dSopenharmony_ci        return AnimationCurve::CreateInterpolatingSpring(curve);
58f857971dSopenharmony_ci    } },
59f857971dSopenharmony_ci    { "responsive-spring-motion", [](const std::vector<float> &curve) {
60f857971dSopenharmony_ci        return AnimationCurve::CreateResponseSpring(curve);
61f857971dSopenharmony_ci    } },
62f857971dSopenharmony_ci    { "steps", [](const std::vector<float> &curve) {
63f857971dSopenharmony_ci        return AnimationCurve::CreateStepsCurve(curve);
64f857971dSopenharmony_ci    } }
65f857971dSopenharmony_ci};
66f857971dSopenharmony_ci
67f857971dSopenharmony_ciRosenCurveType AnimationCurve::CreateCurve(const std::string &curveName, const std::vector<float> &curve)
68f857971dSopenharmony_ci{
69f857971dSopenharmony_ci    if (specialCurveMap_.find(curveName) != specialCurveMap_.end()) {
70f857971dSopenharmony_ci        return specialCurveMap_[curveName];
71f857971dSopenharmony_ci    }
72f857971dSopenharmony_ci    if (curveMap_.find(curveName) == curveMap_.end() || curveMap_[curveName] == nullptr) {
73f857971dSopenharmony_ci        FI_HILOGE("Unknow curve type, use EASE");
74f857971dSopenharmony_ci        return EASE_CURVE;
75f857971dSopenharmony_ci    }
76f857971dSopenharmony_ci    return curveMap_[curveName](curve);
77f857971dSopenharmony_ci}
78f857971dSopenharmony_ci
79f857971dSopenharmony_ciRosenCurveType AnimationCurve::CreateCubicCurve(const std::vector<float> &curve)
80f857971dSopenharmony_ci{
81f857971dSopenharmony_ci    if (curve.size() != CUBIC_PARAM_LIMIT) {
82f857971dSopenharmony_ci        FI_HILOGE("Invalid parameter, use EASE");
83f857971dSopenharmony_ci        return EASE_CURVE;
84f857971dSopenharmony_ci    }
85f857971dSopenharmony_ci    return RosenCurveType::CreateCubicCurve(curve[ARG_0], curve[ARG_1], curve[ARG_2], curve[ARG_3]);
86f857971dSopenharmony_ci}
87f857971dSopenharmony_ci
88f857971dSopenharmony_ciRosenCurveType AnimationCurve::CreateSpringCurve(const std::vector<float> &curve)
89f857971dSopenharmony_ci{
90f857971dSopenharmony_ci    if (curve.size() != SPRING_PARAM_LIMIT) {
91f857971dSopenharmony_ci        FI_HILOGE("Invalid parameter, use EASE");
92f857971dSopenharmony_ci        return EASE_CURVE;
93f857971dSopenharmony_ci    }
94f857971dSopenharmony_ci    return RosenCurveType::CreateSpringCurve(curve[ARG_0], curve[ARG_1], curve[ARG_2], curve[ARG_3]);
95f857971dSopenharmony_ci}
96f857971dSopenharmony_ci
97f857971dSopenharmony_ciRosenCurveType AnimationCurve::CreateInterpolatingSpring(const std::vector<float> &curve)
98f857971dSopenharmony_ci{
99f857971dSopenharmony_ci    if (curve.size() != INTERPOLATING_SPRING_PARAM_LIMIT) {
100f857971dSopenharmony_ci        FI_HILOGE("Invalid parameter, use EASE");
101f857971dSopenharmony_ci        return EASE_CURVE;
102f857971dSopenharmony_ci    }
103f857971dSopenharmony_ci    return RosenCurveType::CreateInterpolatingSpring(curve[ARG_0], curve[ARG_1], curve[ARG_2], curve[ARG_3]);
104f857971dSopenharmony_ci}
105f857971dSopenharmony_ci
106f857971dSopenharmony_ciRosenCurveType AnimationCurve::CreateResponseSpring(const std::vector<float> &curve)
107f857971dSopenharmony_ci{
108f857971dSopenharmony_ci    if (curve.size() != RESPONSE_SPRING_PARAM_LIMIT) {
109f857971dSopenharmony_ci        FI_HILOGE("Invalid parameter, use EASE");
110f857971dSopenharmony_ci        return EASE_CURVE;
111f857971dSopenharmony_ci    }
112f857971dSopenharmony_ci    return RosenCurveType::CreateSpring(curve[ARG_0], curve[ARG_1], curve[ARG_2]);
113f857971dSopenharmony_ci}
114f857971dSopenharmony_ci
115f857971dSopenharmony_ciRosenCurveType AnimationCurve::CreateStepsCurve(const std::vector<float> &curve)
116f857971dSopenharmony_ci{
117f857971dSopenharmony_ci    if (curve.size() != STEPS_PARAM_LIMIT) {
118f857971dSopenharmony_ci        FI_HILOGE("Invalid parameter, use EASE");
119f857971dSopenharmony_ci        return EASE_CURVE;
120f857971dSopenharmony_ci    }
121f857971dSopenharmony_ci    auto steps = static_cast<int32_t>(curve[ARG_0]);
122f857971dSopenharmony_ci    auto stepPosition = static_cast<OHOS::Rosen::StepsCurvePosition>(static_cast<int32_t>(curve[ARG_1]));
123f857971dSopenharmony_ci    return RosenCurveType::CreateStepsCurve(steps, stepPosition);
124f857971dSopenharmony_ci}
125f857971dSopenharmony_ci
126f857971dSopenharmony_ci} // namespace DeviceStatus
127f857971dSopenharmony_ci} // namespace Msdp
128f857971dSopenharmony_ci} // namespace OHOS
129