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 PanGestureDetectorPtr PanGestureDetector::New()
108 return new PanGestureDetector;
111 PanGestureDetector::PanGestureDetector()
112 : GestureDetector(Gesture::Pan),
119 PanGestureDetector::~PanGestureDetector()
123 void PanGestureDetector::SetMinimumTouchesRequired(unsigned int minimum)
125 DALI_ASSERT_ALWAYS( minimum > 0 && "Can only set a positive number of required touches" );
127 if (mMinimumTouches != minimum)
129 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Minimum Touches Set: %d\n", minimum );
131 mMinimumTouches = minimum;
133 if (!mAttachedActors.empty())
135 DALI_LOG_INFO( gLogFilter, Debug::General, "Updating Gesture Detector\n");
137 mGestureEventProcessor.GestureDetectorUpdated(this);
142 void PanGestureDetector::SetMaximumTouchesRequired(unsigned int maximum)
144 DALI_ASSERT_ALWAYS( maximum > 0 && "Can only set a positive number of maximum touches" );
146 if (mMaximumTouches != maximum)
148 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Maximum Touches Set: %d\n", maximum );
150 mMaximumTouches = maximum;
152 if (!mAttachedActors.empty())
154 DALI_LOG_INFO( gLogFilter, Debug::General, "Updating Gesture Detector\n");
156 mGestureEventProcessor.GestureDetectorUpdated(this);
161 unsigned int PanGestureDetector::GetMinimumTouchesRequired() const
163 return mMinimumTouches;
166 unsigned int PanGestureDetector::GetMaximumTouchesRequired() const
168 return mMaximumTouches;
171 void PanGestureDetector::AddAngle( Radian angle, Radian threshold )
173 threshold = fabsf( threshold ); // Ensure the threshold is positive.
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 )
180 threshold = Math::PI;
183 angle = WrapInDomain( angle, -Math::PI, Math::PI );
185 DALI_LOG_INFO( gLogFilter, Debug::Concise, "Angle Added: %.2f, Threshold: %.2f\n", RadiansToDegrees(angle), RadiansToDegrees(threshold) );
187 AngleThresholdPair pair( angle, threshold );
188 mAngleContainer.push_back( pair );
191 void PanGestureDetector::AddDirection( Radian direction, Radian threshold )
193 AddAngle( direction, threshold );
195 // Calculate the opposite angle so that we cover the entire direction.
196 direction = GetOppositeAngle( direction );
198 AddAngle( direction, threshold );
201 const PanGestureDetector::AngleContainer& PanGestureDetector::GetAngles() const
203 return mAngleContainer;
206 void PanGestureDetector::ClearAngles()
208 mAngleContainer.clear();
211 void PanGestureDetector::RemoveAngle( Radian angle )
213 angle = WrapInDomain( angle, -Math::PI, Math::PI );
215 for (AngleContainer::iterator iter = mAngleContainer.begin(), endIter = mAngleContainer.end(); iter != endIter; ++iter )
217 if ( iter->first == angle )
219 mAngleContainer.erase( iter );
225 void PanGestureDetector::RemoveDirection( Radian direction )
227 RemoveAngle( direction );
229 // Calculate the opposite angle so that we cover the entire direction.
230 direction = GetOppositeAngle( direction );
232 RemoveAngle( direction );
235 bool PanGestureDetector::RequiresDirectionalPan() const
237 // If no directional angles have been added to the container then we do not require directional panning
238 return !mAngleContainer.empty();
241 bool PanGestureDetector::CheckAngleAllowed( Radian angle ) const
243 bool allowed( false );
244 if ( mAngleContainer.empty() )
250 for ( AngleContainer::const_iterator iter = mAngleContainer.begin(), endIter = mAngleContainer.end(); iter != endIter; ++iter )
252 float angleAllowed( iter->first );
253 float threshold ( iter->second );
255 DALI_LOG_INFO( gLogFilter, Debug::General,
256 "AngleToCheck: %.2f, CompareWith: %.2f, Threshold: %.2f\n",
257 RadiansToDegrees(angle), RadiansToDegrees(angleAllowed), RadiansToDegrees(threshold) );
259 float relativeAngle( fabsf( WrapInDomain( angle - angleAllowed, -Math::PI, Math::PI ) ) );
260 if ( relativeAngle <= threshold )
271 void PanGestureDetector::EmitPanGestureSignal(Dali::Actor actor, const PanGesture& pan)
273 if ( !mDetectedSignalV2.Empty() )
275 // Guard against destruction during signal emission
276 Dali::PanGestureDetector handle( this );
278 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Emitting Signal (%p)\n", this );
280 mDetectedSignalV2.Emit( actor, pan );
284 void PanGestureDetector::SetSceneObject( const SceneGraph::PanGesture* object )
286 mSceneObject = object;
289 bool PanGestureDetector::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
291 bool connected( true );
292 PanGestureDetector* gesture = dynamic_cast<PanGestureDetector*>(object);
294 if ( Dali::PanGestureDetector::SIGNAL_PAN_DETECTED == signalName )
296 gesture->DetectedSignal().Connect( tracker, functor );
300 // signalName does not match any signal
307 void PanGestureDetector::SetPanGestureProperties( const PanGesture& pan )
309 ThreadLocalStorage::Get().GetGestureEventProcessor().SetGestureProperties( pan );
312 void PanGestureDetector::OnActorAttach(Actor& actor)
317 void PanGestureDetector::OnActorDetach(Actor& actor)
322 void PanGestureDetector::OnActorDestroyed(Object& object)
327 unsigned int PanGestureDetector::GetDefaultPropertyCount() const
329 return DEFAULT_PROPERTY_COUNT;
332 void PanGestureDetector::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
334 indices.reserve( DEFAULT_PROPERTY_COUNT );
336 int index = DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT;
337 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
339 indices.push_back( index );
343 const char* PanGestureDetector::GetDefaultPropertyName( Property::Index index ) const
345 index -= DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT;
346 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
348 return DEFAULT_PROPERTIES[ 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_PROPERTIES[ i ];
364 if( 0 == strcmp( name.c_str(), property->name ) ) // dont want to convert rhs to string
373 bool PanGestureDetector::IsDefaultPropertyWritable(Property::Index index) const
375 // None of our properties should be writable through the Public API
379 bool PanGestureDetector::IsDefaultPropertyAnimatable(Property::Index index) const
381 // None of our properties are animatable
385 bool PanGestureDetector::IsDefaultPropertyAConstraintInput( Property::Index index ) const
387 // All our properties can be used as an input to a constraint.
391 Property::Type PanGestureDetector::GetDefaultPropertyType(Property::Index index) const
393 index -= DEFAULT_GESTURE_DETECTOR_PROPERTY_MAX_COUNT;
394 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
396 return DEFAULT_PROPERTIES[ 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::SCREEN_POSITION:
420 value = mSceneObject->GetScreenPositionProperty().Get();
429 case Dali::PanGestureDetector::SCREEN_DISPLACEMENT:
433 value = mSceneObject->GetScreenDisplacementProperty().Get();
442 case Dali::PanGestureDetector::SCREEN_VELOCITY:
446 value = mSceneObject->GetScreenVelocityProperty().Get();
455 case Dali::PanGestureDetector::LOCAL_POSITION:
459 value = mSceneObject->GetLocalPositionProperty().Get();
468 case Dali::PanGestureDetector::LOCAL_DISPLACEMENT:
472 value = mSceneObject->GetLocalDisplacementProperty().Get();
481 case Dali::PanGestureDetector::LOCAL_VELOCITY:
485 value = mSceneObject->GetLocalVelocityProperty().Get();
494 case Dali::PanGestureDetector::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 >= DEFAULT_PROPERTY_MAX_COUNT )
543 CustomProperty* custom = FindCustomProperty( index );
544 DALI_ASSERT_ALWAYS( custom && "Property index is invalid" );
545 property = custom->GetSceneGraphProperty();
551 case Dali::PanGestureDetector::SCREEN_POSITION:
553 property = &mSceneObject->GetScreenPositionProperty();
557 case Dali::PanGestureDetector::SCREEN_DISPLACEMENT:
559 property = &mSceneObject->GetScreenDisplacementProperty();
563 case Dali::PanGestureDetector::SCREEN_VELOCITY:
565 property = &mSceneObject->GetScreenVelocityProperty();
569 case Dali::PanGestureDetector::LOCAL_POSITION:
571 property = &mSceneObject->GetLocalPositionProperty();
575 case Dali::PanGestureDetector::LOCAL_DISPLACEMENT:
577 property = &mSceneObject->GetLocalDisplacementProperty();
581 case Dali::PanGestureDetector::LOCAL_VELOCITY:
583 property = &mSceneObject->GetLocalVelocityProperty();
587 case Dali::PanGestureDetector::PANNING:
589 property = &mSceneObject->GetPanningProperty();
601 } // namespace Internal