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