1 #ifndef DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H
2 #define DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H
5 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/animation/animation.h>
24 #include <dali/internal/common/buffer-index.h>
25 #include <dali/internal/common/message.h>
26 #include <dali/internal/event/common/event-thread-services.h>
27 #include <dali/internal/event/common/notifier-interface.h>
28 #include <dali/internal/update/animation/scene-graph-animator.h>
37 * Animations are used to change the properties of scene graph objects, as part of a scene
38 * managers "update" phase. An animation is a container of Animator objects; the actual setting
39 * of object values is done by the animators.
41 class Animation : public NotifierInterface
44 using EndAction = Dali::Animation::EndAction;
55 * Construct a new Animation.
56 * @param[in] durationSeconds The duration of the animation in seconds.
57 * @param[in] speedFactor Multiplier to the animation velocity.
58 * @param[in] playRange Minimum and maximum progress between which the animation will play.
59 * @param[in] loopCount The number of times the animation will loop. ( See SetLoopCount() )
60 * @param[in] endAction The action to perform when the animation ends.
61 * @param[in] disconnectAction The action to perform when the property owner of an animator is disconnected.
62 * @return A new Animation
64 static Animation* New(float durationSeconds, float speedFactor, const Vector2& playRange, int32_t loopCount, EndAction endAction, EndAction disconnectAction);
72 * Overriden delete operator
73 * Deletes the animation from its global memory pool
75 void operator delete(void* ptr);
78 * Set the duration of an animation.
79 * @pre durationSeconds must be greater than zero.
80 * @param[in] durationSeconds The duration in seconds.
82 void SetDuration(float durationSeconds);
85 * Set the progress marker to trigger notification
86 * @param[in] progress percent of progress to trigger notification, 0.0f < progress <= 1.0f
88 void SetProgressNotification(float progress);
91 * Retrieve the duration of the animation.
92 * @return The duration in seconds.
94 float GetDuration() const
96 return mDurationSeconds;
100 * Retrieve the current progress of the animation.
101 * @return The current progress as a normalized value between [0,1].
103 float GetCurrentProgress() const
105 if(mDurationSeconds > 0.0f)
107 return mElapsedSeconds / mDurationSeconds;
114 * Sets the progress of the animation.
115 * @param[in] The new progress as a normalized value between [0,1]
117 void SetCurrentProgress(float progress)
119 mElapsedSeconds = mDurationSeconds * progress;
123 * Specifies a speed factor for the animation.
124 * @param[in] factor A value which will multiply the velocity
126 void SetSpeedFactor(float factor)
128 mSpeedFactor = factor;
132 * Set the animation loop count.
133 * 0 is loop forever, N loop play N times
134 * @param[in] loopCount The loop count
136 void SetLoopCount(int32_t loopCount);
139 * Query whether the animation will loop.
140 * @return True if the animation will loop.
142 bool IsLooping() const
144 return mLoopCount != 1;
149 * @return the loop count
151 int32_t GetLoopCount() const
157 * Set the end action of the animation.
158 * @param[in] action The end action.
160 void SetEndAction(EndAction action);
163 * Retrieve the action performed when the animation ends.
164 * @return The end action.
166 EndAction GetEndAction()
172 * Set the disconnect action of the animation when connected objects are disconnected.
173 * This action is performed during the next update when
174 * the connected object is disconnected.
175 * @param[in] action The disconnect action.
177 void SetDisconnectAction(EndAction action);
180 * Retrieve the action performed when the animation is destroyed.
181 * @return The destroy action.
183 EndAction GetDisconnectAction()
185 return mDisconnectAction;
189 * Set the playing range. The animation will only play between the minimum and maximum progress
192 * @param[in] range Two values between [0,1] to specify minimum and maximum progress.
194 void SetPlayRange(const Vector2& range);
197 * @brief Sets the blend point to interpolate animate property
199 * @param[in] blendPoint A value between [0,1], If the value of the keyframe whose progress is 0 is different from the current value,
200 * the property is animated as it smoothly blends until the progress reaches the blendPoint.
201 * @note The blendPoint only affects animation registered with AnimateBetween. Other animations operate the same as when Play() is called.
203 void SetBlendPoint(float blendPoint);
206 * Play the animation.
211 * Play the animation from a given point
212 * @param[in] progress A value between [0,1] form where the animation should start playing
214 void PlayFrom(float progress);
217 * @brief Play the animation after a given delay time.
218 * @param[in] delaySeconds The delay time
220 void PlayAfter(float delaySeconds);
223 * Pause the animation.
228 * Stop the animation.
229 * @param[in] bufferIndex The buffer to update when mEndAction == Bake.
230 * @return True if the animation has finished (otherwise it wasn't playing)
232 bool Stop(BufferIndex bufferIndex);
235 * Called shortly before the animation is destroyed.
236 * @param[in] bufferIndex The buffer to update when mEndAction == Bake.
238 void OnDestroy(BufferIndex bufferIndex);
241 * Query whether the animation is playing, paused or stopped.
242 * Note that even when paused, the Update() method should be called,
243 * since the current progress must be reapplied each frame.
245 State GetState() const
251 * Retrive a count of the number of times the animation has been played to completion.
252 * This can be used to emit "Finised" signals from the public-api
254 int32_t GetPlayedCount() const
260 * Get the current loop count from zero to GetLoopCount().
262 int32_t GetCurrentLoop() const
268 * Query whether the animation is currently active (i.e. at least one of the animators has been updated in either frame)
269 * @return True if the animation is currently active
271 bool IsActive() const
273 // As we have double buffering, if animator is updated in either frame, it needs to be rendered.
274 return mIsActive[0] || mIsActive[1];
278 * @brief Sets the looping mode.
280 * Animation plays forwards and then restarts from the beginning or runs backwards again.
281 * @param[in] loopingMode True when the looping mode is AUTO_REVERSE
283 void SetLoopingMode(bool loopingMode);
286 * Add a newly created animator.
287 * Animators are automatically removed, when orphaned from an animatable scene object.
288 * @param[in] animator The animator to add.
289 * @param[in] propertyOwner The scene-object that owns the animatable property.
290 * @post The animator is owned by this animation.
292 void AddAnimator(OwnerPointer<AnimatorBase>& animator);
295 * This causes the animators to change the properties of objects in the scene graph.
296 * @pre The animation is playing or paused.
297 * @param[in] bufferIndex The buffer to update.
298 * @param[in] elapsedSeconds The time elapsed since the previous frame.
299 * @param[out] looped True if the animation looped
300 * @param[out] finished True if the animation has finished.
301 * @param[out] progressReached True if progress marker reached
303 void Update(BufferIndex bufferIndex, float elapsedSeconds, bool& looped, bool& finished, bool& progressReached);
305 static uint32_t GetMemoryPoolCapacity();
309 * Protected constructor. See New()
311 Animation(float durationSeconds, float speedFactor, const Vector2& playRange, int32_t loopCount, EndAction endAction, EndAction disconnectAction);
315 * Helper for Update, also used to bake when the animation is stopped or destroyed.
316 * @param[in] bufferIndex The buffer to update.
317 * @param[in] bake True if the final result should be baked.
318 * @param[in] animationFinished True if the animation has finished.
320 void UpdateAnimators(BufferIndex bufferIndex, bool bake, bool animationFinished);
323 * Helper function to bake the result of the animation when it is stopped or
325 * @param[in] bufferIndex The buffer to update.
326 * @param[in] action The end action specified.
328 void Bake(BufferIndex bufferIndex, EndAction action);
331 * Helper function to set active state of animators.
332 * @param[in] active Every animator is set to this state
334 void SetAnimatorsActive(bool active);
337 Animation(const Animation&);
340 Animation& operator=(const Animation& rhs);
343 OwnerContainer<AnimatorBase*> mAnimators;
347 float mDurationSeconds;
349 float mElapsedSeconds;
351 float mProgressMarker; // Progress marker to trigger a notification
354 int32_t mPlayedCount; // Incremented at end of animation or completion of all loops
355 // Never incremented when looping forever. Event thread tracks to signal end.
356 int32_t mLoopCount; // N loop setting
357 int32_t mCurrentLoop; // Current loop number
359 EndAction mEndAction;
360 EndAction mDisconnectAction;
364 bool mProgressReachedSignalRequired; // Flag to indicate the progress marker was hit
365 bool mAutoReverseEnabled; // Flag to identify that the looping mode is auto reverse.
366 bool mAnimatorSortRequired; // Flag to whether we need to sort animator or not.
367 bool mIsActive[2]; // Flag to indicate whether the animation is active in the current frame (which is double buffered)
371 }; // namespace SceneGraph
373 // value types used by messages
375 struct ParameterType<Dali::Animation::EndAction> : public BasicType<Dali::Animation::EndAction>
381 // Messages for Animation
383 inline void SetDurationMessage(EventThreadServices& eventThreadServices, const Animation& animation, float durationSeconds)
385 using LocalType = MessageValue1<Animation, float>;
387 // Reserve some memory inside the message queue
388 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
390 // Construct message in the message queue memory; note that delete should not be called on the return value
391 new(slot) LocalType(&animation, &Animation::SetDuration, durationSeconds);
394 inline void SetProgressNotificationMessage(EventThreadServices& eventThreadServices, const Animation& animation, float progress)
396 using LocalType = MessageValue1<Animation, float>;
398 // Reserve some memory inside the message queue
399 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
401 // Construct message in the message queue memory; note that delete should not be called on the return value
402 new(slot) LocalType(&animation, &Animation::SetProgressNotification, progress);
405 inline void SetLoopingMessage(EventThreadServices& eventThreadServices, const Animation& animation, int32_t loopCount)
407 using LocalType = MessageValue1<Animation, int32_t>;
409 // Reserve some memory inside the message queue
410 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
412 // Construct message in the message queue memory; note that delete should not be called on the return value
413 new(slot) LocalType(&animation, &Animation::SetLoopCount, loopCount);
416 inline void SetEndActionMessage(EventThreadServices& eventThreadServices, const Animation& animation, Dali::Animation::EndAction action)
418 using LocalType = MessageValue1<Animation, Dali::Animation::EndAction>;
420 // Reserve some memory inside the message queue
421 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
423 // Construct message in the message queue memory; note that delete should not be called on the return value
424 new(slot) LocalType(&animation, &Animation::SetEndAction, action);
427 inline void SetDisconnectActionMessage(EventThreadServices& eventThreadServices, const Animation& animation, Dali::Animation::EndAction action)
429 using LocalType = MessageValue1<Animation, Dali::Animation::EndAction>;
431 // Reserve some memory inside the message queue
432 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
434 // Construct message in the message queue memory; note that delete should not be called on the return value
435 new(slot) LocalType(&animation, &Animation::SetDisconnectAction, action);
438 inline void SetCurrentProgressMessage(EventThreadServices& eventThreadServices, const Animation& animation, float progress)
440 using LocalType = MessageValue1<Animation, float>;
442 // Reserve some memory inside the message queue
443 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
445 // Construct message in the message queue memory; note that delete should not be called on the return value
446 new(slot) LocalType(&animation, &Animation::SetCurrentProgress, progress);
449 inline void SetSpeedFactorMessage(EventThreadServices& eventThreadServices, const Animation& animation, float factor)
451 using LocalType = MessageValue1<Animation, float>;
453 // Reserve some memory inside the message queue
454 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
456 // Construct message in the message queue memory; note that delete should not be called on the return value
457 new(slot) LocalType(&animation, &Animation::SetSpeedFactor, factor);
460 inline void SetPlayRangeMessage(EventThreadServices& eventThreadServices, const Animation& animation, const Vector2& range)
462 using LocalType = MessageValue1<Animation, Vector2>;
464 // Reserve some memory inside the message queue
465 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
467 // Construct message in the message queue memory; note that delete should not be called on the return value
468 new(slot) LocalType(&animation, &Animation::SetPlayRange, range);
471 inline void SetBlendPointMessage(EventThreadServices& eventThreadServices, const Animation& animation, float blendPoint)
473 using LocalType = MessageValue1<Animation, float>;
475 // Reserve some memory inside the message queue
476 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
478 // Construct message in the message queue memory; note that delete should not be called on the return value
479 new(slot) LocalType(&animation, &Animation::SetBlendPoint, blendPoint);
482 inline void PlayAnimationMessage(EventThreadServices& eventThreadServices, const Animation& animation)
484 using LocalType = Message<Animation>;
486 // Reserve some memory inside the message queue
487 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
489 // Construct message in the message queue memory; note that delete should not be called on the return value
490 new(slot) LocalType(&animation, &Animation::Play);
492 inline void PlayAnimationFromMessage(EventThreadServices& eventThreadServices, const Animation& animation, float progress)
494 using LocalType = MessageValue1<Animation, float>;
496 // Reserve some memory inside the message queue
497 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
499 // Construct message in the message queue memory; note that delete should not be called on the return value
500 new(slot) LocalType(&animation, &Animation::PlayFrom, progress);
503 inline void PauseAnimationMessage(EventThreadServices& eventThreadServices, const Animation& animation)
505 using LocalType = Message<Animation>;
507 // Reserve some memory inside the message queue
508 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
510 // Construct message in the message queue memory; note that delete should not be called on the return value
511 new(slot) LocalType(&animation, &Animation::Pause);
514 inline void AddAnimatorMessage(EventThreadServices& eventThreadServices, const Animation& animation, AnimatorBase& animator)
516 using LocalType = MessageValue1<Animation, OwnerPointer<AnimatorBase> >;
518 // Reserve some memory inside the message queue
519 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
521 // Construct message in the message queue memory; note that delete should not be called on the return value
522 OwnerPointer<AnimatorBase> parameter(&animator);
523 new(slot) LocalType(&animation, &Animation::AddAnimator, parameter);
526 inline void PlayAfterMessage(EventThreadServices& eventThreadServices, const Animation& animation, float delaySeconds)
528 using LocalType = MessageValue1<Animation, float>;
530 // Reserve some memory inside the message queue
531 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
533 // Construct message in the message queue memory; note that delete should not be called on the return value
534 new(slot) LocalType(&animation, &Animation::PlayAfter, delaySeconds);
537 inline void SetLoopingModeMessage(EventThreadServices& eventThreadServices, const Animation& animation, bool loopingMode)
539 using LocalType = MessageValue1<Animation, bool>;
541 // Reserve some memory inside the message queue
542 uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
544 // Construct message in the message queue memory; note that delete should not be called on the return value
545 new(slot) LocalType(&animation, &Animation::SetLoopingMode, loopingMode);
548 } // namespace SceneGraph
550 } // namespace Internal
554 #endif // DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H