Reducing boilerplate on default property metadata
[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, PanGestureDetectorDefaultProperties )
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, PanGestureDetectorDefaultProperties );
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 void PanGestureDetector::SetDefaultProperty( Property::Index index, const Property::Value& property )
330 {
331   // None of our properties should be settable from Public API
332 }
333
334 Property::Value PanGestureDetector::GetDefaultProperty( Property::Index index ) const
335 {
336   return GetDefaultPropertyCurrentValue( index ); // Scene-graph only properties
337 }
338
339 Property::Value PanGestureDetector::GetDefaultPropertyCurrentValue(Property::Index index) const
340 {
341   Property::Value value;
342
343   switch ( index )
344   {
345     case Dali::PanGestureDetector::Property::SCREEN_POSITION:
346     {
347       if(mSceneObject)
348       {
349         value = mSceneObject->GetScreenPositionProperty().Get();
350       }
351       else
352       {
353         value = Vector2();
354       }
355       break;
356     }
357
358     case Dali::PanGestureDetector::Property::SCREEN_DISPLACEMENT:
359     {
360       if(mSceneObject)
361       {
362         value = mSceneObject->GetScreenDisplacementProperty().Get();
363       }
364       else
365       {
366         value = Vector2();
367       }
368       break;
369     }
370
371     case Dali::PanGestureDetector::Property::SCREEN_VELOCITY:
372     {
373       if(mSceneObject)
374       {
375         value = mSceneObject->GetScreenVelocityProperty().Get();
376       }
377       else
378       {
379         value = Vector2();
380       }
381       break;
382     }
383
384     case Dali::PanGestureDetector::Property::LOCAL_POSITION:
385     {
386       if(mSceneObject)
387       {
388         value = mSceneObject->GetLocalPositionProperty().Get();
389       }
390       else
391       {
392         value = Vector2();
393       }
394       break;
395     }
396
397     case Dali::PanGestureDetector::Property::LOCAL_DISPLACEMENT:
398     {
399       if(mSceneObject)
400       {
401         value = mSceneObject->GetLocalDisplacementProperty().Get();
402       }
403       else
404       {
405         value = Vector2();
406       }
407       break;
408     }
409
410     case Dali::PanGestureDetector::Property::LOCAL_VELOCITY:
411     {
412       if(mSceneObject)
413       {
414         value = mSceneObject->GetLocalVelocityProperty().Get();
415       }
416       else
417       {
418         value = Vector2();
419       }
420       break;
421     }
422
423     case Dali::PanGestureDetector::Property::PANNING:
424     {
425       if(mSceneObject)
426       {
427         value = mSceneObject->GetPanningProperty().Get();
428       }
429       else
430       {
431         value = false;
432       }
433       break;
434     }
435
436     default:
437     {
438       DALI_ASSERT_ALWAYS(false && "PanGestureDetector Property index invalid" ); // should not come here
439       break;
440     }
441   }
442
443   return value;
444 }
445
446 const SceneGraph::PropertyOwner* PanGestureDetector::GetSceneObject() const
447 {
448   // This method should only return an object connected to the scene-graph
449   return mSceneObject;
450 }
451
452 const SceneGraph::PropertyBase* PanGestureDetector::GetSceneObjectAnimatableProperty( Property::Index index ) const
453 {
454   DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
455
456   // None of our properties are animatable
457   return NULL;
458 }
459
460 const PropertyInputImpl* PanGestureDetector::GetSceneObjectInputProperty( Property::Index index ) const
461 {
462   const PropertyInputImpl* property( NULL );
463
464   // This method should only return a property of an object connected to the scene-graph
465   if ( !mSceneObject )
466   {
467     return property;
468   }
469
470   if ( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) && // Child properties are also stored as custom properties
471        ( index <= PROPERTY_CUSTOM_MAX_INDEX ) )
472   {
473     CustomPropertyMetadata* custom = FindCustomProperty( index );
474     DALI_ASSERT_ALWAYS( custom && "Property index is invalid" );
475     property = custom->GetSceneGraphProperty();
476   }
477   else
478   {
479     switch ( index )
480     {
481       case Dali::PanGestureDetector::Property::SCREEN_POSITION:
482       {
483         property = &mSceneObject->GetScreenPositionProperty();
484         break;
485       }
486
487       case Dali::PanGestureDetector::Property::SCREEN_DISPLACEMENT:
488       {
489         property = &mSceneObject->GetScreenDisplacementProperty();
490         break;
491       }
492
493       case Dali::PanGestureDetector::Property::SCREEN_VELOCITY:
494       {
495         property = &mSceneObject->GetScreenVelocityProperty();
496         break;
497       }
498
499       case Dali::PanGestureDetector::Property::LOCAL_POSITION:
500       {
501         property = &mSceneObject->GetLocalPositionProperty();
502         break;
503       }
504
505       case Dali::PanGestureDetector::Property::LOCAL_DISPLACEMENT:
506       {
507         property = &mSceneObject->GetLocalDisplacementProperty();
508         break;
509       }
510
511       case Dali::PanGestureDetector::Property::LOCAL_VELOCITY:
512       {
513         property = &mSceneObject->GetLocalVelocityProperty();
514         break;
515       }
516
517       case Dali::PanGestureDetector::Property::PANNING:
518       {
519         property = &mSceneObject->GetPanningProperty();
520         break;
521       }
522
523       default:
524         break;
525     }
526   }
527
528   return property;
529 }
530
531 } // namespace Internal
532
533 } // namespace Dali