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