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_API_ANIMATION_H 17 #define META_API_ANIMATION_H 18 19 #include <meta/api/curves/easing_curve.h> 20 #include <meta/api/internal/animation_api.h> 21 22 META_BEGIN_NAMESPACE() 23 24 /** 25 * @brief The SequentialAnimation class provides a wrapper for ISequentialAnimation. 26 */ 27 class SequentialAnimation final 28 : public Internal::StaggeredAnimationInterfaceAPI<SequentialAnimation, META_NS::ClassId::SequentialAnimation> { 29 META_API(SequentialAnimation) 30 }; 31 32 /** 33 * @brief The ParallelAnimation class provides a wrapper for IParallelAnimation. 34 */ 35 class ParallelAnimation final 36 : public Internal::StaggeredAnimationInterfaceAPI<ParallelAnimation, META_NS::ClassId::ParallelAnimation> { 37 META_API(ParallelAnimation) 38 }; 39 40 /** 41 * @brief The PropertyAnimation class provides a wrapper for IPropertyAnimation. 42 */ 43 class PropertyAnimation final 44 : public Internal::PropertyAnimationInterfaceAPI<PropertyAnimation, META_NS::ClassId::PropertyAnimation> { 45 META_API(PropertyAnimation) 46 }; 47 48 /** 49 * @brief The KeyframeAnimation<T> class provides a wrapper for IKeyframeAnimation, 50 * with automatic typing for From and To property. 51 */ 52 template<class Type> 53 class KeyframeAnimation final : public Internal::StartablePropertyAnimationInterfaceAPI<KeyframeAnimation<Type>, 54 META_NS::ClassId::KeyframeAnimation> { 55 META_API(KeyframeAnimation<Type>) 56 META_API_OBJECT_CONVERTIBLE(META_NS::IKeyframeAnimation) 57 META_API_CACHE_INTERFACE(META_NS::IKeyframeAnimation, KeyframeAnimation) 58 public: 59 /** Creates the contained object */ Create()60 static META_NS::IObject::Ptr Create() 61 { 62 auto object = META_NS::GetObjectRegistry().Create(META_NS::ClassId::KeyframeAnimation); 63 return object; 64 } 65 META_API_INTERFACE_PROPERTY_CACHED(KeyframeAnimation, From, IAny::Ptr) 66 META_API_INTERFACE_PROPERTY_CACHED(KeyframeAnimation, To, IAny::Ptr) 67 /** Get the starting value of the animation */ From(const Type& value)68 auto& From(const Type& value) 69 { 70 if (auto p = META_API_CACHED_INTERFACE(KeyframeAnimation)->From()) { 71 if (auto pv = p->GetValue()) { // IAny::Ptr 72 pv->SetValue(value); 73 } else { 74 p->SetValue(IAny::Ptr { new Any<Type>(value) }); 75 } 76 } 77 return *this; 78 } 79 /** Set the target value of the animation */ To(const Type& value)80 auto& To(const Type& value) 81 { 82 if (auto p = META_API_CACHED_INTERFACE(KeyframeAnimation)->To()) { 83 if (auto pv = p->GetValue()) { // IAny::Ptr 84 pv->SetValue(value); 85 } else { 86 p->SetValue(IAny::Ptr { new Any<Type>(value) }); 87 } 88 } 89 return *this; 90 } 91 }; 92 93 /** 94 * @brief The TrackAnimation<T> class provides a wrapper for ITrackAnimation, 95 * with automatic typing for key-frames. 96 */ 97 template<class Type> 98 class TrackAnimation final 99 : public Internal::StartablePropertyAnimationInterfaceAPI<TrackAnimation<Type>, META_NS::ClassId::TrackAnimation> { 100 META_API(TrackAnimation<Type>) 101 META_API_OBJECT_CONVERTIBLE(META_NS::ITrackAnimation) 102 META_API_CACHE_INTERFACE(META_NS::ITrackAnimation, TrackAnimation) 103 public: 104 /** Creates the contained object */ Create()105 static META_NS::IObject::Ptr Create() 106 { 107 const auto object = META_NS::GetObjectRegistry().Create(META_NS::ClassId::TrackAnimation); 108 if (auto track = interface_cast<META_NS::ITrackAnimation>(object)) { 109 if (auto internal = interface_cast<META_NS::IPropertyInternalAny>(track->Keyframes())) { 110 internal->SetInternalAny(ArrayAny<Type>({}).Clone(false)); 111 } 112 } 113 return object; 114 } 115 META_API_INTERFACE_ARRAY_PROPERTY_CACHED(TrackAnimation, KeyframeCurves, META_NS::ICurve1D::Ptr) 116 META_API_INTERFACE_READONLY_PROPERTY_CACHED(TrackAnimation, CurrentKeyframeIndex, uint32_t) 117 /** Get the track timestamps */ Timestamps()118 META_NS::ArrayProperty<float> Timestamps() 119 { 120 return META_API_CACHED_INTERFACE(TrackAnimation)->Timestamps(); 121 } 122 /** Set the track timestamps */ Timestamps(BASE_NS::array_view<float> timestamps)123 auto& Timestamps(BASE_NS::array_view<float> timestamps) 124 { 125 Timestamps()->SetValue(BASE_NS::move(timestamps)); 126 return *this; 127 } 128 /** Get the track key-frames */ Keyframes()129 META_NS::ArrayProperty<Type> Keyframes() 130 { 131 return META_NS::ArrayProperty<Type>(META_API_CACHED_INTERFACE(TrackAnimation)->Keyframes()); 132 } 133 /** Set the track key-frames */ Keyframes(BASE_NS::array_view<Type> keyframes)134 auto& Keyframes(BASE_NS::array_view<Type> keyframes) 135 { 136 if (auto kf = Keyframes()) { 137 kf->SetValue(BASE_NS::move(keyframes)); 138 } 139 return *this; 140 } 141 /** Get the track timestamps */ KeyframeHandlers()142 META_NS::ArrayProperty<IFunction::Ptr> KeyframeHandlers() 143 { 144 return META_API_CACHED_INTERFACE(TrackAnimation)->KeyframeHandlers(); 145 } KeyframeHandlers(BASE_NS::array_view<IFunction::Ptr> handlers)146 auto& KeyframeHandlers(BASE_NS::array_view<IFunction::Ptr> handlers) 147 { 148 KeyframeHandlers()->SetValue(BASE_NS::move(handlers)); 149 return *this; 150 } AddKeyframe(float timestamp, const IAny::ConstPtr& value)151 size_t AddKeyframe(float timestamp, const IAny::ConstPtr& value) 152 { 153 return META_API_CACHED_INTERFACE(TrackAnimation)->AddKeyframe(timestamp, value); 154 } RemoveKeyframe(size_t index)155 bool RemoveKeyframe(size_t index) 156 { 157 return META_API_CACHED_INTERFACE(TrackAnimation)->RemoveKeyframe(index); 158 } RemoveAllKeyframes()159 void RemoveAllKeyframes() 160 { 161 return META_API_CACHED_INTERFACE(TrackAnimation)->RemoveAllKeyframes(); 162 } 163 }; 164 165 META_END_NAMESPACE() 166 167 #endif // META_API_ANIMATION_H 168