1 #ifndef DALI_INTERNAL_ANIMATION_H
2 #define DALI_INTERNAL_ANIMATION_H
5 * Copyright (c) 2019 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/common/vector-wrapper.h>
23 #include <dali/public-api/object/ref-object.h>
24 #include <dali/public-api/animation/animation.h>
25 #include <dali/public-api/object/base-object.h>
26 #include <dali/devel-api/animation/animation-devel.h>
27 #include <dali/devel-api/common/owner-container.h>
28 #include <dali/internal/event/animation/key-frames-impl.h>
29 #include <dali/internal/event/common/event-thread-services.h>
45 class AnimationPlaylist;
46 class AnimatorConnectorBase;
50 using AnimationPtr = IntrusivePtr<Animation>;
51 using AnimationContainer = std::vector<AnimationPtr>;
53 using AnimationIter = AnimationContainer::iterator;
54 using AnimationConstIter = AnimationContainer::const_iterator;
57 * Animation is a proxy for a SceneGraph::Animation object.
58 * The UpdateManager owns the Animation object, but the lifetime of the animation is
59 * indirectly controlled by the Animation.
61 class Animation : public BaseObject
67 TO, ///< Animating TO the given value
68 BY, ///< Animating BY the given value
69 BETWEEN ///< Animating BETWEEN key-frames
72 using EndAction = Dali::Animation::EndAction;
73 using Interpolation = Dali::Animation::Interpolation;
76 * Create a new Animation object.
77 * @param[in] durationSeconds The duration of the animation.
78 * @return A smart-pointer to the newly allocated Animation.
80 static AnimationPtr New(float durationSeconds);
83 * @copydoc Dali::Animation::SetDuration()
85 void SetDuration(float seconds);
88 * @copydoc Dali::DevelAnimation::SetProgressNotification()
90 void SetProgressNotification( float progress );
93 * @copydoc Dali::DevelAnimation::GetProgressNotification()
95 float GetProgressNotification();
98 * @copydoc Dali::Animation::GetDuration()
100 float GetDuration() const;
103 * @copydoc Dali::Animation::SetLooping()
105 void SetLooping(bool on);
108 * @copydoc Dali::Animation::SetLoopCount()
110 void SetLoopCount(int32_t count);
113 * @copydoc Dali::Animation::GetLoopCount()
115 int32_t GetLoopCount();
118 * @copydoc Dali::Animation::GetCurrentLoop()
120 int32_t GetCurrentLoop();
123 * @copydoc Dali::Animation::IsLooping()
125 bool IsLooping() const;
128 * @copydoc Dali::Animation::SetEndAction()
130 void SetEndAction(EndAction action);
133 * @copydoc Dali::Animation::GetEndAction()
135 EndAction GetEndAction() const;
138 * @copydoc Dali::Animation::SetDisconnectAction()
140 void SetDisconnectAction(EndAction action);
143 * @copydoc Dali::Animation::GetDisconnectAction()
145 EndAction GetDisconnectAction() const;
148 * @copydoc Dali::Animation::SetDefaultAlphaFunction()
150 void SetDefaultAlphaFunction(AlphaFunction alpha)
152 mDefaultAlpha = alpha;
156 * @copydoc Dali::Animation::GetDefaultAlphaFunction()
158 AlphaFunction GetDefaultAlphaFunction() const
160 return mDefaultAlpha;
164 * @copydoc Dali::Animation::Play()
169 * @copydoc Dali::Animation::PlayFrom()
171 void PlayFrom( float progress );
174 * @copydoc Dali::Animation::PlayAfter()
176 void PlayAfter( float delaySeconds );
179 * @copydoc Dali::Animation::Pause()
184 * @copydoc Dali::Animation::GetState()
186 Dali::Animation::State GetState() const;
189 * @copydoc Dali::Animation::Stop()
194 * @copydoc Dali::Animation::Clear()
199 * Query whether a Finished signal should be emitted for this animation.
200 * This should only be called by NotificationManager, before signals are emitted.
201 * @post HasFinished() will return false on subsequent calls, until the animation is replayed to completion.
206 * @copydoc Dali::Animation::FinishedSignal()
208 Dali::Animation::AnimationSignalType& FinishedSignal();
211 * @copydoc Dali::DevelAnimation::ProgressHasBeenReachedSignal()
213 Dali::Animation::AnimationSignalType& ProgressReachedSignal();
216 * Emit the Finished signal
218 void EmitSignalFinish();
221 * Emit the ProgressReached signal
223 void EmitSignalProgressReached();
226 * Connects a callback function with the object's signals.
227 * @param[in] object The object providing the signal.
228 * @param[in] tracker Used to disconnect the signal.
229 * @param[in] signalName The signal to connect to.
230 * @param[in] functor A newly allocated FunctorDelegate.
231 * @return True if the signal was connected.
232 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
234 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
237 * Performs actions as requested using the action name.
238 * @param[in] object The object on which to perform the action.
239 * @param[in] actionName The action to perform.
240 * @param[in] attributes The attributes with which to perfrom this action.
241 * @return true if action was done
243 static bool DoAction(BaseObject* object, const std::string& actionName, const Property::Map& attributes);
246 * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue)
248 void AnimateBy(Property& target, Property::Value& relativeValue);
251 * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha)
253 void AnimateBy(Property& target, Property::Value& relativeValue, AlphaFunction alpha);
256 * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue, TimePeriod period)
258 void AnimateBy(Property& target, Property::Value& relativeValue, TimePeriod period);
261 * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period)
263 void AnimateBy(Property& target, Property::Value& relativeValue, AlphaFunction alpha, TimePeriod period);
266 * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue)
268 void AnimateTo(Property& target, Property::Value& destinationValue);
271 * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha)
273 void AnimateTo(Property& target, Property::Value& destinationValue, AlphaFunction alpha);
276 * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue, TimePeriod period)
278 void AnimateTo(Property& target, Property::Value& destinationValue, TimePeriod period);
281 * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period)
283 void AnimateTo(Property& target, Property::Value& destinationValue, AlphaFunction alpha, TimePeriod period);
286 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames)
288 void AnimateBetween(Property target, const KeyFrames& keyFrames);
291 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation)
293 void AnimateBetween(Property target, const KeyFrames& keyFrames, Interpolation interpolation );
296 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period)
298 void AnimateBetween(Property target, const KeyFrames& keyFrames, TimePeriod period);
301 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation)
303 void AnimateBetween(Property target, const KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
306 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha)
308 void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha);
311 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation)
313 void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
316 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period)
318 void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
321 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation )
323 void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation );
325 // Actor-specific convenience functions
328 * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward )
330 void Animate( Actor& actor, const Path& path, const Vector3& forward );
333 * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha )
335 void Animate( Actor& actor, const Path& path, const Vector3& forward, AlphaFunction alpha );
338 * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period )
340 void Animate( Actor& actor, const Path& path, const Vector3& forward, TimePeriod period );
343 * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period)
345 void Animate( Actor& actor, const Path& path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
348 * @copydoc Dali::Animation::Show()
350 void Show(Actor& actor, float delaySeconds);
353 * @copydoc Dali::Animation::Hide()
355 void Hide(Actor& actor, float delaySeconds);
358 * @copydoc Dali::Animation::SetCurrentProgress()
360 void SetCurrentProgress(float progress);
363 * @copydoc Dali::Animation::GetCurrentProgress()
365 float GetCurrentProgress();
368 * @copydoc Dali::Animation::SetSpeedFactor()
370 void SetSpeedFactor( float factor );
373 * @copydoc Dali::Animation::GetSpeedFactor()
375 float GetSpeedFactor() const;
378 * @copydoc Dali::Animation::SetPlayRange()
380 void SetPlayRange( const Vector2& range );
383 * @copydoc Dali::Animation::GetPlayRange()
385 Vector2 GetPlayRange() const;
388 * @copydoc Dali::Animation::SetLoopingMode()
390 void SetLoopingMode( Dali::Animation::LoopingMode loopingMode );
393 * @copydoc Dali::Animation::GetLoopingMode()
395 Dali::Animation::LoopingMode GetLoopingMode() const;
397 public: // For connecting animators to animations
400 * Add an animator connector.
401 * @param[in] connector The animator connector.
403 void AddAnimatorConnector( AnimatorConnectorBase* connector );
406 * Retrieve the SceneGraph::Animation object.
407 * @return The animation.
409 const SceneGraph::Animation* GetSceneObject()
415 * Retrieve the event thread services object
416 * @return The interface for sending messages to the scene graph
418 EventThreadServices& GetEventThreadServices()
420 return mEventThreadServices;
426 * Construct a new Animation.
427 * @param[in] eventThreadServices The interface for sending messages to the scene graph
428 * @param[in] playlist The list of currently playing animations.
429 * @param[in] durationSeconds The duration of the animation in seconds.
430 * @param[in] endAction The action to perform when the animation ends.
431 * @param[in] disconnectAction The action to perform when the property owner of an animator is disconnected.
432 * @param[in] defaultAlpha The default alpha function to apply to animators.
434 Animation( EventThreadServices& eventThreadServices,
435 AnimationPlaylist& playlist,
436 float durationSeconds,
438 EndAction disconnectAction,
439 AlphaFunction defaultAlpha);
442 * Second-phase constructor.
447 * Helper to create a scene-graph animation
449 void CreateSceneObject();
452 * Helper to create a scene-graph animation
454 void DestroySceneObject();
457 * A reference counted object may only be deleted by calling Unreference()
459 ~Animation() override;
464 * Extends the duration when an animator is added with TimePeriod that exceeds current duration.
465 * @param[in] timePeriod The time period for an animator.
467 void ExtendDuration( const TimePeriod& timePeriod );
470 Animation(const Animation&);
473 Animation& operator=(const Animation& rhs);
477 struct ConnectorTargetValues
479 ConnectorTargetValues()
487 Property::Value targetValue;
488 TimePeriod timePeriod;
489 std::size_t connectorIndex;
490 Animation::Type animatorType;
495 USE_CURRENT_VALUE, ///< Set the current value for the property
496 USE_TARGET_VALUE, ///< Set the animator's target value for the property
497 FORCE_CURRENT_VALUE, ///< Set the current value for the property even if the end action is to discard
503 * Compares the end times of the animators returning true if lhs end time is less than rhs end time.
504 * @param[in] lhs The first comparator
505 * @param[in] rhs The second comparator
506 * @return True if end time of lhs is less, false otherwise.
508 static bool CompareConnectorEndTimes( const ConnectorTargetValues& lhs, const ConnectorTargetValues& rhs );
511 * Notifies all the objects whose properties are being animated.
512 * @param[in] notifyValueType Whether we should set the current or target value
514 void NotifyObjects( Notify notifyValueType );
517 * Sends message to SceneGraph with final progress value
519 void SendFinalProgressNotificationMessage();
523 const SceneGraph::Animation* mAnimation;
525 EventThreadServices& mEventThreadServices;
526 AnimationPlaylist& mPlaylist;
528 Dali::Animation::AnimationSignalType mFinishedSignal;
530 Dali::Animation::AnimationSignalType mProgressReachedSignal;
532 using AnimatorConnectorContainer = OwnerContainer<AnimatorConnectorBase*>;
533 AnimatorConnectorContainer mConnectors; ///< Owned by the Animation
535 using ConnectorTargetValuesContainer = std::vector<ConnectorTargetValues>;
536 ConnectorTargetValuesContainer mConnectorTargetValues; //< Used to store animating property target value information
540 float mDurationSeconds;
542 int32_t mNotificationCount; ///< Keep track of how many Finished signals have been emitted.
544 int32_t mCurrentLoop;
545 EndAction mEndAction;
546 EndAction mDisconnectAction;
547 AlphaFunction mDefaultAlpha;
548 Dali::Animation::State mState;
549 float mProgressReachedMarker;
551 bool mAutoReverseEnabled; ///< Flag to identify that the looping mode is auto reverse.
554 } // namespace Internal
556 // Helpers for public-api forwarding methods
558 inline Internal::Animation& GetImplementation(Dali::Animation& animation)
560 DALI_ASSERT_ALWAYS( animation && "Animation handle is empty" );
562 BaseObject& handle = animation.GetBaseObject();
564 return static_cast<Internal::Animation&>(handle);
567 inline const Internal::Animation& GetImplementation(const Dali::Animation& animation)
569 DALI_ASSERT_ALWAYS( animation && "Animation handle is empty" );
571 const BaseObject& handle = animation.GetBaseObject();
573 return static_cast<const Internal::Animation&>(handle);
578 #endif // DALI_INTERNAL_ANIMATION_H