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