1 /*
2  * Copyright (c) 2024 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 #ifndef META_SRC_CUBIC_BEZIER_EASING_CURVE_H
16 #define META_SRC_CUBIC_BEZIER_EASING_CURVE_H
17 
18 #include <meta/base/namespace.h>
19 #include <meta/interface/animation/builtin_animations.h>
20 #include <meta/interface/builtin_objects.h>
21 #include <meta/interface/curves/intf_bezier.h>
22 #include <meta/interface/curves/intf_easing_curve.h>
23 
24 #include "../meta_object.h"
25 
META_BEGIN_NAMESPACE()26 META_BEGIN_NAMESPACE()
27 
28 namespace Curves {
29 namespace Easing {
30 
31 class CubicBezierEasingCurve final : public META_NS::Internal::MetaObjectFwd<CubicBezierEasingCurve,
32                                          ClassId::CubicBezierEasingCurve, IEasingCurve, ICubicBezier> {
33     using Super = META_NS::Internal::MetaObjectFwd<CubicBezierEasingCurve, ClassId::CubicBezierEasingCurve,
34         IEasingCurve, ICubicBezier>;
35     META_IMPLEMENT_INTERFACE_PROPERTY(ICubicBezier, BASE_NS::Math::Vec2, ControlPoint1, BASE_NS::Math::Vec2(0, 0))
36     META_IMPLEMENT_INTERFACE_PROPERTY(ICubicBezier, BASE_NS::Math::Vec2, ControlPoint2, BASE_NS::Math::Vec2(1, 1))
37 
38 public:
39     bool Build(const IMetadata::Ptr& meta) override;
40     float Transform(float t) const override;
41 
42 private:
43     void UpdateCoefficients();
44     float GetLinearX(float t) const noexcept;
45 
46     constexpr float GetX(float t) const noexcept
47     {
48         return ((coeff_[0].x * t + coeff_[1].x) * t + coeff_[2].x) * t;
49     }
50     constexpr float GetY(float t) const noexcept
51     {
52         return ((coeff_[0].y * t + coeff_[1].y) * t + coeff_[2].y) * t;
53     }
54     constexpr float GetDX(float t) const noexcept
55     {
56         return (3.f * coeff_[0].x * t + 2.f * coeff_[1].x) * t + coeff_[2].x;
57     }
58 
59     BASE_NS::Math::Vec2 coeff_[3];
60 };
61 
62 } // namespace Easing
63 } // namespace Curves
64 
65 META_END_NAMESPACE()
66 
67 #endif // META_SRC_CUBIC_BEZIER_EASING_CURVE_H
68