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 <cstring> // for strcmp
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>
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 )
60 const char* const SIGNAL_PAN_DETECTED = "panDetected";
64 return Dali::PanGestureDetector::New();
67 TypeRegistration mType( typeid(Dali::PanGestureDetector), typeid(Dali::GestureDetector), Create );
69 SignalConnectorType signalConnector1( mType, SIGNAL_PAN_DETECTED, &PanGestureDetector::DoConnectSignal );
71 #if defined(DEBUG_ENABLED)
72 Integration::Log::Filter* gLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_PAN_GESTURE_DETECTOR");
76 * Returns the angle going in the opposite direction to that specified by angle.
78 float GetOppositeAngle( float angle )
80 // Calculate the opposite angle so that we cover both directions.
93 } // unnamed namespace
95 PanGestureDetectorPtr PanGestureDetector::New()
97 return new PanGestureDetector;
100 PanGestureDetector::PanGestureDetector()
101 : GestureDetector(Gesture::Pan),
108 PanGestureDetector::~PanGestureDetector()
112 void PanGestureDetector::SetMinimumTouchesRequired(unsigned int minimum)
114 DALI_ASSERT_ALWAYS( minimum > 0 && "Can only set a positive number of required touches" );
116 if (mMinimumTouches != minimum)
118 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Minimum Touches Set: %d\n", minimum );
120 mMinimumTouches = minimum;
122 if (!mAttachedActors.empty())
124 DALI_LOG_INFO( gLogFilter, Debug::General, "Updating Gesture Detector\n");
126 mGestureEventProcessor.GestureDetectorUpdated(this);
131 void PanGestureDetector::SetMaximumTouchesRequired(unsigned int maximum)
133 DALI_ASSERT_ALWAYS( maximum > 0 && "Can only set a positive number of maximum touches" );
135 if (mMaximumTouches != maximum)
137 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Maximum Touches Set: %d\n", maximum );
139 mMaximumTouches = maximum;
141 if (!mAttachedActors.empty())
143 DALI_LOG_INFO( gLogFilter, Debug::General, "Updating Gesture Detector\n");
145 mGestureEventProcessor.GestureDetectorUpdated(this);
150 unsigned int PanGestureDetector::GetMinimumTouchesRequired() const
152 return mMinimumTouches;
155 unsigned int PanGestureDetector::GetMaximumTouchesRequired() const
157 return mMaximumTouches;
160 void PanGestureDetector::AddAngle( Radian angle, Radian threshold )
162 threshold = fabsf( threshold ); // Ensure the threshold is positive.
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 )
169 threshold = Math::PI;
172 angle = WrapInDomain( angle, -Math::PI, Math::PI );
174 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Angle Added: %.2f, Threshold: %.2f\n", Degree(angle), Degree(threshold) );
176 AngleThresholdPair pair( angle, threshold );
177 mAngleContainer.push_back( pair );
180 void PanGestureDetector::AddDirection( Radian direction, Radian threshold )
182 AddAngle( direction, threshold );
184 // Calculate the opposite angle so that we cover the entire direction.
185 direction = GetOppositeAngle( direction );
187 AddAngle( direction, threshold );
190 size_t PanGestureDetector::GetAngleCount() const
192 return mAngleContainer.size();
195 PanGestureDetector::AngleThresholdPair PanGestureDetector::GetAngle(size_t index) const
197 PanGestureDetector::AngleThresholdPair ret( Radian(0),Radian(0) );
199 if( index < mAngleContainer.size() )
201 ret = mAngleContainer[index];
208 void PanGestureDetector::ClearAngles()
210 mAngleContainer.clear();
213 void PanGestureDetector::RemoveAngle( Radian angle )
215 angle = WrapInDomain( angle, -Math::PI, Math::PI );
217 for (AngleContainer::iterator iter = mAngleContainer.begin(), endIter = mAngleContainer.end(); iter != endIter; ++iter )
219 if ( iter->first == angle )
221 mAngleContainer.erase( iter );
227 void PanGestureDetector::RemoveDirection( Radian direction )
229 RemoveAngle( direction );
231 // Calculate the opposite angle so that we cover the entire direction.
232 direction = GetOppositeAngle( direction );
234 RemoveAngle( direction );
237 bool PanGestureDetector::RequiresDirectionalPan() const
239 // If no directional angles have been added to the container then we do not require directional panning
240 return !mAngleContainer.empty();
243 bool PanGestureDetector::CheckAngleAllowed( Radian angle ) const
245 bool allowed( false );
246 if ( mAngleContainer.empty() )
252 for ( AngleContainer::const_iterator iter = mAngleContainer.begin(), endIter = mAngleContainer.end(); iter != endIter; ++iter )
254 float angleAllowed( iter->first );
255 float threshold ( iter->second );
257 DALI_LOG_INFO( gLogFilter, Debug::General,
258 "AngleToCheck: %.2f, CompareWith: %.2f, Threshold: %.2f\n",
259 Degree(angle), Degree(angleAllowed), Degree(threshold) );
261 float relativeAngle( fabsf( WrapInDomain( angle - angleAllowed, -Math::PI, Math::PI ) ) );
262 if ( relativeAngle <= threshold )
273 void PanGestureDetector::EmitPanGestureSignal(Dali::Actor actor, const PanGesture& pan)
275 if ( !mDetectedSignal.Empty() )
277 // Guard against destruction during signal emission
278 Dali::PanGestureDetector handle( this );
280 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Emitting Signal (%p)\n", this );
282 mDetectedSignal.Emit( actor, pan );
286 void PanGestureDetector::SetSceneObject( const SceneGraph::PanGesture* object )
288 mSceneObject = object;
291 bool PanGestureDetector::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
293 bool connected( true );
294 PanGestureDetector* gesture = dynamic_cast<PanGestureDetector*>(object);
296 if ( 0 == strcmp( signalName.c_str(), SIGNAL_PAN_DETECTED ) )
298 gesture->DetectedSignal().Connect( tracker, functor );
302 // signalName does not match any signal
309 void PanGestureDetector::SetPanGestureProperties( const PanGesture& pan )
311 ThreadLocalStorage::Get().GetGestureEventProcessor().SetGestureProperties( pan );
314 void PanGestureDetector::OnActorAttach(Actor& actor)
319 void PanGestureDetector::OnActorDetach(Actor& actor)
324 void PanGestureDetector::OnActorDestroyed(Object& object)
329 unsigned int PanGestureDetector::GetDefaultPropertyCount() const
331 return DEFAULT_PROPERTY_COUNT;
334 void PanGestureDetector::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
336 indices.Reserve( DEFAULT_PROPERTY_COUNT );
338 int index = DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX;
339 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
341 indices.PushBack( index );
345 const char* PanGestureDetector::GetDefaultPropertyName( Property::Index index ) const
347 index -= DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX;
348 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
350 return DEFAULT_PROPERTY_DETAILS[ index ].name;
356 Property::Index PanGestureDetector::GetDefaultPropertyIndex(const std::string& name) const
358 Property::Index index = Property::INVALID_INDEX;
360 // Look for name in default properties
361 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
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
366 index = DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX + i;
373 bool PanGestureDetector::IsDefaultPropertyWritable(Property::Index index) const
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;
379 bool PanGestureDetector::IsDefaultPropertyAnimatable(Property::Index index) const
381 // None of our properties are animatable
382 return DEFAULT_PROPERTY_DETAILS[ index - DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX ].animatable;
385 bool PanGestureDetector::IsDefaultPropertyAConstraintInput( Property::Index index ) const
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;
391 Property::Type PanGestureDetector::GetDefaultPropertyType(Property::Index index) const
393 index -= DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX;
394 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
396 return DEFAULT_PROPERTY_DETAILS[ index ].type;
400 // Index out-of-range
401 return Property::NONE;
405 void PanGestureDetector::SetDefaultProperty( Property::Index index, const Property::Value& property )
407 // None of our properties should be settable from Public API
410 Property::Value PanGestureDetector::GetDefaultProperty(Property::Index index) const
412 Property::Value value;
416 case Dali::PanGestureDetector::Property::SCREEN_POSITION:
420 value = mSceneObject->GetScreenPositionProperty().Get();
429 case Dali::PanGestureDetector::Property::SCREEN_DISPLACEMENT:
433 value = mSceneObject->GetScreenDisplacementProperty().Get();
442 case Dali::PanGestureDetector::Property::SCREEN_VELOCITY:
446 value = mSceneObject->GetScreenVelocityProperty().Get();
455 case Dali::PanGestureDetector::Property::LOCAL_POSITION:
459 value = mSceneObject->GetLocalPositionProperty().Get();
468 case Dali::PanGestureDetector::Property::LOCAL_DISPLACEMENT:
472 value = mSceneObject->GetLocalDisplacementProperty().Get();
481 case Dali::PanGestureDetector::Property::LOCAL_VELOCITY:
485 value = mSceneObject->GetLocalVelocityProperty().Get();
494 case Dali::PanGestureDetector::Property::PANNING:
498 value = mSceneObject->GetPanningProperty().Get();
509 DALI_ASSERT_ALWAYS(false && "PanGestureDetector Property index invalid" ); // should not come here
517 const SceneGraph::PropertyOwner* PanGestureDetector::GetSceneObject() const
519 // This method should only return an object connected to the scene-graph
523 const SceneGraph::PropertyBase* PanGestureDetector::GetSceneObjectAnimatableProperty( Property::Index index ) const
525 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
527 // None of our properties are animatable
531 const PropertyInputImpl* PanGestureDetector::GetSceneObjectInputProperty( Property::Index index ) const
533 const PropertyInputImpl* property( NULL );
535 // This method should only return a property of an object connected to the scene-graph
541 if ( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) && // Child properties are also stored as custom properties
542 ( index <= PROPERTY_CUSTOM_MAX_INDEX ) )
544 CustomPropertyMetadata* custom = FindCustomProperty( index );
545 DALI_ASSERT_ALWAYS( custom && "Property index is invalid" );
546 property = custom->GetSceneGraphProperty();
552 case Dali::PanGestureDetector::Property::SCREEN_POSITION:
554 property = &mSceneObject->GetScreenPositionProperty();
558 case Dali::PanGestureDetector::Property::SCREEN_DISPLACEMENT:
560 property = &mSceneObject->GetScreenDisplacementProperty();
564 case Dali::PanGestureDetector::Property::SCREEN_VELOCITY:
566 property = &mSceneObject->GetScreenVelocityProperty();
570 case Dali::PanGestureDetector::Property::LOCAL_POSITION:
572 property = &mSceneObject->GetLocalPositionProperty();
576 case Dali::PanGestureDetector::Property::LOCAL_DISPLACEMENT:
578 property = &mSceneObject->GetLocalDisplacementProperty();
582 case Dali::PanGestureDetector::Property::LOCAL_VELOCITY:
584 property = &mSceneObject->GetLocalVelocityProperty();
588 case Dali::PanGestureDetector::Property::PANNING:
590 property = &mSceneObject->GetPanningProperty();
602 } // namespace Internal