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-processor.h>
25 #include <dali/public-api/actors/actor.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/events/pan-gesture.h>
28 #include <dali/public-api/math/vector2.h>
29 #include <dali/integration-api/events/pan-gesture-event.h>
30 #include <dali/integration-api/gesture-manager.h>
31 #include <dali/integration-api/debug.h>
32 #include <dali/internal/event/common/stage-impl.h>
33 #include <dali/internal/event/render-tasks/render-task-impl.h>
34 #include <dali/internal/update/gestures/scene-graph-pan-gesture.h>
42 namespace // unnamed namespace
45 const unsigned long MAXIMUM_TIME_WITH_VALID_LAST_VELOCITY( 50u );
48 * Functor which checks whether the specified actor is attached to the gesture detector.
49 * If the actor is attached, it also checks whether the number of touches of the current pan event
50 * are within the range of that expected by the detector.
51 * It returns true if it is no longer attached or the touches are out of range.
52 * This can be used in remove_if functions.
54 struct IsNotAttachedAndOutsideTouchesRangeFunctor
58 * @param[in] actor The actor to check whether it is attached.
59 * @param[in] touches The number of touches in the current pan event.
60 * @param[in] outsideRangeEmitters Reference to container where emitters outside of the touches range should be added.
62 IsNotAttachedAndOutsideTouchesRangeFunctor(Actor* actor, unsigned int touches, GestureDetectorContainer& outsideRangeEmitters)
63 : actorToCheck(actor),
64 numberOfTouches(touches),
65 outsideTouchesRangeEmitters(outsideRangeEmitters)
70 * Returns true if not attached, false if it is still attached.
71 * Additionally, checks if the number of touches has changed and stops sending the pan to a particular
72 * detector if it exceeds the range of that detector.
73 * @param[in] detector The detector to check.
74 * @return true, if not attached, false otherwise.
76 bool operator()(GestureDetector* detector) const
78 bool remove(!detector->IsAttached(*actorToCheck));
82 PanGestureDetector* panDetector( static_cast< PanGestureDetector* >( detector ) );
84 // Ensure number of touch points is within the range of our emitter. If it isn't then remove
85 // this emitter and add it to the outsideTouchesRangeEmitters container
86 if ( (numberOfTouches < panDetector->GetMinimumTouchesRequired()) ||
87 (numberOfTouches > panDetector->GetMaximumTouchesRequired()) )
90 outsideTouchesRangeEmitters.push_back(detector);
97 Actor* actorToCheck; ///< The actor to check whether it is attached or not.
98 unsigned int numberOfTouches; ///< The number of touches in the pan event.
99 GestureDetectorContainer& outsideTouchesRangeEmitters; ///< Emitters that are outside of the range of current pan.
102 } // unnamed namespace
104 struct PanGestureProcessor::PanEventFunctor : public GestureProcessor::Functor
108 * @param[in] panEvent The current gesture event.
109 * @param[in] processor Reference to the processor.
111 PanEventFunctor( const Integration::PanGestureEvent& panEvent, PanGestureProcessor& processor )
112 : panEvent( panEvent ),
113 processor( processor )
118 * Check if the detector meets the current gesture event parameters.
120 virtual bool operator() ( GestureDetector* detector, Actor* actor )
122 bool retVal( false );
124 PanGestureDetector* panDetector( static_cast< PanGestureDetector* >( detector ) );
126 if ( ( panEvent.numberOfTouches >= panDetector->GetMinimumTouchesRequired() ) &&
127 ( panEvent.numberOfTouches <= panDetector->GetMaximumTouchesRequired() ) )
129 // Check if the detector requires directional panning.
130 if ( panDetector->RequiresDirectionalPan() && processor.mCurrentRenderTask )
132 // It does, calculate the angle of the pan in local actor coordinates and ensures it fits
133 // the detector's criteria.
134 RenderTask& renderTaskImpl( GetImplementation( processor.mCurrentRenderTask ) );
136 Vector2 startPosition, currentPosition;
137 actor->ScreenToLocal( renderTaskImpl, startPosition.x, startPosition.y, processor.mPossiblePanPosition.x, processor.mPossiblePanPosition.y );
138 actor->ScreenToLocal( renderTaskImpl, currentPosition.x, currentPosition.y, panEvent.currentPosition.x, panEvent.currentPosition.y );
139 Vector2 displacement( currentPosition - startPosition );
141 Radian angle( atan( displacement.y / displacement.x ) );
143 /////////////////////////////
146 // Q3 (-,-) | Q4 (+,-) //
148 // ----------------- +x //
150 // Q2 (-,+) | Q1 (+,+) //
154 /////////////////////////////
156 // Quadrant 2: 180 degrees + angle
157 // Quadrant 3: angle - 180 degrees
159 /////////////////////////////
161 if ( displacement.x < 0.0f )
163 if ( displacement.y >= 0.0f )
175 if ( panDetector->CheckAngleAllowed( angle ) )
182 // Directional panning not required so we can use this actor and gesture detector.
191 * Gestured actor and gesture detectors that meet the gesture's parameters found, emit and save required information.
193 virtual void operator() ( Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
195 processor.mCurrentPanEmitters.clear();
196 processor.ResetActor();
198 actor->ScreenToLocal( GetImplementation(processor.mCurrentRenderTask), actorCoordinates.x, actorCoordinates.y, panEvent.currentPosition.x, panEvent.currentPosition.y );
200 processor.EmitPanSignal( actor, gestureDetectors, panEvent, actorCoordinates, panEvent.state, processor.mCurrentRenderTask );
202 if ( actor->OnStage() )
204 processor.mCurrentPanEmitters = gestureDetectors;
205 processor.SetActor( actor );
209 const Integration::PanGestureEvent& panEvent;
210 PanGestureProcessor& processor;
213 PanGestureProcessor::PanGestureProcessor( Stage& stage, Integration::GestureManager& gestureManager )
214 : GestureProcessor( Gesture::Pan ),
216 mGestureManager( gestureManager ),
218 mCurrentPanEmitters(),
219 mCurrentRenderTask(),
220 mPossiblePanPosition(),
221 mMinTouchesRequired( 1 ),
222 mMaxTouchesRequired( 1 ),
223 mSceneObject( SceneGraph::PanGesture::New() ) // Create scene object to store pan information.
225 // Pass ownership to scene-graph
226 AddGestureMessage( mStage.GetUpdateManager(), mSceneObject );
229 PanGestureProcessor::~PanGestureProcessor()
231 if( Stage::IsInstalled() && ( mSceneObject != NULL ) )
233 RemoveGestureMessage( mStage.GetUpdateManager(), mSceneObject );
234 mSceneObject = NULL; // mSceneObject is about to be destroyed
238 void PanGestureProcessor::Process( const Integration::PanGestureEvent& panEvent )
240 switch( panEvent.state )
242 case Gesture::Possible:
244 mCurrentPanEmitters.clear();
247 HitTestAlgorithm::Results hitTestResults;
248 if( HitTest( mStage, panEvent.currentPosition, hitTestResults ) )
250 SetActor( &GetImplementation( hitTestResults.actor ) );
251 mPossiblePanPosition = panEvent.currentPosition;
257 case Gesture::Started:
259 if ( GetCurrentGesturedActor() )
261 // The pan gesture should only be sent to the gesture detector which first received it so that
262 // it can be told when the gesture ends as well.
264 HitTestAlgorithm::Results hitTestResults;
265 HitTest( mStage, mPossiblePanPosition, hitTestResults ); // Hit test original possible position...
267 if ( hitTestResults.actor && ( GetCurrentGesturedActor() == &GetImplementation( hitTestResults.actor ) ) )
269 // Record the current render-task for Screen->Actor coordinate conversions
270 mCurrentRenderTask = hitTestResults.renderTask;
272 PanEventFunctor functor( panEvent, *this );
273 ProcessAndEmit( hitTestResults, functor );
278 mCurrentPanEmitters.clear();
284 case Gesture::Continuing:
285 case Gesture::Finished:
286 case Gesture::Cancelled:
288 // Only send subsequent pan gesture signals if we processed the pan gesture when it started.
289 // Check if actor is still touchable.
291 Actor* currentGesturedActor = GetCurrentGesturedActor();
292 if ( currentGesturedActor )
294 if ( currentGesturedActor->IsHittable() && !mCurrentPanEmitters.empty() && mCurrentRenderTask )
296 GestureDetectorContainer outsideTouchesRangeEmitters;
298 // Removes emitters that no longer have the actor attached
299 // Also remove emitters whose touches are outside the range of the current pan event and add them to outsideTouchesRangeEmitters
300 GestureDetectorContainer::iterator endIter = std::remove_if( mCurrentPanEmitters.begin(), mCurrentPanEmitters.end(),
301 IsNotAttachedAndOutsideTouchesRangeFunctor(currentGesturedActor, panEvent.numberOfTouches, outsideTouchesRangeEmitters) );
302 mCurrentPanEmitters.erase( endIter, mCurrentPanEmitters.end() );
306 if ( !outsideTouchesRangeEmitters.empty() || !mCurrentPanEmitters.empty() )
308 currentGesturedActor->ScreenToLocal( GetImplementation( mCurrentRenderTask ), actorCoords.x, actorCoords.y, panEvent.currentPosition.x, panEvent.currentPosition.y );
310 // EmitPanSignal checks whether we have a valid actor and whether the container we are passing in has emitters before it emits the pan.
311 EmitPanSignal( currentGesturedActor, outsideTouchesRangeEmitters, panEvent, actorCoords, Gesture::Finished, mCurrentRenderTask);
312 EmitPanSignal( currentGesturedActor, mCurrentPanEmitters, panEvent, actorCoords, panEvent.state, mCurrentRenderTask);
315 if ( mCurrentPanEmitters.empty() )
317 // If we have no emitters attached then clear pan actor as well.
321 // Clear current gesture detectors if pan gesture has ended or been cancelled.
322 if ( ( panEvent.state == Gesture::Finished ) || ( panEvent.state == Gesture::Cancelled ) )
324 mCurrentPanEmitters.clear();
330 mCurrentPanEmitters.clear();
338 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Clear\n" );
343 void PanGestureProcessor::AddGestureDetector( PanGestureDetector* gestureDetector )
345 bool firstRegistration(mGestureDetectors.empty());
347 mGestureDetectors.push_back(gestureDetector);
349 // Set the pan scene object on the gesture detector
350 gestureDetector->SetSceneObject( mSceneObject );
352 if (firstRegistration)
354 mMinTouchesRequired = gestureDetector->GetMinimumTouchesRequired();
355 mMaxTouchesRequired = gestureDetector->GetMaximumTouchesRequired();
357 Integration::PanGestureRequest request;
358 request.minTouches = mMinTouchesRequired;
359 request.maxTouches = mMaxTouchesRequired;
360 mGestureManager.Register(request);
368 void PanGestureProcessor::RemoveGestureDetector( PanGestureDetector* gestureDetector )
370 if (!mCurrentPanEmitters.empty())
372 // Check if the removed detector was one that is currently being panned and remove it from emitters.
373 GestureDetectorContainer::iterator endIter = std::remove( mCurrentPanEmitters.begin(), mCurrentPanEmitters.end(), gestureDetector );
374 mCurrentPanEmitters.erase( endIter, mCurrentPanEmitters.end() );
376 // If we no longer have any emitters, then we should clear mCurrentGesturedActor as well
377 if ( mCurrentPanEmitters.empty() )
383 // Find the detector...
384 PanGestureDetectorContainer::iterator endIter = std::remove( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector );
385 DALI_ASSERT_DEBUG( endIter != mGestureDetectors.end() );
388 mGestureDetectors.erase(endIter, mGestureDetectors.end());
390 if (mGestureDetectors.empty())
392 Integration::GestureRequest request(Gesture::Pan);
393 mGestureManager.Unregister(request);
401 void PanGestureProcessor::GestureDetectorUpdated( PanGestureDetector* gestureDetector )
403 DALI_ASSERT_DEBUG(find(mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector) != mGestureDetectors.end());
408 void PanGestureProcessor::SetPanGestureProperties( const PanGesture& pan )
410 // If we are currently processing a pan gesture then just ignore
411 if ( mCurrentPanEmitters.empty() && mSceneObject )
413 // We update the scene object directly rather than sending a message.
414 // Sending a message could cause unnecessary delays, the scene object ensure thread safe behaviour.
415 mSceneObject->AddGesture( pan );
419 void PanGestureProcessor::EnableProfiling()
421 mSceneObject->EnableProfiling();
424 void PanGestureProcessor::SetPredictionMode(int mode)
427 || (mode >= SceneGraph::PanGesture::NUM_PREDICTION_MODES) )
429 mode = SceneGraph::PanGesture::DEFAULT_PREDICTION_MODE;
431 SceneGraph::PanGesture::PredictionMode modeEnum = static_cast<SceneGraph::PanGesture::PredictionMode>(mode);
432 mSceneObject->SetPredictionMode(modeEnum);
435 void PanGestureProcessor::SetPredictionAmount(unsigned int amount)
437 mSceneObject->SetPredictionAmount(amount);
440 void PanGestureProcessor::UpdateDetection()
442 DALI_ASSERT_DEBUG(!mGestureDetectors.empty());
444 unsigned int minimumRequired = UINT_MAX;
445 unsigned int maximumRequired = 0;
447 for ( PanGestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter != endIter; ++iter )
449 PanGestureDetector* detector(*iter);
451 unsigned int minimum = detector->GetMinimumTouchesRequired();
452 if (minimum < minimumRequired)
454 minimumRequired = minimum;
457 unsigned int maximum = detector->GetMaximumTouchesRequired();
458 if (maximum > maximumRequired)
460 maximumRequired = maximum;
464 if ( (minimumRequired != mMinTouchesRequired)||(maximumRequired != mMaxTouchesRequired) )
466 mMinTouchesRequired = minimumRequired;
467 mMaxTouchesRequired = maximumRequired;
469 Integration::PanGestureRequest request;
470 request.minTouches = mMinTouchesRequired;
471 request.maxTouches = mMaxTouchesRequired;
472 mGestureManager.Update(request);
476 void PanGestureProcessor::EmitPanSignal( Actor* actor,
477 const GestureDetectorContainer& gestureDetectors,
478 const Integration::PanGestureEvent& panEvent,
479 Vector2 localCurrent,
480 Gesture::State state,
481 Dali::RenderTask renderTask )
483 if ( actor && !gestureDetectors.empty() )
485 PanGesture pan(state);
486 pan.time = panEvent.time;
488 pan.numberOfTouches = panEvent.numberOfTouches;
489 pan.screenPosition = panEvent.currentPosition;
490 pan.position = localCurrent;
492 RenderTask& renderTaskImpl( GetImplementation( renderTask ) );
494 Vector2 localPrevious;
495 actor->ScreenToLocal( renderTaskImpl, localPrevious.x, localPrevious.y, panEvent.previousPosition.x, panEvent.previousPosition.y );
497 pan.displacement = localCurrent - localPrevious;
498 Vector2 previousPos( panEvent.previousPosition );
499 if ( state == Gesture::Started )
501 previousPos = mPossiblePanPosition;
504 pan.screenDisplacement = panEvent.currentPosition - previousPos;
506 pan.velocity.x = pan.displacement.x / panEvent.timeDelta;
507 pan.velocity.y = pan.displacement.y / panEvent.timeDelta;
509 pan.screenVelocity.x = pan.screenDisplacement.x / panEvent.timeDelta;
510 pan.screenVelocity.y = pan.screenDisplacement.y / panEvent.timeDelta;
512 // When the gesture ends, we may incorrectly get a ZERO velocity (as we have lifted our finger without any movement)
513 // so we should use the last recorded velocity instead in this scenario.
514 if ( ( state == Gesture::Finished ) && ( pan.screenVelocity == Vector2::ZERO ) &&
515 ( panEvent.timeDelta < MAXIMUM_TIME_WITH_VALID_LAST_VELOCITY ) )
517 pan.velocity = mLastVelocity;
518 pan.screenVelocity = mLastScreenVelocity;
522 // Store the current velocity for future iterations.
523 mLastVelocity = pan.velocity;
524 mLastScreenVelocity = pan.screenVelocity;
529 // We update the scene object directly rather than sending a message.
530 // Sending a message could cause unnecessary delays, the scene object ensure thread safe behaviour.
531 mSceneObject->AddGesture( pan );
534 Dali::Actor actorHandle( actor );
535 const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
536 for ( GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter )
538 static_cast< PanGestureDetector* >( *iter )->EmitPanGestureSignal( actorHandle, pan );
543 void PanGestureProcessor::OnGesturedActorStageDisconnection()
545 mCurrentPanEmitters.clear();
548 } // namespace Internal