2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/event/animation/animation-playlist.h>
23 #include <dali/public-api/common/vector-wrapper.h>
24 #include <dali/internal/event/animation/animation-impl.h>
33 AnimationPlaylist* AnimationPlaylist::New()
35 return new AnimationPlaylist();
38 AnimationPlaylist::AnimationPlaylist()
42 AnimationPlaylist::~AnimationPlaylist()
46 void AnimationPlaylist::AnimationCreated( Animation& animation )
48 mAnimations.PushBack( &animation );
51 void AnimationPlaylist::AnimationDestroyed( Animation& animation )
53 Dali::Vector< Animation* >::Iterator iter = std::find( mAnimations.Begin(), mAnimations.End(), &animation );
54 DALI_ASSERT_ALWAYS( iter != mAnimations.End() && "Animation not found" );
56 mAnimations.Remove( iter );
59 void AnimationPlaylist::OnPlay( Animation& animation )
61 mPlaylist.push_back( Dali::Animation(&animation) );
64 void AnimationPlaylist::OnClear( Animation& animation )
66 std::vector< Dali::Animation >::iterator iter = std::find( mPlaylist.begin(), mPlaylist.end(), Dali::Animation(&animation) );
67 std::vector< Dali::Animation >::iterator last = mPlaylist.end();
70 --last; // move to real last
71 std::swap( *iter, *last ); // swap
72 mPlaylist.resize( mPlaylist.size() - 1u );
76 void AnimationPlaylist::NotifyCompleted()
78 std::vector< Dali::Animation > finishedAnimations;
80 // Since animations can be unreferenced during the signal emissions, iterators into animationPointers may be invalidated.
81 // First copy and reference the finished animations, then emit signals
82 for ( Dali::Vector< Animation* >::Iterator iter = mAnimations.Begin(); iter != mAnimations.End(); ++iter )
84 Animation* animation = *iter;
86 if ( animation->HasFinished() )
88 finishedAnimations.push_back( Dali::Animation(animation) );
90 // The animation may be present in mPlaylist - remove if necessary
91 // Note that the animation "Finish" signal is emitted after Stop() has been called
92 std::vector< Dali::Animation >::iterator iter = std::find( mPlaylist.begin(), mPlaylist.end(), Dali::Animation(animation) );
93 DALI_ASSERT_DEBUG(iter != mPlaylist.end());
94 mPlaylist.erase( iter );
98 // Now it's safe to emit the signals
99 for ( std::vector< Dali::Animation >::iterator iter = finishedAnimations.begin(); iter != finishedAnimations.end(); ++iter )
101 Dali::Animation& handle = *iter;
103 GetImplementation(handle).EmitSignalFinish();
107 void AnimationPlaylist::NotifyProgressReached( const SceneGraph::Animation* sceneGraphAnimation )
109 std::vector< Dali::Animation > notifyProgressAnimations; // Will own animations until all emits have been done
111 for ( Dali::Vector< Animation* >::Iterator iter = mAnimations.Begin(); iter != mAnimations.End(); ++iter )
113 Animation* animation = *iter;
115 if ( ( animation->GetSceneObject() ) == sceneGraphAnimation )
117 // Store handles to animations that need signals emitted in the case of an animation being cleared in-between emits
118 notifyProgressAnimations.push_back( Dali::Animation( animation ) );
122 for ( std::vector< Dali::Animation >::iterator iter = notifyProgressAnimations.begin(); iter != notifyProgressAnimations.end(); ++iter )
124 Dali::Animation& handle = *iter;
126 GetImplementation(handle).EmitSignalProgressReached();
130 } // namespace Internal