c819a82afda01738bc332470655efa3d40b81f4a
[platform/core/uifw/dali-core.git] / dali / internal / event / events / pan-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/pan-gesture-detector-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for strcmp
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/events/pan-gesture.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/public-api/math/radian.h>
28 #include <dali/public-api/math/degree.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/internal/event/actors/actor-impl.h>
31 #include <dali/internal/event/common/property-helper.h>
32 #include <dali/internal/event/common/thread-local-storage.h>
33 #include <dali/internal/event/events/gesture-event-processor.h>
34 #include <dali/internal/update/gestures/scene-graph-pan-gesture.h>
35
36 namespace Dali
37 {
38
39 namespace Internal
40 {
41
42 namespace
43 {
44
45 // Properties
46
47 //              Name                  Type   writable animatable constraint-input  enum for index-checking
48 DALI_PROPERTY_TABLE_BEGIN
49 DALI_PROPERTY( "screenPosition",      VECTOR2, false, false, true,   Dali::PanGestureDetector::Property::SCREEN_POSITION     )
50 DALI_PROPERTY( "screenDisplacement",  VECTOR2, false, false, true,   Dali::PanGestureDetector::Property::SCREEN_DISPLACEMENT )
51 DALI_PROPERTY( "screenVelocity",      VECTOR2, false, false, true,   Dali::PanGestureDetector::Property::SCREEN_VELOCITY     )
52 DALI_PROPERTY( "localPosition",       VECTOR2, false, false, true,   Dali::PanGestureDetector::Property::LOCAL_POSITION      )
53 DALI_PROPERTY( "localDisplacement",   VECTOR2, false, false, true,   Dali::PanGestureDetector::Property::LOCAL_DISPLACEMENT  )
54 DALI_PROPERTY( "localVelocity",       VECTOR2, false, false, true,   Dali::PanGestureDetector::Property::LOCAL_VELOCITY      )
55 DALI_PROPERTY( "panning",             BOOLEAN, false, false, true,   Dali::PanGestureDetector::Property::PANNING             )
56 DALI_PROPERTY_TABLE_END( DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX )
57
58 // Signals
59
60 const char* const SIGNAL_PAN_DETECTED = "panDetected";
61
62 BaseHandle Create()
63 {
64   return Dali::PanGestureDetector::New();
65 }
66
67 TypeRegistration mType( typeid(Dali::PanGestureDetector), typeid(Dali::GestureDetector), Create );
68
69 SignalConnectorType signalConnector1( mType, SIGNAL_PAN_DETECTED, &PanGestureDetector::DoConnectSignal );
70
71 #if defined(DEBUG_ENABLED)
72 Integration::Log::Filter* gLogFilter  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_PAN_GESTURE_DETECTOR");
73 #endif
74
75 /**
76  * Returns the angle going in the opposite direction to that specified by angle.
77  */
78 float GetOppositeAngle( float angle )
79 {
80   // Calculate the opposite angle so that we cover both directions.
81   if ( angle <= 0.0f )
82   {
83     angle += Math::PI;
84   }
85   else
86   {
87     angle -= Math::PI;
88   }
89
90   return angle;
91 }
92
93 } // unnamed namespace
94
95 PanGestureDetectorPtr PanGestureDetector::New()
96 {
97   return new PanGestureDetector;
98 }
99
100 PanGestureDetector::PanGestureDetector()
101 : GestureDetector(Gesture::Pan),
102   mMinimumTouches(1),
103   mMaximumTouches(1),
104   mSceneObject(NULL)
105 {
106 }
107
108 PanGestureDetector::~PanGestureDetector()
109 {
110 }
111
112 void PanGestureDetector::SetMinimumTouchesRequired(unsigned int minimum)
113 {
114   DALI_ASSERT_ALWAYS( minimum > 0 && "Can only set a positive number of required touches" );
115
116   if (mMinimumTouches != minimum)
117   {
118     DALI_LOG_INFO( gLogFilter, Debug::Concise, "Minimum Touches Set: %d\n", minimum );
119
120     mMinimumTouches = minimum;
121
122     if (!mAttachedActors.empty())
123     {
124       DALI_LOG_INFO( gLogFilter, Debug::General, "Updating Gesture Detector\n");
125
126       mGestureEventProcessor.GestureDetectorUpdated(this);
127     }
128   }
129 }
130
131 void PanGestureDetector::SetMaximumTouchesRequired(unsigned int maximum)
132 {
133   DALI_ASSERT_ALWAYS( maximum > 0 && "Can only set a positive number of maximum touches" );
134
135   if (mMaximumTouches != maximum)
136   {
137     DALI_LOG_INFO( gLogFilter, Debug::Concise, "Maximum Touches Set: %d\n", maximum );
138
139     mMaximumTouches = maximum;
140
141     if (!mAttachedActors.empty())
142     {
143       DALI_LOG_INFO( gLogFilter, Debug::General, "Updating Gesture Detector\n");
144
145       mGestureEventProcessor.GestureDetectorUpdated(this);
146     }
147   }
148 }
149
150 unsigned int PanGestureDetector::GetMinimumTouchesRequired() const
151 {
152   return mMinimumTouches;
153 }
154
155 unsigned int PanGestureDetector::GetMaximumTouchesRequired() const
156 {
157   return mMaximumTouches;
158 }
159
160 void PanGestureDetector::AddAngle( Radian angle, Radian threshold )
161 {
162   threshold = fabsf( threshold ); // Ensure the threshold is positive.
163
164   // If the threshold is greater than PI, then just use PI
165   // This means that any panned angle will invoke the pan gesture. We should still add this angle as
166   // an angle may have been added previously with a small threshold.
167   if ( threshold > Math::PI )
168   {
169     threshold = Math::PI;
170   }
171
172   angle = WrapInDomain( angle, -Math::PI, Math::PI );
173
174   DALI_LOG_INFO( gLogFilter, Debug::Concise, "Angle Added: %.2f, Threshold: %.2f\n", Degree(angle), Degree(threshold) );
175
176   AngleThresholdPair pair( angle, threshold );
177   mAngleContainer.push_back( pair );
178 }
179
180 void PanGestureDetector::AddDirection( Radian direction, Radian threshold )
181 {
182   AddAngle( direction, threshold );
183
184   // Calculate the opposite angle so that we cover the entire direction.
185   direction = GetOppositeAngle( direction );
186
187   AddAngle( direction, threshold );
188 }
189
190 size_t PanGestureDetector::GetAngleCount() const
191 {
192   return mAngleContainer.size();
193 }
194
195 PanGestureDetector::AngleThresholdPair PanGestureDetector::GetAngle(size_t index) const
196 {
197   PanGestureDetector::AngleThresholdPair ret( Radian(0),Radian(0) );
198
199   if( index < mAngleContainer.size() )
200   {
201     ret = mAngleContainer[index];
202   }
203
204   return ret;
205 }
206
207
208 void PanGestureDetector::ClearAngles()
209 {
210   mAngleContainer.clear();
211 }
212
213 void PanGestureDetector::RemoveAngle( Radian angle )
214 {
215   angle = WrapInDomain( angle, -Math::PI, Math::PI );
216
217   for (AngleContainer::iterator iter = mAngleContainer.begin(), endIter = mAngleContainer.end(); iter != endIter; ++iter )
218   {
219     if ( iter->first == angle )
220     {
221       mAngleContainer.erase( iter );
222       break;
223     }
224   }
225 }
226
227 void PanGestureDetector::RemoveDirection( Radian direction )
228 {
229   RemoveAngle( direction );
230
231   // Calculate the opposite angle so that we cover the entire direction.
232   direction = GetOppositeAngle( direction );
233
234   RemoveAngle( direction );
235 }
236
237 bool PanGestureDetector::RequiresDirectionalPan() const
238 {
239   // If no directional angles have been added to the container then we do not require directional panning
240   return !mAngleContainer.empty();
241 }
242
243 bool PanGestureDetector::CheckAngleAllowed( Radian angle ) const
244 {
245   bool allowed( false );
246   if ( mAngleContainer.empty() )
247   {
248     allowed = true;
249   }
250   else
251   {
252     for ( AngleContainer::const_iterator iter = mAngleContainer.begin(), endIter = mAngleContainer.end(); iter != endIter; ++iter )
253     {
254       float angleAllowed( iter->first );
255       float threshold ( iter->second );
256
257       DALI_LOG_INFO( gLogFilter, Debug::General,
258                      "AngleToCheck: %.2f, CompareWith: %.2f, Threshold: %.2f\n",
259                      Degree(angle), Degree(angleAllowed), Degree(threshold) );
260
261       float relativeAngle( fabsf( WrapInDomain( angle - angleAllowed, -Math::PI, Math::PI ) ) );
262       if ( relativeAngle <= threshold )
263       {
264         allowed = true;
265         break;
266       }
267     }
268   }
269
270   return allowed;
271 }
272
273 void PanGestureDetector::EmitPanGestureSignal(Dali::Actor actor, const PanGesture& pan)
274 {
275   if ( !mDetectedSignal.Empty() )
276   {
277     // Guard against destruction during signal emission
278     Dali::PanGestureDetector handle( this );
279
280     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Emitting Signal (%p)\n", this );
281
282     mDetectedSignal.Emit( actor, pan );
283   }
284 }
285
286 void PanGestureDetector::SetSceneObject( const SceneGraph::PanGesture* object )
287 {
288   mSceneObject = object;
289 }
290
291 bool PanGestureDetector::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
292 {
293   bool connected( true );
294   PanGestureDetector* gesture = dynamic_cast<PanGestureDetector*>(object);
295
296   if ( 0 == strcmp( signalName.c_str(), SIGNAL_PAN_DETECTED ) )
297   {
298     gesture->DetectedSignal().Connect( tracker, functor );
299   }
300   else
301   {
302     // signalName does not match any signal
303     connected = false;
304   }
305
306   return connected;
307 }
308
309 void PanGestureDetector::SetPanGestureProperties( const PanGesture& pan )
310 {
311   ThreadLocalStorage::Get().GetGestureEventProcessor().SetGestureProperties( pan );
312 }
313
314 void PanGestureDetector::OnActorAttach(Actor& actor)
315 {
316   // Do nothing
317 }
318
319 void PanGestureDetector::OnActorDetach(Actor& actor)
320 {
321   // Do nothing
322 }
323
324 void PanGestureDetector::OnActorDestroyed(Object& object)
325 {
326   // Do nothing
327 }
328
329 unsigned int PanGestureDetector::GetDefaultPropertyCount() const
330 {
331   return DEFAULT_PROPERTY_COUNT;
332 }
333
334 void PanGestureDetector::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
335 {
336   indices.Reserve( DEFAULT_PROPERTY_COUNT );
337
338   int index = DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX;
339   for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
340   {
341     indices.PushBack( index );
342   }
343 }
344
345 const char* PanGestureDetector::GetDefaultPropertyName( Property::Index index ) const
346 {
347   index -= DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX;
348   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
349   {
350     return DEFAULT_PROPERTY_DETAILS[ index ].name;
351   }
352
353   return NULL;
354 }
355
356 Property::Index PanGestureDetector::GetDefaultPropertyIndex(const std::string& name) const
357 {
358   Property::Index index = Property::INVALID_INDEX;
359
360   // Look for name in default properties
361   for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
362   {
363     const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
364     if( 0 == strcmp( name.c_str(), property->name ) ) // dont want to convert rhs to string
365     {
366       index = DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX + i;
367       break;
368     }
369   }
370   return index;
371 }
372
373 bool PanGestureDetector::IsDefaultPropertyWritable(Property::Index index) const
374 {
375   // None of our properties should be writable through the Public API
376   return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX ].writable;
377 }
378
379 bool PanGestureDetector::IsDefaultPropertyAnimatable(Property::Index index) const
380 {
381   // None of our properties are animatable
382   return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX ].animatable;
383 }
384
385 bool PanGestureDetector::IsDefaultPropertyAConstraintInput( Property::Index index ) const
386 {
387   // All our properties can be used as an input to a constraint.
388   return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX ].constraintInput;
389 }
390
391 Property::Type PanGestureDetector::GetDefaultPropertyType(Property::Index index) const
392 {
393   index -= DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX;
394   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
395   {
396     return DEFAULT_PROPERTY_DETAILS[ index ].type;
397   }
398   else
399   {
400     // Index out-of-range
401     return Property::NONE;
402   }
403 }
404
405 void PanGestureDetector::SetDefaultProperty( Property::Index index, const Property::Value& property )
406 {
407   // None of our properties should be settable from Public API
408 }
409
410 Property::Value PanGestureDetector::GetDefaultProperty(Property::Index index) const
411 {
412   Property::Value value;
413
414   switch ( index )
415   {
416     case Dali::PanGestureDetector::Property::SCREEN_POSITION:
417     {
418       if(mSceneObject)
419       {
420         value = mSceneObject->GetScreenPositionProperty().Get();
421       }
422       else
423       {
424         value = Vector2();
425       }
426       break;
427     }
428
429     case Dali::PanGestureDetector::Property::SCREEN_DISPLACEMENT:
430     {
431       if(mSceneObject)
432       {
433         value = mSceneObject->GetScreenDisplacementProperty().Get();
434       }
435       else
436       {
437         value = Vector2();
438       }
439       break;
440     }
441
442     case Dali::PanGestureDetector::Property::SCREEN_VELOCITY:
443     {
444       if(mSceneObject)
445       {
446         value = mSceneObject->GetScreenVelocityProperty().Get();
447       }
448       else
449       {
450         value = Vector2();
451       }
452       break;
453     }
454
455     case Dali::PanGestureDetector::Property::LOCAL_POSITION:
456     {
457       if(mSceneObject)
458       {
459         value = mSceneObject->GetLocalPositionProperty().Get();
460       }
461       else
462       {
463         value = Vector2();
464       }
465       break;
466     }
467
468     case Dali::PanGestureDetector::Property::LOCAL_DISPLACEMENT:
469     {
470       if(mSceneObject)
471       {
472         value = mSceneObject->GetLocalDisplacementProperty().Get();
473       }
474       else
475       {
476         value = Vector2();
477       }
478       break;
479     }
480
481     case Dali::PanGestureDetector::Property::LOCAL_VELOCITY:
482     {
483       if(mSceneObject)
484       {
485         value = mSceneObject->GetLocalVelocityProperty().Get();
486       }
487       else
488       {
489         value = Vector2();
490       }
491       break;
492     }
493
494     case Dali::PanGestureDetector::Property::PANNING:
495     {
496       if(mSceneObject)
497       {
498         value = mSceneObject->GetPanningProperty().Get();
499       }
500       else
501       {
502         value = false;
503       }
504       break;
505     }
506
507     default:
508     {
509       DALI_ASSERT_ALWAYS(false && "PanGestureDetector Property index invalid" ); // should not come here
510       break;
511     }
512   }
513
514   return value;
515 }
516
517 const SceneGraph::PropertyOwner* PanGestureDetector::GetSceneObject() const
518 {
519   // This method should only return an object connected to the scene-graph
520   return mSceneObject;
521 }
522
523 const SceneGraph::PropertyBase* PanGestureDetector::GetSceneObjectAnimatableProperty( Property::Index index ) const
524 {
525   DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
526
527   // None of our properties are animatable
528   return NULL;
529 }
530
531 const PropertyInputImpl* PanGestureDetector::GetSceneObjectInputProperty( Property::Index index ) const
532 {
533   const PropertyInputImpl* property( NULL );
534
535   // This method should only return a property of an object connected to the scene-graph
536   if ( !mSceneObject )
537   {
538     return property;
539   }
540
541   if ( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) && // Child properties are also stored as custom properties
542        ( index <= PROPERTY_CUSTOM_MAX_INDEX ) )
543   {
544     CustomPropertyMetadata* custom = FindCustomProperty( index );
545     DALI_ASSERT_ALWAYS( custom && "Property index is invalid" );
546     property = custom->GetSceneGraphProperty();
547   }
548   else
549   {
550     switch ( index )
551     {
552       case Dali::PanGestureDetector::Property::SCREEN_POSITION:
553       {
554         property = &mSceneObject->GetScreenPositionProperty();
555         break;
556       }
557
558       case Dali::PanGestureDetector::Property::SCREEN_DISPLACEMENT:
559       {
560         property = &mSceneObject->GetScreenDisplacementProperty();
561         break;
562       }
563
564       case Dali::PanGestureDetector::Property::SCREEN_VELOCITY:
565       {
566         property = &mSceneObject->GetScreenVelocityProperty();
567         break;
568       }
569
570       case Dali::PanGestureDetector::Property::LOCAL_POSITION:
571       {
572         property = &mSceneObject->GetLocalPositionProperty();
573         break;
574       }
575
576       case Dali::PanGestureDetector::Property::LOCAL_DISPLACEMENT:
577       {
578         property = &mSceneObject->GetLocalDisplacementProperty();
579         break;
580       }
581
582       case Dali::PanGestureDetector::Property::LOCAL_VELOCITY:
583       {
584         property = &mSceneObject->GetLocalVelocityProperty();
585         break;
586       }
587
588       case Dali::PanGestureDetector::Property::PANNING:
589       {
590         property = &mSceneObject->GetPanningProperty();
591         break;
592       }
593
594       default:
595         break;
596     }
597   }
598
599   return property;
600 }
601
602 } // namespace Internal
603
604 } // namespace Dali