• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      * @param progress Current progress
75      * @return Current index of the track animation after jump.
76      */
77     uint32_t JumpTo(size_t index, float progress);
78     /** Add a keyframe at given index */
79     size_t AddKeyframe(float timestamp, const IAny::ConstPtr& value);
80     /** Remove a keyframe at given index */
81     bool RemoveKeyframe(size_t index);
82     /** Resets the current track */
83     void ResetCurrentTrack();
84     /**  Updates the keyframe array containing the keyframes of the animation */
85     bool SetKeyframes(const IArrayAny::Ptr& keyframes);
86 
87 private:
88     void Reset();
89     bool ValidateValues();
90     float GetCurrentTrackProgress(float progress) const noexcept;
91     bool IsInCurrentRange(float progress) const noexcept;
92     void SetPrePostFrameValues(float progress);
93 
94 private:
95     IArrayAny::Ptr keyframeArray_; // Keyframe values
96     IAny::Ptr currentValue_;       // Latest evaluated value (between trackStart_ and trackEnd_)
97     IAny::Ptr trackStart_;         // Current keyframe value
98     IAny::Ptr trackEnd_;           // Next keyframe value
99     float startProgress_ {};       // First timestamp
100     float endProgress_ {};         // Last timestamp
101     float currentRangeStartTs_ {}; // Timestamp of the current keyframe
102     float currentRangeEndTs_ {};   // Timestamp of the next keyframe
103     size_t currentIndex_ { ITrackAnimation::INVALID_INDEX };
104 
105     ArrayProperty<float>& GetTimeStamps() noexcept
106     {
107         return trackParams_.timestamps;
108     }
109     TrackDataParams trackParams_;
110 };
111 
112 } // namespace Internal
113 
114 META_END_NAMESPACE()
115 
116 #endif // META_SRC_ANIMATION_TRACK_ANIMATION_STATE_H
117