Synchronous Set/Get behaviour for default properties
[platform/core/uifw/dali-core.git] / dali / internal / event / events / pan-gesture-detector-impl.cpp
1 /*
2  * Copyright (c) 2017 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 = static_cast< PanGestureDetector* >(object); // TypeRegistry guarantees that this is the correct type.
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   return GetDefaultPropertyCurrentValue( index ); // Scene-graph only properties
413 }
414
415 Property::Value PanGestureDetector::GetDefaultPropertyCurrentValue(Property::Index index) const
416 {
417   Property::Value value;
418
419   switch ( index )
420   {
421     case Dali::PanGestureDetector::Property::SCREEN_POSITION:
422     {
423       if(mSceneObject)
424       {
425         value = mSceneObject->GetScreenPositionProperty().Get();
426       }
427       else
428       {
429         value = Vector2();
430       }
431       break;
432     }
433
434     case Dali::PanGestureDetector::Property::SCREEN_DISPLACEMENT:
435     {
436       if(mSceneObject)
437       {
438         value = mSceneObject->GetScreenDisplacementProperty().Get();
439       }
440       else
441       {
442         value = Vector2();
443       }
444       break;
445     }
446
447     case Dali::PanGestureDetector::Property::SCREEN_VELOCITY:
448     {
449       if(mSceneObject)
450       {
451         value = mSceneObject->GetScreenVelocityProperty().Get();
452       }
453       else
454       {
455         value = Vector2();
456       }
457       break;
458     }
459
460     case Dali::PanGestureDetector::Property::LOCAL_POSITION:
461     {
462       if(mSceneObject)
463       {
464         value = mSceneObject->GetLocalPositionProperty().Get();
465       }
466       else
467       {
468         value = Vector2();
469       }
470       break;
471     }
472
473     case Dali::PanGestureDetector::Property::LOCAL_DISPLACEMENT:
474     {
475       if(mSceneObject)
476       {
477         value = mSceneObject->GetLocalDisplacementProperty().Get();
478       }
479       else
480       {
481         value = Vector2();
482       }
483       break;
484     }
485
486     case Dali::PanGestureDetector::Property::LOCAL_VELOCITY:
487     {
488       if(mSceneObject)
489       {
490         value = mSceneObject->GetLocalVelocityProperty().Get();
491       }
492       else
493       {
494         value = Vector2();
495       }
496       break;
497     }
498
499     case Dali::PanGestureDetector::Property::PANNING:
500     {
501       if(mSceneObject)
502       {
503         value = mSceneObject->GetPanningProperty().Get();
504       }
505       else
506       {
507         value = false;
508       }
509       break;
510     }
511
512     default:
513     {
514       DALI_ASSERT_ALWAYS(false && "PanGestureDetector Property index invalid" ); // should not come here
515       break;
516     }
517   }
518
519   return value;
520 }
521
522 const SceneGraph::PropertyOwner* PanGestureDetector::GetSceneObject() const
523 {
524   // This method should only return an object connected to the scene-graph
525   return mSceneObject;
526 }
527
528 const SceneGraph::PropertyBase* PanGestureDetector::GetSceneObjectAnimatableProperty( Property::Index index ) const
529 {
530   DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
531
532   // None of our properties are animatable
533   return NULL;
534 }
535
536 const PropertyInputImpl* PanGestureDetector::GetSceneObjectInputProperty( Property::Index index ) const
537 {
538   const PropertyInputImpl* property( NULL );
539
540   // This method should only return a property of an object connected to the scene-graph
541   if ( !mSceneObject )
542   {
543     return property;
544   }
545
546   if ( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) && // Child properties are also stored as custom properties
547        ( index <= PROPERTY_CUSTOM_MAX_INDEX ) )
548   {
549     CustomPropertyMetadata* custom = FindCustomProperty( index );
550     DALI_ASSERT_ALWAYS( custom && "Property index is invalid" );
551     property = custom->GetSceneGraphProperty();
552   }
553   else
554   {
555     switch ( index )
556     {
557       case Dali::PanGestureDetector::Property::SCREEN_POSITION:
558       {
559         property = &mSceneObject->GetScreenPositionProperty();
560         break;
561       }
562
563       case Dali::PanGestureDetector::Property::SCREEN_DISPLACEMENT:
564       {
565         property = &mSceneObject->GetScreenDisplacementProperty();
566         break;
567       }
568
569       case Dali::PanGestureDetector::Property::SCREEN_VELOCITY:
570       {
571         property = &mSceneObject->GetScreenVelocityProperty();
572         break;
573       }
574
575       case Dali::PanGestureDetector::Property::LOCAL_POSITION:
576       {
577         property = &mSceneObject->GetLocalPositionProperty();
578         break;
579       }
580
581       case Dali::PanGestureDetector::Property::LOCAL_DISPLACEMENT:
582       {
583         property = &mSceneObject->GetLocalDisplacementProperty();
584         break;
585       }
586
587       case Dali::PanGestureDetector::Property::LOCAL_VELOCITY:
588       {
589         property = &mSceneObject->GetLocalVelocityProperty();
590         break;
591       }
592
593       case Dali::PanGestureDetector::Property::PANNING:
594       {
595         property = &mSceneObject->GetPanningProperty();
596         break;
597       }
598
599       default:
600         break;
601     }
602   }
603
604   return property;
605 }
606
607 } // namespace Internal
608
609 } // namespace Dali