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 
16 #ifndef META_SRC_ANIMATION_TRACK_ANIMATION_STATE_H
17 #define META_SRC_ANIMATION_TRACK_ANIMATION_STATE_H
18 
19 #include <base/containers/unordered_map.h>
20 
21 #include <meta/base/meta_types.h>
22 #include <meta/interface/animation/intf_animation.h>
23 #include <meta/interface/property/array_property.h>
24 
25 #include "animation_state.h"
26 
META_BEGIN_NAMESPACE()27 META_BEGIN_NAMESPACE()
28 
29 namespace Internal {
30 
31 class TrackAnimationState final : public PropertyAnimationState {
32 public:
33     struct TrackDataParams {
34         ArrayProperty<float> timestamps;
35     };
36     /**
37      * @brief Set TrackAnimationState parameters
38      */
39     void SetTrackDataParams(TrackDataParams&& params);
40     /**
41      * @brief Updates the validity of the track animation.
42      * @return True if the animation is in valid state, false otherwise.
43      */
44     bool UpdateValid();
45     /** Return latest evaluated value */
46     const IAny::Ptr& GetCurrentValue() const noexcept
47     {
48         return currentValue_;
49     }
50     /** Return value of the start of current track */
51     const IAny::Ptr& GetCurrentTrackStart() const noexcept
52     {
53         return trackStart_;
54     }
55     /** Return value of the end of current track */
56     const IAny::Ptr& GetCurrentTrackEnd() const noexcept
57     {
58         return trackEnd_;
59     }
60     /** Return TypeId of each keyframe */
61     TypeId GetKeyframeItemTypeId() const noexcept
62     {
63         return keyframeArray_ ? keyframeArray_->GetTypeId(TypeIdRole::ITEM) : TypeId {};
64     }
65     /**
66      * @brief Updates keyframe index based on given progress.
67      * @param progress The animation progress to update to.
68      * @return Index and track progress at given animation progress.
69      */
70     BASE_NS::pair<uint32_t, float> UpdateIndex(float progress);
71     /**
72      * @brief Jump to given keyframe
73      * @param index Index to jump to.
74      * @return Current index of the track animation after jump.
75      */
76     uint32_t JumpTo(size_t index);
77     /** Add a keyframe at given index */
78     size_t AddKeyframe(float timestamp, const IAny::ConstPtr& value);
79     /** Remove a keyframe at given index */
80     bool RemoveKeyframe(size_t index);
81     /** Resets the current track */
82     void ResetCurrentTrack();
83     /**  Updates the keyframe array containing the keyframes of the animation */
84     bool SetKeyframes(const IArrayAny::Ptr& keyframes);
85 
86 private:
87     void Reset();
88     bool ValidateValues();
89     float GetCurrentTrackProgress(float progress) const noexcept;
90     bool IsInCurrentRange(float progress) const noexcept;
91 
92     IArrayAny::Ptr keyframeArray_; // Keyframe values
93     IAny::Ptr currentValue_;       // Latest evaluated value (between trackStart_ and trackEnd_)
94     IAny::Ptr trackStart_;         // Current keyframe value
95     IAny::Ptr trackEnd_;           // Next keyframe value
96     float startProgress_ {};       // First timestamp
97     float endProgress_ {};         // Last timestamp
98     float currentRangeStartTs_ {}; // Timestamp of the current keyframe
99     float currentRangeEndTs_ {};   // Timestamp of the next keyframe
100     size_t currentIndex_ { ITrackAnimation::INVALID_INDEX };
101 
102     ArrayProperty<float>& GetTimeStamps() noexcept
103     {
104         return trackParams_.timestamps;
105     }
106     TrackDataParams trackParams_;
107 };
108 
109 } // namespace Internal
110 
111 META_END_NAMESPACE()
112 
113 #endif // META_SRC_ANIMATION_TRACK_ANIMATION_STATE_H
114