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