bf012cc29a458ba73bd943cb376ad9e518e077a7
[platform/core/uifw/dali-core.git] / dali / internal / event / events / gesture-detector-impl.cpp
1 /*
2  * Copyright (c) 2014 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/events/gesture-detector-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <dali/internal/event/events/actor-gesture-data.h>
27 #include <dali/internal/event/events/gesture-event-processor.h>
28 #include <dali/internal/event/common/thread-local-storage.h>
29 #include <dali/internal/event/common/stage-impl.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 GestureDetector::GestureDetector(Gesture::Type type)
38 : mType(type),
39   mGestureEventProcessor(ThreadLocalStorage::Get().GetGestureEventProcessor())
40 {
41 }
42
43 GestureDetector::~GestureDetector()
44 {
45   if ( !mAttachedActors.empty() )
46   {
47     for ( GestureDetectorActorContainer::iterator iter = mAttachedActors.begin(), endIter = mAttachedActors.end(); iter != endIter; ++iter )
48     {
49       Actor* actor( *iter );
50       actor->RemoveObserver( *this );
51       actor->GetGestureData().RemoveGestureDetector( *this );
52     }
53
54     mAttachedActors.clear();
55
56     // Guard to allow handle destruction after Core has been destroyed
57     if ( Stage::IsInstalled() )
58     {
59       mGestureEventProcessor.RemoveGestureDetector( this );
60     }
61   }
62 }
63
64 void GestureDetector::Attach(Actor& actor)
65 {
66   if ( !IsAttached(actor) )
67   {
68     // Register with EventProcessor if first actor being added
69     if ( mAttachedActors.empty() )
70     {
71       mGestureEventProcessor.AddGestureDetector(this);
72     }
73
74     mAttachedActors.push_back(&actor);
75
76     // We need to observe the actor's destruction
77     actor.AddObserver(*this);
78
79     // Add the detector to the actor (so the actor knows it requires this gesture when going through hit-test algorithm)
80     actor.GetGestureData().AddGestureDetector( *this );
81
82     // Notification for derived classes
83     OnActorAttach(actor);
84   }
85 }
86
87 void GestureDetector::Detach(Actor& actor)
88 {
89   if ( !mAttachedActors.empty() )
90   {
91     GestureDetectorActorContainer::iterator match = find(mAttachedActors.begin(), mAttachedActors.end(), &actor);
92
93     if ( match != mAttachedActors.end() )
94     {
95       // We no longer need to observe the actor's destruction
96       actor.RemoveObserver(*this);
97
98       // Remove detector from actor-gesture-data
99       actor.GetGestureData().RemoveGestureDetector( *this );
100
101       mAttachedActors.erase(match);
102
103       // Notification for derived classes
104       OnActorDetach(actor);
105
106       // Unregister from gesture event processor if we do not have any actors
107       if ( mAttachedActors.empty() )
108       {
109         mGestureEventProcessor.RemoveGestureDetector(this);
110       }
111     }
112   }
113 }
114
115 void GestureDetector::DetachAll()
116 {
117   if ( !mAttachedActors.empty() )
118   {
119     GestureDetectorActorContainer attachedActors(mAttachedActors);
120
121     // Clear mAttachedActors before we call OnActorDetach in case derived classes call a method which manipulates mAttachedActors.
122     mAttachedActors.clear();
123
124     for ( GestureDetectorActorContainer::iterator iter = attachedActors.begin(), endIter = attachedActors.end(); iter != endIter; ++iter )
125     {
126       Actor* actor(*iter);
127
128       // We no longer need to observe the actor's destruction
129       actor->RemoveObserver(*this);
130
131       // Remove detector from actor-gesture-data
132       actor->GetGestureData().RemoveGestureDetector( *this );
133
134       // Notification for derived classes
135       OnActorDetach(*actor);
136     }
137
138     // Unregister from gesture event processor
139     mGestureEventProcessor.RemoveGestureDetector(this);
140   }
141 }
142
143 std::vector<Dali::Actor> GestureDetector::GetAttachedActors() const
144 {
145   // Will only be used by Public API.
146   // Unlikely that it will be called that often so copying should be OK.
147
148   std::vector<Dali::Actor> actors;
149
150   for ( GestureDetectorActorContainer::const_iterator iter = mAttachedActors.begin(), endIter = mAttachedActors.end(); iter != endIter; ++iter )
151   {
152     actors.push_back(Dali::Actor(*iter));
153   }
154
155   return actors;
156 }
157
158 bool GestureDetector::IsAttached(Actor& actor) const
159 {
160   return find(mAttachedActors.begin(), mAttachedActors.end(), &actor) != mAttachedActors.end();
161 }
162
163 void GestureDetector::ProxyDestroyed(ProxyObject& proxy)
164 {
165   if ( !mAttachedActors.empty() )
166   {
167     GestureDetectorActorContainer::iterator match = find(mAttachedActors.begin(), mAttachedActors.end(), &proxy);
168
169     if ( match != mAttachedActors.end() )
170     {
171       mAttachedActors.erase(match);
172
173       // Notification for derived classes
174       OnActorDestroyed(proxy);
175
176       // Unregister from gesture event processor if we do not have any actors
177       if ( mAttachedActors.empty() )
178       {
179         mGestureEventProcessor.RemoveGestureDetector(this);
180       }
181     }
182   }
183 }
184
185 unsigned int GestureDetector::GetDefaultPropertyCount() const
186 {
187   return 0;
188 }
189
190 void GestureDetector::GetDefaultPropertyIndices( Property::IndexContainer& ) const
191 {
192 }
193
194 const char* GestureDetector::GetDefaultPropertyName( Property::Index index ) const
195 {
196   return NULL;
197 }
198
199 Property::Index GestureDetector::GetDefaultPropertyIndex(const std::string& name) const
200 {
201   return 0;
202 }
203
204 bool GestureDetector::IsDefaultPropertyWritable(Property::Index index) const
205 {
206   return false;
207 }
208
209 bool GestureDetector::IsDefaultPropertyAnimatable(Property::Index index) const
210 {
211   return false;
212 }
213
214 bool GestureDetector::IsDefaultPropertyAConstraintInput( Property::Index index ) const
215 {
216   return false;
217 }
218
219 Property::Type GestureDetector::GetDefaultPropertyType(Property::Index index) const
220 {
221   return Property::NONE;
222 }
223
224 void GestureDetector::SetDefaultProperty( Property::Index index, const Property::Value& property )
225 {
226   // None of our properties should be settable from Public API
227 }
228
229 Property::Value GestureDetector::GetDefaultProperty(Property::Index index) const
230 {
231   return Property::Value();
232 }
233
234 const SceneGraph::PropertyOwner* GestureDetector::GetSceneObject() const
235 {
236   return NULL;
237 }
238
239 const SceneGraph::PropertyBase* GestureDetector::GetSceneObjectAnimatableProperty( Property::Index index ) const
240 {
241   return NULL;
242 }
243
244 const PropertyInputImpl* GestureDetector::GetSceneObjectInputProperty( Property::Index index ) const
245 {
246   return NULL;
247 }
248
249 } // namespace Internal
250
251 } // namespace Dali