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_API_H
17 #define META_API_ANIMATION_API_H
18
19 #include <base/containers/type_traits.h>
20
21 #include <meta/interface/animation/builtin_animations.h>
22 #include <meta/interface/animation/intf_animation.h>
23 #include <meta/interface/animation/intf_animation_modifier.h>
24 #include <meta/interface/intf_containable.h>
25 #include <meta/interface/intf_container.h>
26 #include <meta/interface/intf_named.h>
27
28 #include "object_api.h"
29
META_BEGIN_NAMESPACE()30 META_BEGIN_NAMESPACE()
31
32 namespace Internal {
33
34 /**
35 * @brief IAnimation user API property forwarder.
36 */
37 template<class FinalClass, const META_NS::ClassInfo& Class>
38 class AnimationInterfaceAPI : public ObjectInterfaceAPI<FinalClass, Class> {
39 META_INTERFACE_API(AnimationInterfaceAPI)
40 META_API_OBJECT_CONVERTIBLE(META_NS::IAnimation)
41 META_API_OBJECT_CONVERTIBLE(META_NS::IAttachment)
42 META_API_OBJECT_CONVERTIBLE(META_NS::INamed)
43 META_API_CACHE_INTERFACE(META_NS::IAnimation, Animation)
44
45 public:
46 META_API_INTERFACE_PROPERTY_CACHED(Animation, Name, BASE_NS::string)
47 META_API_INTERFACE_PROPERTY_CACHED(Animation, Enabled, bool)
48 META_API_INTERFACE_PROPERTY_CACHED(Animation, Curve, META_NS::ICurve1D::Ptr)
49 META_API_INTERFACE_PROPERTY_CACHED(Animation, Controller, META_NS::IAnimationController::WeakPtr)
50
51 META_API_INTERFACE_READONLY_PROPERTY_CACHED(Animation, TotalDuration, TimeSpan)
52 META_API_INTERFACE_READONLY_PROPERTY_CACHED(Animation, Running, bool)
53 META_API_INTERFACE_READONLY_PROPERTY_CACHED(Animation, Progress, float)
54 META_API_INTERFACE_READONLY_PROPERTY_CACHED(Animation, Valid, bool)
55
56 using Super = ObjectInterfaceAPI<FinalClass, Class>;
57
58 FinalClass& Parent(const META_NS::IStaggeredAnimation::Ptr& parent)
59 {
60 META_API_CACHED_INTERFACE(Animation)->SetParent(parent);
61 return static_cast<FinalClass&>(*this);
62 }
63
64 /**
65 * @brief See META_NS::IAnimation::Step()
66 */
67 void Step(const IClock::ConstPtr& clock)
68 {
69 META_API_CACHED_INTERFACE(Animation)->Step(clock);
70 }
71 /**
72 * @brief See META_NS::IAnimation::Reset()
73 */
74 void Reset()
75 {
76 META_API_CACHED_INTERFACE(Animation)->Stop();
77 }
78
79 Event<IOnChanged> OnStarted()
80 {
81 return META_API_CACHED_INTERFACE(Animation)->OnStarted();
82 }
83
84 Event<IOnChanged> OnFinished()
85 {
86 return META_API_CACHED_INTERFACE(Animation)->OnFinished();
87 }
88
89 /**
90 * @brief Attaches animation modifier to this animation.
91 */
92 FinalClass& Modifier(const IAnimationModifier::Ptr& modifier)
93 {
94 if (auto attachment = interface_pointer_cast<IAttachment>(modifier)) {
95 return static_cast<FinalClass&>(Super::Attach(attachment));
96 }
97
98 return static_cast<FinalClass&>(*this);
99 }
100 };
101
102 /**
103 * @brief IPropertyAnimation user API property forwarder.
104 */
105 template<class FinalClass, const META_NS::ClassInfo& Class>
106 class PropertyAnimationInterfaceAPI : public AnimationInterfaceAPI<FinalClass, Class> {
107 META_INTERFACE_API(PropertyAnimationInterfaceAPI)
108 META_API_OBJECT_CONVERTIBLE(META_NS::IPropertyAnimation)
109 META_API_CACHE_INTERFACE(META_NS::IPropertyAnimation, PropertyAnimation)
110 META_API_OBJECT_CONVERTIBLE(META_NS::ITimedAnimation)
111 META_API_CACHE_INTERFACE(META_NS::ITimedAnimation, TimedAnimation)
112 public:
113 META_API_INTERFACE_PROPERTY_CACHED(TimedAnimation, Duration, TimeSpan)
114
115 inline Meta::Property<Meta::IProperty::WeakPtr> Property() noexcept
116 {
117 return GetPropertyAnimationInterface()->Property();
118 }
119 inline FinalClassType& Property(const Meta::IProperty::WeakPtr& value)
120 {
121 Property()->SetValue(value);
122 return static_cast<FinalClassType&>(*this);
123 }
124 };
125
126 /**
127 * @brief IStartableAnimation user API property forwarder.
128 */
129 template<class FinalClass, const META_NS::ClassInfo& Class>
130 class StartableAnimationInterfaceAPI : public AnimationInterfaceAPI<FinalClass, Class> {
131 META_INTERFACE_API(StartableAnimationInterfaceAPI)
132 META_API_OBJECT_CONVERTIBLE(META_NS::IStartableAnimation)
133 META_API_CACHE_INTERFACE(META_NS::IStartableAnimation, StartableAnimation)
134 public:
135 /**
136 * @brief See META_NS::IStartableAnimation::Pause()
137 */
138 void Pause()
139 {
140 META_API_CACHED_INTERFACE(StartableAnimation)->Pause();
141 }
142 /**
143 * @brief See META_NS::IStartableAnimation::Restart()
144 */
145 void Restart()
146 {
147 META_API_CACHED_INTERFACE(StartableAnimation)->Restart();
148 }
149 /**
150 * @brief See META_NS::IStartableAnimation::Seek()
151 */
152 void Seek(float position)
153 {
154 META_API_CACHED_INTERFACE(StartableAnimation)->Seek(position);
155 }
156 /**
157 * @brief See META_NS::IStartableAnimation::Start()
158 */
159 void Start()
160 {
161 META_API_CACHED_INTERFACE(StartableAnimation)->Start();
162 }
163 /**
164 * @brief See META_NS::IStartableAnimation::Stop()
165 */
166 void Stop()
167 {
168 META_API_CACHED_INTERFACE(StartableAnimation)->Stop();
169 }
170 };
171
172 template<class FinalClass, const META_NS::ClassInfo& Class>
173 class StartablePropertyAnimationInterfaceAPI : public StartableAnimationInterfaceAPI<FinalClass, Class> {
174 META_INTERFACE_API(StartablePropertyAnimationInterfaceAPI)
175 META_API_OBJECT_CONVERTIBLE(META_NS::IPropertyAnimation)
176 META_API_CACHE_INTERFACE(META_NS::IPropertyAnimation, PropertyAnimation)
177 META_API_OBJECT_CONVERTIBLE(META_NS::ITimedAnimation)
178 META_API_CACHE_INTERFACE(META_NS::ITimedAnimation, TimedAnimation)
179 public:
180 inline Meta::Property<Meta::IProperty::WeakPtr> Property() noexcept
181 {
182 return GetPropertyAnimationInterface()->Property();
183 }
184 inline FinalClassType& Property(const Meta::IProperty::WeakPtr& value)
185 {
186 Property()->SetValue(value);
187 return static_cast<FinalClassType&>(*this);
188 }
189 META_API_INTERFACE_PROPERTY_CACHED(TimedAnimation, Duration, TimeSpan)
190 };
191
192 /**
193 * @brief IStaggeredAnimation user API property forwarder.
194 */
195 template<class FinalClass, const META_NS::ClassInfo& Class>
196 class StaggeredAnimationInterfaceAPI : public StartableAnimationInterfaceAPI<FinalClass, Class> {
197 META_INTERFACE_API(StaggeredAnimationInterfaceAPI)
198 META_API_OBJECT_CONVERTIBLE(META_NS::IContainer)
199 META_API_CACHE_INTERFACE(META_NS::IContainer, Container)
200 META_API_OBJECT_CONVERTIBLE(META_NS::IStaggeredAnimation)
201 META_API_CACHE_INTERFACE(META_NS::IStaggeredAnimation, StaggeredAnimation)
202 public:
203 FinalClassType& Child(const META_NS::IAnimation::Ptr& child)
204 {
205 META_API_CACHED_INTERFACE(Container)->Add(child);
206 return static_cast<FinalClassType&>(*this);
207 }
208
209 BASE_NS::vector<META_NS::IAnimation::Ptr> GetAnimations()
210 {
211 return META_API_CACHED_INTERFACE(StaggeredAnimation)->GetAnimations();
212 }
213 };
214
215 } // namespace Internal
216
217 META_END_NAMESPACE()
218
219 #endif // META_API_ANIMATION_API_H
220