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