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_H
17 #define META_SRC_ANIMATION_TRACK_ANIMATION_H
18 
19 #include <meta/base/meta_types.h>
20 
21 #include "animation.h"
22 #include "track_animation_state.h"
23 
META_BEGIN_NAMESPACE()24 META_BEGIN_NAMESPACE()
25 
26 namespace Internal {
27 
28 class TrackAnimation final : public BasePropertyAnimationFwd<TrackAnimation, META_NS::ClassId::TrackAnimation,
29                                  ITimedAnimation, ITrackAnimation, IStartableAnimation, ISerializable> {
30     using Super = BasePropertyAnimationFwd<TrackAnimation, META_NS::ClassId::TrackAnimation, ITimedAnimation,
31         ITrackAnimation, IStartableAnimation, ISerializable>;
32 
33 protected:
34     bool Build(const IMetadata::Ptr& data) override;
35 
36 protected: // IAnimation
37     void Step(const IClock::ConstPtr& clock) override;
38 
39 protected: // ITrackAnimation
40     META_IMPLEMENT_INTERFACE_ARRAY_PROPERTY(ITrackAnimation, float, Timestamps)
41     IProperty::Ptr Keyframes() const override
42     {
43         return keyframes_;
44     }
45     META_IMPLEMENT_INTERFACE_ARRAY_PROPERTY(ITrackAnimation, ICurve1D::Ptr, KeyframeCurves)
46     META_IMPLEMENT_INTERFACE_ARRAY_PROPERTY(ITrackAnimation, IFunction::Ptr, KeyframeHandlers)
47     META_IMPLEMENT_INTERFACE_READONLY_PROPERTY(ITrackAnimation, uint32_t, CurrentKeyframeIndex)
48     size_t AddKeyframe(float timestamp, const IAny::ConstPtr& value) override;
49     bool RemoveKeyframe(size_t index) override;
50     void RemoveAllKeyframes() override;
51 
52 protected: // IStartableAnimation
53     void Pause() override;
54     void Restart() override;
55     void Seek(float position) override;
56     void Start() override;
57     void Stop() override;
58     void Finish() override;
59 
60 public: // IModifier
61     EvaluationResult ProcessOnGet(IAny& value) override;
62 
63     TrackAnimationState& GetState() noexcept override
64     {
65         return state_;
66     }
67 
68 protected: // IAnimationInternal
69     void OnAnimationStateChanged(const IAnimationInternal::AnimationStateChangedInfo& info) override;
70 
71 protected:
72     ReturnError Export(IExportContext&) const override;
73     ReturnError Import(IImportContext&) override;
74     ReturnError Finalize(IImportFunctions&) override;
75 
76 private:
77     AnimationState::AnimationStateParams GetParams() override;
78     void OnPropertyChanged(const TargetProperty& property, const IStackProperty::Ptr& previous) override;
79     void Initialize();
80     void RemoveModifier(const IStackProperty::Ptr& stack);
81 
82     void UpdateValid();
83     void ResetTrack();
84     void UpdateAnimation();
85     void UpdateCurrentTrack(uint32_t index);
86     void UpdateKeyframes();
87 
88 private:
89     void Evaluate() override;
90     IProperty::Ptr keyframes_; // Array property containing the keyframes
91 
92     TrackAnimationState state_;
93 };
94 
95 } // namespace Internal
96 
97 META_END_NAMESPACE()
98 
99 #endif // META_SRC_ANIMATION_TRACK_ANIMATION_H
100