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 "object.h"
25
META_BEGIN_NAMESPACE()26 META_BEGIN_NAMESPACE()
27
28 namespace Curves {
29 namespace Easing {
30
31 class CubicBezierEasingCurve final : public IntroduceInterfaces<MetaObject, IEasingCurve, ICubicBezier> {
32 META_OBJECT(CubicBezierEasingCurve, ClassId::CubicBezierEasingCurve, IntroduceInterfaces)
33
34 public:
35 bool Build(const IMetadata::Ptr& meta) override;
36 float Transform(float t) const override;
37
38 META_BEGIN_STATIC_DATA()
39 META_STATIC_PROPERTY_DATA(ICubicBezier, BASE_NS::Math::Vec2, ControlPoint1, BASE_NS::Math::Vec2(0, 0))
40 META_STATIC_PROPERTY_DATA(ICubicBezier, BASE_NS::Math::Vec2, ControlPoint2, BASE_NS::Math::Vec2(1, 1))
41 META_END_STATIC_DATA()
42 META_IMPLEMENT_PROPERTY(BASE_NS::Math::Vec2, ControlPoint1)
43 META_IMPLEMENT_PROPERTY(BASE_NS::Math::Vec2, ControlPoint2)
44
45 private:
46 void UpdateCoefficients();
47 float GetLinearX(float t) const noexcept;
48
49 constexpr float GetX(float t) const noexcept
50 {
51 return ((coeff_[0].x * t + coeff_[1].x) * t + coeff_[2].x) * t;
52 }
53 constexpr float GetY(float t) const noexcept
54 {
55 return ((coeff_[0].y * t + coeff_[1].y) * t + coeff_[2].y) * t;
56 }
57 constexpr float GetDX(float t) const noexcept
58 {
59 return (3.f * coeff_[0].x * t + 2.f * coeff_[1].x) * t + coeff_[2].x;
60 }
61
62 BASE_NS::Math::Vec2 coeff_[3];
63 };
64
65 } // namespace Easing
66 } // namespace Curves
67
68 META_END_NAMESPACE()
69
70 #endif // META_SRC_CUBIC_BEZIER_EASING_CURVE_H
71