2 * Copyright (c) 2021 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>
22 #include <dali/integration-api/debug.h>
23 #include <dali/internal/event/animation/animation-impl.h>
24 #include <dali/public-api/common/vector-wrapper.h>
30 AnimationPlaylist* AnimationPlaylist::New()
32 return new AnimationPlaylist();
35 AnimationPlaylist::AnimationPlaylist() = default;
37 AnimationPlaylist::~AnimationPlaylist() = default;
39 void AnimationPlaylist::AnimationCreated(Animation& animation)
41 mAnimations.PushBack(&animation);
44 void AnimationPlaylist::AnimationDestroyed(Animation& animation)
46 Dali::Vector<Animation*>::Iterator iter = std::find(mAnimations.Begin(), mAnimations.End(), &animation);
47 DALI_ASSERT_ALWAYS(iter != mAnimations.End() && "Animation not found");
49 mAnimations.Remove(iter);
52 void AnimationPlaylist::OnPlay(Animation& animation)
54 mPlaylist.push_back(Dali::Animation(&animation));
57 void AnimationPlaylist::OnClear(Animation& animation)
59 std::vector<Dali::Animation>::iterator iter = std::find(mPlaylist.begin(), mPlaylist.end(), Dali::Animation(&animation));
60 std::vector<Dali::Animation>::iterator last = mPlaylist.end();
63 --last; // move to real last
64 std::swap(*iter, *last); // swap
65 mPlaylist.resize(mPlaylist.size() - 1u);
69 void AnimationPlaylist::NotifyCompleted()
71 std::vector<Dali::Animation> finishedAnimations;
73 // Since animations can be unreferenced during the signal emissions, iterators into animationPointers may be invalidated.
74 // First copy and reference the finished animations, then emit signals
75 for(Dali::Vector<Animation*>::Iterator iter = mAnimations.Begin(); iter != mAnimations.End(); ++iter)
77 Animation* animation = *iter;
79 if(animation->HasFinished())
81 finishedAnimations.push_back(Dali::Animation(animation));
83 // The animation may be present in mPlaylist - remove if necessary
84 // Note that the animation "Finish" signal is emitted after Stop() has been called
85 std::vector<Dali::Animation>::iterator iter = std::find(mPlaylist.begin(), mPlaylist.end(), Dali::Animation(animation));
86 DALI_ASSERT_DEBUG(iter != mPlaylist.end());
87 mPlaylist.erase(iter);
91 // Now it's safe to emit the signals
92 for(std::vector<Dali::Animation>::iterator iter = finishedAnimations.begin(); iter != finishedAnimations.end(); ++iter)
94 Dali::Animation& handle = *iter;
96 GetImplementation(handle).EmitSignalFinish();
100 void AnimationPlaylist::NotifyProgressReached(const SceneGraph::Animation* sceneGraphAnimation)
102 std::vector<Dali::Animation> notifyProgressAnimations; // Will own animations until all emits have been done
104 for(Dali::Vector<Animation*>::Iterator iter = mAnimations.Begin(); iter != mAnimations.End(); ++iter)
106 Animation* animation = *iter;
108 if((animation->GetSceneObject()) == sceneGraphAnimation)
110 // Store handles to animations that need signals emitted in the case of an animation being cleared in-between emits
111 notifyProgressAnimations.push_back(Dali::Animation(animation));
115 for(std::vector<Dali::Animation>::iterator iter = notifyProgressAnimations.begin(); iter != notifyProgressAnimations.end(); ++iter)
117 Dali::Animation& handle = *iter;
119 GetImplementation(handle).EmitSignalProgressReached();
123 uint32_t AnimationPlaylist::GetAnimationCount()
125 return mAnimations.Size();
128 Dali::Animation AnimationPlaylist::GetAnimationAt(uint32_t index)
130 if(index >= mAnimations.Size())
132 DALI_LOG_ERROR("Animation index is out of bounds.\n");
133 return Dali::Animation();
135 return Dali::Animation(mAnimations[index]);
138 } // namespace Internal