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-helper.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>
44 // Name Type writable animatable constraint-input enum for index-checking
45 DALI_PROPERTY_TABLE_BEGIN
46 DALI_PROPERTY( "screen-position", VECTOR2, false, false, true, Dali::PanGestureDetector::Property::SCREEN_POSITION )
47 DALI_PROPERTY( "screen-displacement", VECTOR2, false, false, true, Dali::PanGestureDetector::Property::SCREEN_DISPLACEMENT )
48 DALI_PROPERTY( "screen-velocity", VECTOR2, false, false, true, Dali::PanGestureDetector::Property::SCREEN_VELOCITY )
49 DALI_PROPERTY( "local-position", VECTOR2, false, false, true, Dali::PanGestureDetector::Property::LOCAL_POSITION )
50 DALI_PROPERTY( "local-displacement", VECTOR2, false, false, true, Dali::PanGestureDetector::Property::LOCAL_DISPLACEMENT )
51 DALI_PROPERTY( "local-velocity", VECTOR2, false, false, true, Dali::PanGestureDetector::Property::LOCAL_VELOCITY )
52 DALI_PROPERTY( "panning", BOOLEAN, false, false, true, Dali::PanGestureDetector::Property::PANNING )
53 DALI_PROPERTY_TABLE_END( DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX )
57 const char* const SIGNAL_PAN_DETECTED = "pan-detected";
61 return Dali::PanGestureDetector::New();
64 TypeRegistration mType( typeid(Dali::PanGestureDetector), typeid(Dali::GestureDetector), Create );
66 SignalConnectorType signalConnector1( mType, SIGNAL_PAN_DETECTED, &PanGestureDetector::DoConnectSignal );
68 #if defined(DEBUG_ENABLED)
69 Integration::Log::Filter* gLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_PAN_GESTURE_DETECTOR");
72 * When debugging, helper for converting radians to degrees.
74 inline float RadiansToDegrees( float radian )
76 return radian * 180.0f / Math::PI;
82 * Returns the angle going in the opposite direction to that specified by angle.
84 float GetOppositeAngle( float angle )
86 // Calculate the opposite angle so that we cover both directions.
99 } // unnamed namespace
101 PanGestureDetectorPtr PanGestureDetector::New()
103 return new PanGestureDetector;
106 PanGestureDetector::PanGestureDetector()
107 : GestureDetector(Gesture::Pan),
114 PanGestureDetector::~PanGestureDetector()
118 void PanGestureDetector::SetMinimumTouchesRequired(unsigned int minimum)
120 DALI_ASSERT_ALWAYS( minimum > 0 && "Can only set a positive number of required touches" );
122 if (mMinimumTouches != minimum)
124 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Minimum Touches Set: %d\n", minimum );
126 mMinimumTouches = minimum;
128 if (!mAttachedActors.empty())
130 DALI_LOG_INFO( gLogFilter, Debug::General, "Updating Gesture Detector\n");
132 mGestureEventProcessor.GestureDetectorUpdated(this);
137 void PanGestureDetector::SetMaximumTouchesRequired(unsigned int maximum)
139 DALI_ASSERT_ALWAYS( maximum > 0 && "Can only set a positive number of maximum touches" );
141 if (mMaximumTouches != maximum)
143 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Maximum Touches Set: %d\n", maximum );
145 mMaximumTouches = maximum;
147 if (!mAttachedActors.empty())
149 DALI_LOG_INFO( gLogFilter, Debug::General, "Updating Gesture Detector\n");
151 mGestureEventProcessor.GestureDetectorUpdated(this);
156 unsigned int PanGestureDetector::GetMinimumTouchesRequired() const
158 return mMinimumTouches;
161 unsigned int PanGestureDetector::GetMaximumTouchesRequired() const
163 return mMaximumTouches;
166 void PanGestureDetector::AddAngle( Radian angle, Radian threshold )
168 threshold = fabsf( threshold ); // Ensure the threshold is positive.
170 // If the threshold is greater than PI, then just use PI
171 // This means that any panned angle will invoke the pan gesture. We should still add this angle as
172 // an angle may have been added previously with a small threshold.
173 if ( threshold > Math::PI )
175 threshold = Math::PI;
178 angle = WrapInDomain( angle, -Math::PI, Math::PI );
180 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Angle Added: %.2f, Threshold: %.2f\n", RadiansToDegrees(angle), RadiansToDegrees(threshold) );
182 AngleThresholdPair pair( angle, threshold );
183 mAngleContainer.push_back( pair );
186 void PanGestureDetector::AddDirection( Radian direction, Radian threshold )
188 AddAngle( direction, threshold );
190 // Calculate the opposite angle so that we cover the entire direction.
191 direction = GetOppositeAngle( direction );
193 AddAngle( direction, threshold );
196 const PanGestureDetector::AngleContainer& PanGestureDetector::GetAngles() const
198 return mAngleContainer;
201 void PanGestureDetector::ClearAngles()
203 mAngleContainer.clear();
206 void PanGestureDetector::RemoveAngle( Radian angle )
208 angle = WrapInDomain( angle, -Math::PI, Math::PI );
210 for (AngleContainer::iterator iter = mAngleContainer.begin(), endIter = mAngleContainer.end(); iter != endIter; ++iter )
212 if ( iter->first == angle )
214 mAngleContainer.erase( iter );
220 void PanGestureDetector::RemoveDirection( Radian direction )
222 RemoveAngle( direction );
224 // Calculate the opposite angle so that we cover the entire direction.
225 direction = GetOppositeAngle( direction );
227 RemoveAngle( direction );
230 bool PanGestureDetector::RequiresDirectionalPan() const
232 // If no directional angles have been added to the container then we do not require directional panning
233 return !mAngleContainer.empty();
236 bool PanGestureDetector::CheckAngleAllowed( Radian angle ) const
238 bool allowed( false );
239 if ( mAngleContainer.empty() )
245 for ( AngleContainer::const_iterator iter = mAngleContainer.begin(), endIter = mAngleContainer.end(); iter != endIter; ++iter )
247 float angleAllowed( iter->first );
248 float threshold ( iter->second );
250 DALI_LOG_INFO( gLogFilter, Debug::General,
251 "AngleToCheck: %.2f, CompareWith: %.2f, Threshold: %.2f\n",
252 RadiansToDegrees(angle), RadiansToDegrees(angleAllowed), RadiansToDegrees(threshold) );
254 float relativeAngle( fabsf( WrapInDomain( angle - angleAllowed, -Math::PI, Math::PI ) ) );
255 if ( relativeAngle <= threshold )
266 void PanGestureDetector::EmitPanGestureSignal(Dali::Actor actor, const PanGesture& pan)
268 if ( !mDetectedSignal.Empty() )
270 // Guard against destruction during signal emission
271 Dali::PanGestureDetector handle( this );
273 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Emitting Signal (%p)\n", this );
275 mDetectedSignal.Emit( actor, pan );
279 void PanGestureDetector::SetSceneObject( const SceneGraph::PanGesture* object )
281 mSceneObject = object;
284 bool PanGestureDetector::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
286 bool connected( true );
287 PanGestureDetector* gesture = dynamic_cast<PanGestureDetector*>(object);
289 if ( 0 == strcmp( signalName.c_str(), SIGNAL_PAN_DETECTED ) )
291 gesture->DetectedSignal().Connect( tracker, functor );
295 // signalName does not match any signal
302 void PanGestureDetector::SetPanGestureProperties( const PanGesture& pan )
304 ThreadLocalStorage::Get().GetGestureEventProcessor().SetGestureProperties( pan );
307 void PanGestureDetector::OnActorAttach(Actor& actor)
312 void PanGestureDetector::OnActorDetach(Actor& actor)
317 void PanGestureDetector::OnActorDestroyed(Object& object)
322 unsigned int PanGestureDetector::GetDefaultPropertyCount() const
324 return DEFAULT_PROPERTY_COUNT;
327 void PanGestureDetector::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
329 indices.reserve( DEFAULT_PROPERTY_COUNT );
331 int index = DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX;
332 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
334 indices.push_back( index );
338 const char* PanGestureDetector::GetDefaultPropertyName( Property::Index index ) const
340 index -= DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX;
341 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
343 return DEFAULT_PROPERTY_DETAILS[ index ].name;
349 Property::Index PanGestureDetector::GetDefaultPropertyIndex(const std::string& name) const
351 Property::Index index = Property::INVALID_INDEX;
353 // Look for name in default properties
354 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
356 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
357 if( 0 == strcmp( name.c_str(), property->name ) ) // dont want to convert rhs to string
366 bool PanGestureDetector::IsDefaultPropertyWritable(Property::Index index) const
368 // None of our properties should be writable through the Public API
369 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX ].writable;
372 bool PanGestureDetector::IsDefaultPropertyAnimatable(Property::Index index) const
374 // None of our properties are animatable
375 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX ].animatable;
378 bool PanGestureDetector::IsDefaultPropertyAConstraintInput( Property::Index index ) const
380 // All our properties can be used as an input to a constraint.
381 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX ].constraintInput;
384 Property::Type PanGestureDetector::GetDefaultPropertyType(Property::Index index) const
386 index -= DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX;
387 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
389 return DEFAULT_PROPERTY_DETAILS[ index ].type;
393 // Index out-of-range
394 return Property::NONE;
398 void PanGestureDetector::SetDefaultProperty( Property::Index index, const Property::Value& property )
400 // None of our properties should be settable from Public API
403 Property::Value PanGestureDetector::GetDefaultProperty(Property::Index index) const
405 Property::Value value;
409 case Dali::PanGestureDetector::Property::SCREEN_POSITION:
413 value = mSceneObject->GetScreenPositionProperty().Get();
422 case Dali::PanGestureDetector::Property::SCREEN_DISPLACEMENT:
426 value = mSceneObject->GetScreenDisplacementProperty().Get();
435 case Dali::PanGestureDetector::Property::SCREEN_VELOCITY:
439 value = mSceneObject->GetScreenVelocityProperty().Get();
448 case Dali::PanGestureDetector::Property::LOCAL_POSITION:
452 value = mSceneObject->GetLocalPositionProperty().Get();
461 case Dali::PanGestureDetector::Property::LOCAL_DISPLACEMENT:
465 value = mSceneObject->GetLocalDisplacementProperty().Get();
474 case Dali::PanGestureDetector::Property::LOCAL_VELOCITY:
478 value = mSceneObject->GetLocalVelocityProperty().Get();
487 case Dali::PanGestureDetector::Property::PANNING:
491 value = mSceneObject->GetPanningProperty().Get();
502 DALI_ASSERT_ALWAYS(false && "PanGestureDetector Property index invalid" ); // should not come here
510 const SceneGraph::PropertyOwner* PanGestureDetector::GetSceneObject() const
512 // This method should only return an object connected to the scene-graph
516 const SceneGraph::PropertyBase* PanGestureDetector::GetSceneObjectAnimatableProperty( Property::Index index ) const
518 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
520 // None of our properties are animatable
524 const PropertyInputImpl* PanGestureDetector::GetSceneObjectInputProperty( Property::Index index ) const
526 const PropertyInputImpl* property( NULL );
528 // This method should only return a property of an object connected to the scene-graph
534 if ( index >= DEFAULT_PROPERTY_MAX_COUNT )
536 CustomPropertyMetadata* custom = FindCustomProperty( index );
537 DALI_ASSERT_ALWAYS( custom && "Property index is invalid" );
538 property = custom->GetSceneGraphProperty();
544 case Dali::PanGestureDetector::Property::SCREEN_POSITION:
546 property = &mSceneObject->GetScreenPositionProperty();
550 case Dali::PanGestureDetector::Property::SCREEN_DISPLACEMENT:
552 property = &mSceneObject->GetScreenDisplacementProperty();
556 case Dali::PanGestureDetector::Property::SCREEN_VELOCITY:
558 property = &mSceneObject->GetScreenVelocityProperty();
562 case Dali::PanGestureDetector::Property::LOCAL_POSITION:
564 property = &mSceneObject->GetLocalPositionProperty();
568 case Dali::PanGestureDetector::Property::LOCAL_DISPLACEMENT:
570 property = &mSceneObject->GetLocalDisplacementProperty();
574 case Dali::PanGestureDetector::Property::LOCAL_VELOCITY:
576 property = &mSceneObject->GetLocalVelocityProperty();
580 case Dali::PanGestureDetector::Property::PANNING:
582 property = &mSceneObject->GetPanningProperty();
594 } // namespace Internal