2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali/internal/event/events/pan-gesture-detector-impl.h>
21 #include <dali/public-api/events/pan-gesture.h>
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/public-api/math/radian.h>
24 #include <dali/public-api/math/degree.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/internal/event/common/property-index-ranges.h>
27 #include <dali/internal/event/common/thread-local-storage.h>
28 #include <dali/internal/event/events/gesture-event-processor.h>
29 #include <dali/internal/update/gestures/scene-graph-pan-gesture.h>
34 const Property::Index PanGestureDetector::SCREEN_POSITION = DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT;
35 const Property::Index PanGestureDetector::SCREEN_DISPLACEMENT = DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT + 1;
36 const Property::Index PanGestureDetector::LOCAL_POSITION = DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT + 2;
37 const Property::Index PanGestureDetector::LOCAL_DISPLACEMENT = DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT + 3;
47 const std::string DEFAULT_PROPERTY_NAMES[] =
50 "screen-displacement",
54 const int DEFAULT_PROPERTY_COUNT = sizeof( DEFAULT_PROPERTY_NAMES ) / sizeof( std::string );
56 const Property::Type DEFAULT_PROPERTY_TYPES[DEFAULT_PROPERTY_COUNT] =
58 Property::VECTOR2, // SCREEN_POSITION
59 Property::VECTOR2, // SCREEN_DISPLACEMENT
60 Property::VECTOR2, // LOCAL_POSITION
61 Property::VECTOR2, // LOCAL_DISPLACEMENT
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_PROPERTY_NAMES[i] ] = 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 const std::string& PanGestureDetector::GetDefaultPropertyName( Property::Index index ) const
350 // ProxyObject guarantees that index is within range
351 return DEFAULT_PROPERTY_NAMES[index];
354 Property::Index PanGestureDetector::GetDefaultPropertyIndex(const std::string& name) const
356 //Property::Index index = Property::INVALID_INDEX;
357 Property::Index index = 0;
359 DALI_ASSERT_DEBUG( NULL != mDefaultPropertyLookup );
361 // Look for name in default properties
362 DefaultPropertyLookup::const_iterator result = mDefaultPropertyLookup->find( name );
363 if ( mDefaultPropertyLookup->end() != result )
365 index = result->second;
371 bool PanGestureDetector::IsDefaultPropertyWritable(Property::Index index) const
373 // None of our properties should be writable through the Public API
377 bool PanGestureDetector::IsDefaultPropertyAnimatable(Property::Index index) const
379 // None of our properties are animatable
383 Property::Type PanGestureDetector::GetDefaultPropertyType(Property::Index index) const
385 // ProxyObject guarantees that index is within range
386 return DEFAULT_PROPERTY_TYPES[index];
389 void PanGestureDetector::SetDefaultProperty( Property::Index index, const Property::Value& property )
391 // None of our properties should be settable from Public API
394 void PanGestureDetector::SetCustomProperty( Property::Index index, const CustomProperty& entry, const Property::Value& value )
396 // None of our properties should be settable from Public API
399 Property::Value PanGestureDetector::GetDefaultProperty(Property::Index index) const
401 Property::Value value;
403 // ProxyObject guarantees that index is within range
406 case Dali::PanGestureDetector::SCREEN_POSITION:
410 value = mSceneObject->GetScreenPositionProperty().Get();
419 case Dali::PanGestureDetector::SCREEN_DISPLACEMENT:
423 value = mSceneObject->GetScreenDisplacementProperty().Get();
432 case Dali::PanGestureDetector::LOCAL_POSITION:
436 value = mSceneObject->GetLocalPositionProperty().Get();
445 case Dali::PanGestureDetector::LOCAL_DISPLACEMENT:
449 value = mSceneObject->GetLocalDisplacementProperty().Get();
460 DALI_ASSERT_ALWAYS(false && "PanGestureDetector Property index invalid" ); // should not come here
468 void PanGestureDetector::InstallSceneObjectProperty( SceneGraph::PropertyBase& newProperty, const std::string& name, unsigned int index )
470 // We do not want the user to install custom properties
471 DALI_ASSERT_ALWAYS(false && "PanGestureDetector does not allow custom properties" );
474 const SceneGraph::PropertyOwner* PanGestureDetector::GetSceneObject() const
476 // This method should only return an object connected to the scene-graph
480 const SceneGraph::PropertyBase* PanGestureDetector::GetSceneObjectAnimatableProperty( Property::Index index ) const
482 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
484 // None of our properties are animatable
488 const PropertyInputImpl* PanGestureDetector::GetSceneObjectInputProperty( Property::Index index ) const
490 const PropertyInputImpl* property( NULL );
492 // This method should only return a property of an object connected to the scene-graph
498 if ( index >= DEFAULT_PROPERTY_MAX_COUNT )
500 CustomPropertyLookup::const_iterator entry = GetCustomPropertyLookup().find( index );
502 DALI_ASSERT_ALWAYS( GetCustomPropertyLookup().end() != entry && "property index is invalid" );
504 property = entry->second.GetSceneGraphProperty();
510 case Dali::PanGestureDetector::SCREEN_POSITION:
512 property = &mSceneObject->GetScreenPositionProperty();
516 case Dali::PanGestureDetector::SCREEN_DISPLACEMENT:
518 property = &mSceneObject->GetScreenDisplacementProperty();
522 case Dali::PanGestureDetector::LOCAL_POSITION:
524 property = &mSceneObject->GetLocalPositionProperty();
528 case Dali::PanGestureDetector::LOCAL_DISPLACEMENT:
530 property = &mSceneObject->GetLocalDisplacementProperty();
542 } // namespace Internal