1 /*
2 * Copyright (c) 2023 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_curve.h"
17
18 #include "devicestatus_define.h"
19
20 constexpr int32_t CUBIC_PARAM_LIMIT { 4 };
21 constexpr int32_t SPRING_PARAM_LIMIT { 4 };
22 constexpr int32_t INTERPOLATING_SPRING_PARAM_LIMIT { 4 };
23 constexpr int32_t RESPONSE_SPRING_PARAM_LIMIT { 3 };
24 constexpr int32_t STEPS_PARAM_LIMIT { 2 };
25
26 constexpr int32_t ARG_0 { 0 };
27 constexpr int32_t ARG_1 { 1 };
28 constexpr int32_t ARG_2 { 2 };
29 constexpr int32_t ARG_3 { 3 };
30
31 namespace OHOS {
32 namespace Msdp {
33 namespace DeviceStatus {
34 namespace {
35 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "AnimationCurve" };
36 static const RosenCurveType EASE_CURVE = Rosen::RSAnimationTimingCurve::EASE;
37 } // namespace
38
39 std::unordered_map<std::string, RosenCurveType> AnimationCurve::specialCurveMap_ = {
40 { "ease", Rosen::RSAnimationTimingCurve::EASE },
41 { "ease-in", Rosen::RSAnimationTimingCurve::EASE_IN },
42 { "ease-out", Rosen::RSAnimationTimingCurve::EASE_OUT },
43 { "ease-in-out", Rosen::RSAnimationTimingCurve::EASE_IN_OUT },
44 { "linear", Rosen::RSAnimationTimingCurve::LINEAR }
45 };
46
47 std::unordered_map<std::string, AnimationCurve::CurveCreator> AnimationCurve::curveMap_ = {
48 { "cubic-bezier", std::bind(&AnimationCurve::CreateCubicCurve, std::placeholders::_1) },
49 { "spring", std::bind(&AnimationCurve::CreateSpringCurve, std::placeholders::_1) },
50 { "interpolating-spring", std::bind(&AnimationCurve::CreateInterpolatingSpring, std::placeholders::_1) },
51 { "responsive-spring-motion", std::bind(&AnimationCurve::CreateResponseSpring, std::placeholders::_1) },
52 { "steps", std::bind(&AnimationCurve::CreateStepsCurve, std::placeholders::_1) }
53 };
54
CreateCurve(const std::string & curveName,const std::vector<float> & curve)55 RosenCurveType AnimationCurve::CreateCurve(const std::string &curveName, const std::vector<float> &curve)
56 {
57 if (specialCurveMap_.find(curveName) != specialCurveMap_.end()) {
58 return specialCurveMap_[curveName];
59 }
60 if (curveMap_.find(curveName) == curveMap_.end() || curveMap_[curveName] == nullptr) {
61 FI_HILOGE("Unknow curve type, use EASE");
62 return EASE_CURVE;
63 }
64 return curveMap_[curveName](curve);
65 }
66
CreateCubicCurve(const std::vector<float> & curve)67 RosenCurveType AnimationCurve::CreateCubicCurve(const std::vector<float> &curve)
68 {
69 if (curve.size() != CUBIC_PARAM_LIMIT) {
70 FI_HILOGE("Invalid parameter, use EASE");
71 return EASE_CURVE;
72 }
73 return RosenCurveType::CreateCubicCurve(curve[ARG_0], curve[ARG_1], curve[ARG_2], curve[ARG_3]);
74 }
75
CreateSpringCurve(const std::vector<float> & curve)76 RosenCurveType AnimationCurve::CreateSpringCurve(const std::vector<float> &curve)
77 {
78 if (curve.size() != SPRING_PARAM_LIMIT) {
79 FI_HILOGE("Invalid parameter, use EASE");
80 return EASE_CURVE;
81 }
82 return RosenCurveType::CreateSpringCurve(curve[ARG_0], curve[ARG_1], curve[ARG_2], curve[ARG_3]);
83 }
84
CreateInterpolatingSpring(const std::vector<float> & curve)85 RosenCurveType AnimationCurve::CreateInterpolatingSpring(const std::vector<float> &curve)
86 {
87 if (curve.size() != INTERPOLATING_SPRING_PARAM_LIMIT) {
88 FI_HILOGE("Invalid parameter, use EASE");
89 return EASE_CURVE;
90 }
91 return RosenCurveType::CreateInterpolatingSpring(curve[ARG_0], curve[ARG_1], curve[ARG_2], curve[ARG_3]);
92 }
93
CreateResponseSpring(const std::vector<float> & curve)94 RosenCurveType AnimationCurve::CreateResponseSpring(const std::vector<float> &curve)
95 {
96 if (curve.size() != RESPONSE_SPRING_PARAM_LIMIT) {
97 FI_HILOGE("Invalid parameter, use EASE");
98 return EASE_CURVE;
99 }
100 return RosenCurveType::CreateSpring(curve[ARG_0], curve[ARG_1], curve[ARG_2]);
101 }
102
CreateStepsCurve(const std::vector<float> & curve)103 RosenCurveType AnimationCurve::CreateStepsCurve(const std::vector<float> &curve)
104 {
105 if (curve.size() != STEPS_PARAM_LIMIT) {
106 FI_HILOGE("Invalid parameter, use EASE");
107 return EASE_CURVE;
108 }
109 auto steps = static_cast<int32_t>(curve[ARG_0]);
110 auto stepPosition = static_cast<OHOS::Rosen::StepsCurvePosition>(static_cast<int32_t>(curve[ARG_1]));
111 return RosenCurveType::CreateStepsCurve(steps, stepPosition);
112 }
113
114 } // namespace DeviceStatus
115 } // namespace Msdp
116 } // namespace OHOS
117