(Vector) Change event processing
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-vector-image / vector-animation-manager.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-toolkit/internal/visuals/animated-vector-image/vector-animation-manager.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/adaptor-framework/adaptor.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/visuals/animated-vector-image/vector-animation-thread.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Internal
35 {
36
37 namespace
38 {
39
40 #if defined(DEBUG_ENABLED)
41 Debug::Filter* gVectorAnimationLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VECTOR_ANIMATION" );
42 #endif
43
44 } // unnamed namespace
45
46 VectorAnimationManager::VectorAnimationManager()
47 : mEventCallbacks(),
48   mVectorAnimationThread( nullptr ),
49   mProcessorRegistered( false )
50 {
51 }
52
53 VectorAnimationManager::~VectorAnimationManager()
54 {
55   for( auto&& iter : mEventCallbacks )
56   {
57     delete iter;
58   }
59   mEventCallbacks.clear();
60
61   if( mProcessorRegistered )
62   {
63     Adaptor::Get().UnregisterProcessor( *this );
64   }
65 }
66
67 VectorAnimationThread& VectorAnimationManager::GetVectorAnimationThread()
68 {
69   if( !mVectorAnimationThread )
70   {
71     mVectorAnimationThread = std::unique_ptr< VectorAnimationThread >( new VectorAnimationThread() );
72     mVectorAnimationThread->Start();
73   }
74   return *mVectorAnimationThread;
75 }
76
77 void VectorAnimationManager::RegisterEventCallback( CallbackBase* callback )
78 {
79   mEventCallbacks.push_back( callback );
80
81   if( !mProcessorRegistered )
82   {
83     Adaptor::Get().RegisterProcessor( *this );
84     mProcessorRegistered = true;
85   }
86 }
87
88 void VectorAnimationManager::UnregisterEventCallback( CallbackBase* callback )
89 {
90   auto iter = std::find( mEventCallbacks.begin(), mEventCallbacks.end(), callback );
91   if( iter != mEventCallbacks.end() )
92   {
93     mEventCallbacks.erase( iter );
94
95     if( mEventCallbacks.empty() )
96     {
97       if( Adaptor::IsAvailable() )
98       {
99         Adaptor::Get().UnregisterProcessor( *this );
100         mProcessorRegistered = false;
101       }
102     }
103   }
104 }
105
106 void VectorAnimationManager::Process()
107 {
108   for( auto&& iter : mEventCallbacks )
109   {
110     CallbackBase::Execute( *iter );
111     delete iter;
112   }
113   mEventCallbacks.clear();
114
115   Adaptor::Get().UnregisterProcessor( *this );
116   mProcessorRegistered = false;
117 }
118
119 } // namespace Internal
120
121 } // namespace Toolkit
122
123 } // namespace Dali