1 #ifndef __DALI_INTERNAL_ANIMATION_H__
2 #define __DALI_INTERNAL_ANIMATION_H__
5 * Copyright (c) 2017 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 typedef IntrusivePtr<Animation> AnimationPtr;
51 typedef std::vector<AnimationPtr> AnimationContainer;
53 typedef AnimationContainer::iterator AnimationIter;
54 typedef AnimationContainer::const_iterator AnimationConstIter;
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 typedef Dali::Animation::EndAction EndAction;
73 typedef Dali::Animation::Interpolation 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(int count);
113 * @copydoc Dali::Animation::GetLoopCount()
118 * @copydoc Dali::Animation::GetCurrentLoop()
120 int 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 * Animate a property to a destination value.
287 * @param [in] targetObject The target object to animate.
288 * @param [in] targetPropertyIndex The index of the target property.
289 * @param [in] componentIndex Index to a sub component of a property, for use with Vector2, Vector3 and Vector4
290 * @param [in] destinationValue The destination value.
291 * @param [in] alpha The alpha function to apply.
292 * @param [in] period The effect will occur during this time period.
294 void AnimateTo(Object& targetObject, Property::Index targetPropertyIndex, int componentIndex, Property::Value& destinationValue, AlphaFunction alpha, TimePeriod period);
297 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames)
299 void AnimateBetween(Property target, const KeyFrames& keyFrames);
302 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation)
304 void AnimateBetween(Property target, const KeyFrames& keyFrames, Interpolation interpolation );
307 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period)
309 void AnimateBetween(Property target, const KeyFrames& keyFrames, TimePeriod period);
312 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation)
314 void AnimateBetween(Property target, const KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
317 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha)
319 void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha);
322 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation)
324 void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
327 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period)
329 void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
332 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation )
334 void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation );
336 // Actor-specific convenience functions
339 * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward )
341 void Animate( Actor& actor, const Path& path, const Vector3& forward );
344 * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha )
346 void Animate( Actor& actor, const Path& path, const Vector3& forward, AlphaFunction alpha );
349 * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period )
351 void Animate( Actor& actor, const Path& path, const Vector3& forward, TimePeriod period );
354 * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period)
356 void Animate( Actor& actor, const Path& path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
359 * @copydoc Dali::Animation::Show()
361 void Show(Actor& actor, float delaySeconds);
364 * @copydoc Dali::Animation::Hide()
366 void Hide(Actor& actor, float delaySeconds);
369 * @copydoc Dali::Animation::SetCurrentProgress()
371 void SetCurrentProgress(float progress);
374 * @copydoc Dali::Animation::GetCurrentProgress()
376 float GetCurrentProgress();
379 * @copydoc Dali::Animation::SetSpeedFactor()
381 void SetSpeedFactor( float factor );
384 * @copydoc Dali::Animation::GetSpeedFactor()
386 float GetSpeedFactor() const;
389 * @copydoc Dali::Animation::SetPlayRange()
391 void SetPlayRange( const Vector2& range );
394 * @copydoc Dali::Animation::GetPlayRange()
396 Vector2 GetPlayRange() const;
399 * @copydoc Dali::Animation::SetLoopingMode()
401 void SetLoopingMode( Dali::Animation::LoopingMode loopingMode );
404 * @copydoc Dali::Animation::GetLoopingMode()
406 Dali::Animation::LoopingMode GetLoopingMode() const;
408 public: // For connecting animators to animations
411 * Add an animator connector.
412 * @param[in] connector The animator connector.
414 void AddAnimatorConnector( AnimatorConnectorBase* connector );
417 * Retrieve the SceneGraph::Animation object.
418 * @return The animation.
420 const SceneGraph::Animation* GetSceneObject()
426 * Retrieve the event thread services object
427 * @return The interface for sending messages to the scene graph
429 EventThreadServices& GetEventThreadServices()
431 return mEventThreadServices;
437 * Construct a new Animation.
438 * @param[in] eventThreadServices The interface for sending messages to the scene graph
439 * @param[in] playlist The list of currently playing animations.
440 * @param[in] durationSeconds The duration of the animation in seconds.
441 * @param[in] endAction The action to perform when the animation ends.
442 * @param[in] disconnectAction The action to perform when the property owner of an animator is disconnected.
443 * @param[in] defaultAlpha The default alpha function to apply to animators.
445 Animation( EventThreadServices& eventThreadServices,
446 AnimationPlaylist& playlist,
447 float durationSeconds,
449 EndAction disconnectAction,
450 AlphaFunction defaultAlpha);
453 * Second-phase constructor.
458 * Helper to create a scene-graph animation
460 void CreateSceneObject();
463 * Helper to create a scene-graph animation
465 void DestroySceneObject();
468 * A reference counted object may only be deleted by calling Unreference()
470 virtual ~Animation();
475 * Extends the duration when an animator is added with TimePeriod that exceeds current duration.
476 * @param[in] timePeriod The time period for an animator.
478 void ExtendDuration( const TimePeriod& timePeriod );
481 Animation(const Animation&);
484 Animation& operator=(const Animation& rhs);
488 struct ConnectorTargetValues
490 ConnectorTargetValues()
498 Property::Value targetValue;
499 TimePeriod timePeriod;
500 std::size_t connectorIndex;
501 Animation::Type animatorType;
507 * Compares the end times of the animators returning true if lhs end time is less than rhs end time.
508 * @param[in] lhs The first comparator
509 * @param[in] rhs The second comparator
510 * @return True if end time of lhs is less, false otherwise.
512 static bool CompareConnectorEndTimes( const ConnectorTargetValues& lhs, const ConnectorTargetValues& rhs );
515 * Notifies all the objects whose properties are being animated.
517 void NotifyObjects();
520 * Sends message to SceneGraph with final progress value
522 void SendFinalProgressNotificationMessage();
526 const SceneGraph::Animation* mAnimation;
528 EventThreadServices& mEventThreadServices;
529 AnimationPlaylist& mPlaylist;
531 Dali::Animation::AnimationSignalType mFinishedSignal;
533 Dali::Animation::AnimationSignalType mProgressReachedSignal;
535 typedef OwnerContainer< AnimatorConnectorBase* > AnimatorConnectorContainer;
536 AnimatorConnectorContainer mConnectors; ///< Owned by the Animation
538 typedef std::vector< ConnectorTargetValues > ConnectorTargetValuesContainer;
539 ConnectorTargetValuesContainer mConnectorTargetValues; //< Used to store animating property target value information
543 float mDurationSeconds;
545 int mNotificationCount; ///< Keep track of how many Finished signals have been emitted.
548 EndAction mEndAction;
549 EndAction mDisconnectAction;
550 AlphaFunction mDefaultAlpha;
551 Dali::Animation::State mState;
552 float mProgressReachedMarker;
554 bool mAutoReverseEnabled; ///< Flag to identify that the looping mode is auto reverse.
557 } // namespace Internal
559 // Helpers for public-api forwarding methods
561 inline Internal::Animation& GetImplementation(Dali::Animation& animation)
563 DALI_ASSERT_ALWAYS( animation && "Animation handle is empty" );
565 BaseObject& handle = animation.GetBaseObject();
567 return static_cast<Internal::Animation&>(handle);
570 inline const Internal::Animation& GetImplementation(const Dali::Animation& animation)
572 DALI_ASSERT_ALWAYS( animation && "Animation handle is empty" );
574 const BaseObject& handle = animation.GetBaseObject();
576 return static_cast<const Internal::Animation&>(handle);
581 #endif // __DALI_INTERNAL_ANIMATION_H__