Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / animation-playlist.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/animation/animation-playlist.h>
20
21
22 // INTERNAL INCLUDES
23 #include <dali/public-api/common/vector-wrapper.h>
24 #include <dali/internal/event/animation/animation-impl.h>
25
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 AnimationPlaylist* AnimationPlaylist::New()
34 {
35   return new AnimationPlaylist();
36 }
37
38 AnimationPlaylist::AnimationPlaylist() = default;
39
40 AnimationPlaylist::~AnimationPlaylist() = default;
41
42 void AnimationPlaylist::AnimationCreated( Animation& animation )
43 {
44   mAnimations.PushBack( &animation );
45 }
46
47 void AnimationPlaylist::AnimationDestroyed( Animation& animation )
48 {
49   Dali::Vector< Animation* >::Iterator iter = std::find( mAnimations.Begin(), mAnimations.End(), &animation );
50   DALI_ASSERT_ALWAYS( iter != mAnimations.End() && "Animation not found" );
51
52   mAnimations.Remove( iter );
53 }
54
55 void AnimationPlaylist::OnPlay( Animation& animation )
56 {
57   mPlaylist.push_back( Dali::Animation(&animation) );
58 }
59
60 void AnimationPlaylist::OnClear( Animation& animation )
61 {
62   std::vector< Dali::Animation >::iterator iter = std::find( mPlaylist.begin(), mPlaylist.end(), Dali::Animation(&animation) );
63   std::vector< Dali::Animation >::iterator last = mPlaylist.end();
64   if( iter != last )
65   {
66     --last; // move to real last
67     std::swap( *iter, *last ); // swap
68     mPlaylist.resize( mPlaylist.size() - 1u );
69   }
70 }
71
72 void AnimationPlaylist::NotifyCompleted()
73 {
74   std::vector< Dali::Animation > finishedAnimations;
75
76   // Since animations can be unreferenced during the signal emissions, iterators into animationPointers may be invalidated.
77   // First copy and reference the finished animations, then emit signals
78   for ( Dali::Vector< Animation* >::Iterator iter = mAnimations.Begin(); iter != mAnimations.End(); ++iter )
79   {
80     Animation* animation = *iter;
81
82     if ( animation->HasFinished() )
83     {
84       finishedAnimations.push_back( Dali::Animation(animation) );
85
86       // The animation may be present in mPlaylist - remove if necessary
87       // Note that the animation "Finish" signal is emitted after Stop() has been called
88       std::vector< Dali::Animation >::iterator iter = std::find( mPlaylist.begin(), mPlaylist.end(), Dali::Animation(animation) );
89       DALI_ASSERT_DEBUG(iter != mPlaylist.end());
90       mPlaylist.erase( iter );
91     }
92   }
93
94   // Now it's safe to emit the signals
95   for ( std::vector< Dali::Animation >::iterator iter = finishedAnimations.begin(); iter != finishedAnimations.end(); ++iter )
96   {
97     Dali::Animation& handle = *iter;
98
99     GetImplementation(handle).EmitSignalFinish();
100   }
101 }
102
103 void AnimationPlaylist::NotifyProgressReached( const SceneGraph::Animation* sceneGraphAnimation )
104 {
105   std::vector< Dali::Animation > notifyProgressAnimations; // Will own animations until all emits have been done
106
107   for ( Dali::Vector< Animation* >::Iterator iter = mAnimations.Begin(); iter != mAnimations.End(); ++iter )
108   {
109     Animation* animation = *iter;
110
111     if ( ( animation->GetSceneObject() ) == sceneGraphAnimation )
112     {
113       // Store handles to animations that need signals emitted in the case of an animation being cleared in-between emits
114       notifyProgressAnimations.push_back(  Dali::Animation( animation ) );
115     }
116   }
117
118   for ( std::vector< Dali::Animation >::iterator iter = notifyProgressAnimations.begin(); iter != notifyProgressAnimations.end(); ++iter )
119   {
120     Dali::Animation& handle = *iter;
121
122     GetImplementation(handle).EmitSignalProgressReached();
123   }
124 }
125
126 uint32_t AnimationPlaylist::GetAnimationCount()
127 {
128   return mAnimations.Size();
129 }
130
131 Dali::Animation AnimationPlaylist::GetAnimationAt( uint32_t index )
132 {
133   if( index >= mAnimations.Size() )
134   {
135     DALI_LOG_ERROR( "Animation index is out of bounds.\n" );
136     return Dali::Animation();
137   }
138   return Dali::Animation( mAnimations[index] );
139 }
140
141 } // namespace Internal
142
143 } // namespace Dali