Merge "Fixed shader compilation error handling" into tizen
[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 ( !mDetectedSignalV2.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     mDetectedSignalV2.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 void PanGestureDetector::SetCustomProperty( Property::Index index, const CustomProperty& entry, const Property::Value& value )
411 {
412   // None of our properties should be settable from Public API
413 }
414
415 Property::Value PanGestureDetector::GetDefaultProperty(Property::Index index) const
416 {
417   Property::Value value;
418
419   switch ( index )
420   {
421     case Dali::PanGestureDetector::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::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::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::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::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::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::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 void PanGestureDetector::InstallSceneObjectProperty( SceneGraph::PropertyBase& newProperty, const std::string& name, unsigned int index )
523 {
524   // We do not want the user to install custom properties
525   DALI_ASSERT_ALWAYS(false && "PanGestureDetector does not allow custom properties" );
526 }
527
528 const SceneGraph::PropertyOwner* PanGestureDetector::GetSceneObject() const
529 {
530   // This method should only return an object connected to the scene-graph
531   return mSceneObject;
532 }
533
534 const SceneGraph::PropertyBase* PanGestureDetector::GetSceneObjectAnimatableProperty( Property::Index index ) const
535 {
536   DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
537
538   // None of our properties are animatable
539   return NULL;
540 }
541
542 const PropertyInputImpl* PanGestureDetector::GetSceneObjectInputProperty( Property::Index index ) const
543 {
544   const PropertyInputImpl* property( NULL );
545
546   // This method should only return a property of an object connected to the scene-graph
547   if ( !mSceneObject )
548   {
549     return property;
550   }
551
552   if ( index >= DEFAULT_PROPERTY_MAX_COUNT )
553   {
554     CustomProperty* custom = FindCustomProperty( index );
555     DALI_ASSERT_ALWAYS( custom && "Property index is invalid" );
556     property = custom->GetSceneGraphProperty();
557   }
558   else
559   {
560     switch ( index )
561     {
562       case Dali::PanGestureDetector::SCREEN_POSITION:
563       {
564         property = &mSceneObject->GetScreenPositionProperty();
565         break;
566       }
567
568       case Dali::PanGestureDetector::SCREEN_DISPLACEMENT:
569       {
570         property = &mSceneObject->GetScreenDisplacementProperty();
571         break;
572       }
573
574       case Dali::PanGestureDetector::SCREEN_VELOCITY:
575       {
576         property = &mSceneObject->GetScreenVelocityProperty();
577         break;
578       }
579
580       case Dali::PanGestureDetector::LOCAL_POSITION:
581       {
582         property = &mSceneObject->GetLocalPositionProperty();
583         break;
584       }
585
586       case Dali::PanGestureDetector::LOCAL_DISPLACEMENT:
587       {
588         property = &mSceneObject->GetLocalDisplacementProperty();
589         break;
590       }
591
592       case Dali::PanGestureDetector::LOCAL_VELOCITY:
593       {
594         property = &mSceneObject->GetLocalVelocityProperty();
595         break;
596       }
597
598       case Dali::PanGestureDetector::PANNING:
599       {
600         property = &mSceneObject->GetPanningProperty();
601         break;
602       }
603
604       default:
605         break;
606     }
607   }
608
609   return property;
610 }
611
612 } // namespace Internal
613
614 } // namespace Dali