2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/events/pan-gesture-detector-impl.h>
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>
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;
52 PropertyDetails DEFAULT_PROPERTIES[] =
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 },
62 const int DEFAULT_PROPERTY_COUNT = sizeof( DEFAULT_PROPERTIES ) / sizeof( DEFAULT_PROPERTIES[0] );
66 return Dali::PanGestureDetector::New();
69 TypeRegistration mType( typeid(Dali::PanGestureDetector), typeid(Dali::GestureDetector), Create );
71 SignalConnectorType signalConnector1( mType, Dali::PanGestureDetector::SIGNAL_PAN_DETECTED, &PanGestureDetector::DoConnectSignal );
73 #if defined(DEBUG_ENABLED)
74 Integration::Log::Filter* gLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_PAN_GESTURE_DETECTOR");
77 * When debugging, helper for converting radians to degrees.
79 inline float RadiansToDegrees( float radian )
81 return radian * 180.0f / Math::PI;
87 * Returns the angle going in the opposite direction to that specified by angle.
89 float GetOppositeAngle( float angle )
91 // Calculate the opposite angle so that we cover both directions.
104 } // unnamed namespace
106 PanGestureDetector::DefaultPropertyLookup* PanGestureDetector::mDefaultPropertyLookup = NULL;
108 PanGestureDetectorPtr PanGestureDetector::New()
110 return new PanGestureDetector;
113 PanGestureDetector::PanGestureDetector()
114 : GestureDetector(Gesture::Pan),
119 if( !mDefaultPropertyLookup )
121 mDefaultPropertyLookup = new DefaultPropertyLookup();
122 const int start = DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT;
123 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
125 ( *mDefaultPropertyLookup )[ DEFAULT_PROPERTIES[i].name ] = i + start;
130 PanGestureDetector::~PanGestureDetector()
134 void PanGestureDetector::SetMinimumTouchesRequired(unsigned int minimum)
136 DALI_ASSERT_ALWAYS( minimum > 0 && "Can only set a positive number of required touches" );
138 if (mMinimumTouches != minimum)
140 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Minimum Touches Set: %d\n", minimum );
142 mMinimumTouches = minimum;
144 if (!mAttachedActors.empty())
146 DALI_LOG_INFO( gLogFilter, Debug::General, "Updating Gesture Detector\n");
148 mGestureEventProcessor.GestureDetectorUpdated(this);
153 void PanGestureDetector::SetMaximumTouchesRequired(unsigned int maximum)
155 DALI_ASSERT_ALWAYS( maximum > 0 && "Can only set a positive number of maximum touches" );
157 if (mMaximumTouches != maximum)
159 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Maximum Touches Set: %d\n", maximum );
161 mMaximumTouches = maximum;
163 if (!mAttachedActors.empty())
165 DALI_LOG_INFO( gLogFilter, Debug::General, "Updating Gesture Detector\n");
167 mGestureEventProcessor.GestureDetectorUpdated(this);
172 unsigned int PanGestureDetector::GetMinimumTouchesRequired() const
174 return mMinimumTouches;
177 unsigned int PanGestureDetector::GetMaximumTouchesRequired() const
179 return mMaximumTouches;
182 void PanGestureDetector::AddAngle( Radian angle, Radian threshold )
184 threshold = fabsf( threshold ); // Ensure the threshold is positive.
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 )
191 threshold = Math::PI;
194 angle = WrapInDomain( angle, -Math::PI, Math::PI );
196 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Angle Added: %.2f, Threshold: %.2f\n", RadiansToDegrees(angle), RadiansToDegrees(threshold) );
198 AngleThresholdPair pair( angle, threshold );
199 mAngleContainer.push_back( pair );
202 void PanGestureDetector::AddDirection( Radian direction, Radian threshold )
204 AddAngle( direction, threshold );
206 // Calculate the opposite angle so that we cover the entire direction.
207 direction = GetOppositeAngle( direction );
209 AddAngle( direction, threshold );
212 const PanGestureDetector::AngleContainer& PanGestureDetector::GetAngles() const
214 return mAngleContainer;
217 void PanGestureDetector::ClearAngles()
219 mAngleContainer.clear();
222 void PanGestureDetector::RemoveAngle( Radian angle )
224 angle = WrapInDomain( angle, -Math::PI, Math::PI );
226 for (AngleContainer::iterator iter = mAngleContainer.begin(), endIter = mAngleContainer.end(); iter != endIter; ++iter )
228 if ( iter->first == angle )
230 mAngleContainer.erase( iter );
236 void PanGestureDetector::RemoveDirection( Radian direction )
238 RemoveAngle( direction );
240 // Calculate the opposite angle so that we cover the entire direction.
241 direction = GetOppositeAngle( direction );
243 RemoveAngle( direction );
246 bool PanGestureDetector::RequiresDirectionalPan() const
248 // If no directional angles have been added to the container then we do not require directional panning
249 return !mAngleContainer.empty();
252 bool PanGestureDetector::CheckAngleAllowed( Radian angle ) const
254 bool allowed( false );
255 if ( mAngleContainer.empty() )
261 for ( AngleContainer::const_iterator iter = mAngleContainer.begin(), endIter = mAngleContainer.end(); iter != endIter; ++iter )
263 float angleAllowed( iter->first );
264 float threshold ( iter->second );
266 DALI_LOG_INFO( gLogFilter, Debug::General,
267 "AngleToCheck: %.2f, CompareWith: %.2f, Threshold: %.2f\n",
268 RadiansToDegrees(angle), RadiansToDegrees(angleAllowed), RadiansToDegrees(threshold) );
270 float relativeAngle( fabsf( WrapInDomain( angle - angleAllowed, -Math::PI, Math::PI ) ) );
271 if ( relativeAngle <= threshold )
282 void PanGestureDetector::EmitPanGestureSignal(Dali::Actor actor, const PanGesture& pan)
284 if ( !mDetectedSignalV2.Empty() )
286 // Guard against destruction during signal emission
287 Dali::PanGestureDetector handle( this );
289 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Emitting Signal (%p)\n", this );
291 mDetectedSignalV2.Emit( actor, pan );
295 void PanGestureDetector::SetSceneObject( const SceneGraph::PanGesture* object )
297 mSceneObject = object;
300 bool PanGestureDetector::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
302 bool connected( true );
303 PanGestureDetector* gesture = dynamic_cast<PanGestureDetector*>(object);
305 if ( Dali::PanGestureDetector::SIGNAL_PAN_DETECTED == signalName )
307 gesture->DetectedSignal().Connect( tracker, functor );
311 // signalName does not match any signal
318 void PanGestureDetector::SetPanGestureProperties( const PanGesture& pan )
320 ThreadLocalStorage::Get().GetGestureEventProcessor().SetGestureProperties( pan );
323 void PanGestureDetector::OnActorAttach(Actor& actor)
328 void PanGestureDetector::OnActorDetach(Actor& actor)
333 void PanGestureDetector::OnActorDestroyed(Object& object)
338 bool PanGestureDetector::IsSceneObjectRemovable() const
343 unsigned int PanGestureDetector::GetDefaultPropertyCount() const
345 return DEFAULT_PROPERTY_COUNT;
348 void PanGestureDetector::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
350 indices.reserve( DEFAULT_PROPERTY_COUNT );
352 int index = DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT;
353 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
355 indices.push_back( index );
359 const std::string& PanGestureDetector::GetDefaultPropertyName( Property::Index index ) const
361 index -= DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT;
362 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
364 return DEFAULT_PROPERTIES[ index ].name;
368 // Index out-of-range... return empty string.
369 static const std::string INVALID_PROPERTY_NAME;
370 return INVALID_PROPERTY_NAME;
374 Property::Index PanGestureDetector::GetDefaultPropertyIndex(const std::string& name) const
376 Property::Index index = Property::INVALID_INDEX;
378 DALI_ASSERT_DEBUG( NULL != mDefaultPropertyLookup );
380 // Look for name in default properties
381 DefaultPropertyLookup::const_iterator result = mDefaultPropertyLookup->find( name );
382 if ( mDefaultPropertyLookup->end() != result )
384 index = result->second;
390 bool PanGestureDetector::IsDefaultPropertyWritable(Property::Index index) const
392 // None of our properties should be writable through the Public API
396 bool PanGestureDetector::IsDefaultPropertyAnimatable(Property::Index index) const
398 // None of our properties are animatable
402 bool PanGestureDetector::IsDefaultPropertyAConstraintInput( Property::Index index ) const
404 // All our properties can be used as an input to a constraint.
408 Property::Type PanGestureDetector::GetDefaultPropertyType(Property::Index index) const
410 index -= DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT;
411 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
413 return DEFAULT_PROPERTIES[ index ].type;
417 // Index out-of-range
418 return Property::NONE;
422 void PanGestureDetector::SetDefaultProperty( Property::Index index, const Property::Value& property )
424 // None of our properties should be settable from Public API
427 void PanGestureDetector::SetCustomProperty( Property::Index index, const CustomProperty& entry, const Property::Value& value )
429 // None of our properties should be settable from Public API
432 Property::Value PanGestureDetector::GetDefaultProperty(Property::Index index) const
434 Property::Value value;
438 case Dali::PanGestureDetector::SCREEN_POSITION:
442 value = mSceneObject->GetScreenPositionProperty().Get();
451 case Dali::PanGestureDetector::SCREEN_DISPLACEMENT:
455 value = mSceneObject->GetScreenDisplacementProperty().Get();
464 case Dali::PanGestureDetector::SCREEN_VELOCITY:
468 value = mSceneObject->GetScreenVelocityProperty().Get();
477 case Dali::PanGestureDetector::LOCAL_POSITION:
481 value = mSceneObject->GetLocalPositionProperty().Get();
490 case Dali::PanGestureDetector::LOCAL_DISPLACEMENT:
494 value = mSceneObject->GetLocalDisplacementProperty().Get();
503 case Dali::PanGestureDetector::LOCAL_VELOCITY:
507 value = mSceneObject->GetLocalVelocityProperty().Get();
516 case Dali::PanGestureDetector::PANNING:
520 value = mSceneObject->GetPanningProperty().Get();
531 DALI_ASSERT_ALWAYS(false && "PanGestureDetector Property index invalid" ); // should not come here
539 void PanGestureDetector::InstallSceneObjectProperty( SceneGraph::PropertyBase& newProperty, const std::string& name, unsigned int index )
541 // We do not want the user to install custom properties
542 DALI_ASSERT_ALWAYS(false && "PanGestureDetector does not allow custom properties" );
545 const SceneGraph::PropertyOwner* PanGestureDetector::GetSceneObject() const
547 // This method should only return an object connected to the scene-graph
551 const SceneGraph::PropertyBase* PanGestureDetector::GetSceneObjectAnimatableProperty( Property::Index index ) const
553 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
555 // None of our properties are animatable
559 const PropertyInputImpl* PanGestureDetector::GetSceneObjectInputProperty( Property::Index index ) const
561 const PropertyInputImpl* property( NULL );
563 // This method should only return a property of an object connected to the scene-graph
569 if ( index >= DEFAULT_PROPERTY_MAX_COUNT )
571 CustomPropertyLookup::const_iterator entry = GetCustomPropertyLookup().find( index );
573 DALI_ASSERT_ALWAYS( GetCustomPropertyLookup().end() != entry && "property index is invalid" );
575 property = entry->second.GetSceneGraphProperty();
581 case Dali::PanGestureDetector::SCREEN_POSITION:
583 property = &mSceneObject->GetScreenPositionProperty();
587 case Dali::PanGestureDetector::SCREEN_DISPLACEMENT:
589 property = &mSceneObject->GetScreenDisplacementProperty();
593 case Dali::PanGestureDetector::SCREEN_VELOCITY:
595 property = &mSceneObject->GetScreenVelocityProperty();
599 case Dali::PanGestureDetector::LOCAL_POSITION:
601 property = &mSceneObject->GetLocalPositionProperty();
605 case Dali::PanGestureDetector::LOCAL_DISPLACEMENT:
607 property = &mSceneObject->GetLocalDisplacementProperty();
611 case Dali::PanGestureDetector::LOCAL_VELOCITY:
613 property = &mSceneObject->GetLocalVelocityProperty();
617 case Dali::PanGestureDetector::PANNING:
619 property = &mSceneObject->GetPanningProperty();
631 } // namespace Internal