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/internal/event/animation/animator-connector-base.h>
27 #include <dali/internal/event/animation/key-frames-impl.h>
28 #include <dali/internal/event/animation/path-impl.h>
44 class AnimationPlaylist;
47 typedef IntrusivePtr<Animation> AnimationPtr;
48 typedef std::vector<AnimationPtr> AnimationContainer;
50 typedef AnimationContainer::iterator AnimationIter;
51 typedef AnimationContainer::const_iterator AnimationConstIter;
54 * Animation is a proxy for a SceneGraph::Animation object.
55 * The UpdateManager owns the Animation object, but the lifetime of the animation is
56 * indirectly controlled by the Animation.
58 class Animation : public BaseObject
62 typedef Dali::Animation::EndAction EndAction;
63 typedef Dali::Animation::Interpolation Interpolation;
66 * Create a new Animation object.
67 * @param[in] durationSeconds The duration of the animation.
68 * @return A smart-pointer to the newly allocated Animation.
70 static AnimationPtr New(float durationSeconds);
73 * @copydoc Dali::Animation::SetDuration()
75 void SetDuration(float seconds);
78 * @copydoc Dali::Animation::GetDuration()
80 float GetDuration() const;
83 * @copydoc Dali::Animation::SetLooping()
85 void SetLooping(bool on);
88 * @copydoc Dali::Animation::SetLoopCount()
90 void SetLoopCount(int count);
93 * @copydoc Dali::Animation::GetLoopCount()
98 * @copydoc Dali::Animation::GetCurrentLoop()
100 int GetCurrentLoop();
103 * @copydoc Dali::Animation::IsLooping()
105 bool IsLooping() const;
108 * @copydoc Dali::Animation::SetEndAction()
110 void SetEndAction(EndAction action);
113 * @copydoc Dali::Animation::GetEndAction()
115 EndAction GetEndAction() const;
118 * @copydoc Dali::Animation::SetDisconnectAction()
120 void SetDisconnectAction(EndAction action);
123 * @copydoc Dali::Animation::GetDisconnectAction()
125 EndAction GetDisconnectAction() const;
128 * @copydoc Dali::Animation::SetDefaultAlphaFunction()
130 void SetDefaultAlphaFunction(AlphaFunction alpha)
132 mDefaultAlpha = alpha;
136 * @copydoc Dali::Animation::GetDefaultAlphaFunction()
138 AlphaFunction GetDefaultAlphaFunction() const
140 return mDefaultAlpha;
144 * @copydoc Dali::Animation::Play()
149 * @copydoc Dali::Animation::PlayFrom()
151 void PlayFrom( float progress );
154 * @copydoc Dali::Animation::Pause()
159 * @copydoc Dali::Animation::GetState()
161 Dali::Animation::State GetState() const;
164 * @copydoc Dali::Animation::Stop()
169 * @copydoc Dali::Animation::Clear()
174 * Query whether a Finished signal should be emitted for this animation.
175 * This should only be called by NotificationManager, before signals are emitted.
176 * @post HasFinished() will return false on subsequent calls, until the animation is replayed to completion.
181 * @copydoc Dali::Animation::FinishedSignal()
183 Dali::Animation::AnimationSignalType& FinishedSignal();
186 * Emit the Finished signal
188 void EmitSignalFinish();
191 * Connects a callback function with the object's signals.
192 * @param[in] object The object providing the signal.
193 * @param[in] tracker Used to disconnect the signal.
194 * @param[in] signalName The signal to connect to.
195 * @param[in] functor A newly allocated FunctorDelegate.
196 * @return True if the signal was connected.
197 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
199 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
202 * Performs actions as requested using the action name.
203 * @param[in] object The object on which to perform the action.
204 * @param[in] actionName The action to perform.
205 * @param[in] attributes The attributes with which to perfrom this action.
206 * @return true if action was done
208 static bool DoAction(BaseObject* object, const std::string& actionName, const Property::Map& attributes);
211 * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue)
213 void AnimateBy(Property& target, Property::Value& relativeValue);
216 * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha)
218 void AnimateBy(Property& target, Property::Value& relativeValue, AlphaFunction alpha);
221 * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue, TimePeriod period)
223 void AnimateBy(Property& target, Property::Value& relativeValue, TimePeriod period);
226 * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period)
228 void AnimateBy(Property& target, Property::Value& relativeValue, AlphaFunction alpha, TimePeriod period);
231 * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue)
233 void AnimateTo(Property& target, Property::Value& destinationValue);
236 * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha)
238 void AnimateTo(Property& target, Property::Value& destinationValue, AlphaFunction alpha);
241 * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue, TimePeriod period)
243 void AnimateTo(Property& target, Property::Value& destinationValue, TimePeriod period);
246 * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period)
248 void AnimateTo(Property& target, Property::Value& destinationValue, AlphaFunction alpha, TimePeriod period);
251 * Animate a property to a destination value.
252 * @param [in] targetObject The target object to animate.
253 * @param [in] targetPropertyIndex The index of the target property.
254 * @param [in] componentIndex Index to a sub component of a property, for use with Vector2, Vector3 and Vector4
255 * @param [in] destinationValue The destination value.
256 * @param [in] alpha The alpha function to apply.
257 * @param [in] period The effect will occur during this time period.
259 void AnimateTo(Object& targetObject, Property::Index targetPropertyIndex, int componentIndex, Property::Value& destinationValue, AlphaFunction alpha, TimePeriod period);
262 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames)
264 void AnimateBetween(Property target, const KeyFrames& keyFrames);
267 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation)
269 void AnimateBetween(Property target, const KeyFrames& keyFrames, Interpolation interpolation );
272 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period)
274 void AnimateBetween(Property target, const KeyFrames& keyFrames, TimePeriod period);
277 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation)
279 void AnimateBetween(Property target, const KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
282 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha)
284 void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha);
287 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation)
289 void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
292 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period)
294 void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
297 * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation )
299 void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation );
301 // Actor-specific convenience functions
304 * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward )
306 void Animate( Actor& actor, const Path& path, const Vector3& forward );
309 * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha )
311 void Animate( Actor& actor, const Path& path, const Vector3& forward, AlphaFunction alpha );
314 * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period )
316 void Animate( Actor& actor, const Path& path, const Vector3& forward, TimePeriod period );
319 * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period)
321 void Animate( Actor& actor, const Path& path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
324 * @copydoc Dali::Animation::Show()
326 void Show(Actor& actor, float delaySeconds);
329 * @copydoc Dali::Animation::Hide()
331 void Hide(Actor& actor, float delaySeconds);
334 * @copydoc Dali::Animation::SetCurrentProgress()
336 void SetCurrentProgress(float progress);
339 * @copydoc Dali::Animation::GetCurrentProgress()
341 float GetCurrentProgress();
344 * @copydoc Dali::Animation::SetSpeedFactor()
346 void SetSpeedFactor( float factor );
349 * @copydoc Dali::Animation::GetSpeedFactor()
351 float GetSpeedFactor() const;
354 * @copydoc Dali::Animation::SetPlayRange()
356 void SetPlayRange( const Vector2& range );
359 * @copydoc Dali::Animation::GetPlayRange
361 Vector2 GetPlayRange() const;
363 public: // For connecting animators to animations
366 * Add an animator connector.
367 * @param[in] connector The animator connector.
369 void AddAnimatorConnector( AnimatorConnectorBase* connector );
372 * Retrieve the SceneGraph::Animation object.
373 * @return The animation.
375 const SceneGraph::Animation* GetSceneObject()
381 * Retrieve the event thread services object
382 * @return The interface for sending messages to the scene graph
384 EventThreadServices& GetEventThreadServices()
386 return mEventThreadServices;
392 * Construct a new Animation.
393 * @param[in] eventThreadServices The interface for sending messages to the scene graph
394 * @param[in] playlist The list of currently playing animations.
395 * @param[in] durationSeconds The duration of the animation in seconds.
396 * @param[in] endAction The action to perform when the animation ends.
397 * @param[in] disconnectAction The action to perform when the property owner of an animator is disconnected.
398 * @param[in] defaultAlpha The default alpha function to apply to animators.
400 Animation( EventThreadServices& eventThreadServices,
401 AnimationPlaylist& playlist,
402 float durationSeconds,
404 EndAction disconnectAction,
405 AlphaFunction defaultAlpha);
408 * Second-phase constructor.
413 * Helper to create a scene-graph animation
415 void CreateSceneObject();
418 * Helper to create a scene-graph animation
420 void DestroySceneObject();
423 * A reference counted object may only be deleted by calling Unreference()
425 virtual ~Animation();
430 * Extends the duration when an animator is added with TimePeriod that exceeds current duration.
431 * @param[in] timePeriod The time period for an animator.
433 void ExtendDuration( const TimePeriod& timePeriod );
436 Animation(const Animation&);
439 Animation& operator=(const Animation& rhs);
443 struct ConnectorTargetValues
445 ConnectorTargetValues()
452 Property::Value targetValue;
453 TimePeriod timePeriod;
454 unsigned int connectorIndex;
460 * Compares the end times of the animators returning true if lhs end time is less than rhs end time.
461 * @param[in] lhs The first comparator
462 * @param[in] rhs The second comparator
463 * @return True if end time of lhs is less, false otherwise.
465 static bool CompareConnectorEndTimes( const ConnectorTargetValues& lhs, const ConnectorTargetValues& rhs );
469 const SceneGraph::Animation* mAnimation;
471 EventThreadServices& mEventThreadServices;
472 AnimationPlaylist& mPlaylist;
474 Dali::Animation::AnimationSignalType mFinishedSignal;
476 AnimatorConnectorContainer mConnectors; ///< Owned by the Animation
478 typedef std::vector< ConnectorTargetValues > ConnectorTargetValuesContainer;
479 ConnectorTargetValuesContainer mConnectorTargetValues; //< Used to store animating property target value information
483 float mDurationSeconds;
485 int mNotificationCount; ///< Keep track of how many Finished signals have been emitted.
488 EndAction mEndAction;
489 EndAction mDisconnectAction;
490 AlphaFunction mDefaultAlpha;
491 Dali::Animation::State mState;
494 } // namespace Internal
496 // Helpers for public-api forwarding methods
498 inline Internal::Animation& GetImplementation(Dali::Animation& animation)
500 DALI_ASSERT_ALWAYS( animation && "Animation handle is empty" );
502 BaseObject& handle = animation.GetBaseObject();
504 return static_cast<Internal::Animation&>(handle);
507 inline const Internal::Animation& GetImplementation(const Dali::Animation& animation)
509 DALI_ASSERT_ALWAYS( animation && "Animation handle is empty" );
511 const BaseObject& handle = animation.GetBaseObject();
513 return static_cast<const Internal::Animation&>(handle);
518 #endif // __DALI_INTERNAL_ANIMATION_H__