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