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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_KEYFRAME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_KEYFRAME_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/animation/curve.h"
21 
22 namespace OHOS::Ace {
23 
24 template<typename T>
25 class Keyframe : public AceType {
26 public:
27     // keyTime is normalized time.
Keyframe(float keyTime,const T & keyValue)28     Keyframe(float keyTime, const T& keyValue) : keyTime_(keyTime), keyValue_(keyValue) {}
29 
30     ~Keyframe() override = default;
31 
IsValid()32     bool IsValid() const
33     {
34         if (keyTime_ < NORMALIZED_DURATION_MIN || keyTime_ > NORMALIZED_DURATION_MAX) {
35             LOGE("invalid normalized key time: %{public}f", keyTime_);
36             return false;
37         }
38         return true;
39     }
40 
GetKeyTime()41     float GetKeyTime() const
42     {
43         return keyTime_;
44     }
45 
SetKeyValue(const T & keyValue)46     void SetKeyValue(const T& keyValue)
47     {
48         keyValue_ = keyValue;
49     }
50 
GetKeyValue()51     const T& GetKeyValue() const
52     {
53         return keyValue_;
54     }
55 
SetCurve(const RefPtr<Curve> & curve)56     void SetCurve(const RefPtr<Curve>& curve)
57     {
58         if (!curve) {
59             LOGE("set curve failed, input curve is null.");
60             return;
61         }
62         curve_ = curve;
63     }
64 
GetCurve()65     const RefPtr<Curve>& GetCurve() const
66     {
67         return curve_;
68     }
69 
70 private:
71     const float keyTime_ { 0.0f };
72     T keyValue_;
73     RefPtr<Curve> curve_ { Curves::EASE };
74 };
75 
76 } // namespace OHOS::Ace
77 
78 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_KEYFRAME_H
79