d1f373437c25861974d7ff5d6e71c3ad12e4e5de
[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 ( !mDetectedSignal.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     mDetectedSignal.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 unsigned int PanGestureDetector::GetDefaultPropertyCount() const
328 {
329   return DEFAULT_PROPERTY_COUNT;
330 }
331
332 void PanGestureDetector::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
333 {
334   indices.reserve( DEFAULT_PROPERTY_COUNT );
335
336   int index = DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT;
337   for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
338   {
339     indices.push_back( index );
340   }
341 }
342
343 const char* PanGestureDetector::GetDefaultPropertyName( Property::Index index ) const
344 {
345   index -= DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT;
346   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
347   {
348     return DEFAULT_PROPERTIES[ index ].name;
349   }
350   else
351   {
352     return NULL;
353   }
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_PROPERTIES[ i ];
364     if( 0 == strcmp( name.c_str(), property->name ) ) // dont want to convert rhs to string
365     {
366       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 false;
377 }
378
379 bool PanGestureDetector::IsDefaultPropertyAnimatable(Property::Index index) const
380 {
381   // None of our properties are animatable
382   return false;
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 true;
389 }
390
391 Property::Type PanGestureDetector::GetDefaultPropertyType(Property::Index index) const
392 {
393   index -= DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT;
394   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
395   {
396     return DEFAULT_PROPERTIES[ 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::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::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::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::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::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::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::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 >= DEFAULT_PROPERTY_MAX_COUNT )
542   {
543     CustomProperty* custom = FindCustomProperty( index );
544     DALI_ASSERT_ALWAYS( custom && "Property index is invalid" );
545     property = custom->GetSceneGraphProperty();
546   }
547   else
548   {
549     switch ( index )
550     {
551       case Dali::PanGestureDetector::SCREEN_POSITION:
552       {
553         property = &mSceneObject->GetScreenPositionProperty();
554         break;
555       }
556
557       case Dali::PanGestureDetector::SCREEN_DISPLACEMENT:
558       {
559         property = &mSceneObject->GetScreenDisplacementProperty();
560         break;
561       }
562
563       case Dali::PanGestureDetector::SCREEN_VELOCITY:
564       {
565         property = &mSceneObject->GetScreenVelocityProperty();
566         break;
567       }
568
569       case Dali::PanGestureDetector::LOCAL_POSITION:
570       {
571         property = &mSceneObject->GetLocalPositionProperty();
572         break;
573       }
574
575       case Dali::PanGestureDetector::LOCAL_DISPLACEMENT:
576       {
577         property = &mSceneObject->GetLocalDisplacementProperty();
578         break;
579       }
580
581       case Dali::PanGestureDetector::LOCAL_VELOCITY:
582       {
583         property = &mSceneObject->GetLocalVelocityProperty();
584         break;
585       }
586
587       case Dali::PanGestureDetector::PANNING:
588       {
589         property = &mSceneObject->GetPanningProperty();
590         break;
591       }
592
593       default:
594         break;
595     }
596   }
597
598   return property;
599 }
600
601 } // namespace Internal
602
603 } // namespace Dali