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-processor.h>
24 #include <dali/public-api/actors/actor.h>
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/events/pan-gesture.h>
27 #include <dali/public-api/math/vector2.h>
28 #include <dali/integration-api/events/pan-gesture-event.h>
29 #include <dali/integration-api/gesture-manager.h>
30 #include <dali/integration-api/debug.h>
31 #include <dali/internal/event/common/stage-impl.h>
32 #include <dali/internal/event/render-tasks/render-task-impl.h>
33 #include <dali/internal/update/gestures/scene-graph-pan-gesture.h>
41 namespace // unnamed namespace
44 const unsigned long MAXIMUM_TIME_WITH_VALID_LAST_VELOCITY( 50u );
47 * Functor which checks whether the specified actor is attached to the gesture detector.
48 * If the actor is attached, it also checks whether the number of touches of the current pan event
49 * are within the range of that expected by the detector.
50 * It returns true if it is no longer attached or the touches are out of range.
51 * This can be used in remove_if functions.
53 struct IsNotAttachedAndOutsideTouchesRangeFunctor
57 * @param[in] actor The actor to check whether it is attached.
58 * @param[in] touches The number of touches in the current pan event.
59 * @param[in] outsideRangeEmitters Reference to container where emitters outside of the touches range should be added.
61 IsNotAttachedAndOutsideTouchesRangeFunctor(Actor* actor, unsigned int touches, PanGestureDetectorContainer& outsideRangeEmitters)
62 : actorToCheck(actor),
63 numberOfTouches(touches),
64 outsideTouchesRangeEmitters(outsideRangeEmitters)
69 * Returns true if not attached, false if it is still attached.
70 * Additionally, checks if the number of touches has changed and stops sending the pan to a particular
71 * detector if it exceeds the range of that detector.
72 * @param[in] detector The detector to check.
73 * @return true, if not attached, false otherwise.
75 bool operator()(PanGestureDetector* detector) const
77 bool remove(!detector->IsAttached(*actorToCheck));
81 // Ensure number of touch points is within the range of our emitter. If it isn't then remove
82 // this emitter and add it to the outsideTouchesRangeEmitters container
83 if ( (numberOfTouches < detector->GetMinimumTouchesRequired()) ||
84 (numberOfTouches > detector->GetMaximumTouchesRequired()) )
87 outsideTouchesRangeEmitters.push_back(detector);
94 Actor* actorToCheck; ///< The actor to check whether it is attached or not.
95 unsigned int numberOfTouches; ///< The number of touches in the pan event.
96 PanGestureDetectorContainer& outsideTouchesRangeEmitters; ///< Emitters that are outside of the range of current pan.
99 } // unnamed namespace
101 struct PanGestureProcessor::PanEventFunctor : public GestureProcessor::Functor
105 * @param[in] panEvent The current gesture event.
106 * @param[in] processor Reference to the processor.
108 PanEventFunctor( const Integration::PanGestureEvent& panEvent, PanGestureProcessor& processor )
109 : panEvent( panEvent ),
110 processor( processor )
115 * Check if the detector meets the current gesture event parameters.
117 virtual bool operator() ( GestureDetector* detector, Actor* actor )
119 bool retVal( false );
121 PanGestureDetector* panDetector( static_cast< PanGestureDetector* >( detector ) );
123 if ( ( panEvent.numberOfTouches >= panDetector->GetMinimumTouchesRequired() ) &&
124 ( panEvent.numberOfTouches <= panDetector->GetMaximumTouchesRequired() ) )
126 // Check if the detector requires directional panning.
127 if ( panDetector->RequiresDirectionalPan() && processor.mCurrentRenderTask )
129 // It does, calculate the angle of the pan in local actor coordinates and ensures it fits
130 // the detector's criteria.
131 RenderTask& renderTaskImpl( GetImplementation( processor.mCurrentRenderTask ) );
133 Vector2 startPosition, currentPosition;
134 actor->ScreenToLocal( renderTaskImpl, startPosition.x, startPosition.y, processor.mPossiblePanPosition.x, processor.mPossiblePanPosition.y );
135 actor->ScreenToLocal( renderTaskImpl, currentPosition.x, currentPosition.y, panEvent.currentPosition.x, panEvent.currentPosition.y );
136 Vector2 displacement( currentPosition - startPosition );
138 Radian angle( atan( displacement.y / displacement.x ) );
140 /////////////////////////////
143 // Q3 (-,-) | Q4 (+,-) //
145 // ----------------- +x //
147 // Q2 (-,+) | Q1 (+,+) //
151 /////////////////////////////
153 // Quadrant 2: 180 degrees + angle
154 // Quadrant 3: angle - 180 degrees
156 /////////////////////////////
158 if ( displacement.x < 0.0f )
160 if ( displacement.y >= 0.0f )
172 if ( panDetector->CheckAngleAllowed( angle ) )
179 // Directional panning not required so we can use this actor and gesture detector.
188 * Gestured actor and gesture detectors that meet the gesture's parameters found, emit and save required information.
190 virtual void operator() ( Dali::Actor actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
192 PanGestureDetectorContainer derivedContainer;
193 DownCastContainer<PanGestureDetector>( gestureDetectors, derivedContainer );
195 processor.mCurrentPanEmitters.clear();
196 processor.ResetActor();
198 Actor* actorImpl( &GetImplementation( actor ) );
199 actorImpl->ScreenToLocal( GetImplementation(processor.mCurrentRenderTask), actorCoordinates.x, actorCoordinates.y, panEvent.currentPosition.x, panEvent.currentPosition.y );
201 processor.EmitPanSignal( actor, derivedContainer, panEvent, actorCoordinates, panEvent.state, processor.mCurrentRenderTask );
203 if ( actorImpl->OnStage() )
205 processor.mCurrentPanEmitters = derivedContainer;
206 processor.SetActor( actor );
210 const Integration::PanGestureEvent& panEvent;
211 PanGestureProcessor& processor;
214 PanGestureProcessor::PanGestureProcessor( Stage& stage, Integration::GestureManager& gestureManager )
215 : GestureProcessor( Gesture::Pan ),
217 mGestureManager( gestureManager ),
219 mCurrentPanEmitters(),
220 mCurrentRenderTask(),
221 mPossiblePanPosition(),
222 mMinTouchesRequired( 1 ),
223 mMaxTouchesRequired( 1 ),
224 mSceneObject( SceneGraph::PanGesture::New() ) // Create scene object to store pan information.
226 // Pass ownership to scene-graph
227 AddGestureMessage( mStage.GetUpdateManager(), mSceneObject );
230 PanGestureProcessor::~PanGestureProcessor()
232 if( Stage::IsInstalled() && ( mSceneObject != NULL ) )
234 RemoveGestureMessage( mStage.GetUpdateManager(), mSceneObject );
235 mSceneObject = NULL; // mSceneObject is about to be destroyed
239 void PanGestureProcessor::Process( const Integration::PanGestureEvent& panEvent )
241 switch( panEvent.state )
243 case Gesture::Possible:
245 mCurrentPanEmitters.clear();
248 HitTestAlgorithm::Results hitTestResults;
249 if( HitTest( mStage, panEvent.currentPosition, hitTestResults ) )
251 SetActor( hitTestResults.actor );
252 mPossiblePanPosition = panEvent.currentPosition;
258 case Gesture::Started:
260 if ( GetCurrentGesturedActor() )
262 // The pan gesture should only be sent to the gesture detector which first received it so that
263 // it can be told when the gesture ends as well.
265 HitTestAlgorithm::Results hitTestResults;
266 HitTest( mStage, mPossiblePanPosition, hitTestResults ); // Hit test original possible position...
268 if ( hitTestResults.actor && ( GetCurrentGesturedActor() == &GetImplementation( hitTestResults.actor ) ) )
270 // Record the current render-task for Screen->Actor coordinate conversions
271 mCurrentRenderTask = hitTestResults.renderTask;
273 PanEventFunctor functor( panEvent, *this );
274 GestureDetectorContainer gestureDetectors;
275 UpCastContainer<PanGestureDetector>( mGestureDetectors, gestureDetectors );
276 ProcessAndEmit( hitTestResults, gestureDetectors, functor );
281 mCurrentPanEmitters.clear();
287 case Gesture::Continuing:
288 case Gesture::Finished:
289 case Gesture::Cancelled:
291 // Only send subsequent pan gesture signals if we processed the pan gesture when it started.
292 // Check if actor is still touchable.
294 Actor* currentGesturedActor = GetCurrentGesturedActor();
295 if ( currentGesturedActor )
297 if ( currentGesturedActor->IsHittable() && !mCurrentPanEmitters.empty() && mCurrentRenderTask )
299 PanGestureDetectorContainer outsideTouchesRangeEmitters;
301 // Removes emitters that no longer have the actor attached
302 // Also remove emitters whose touches are outside the range of the current pan event and add them to outsideTouchesRangeEmitters
303 PanGestureDetectorContainer::iterator endIter = std::remove_if( mCurrentPanEmitters.begin(), mCurrentPanEmitters.end(),
304 IsNotAttachedAndOutsideTouchesRangeFunctor(currentGesturedActor, panEvent.numberOfTouches, outsideTouchesRangeEmitters) );
305 mCurrentPanEmitters.erase( endIter, mCurrentPanEmitters.end() );
309 if ( !outsideTouchesRangeEmitters.empty() || !mCurrentPanEmitters.empty() )
311 currentGesturedActor->ScreenToLocal( GetImplementation( mCurrentRenderTask ), actorCoords.x, actorCoords.y, panEvent.currentPosition.x, panEvent.currentPosition.y );
313 // EmitPanSignal checks whether we have a valid actor and whether the container we are passing in has emitters before it emits the pan.
314 EmitPanSignal(Dali::Actor(currentGesturedActor), outsideTouchesRangeEmitters, panEvent, actorCoords, Gesture::Finished, mCurrentRenderTask);
315 EmitPanSignal(Dali::Actor(currentGesturedActor), mCurrentPanEmitters, panEvent, actorCoords, panEvent.state, mCurrentRenderTask);
318 if ( mCurrentPanEmitters.empty() )
320 // If we have no emitters attached then clear pan actor as well.
324 // Clear current gesture detectors if pan gesture has ended or been cancelled.
325 if ( ( panEvent.state == Gesture::Finished ) || ( panEvent.state == Gesture::Cancelled ) )
327 mCurrentPanEmitters.clear();
333 mCurrentPanEmitters.clear();
341 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Clear\n" );
346 void PanGestureProcessor::AddGestureDetector( PanGestureDetector* gestureDetector )
348 bool firstRegistration(mGestureDetectors.empty());
350 mGestureDetectors.push_back(gestureDetector);
352 // Set the pan scene object on the gesture detector
353 gestureDetector->SetSceneObject( mSceneObject );
355 if (firstRegistration)
357 mMinTouchesRequired = gestureDetector->GetMinimumTouchesRequired();
358 mMaxTouchesRequired = gestureDetector->GetMaximumTouchesRequired();
360 Integration::PanGestureRequest request;
361 request.minTouches = mMinTouchesRequired;
362 request.maxTouches = mMaxTouchesRequired;
363 mGestureManager.Register(request);
371 void PanGestureProcessor::RemoveGestureDetector( PanGestureDetector* gestureDetector )
373 if (!mCurrentPanEmitters.empty())
375 // Check if the removed detector was one that is currently being panned and remove it from emitters.
376 PanGestureDetectorContainer::iterator endIter = std::remove( mCurrentPanEmitters.begin(), mCurrentPanEmitters.end(), gestureDetector );
377 mCurrentPanEmitters.erase( endIter, mCurrentPanEmitters.end() );
379 // If we no longer have any emitters, then we should clear mCurrentGesturedActor as well
380 if ( mCurrentPanEmitters.empty() )
386 // Find the detector...
387 PanGestureDetectorContainer::iterator endIter = std::remove( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector );
388 DALI_ASSERT_DEBUG( endIter != mGestureDetectors.end() );
391 mGestureDetectors.erase(endIter, mGestureDetectors.end());
393 if (mGestureDetectors.empty())
395 Integration::GestureRequest request(Gesture::Pan);
396 mGestureManager.Unregister(request);
404 void PanGestureProcessor::GestureDetectorUpdated( PanGestureDetector* gestureDetector )
406 DALI_ASSERT_DEBUG(find(mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector) != mGestureDetectors.end());
411 void PanGestureProcessor::SetPanGestureProperties( const PanGesture& pan )
413 // If we are currently processing a pan gesture then just ignore
414 if ( mCurrentPanEmitters.empty() && mSceneObject )
416 // We update the scene object directly rather than sending a message.
417 // Sending a message could cause unnecessary delays, the scene object ensure thread safe behaviour.
418 mSceneObject->AddGesture( pan );
422 void PanGestureProcessor::EnableProfiling()
424 mSceneObject->EnableProfiling();
427 void PanGestureProcessor::SetPredictionMode(int mode)
430 || (mode >= SceneGraph::PanGesture::NUM_PREDICTION_MODES) )
432 mode = SceneGraph::PanGesture::DEFAULT_PREDICTION_MODE;
434 SceneGraph::PanGesture::PredictionMode modeEnum = static_cast<SceneGraph::PanGesture::PredictionMode>(mode);
435 mSceneObject->SetPredictionMode(modeEnum);
438 void PanGestureProcessor::SetPredictionAmount(float amount)
440 mSceneObject->SetPredictionAmount(amount);
443 void PanGestureProcessor::UpdateDetection()
445 DALI_ASSERT_DEBUG(!mGestureDetectors.empty());
447 unsigned int minimumRequired = UINT_MAX;
448 unsigned int maximumRequired = 0;
450 for ( PanGestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter != endIter; ++iter )
452 PanGestureDetector* detector(*iter);
454 unsigned int minimum = detector->GetMinimumTouchesRequired();
455 if (minimum < minimumRequired)
457 minimumRequired = minimum;
460 unsigned int maximum = detector->GetMaximumTouchesRequired();
461 if (maximum > maximumRequired)
463 maximumRequired = maximum;
467 if ( (minimumRequired != mMinTouchesRequired)||(maximumRequired != mMaxTouchesRequired) )
469 mMinTouchesRequired = minimumRequired;
470 mMaxTouchesRequired = maximumRequired;
472 Integration::PanGestureRequest request;
473 request.minTouches = mMinTouchesRequired;
474 request.maxTouches = mMaxTouchesRequired;
475 mGestureManager.Update(request);
479 void PanGestureProcessor::EmitPanSignal( Dali::Actor actorHandle,
480 PanGestureDetectorContainer& gestureDetectors,
481 const Integration::PanGestureEvent& panEvent,
482 Vector2 localCurrent,
483 Gesture::State state,
484 Dali::RenderTask renderTask )
486 if ( actorHandle && !gestureDetectors.empty() )
488 Actor& actor = GetImplementation(actorHandle);
490 PanGesture pan(state);
491 pan.time = panEvent.time;
493 pan.numberOfTouches = panEvent.numberOfTouches;
494 pan.screenPosition = panEvent.currentPosition;
495 pan.position = localCurrent;
497 RenderTask& renderTaskImpl( GetImplementation( renderTask ) );
499 Vector2 localPrevious;
500 actor.ScreenToLocal( renderTaskImpl, localPrevious.x, localPrevious.y, panEvent.previousPosition.x, panEvent.previousPosition.y );
502 pan.displacement = localCurrent - localPrevious;
503 Vector2 previousPos( panEvent.previousPosition );
504 if ( state == Gesture::Started )
506 previousPos = mPossiblePanPosition;
509 pan.screenDisplacement = panEvent.currentPosition - previousPos;
511 pan.velocity.x = pan.displacement.x / panEvent.timeDelta;
512 pan.velocity.y = pan.displacement.y / panEvent.timeDelta;
514 pan.screenVelocity.x = pan.screenDisplacement.x / panEvent.timeDelta;
515 pan.screenVelocity.y = pan.screenDisplacement.y / panEvent.timeDelta;
517 // When the gesture ends, we may incorrectly get a ZERO velocity (as we have lifted our finger without any movement)
518 // so we should use the last recorded velocity instead in this scenario.
519 if ( ( state == Gesture::Finished ) && ( pan.screenVelocity == Vector2::ZERO ) &&
520 ( panEvent.timeDelta < MAXIMUM_TIME_WITH_VALID_LAST_VELOCITY ) )
522 pan.velocity = mLastVelocity;
523 pan.screenVelocity = mLastScreenVelocity;
527 // Store the current velocity for future iterations.
528 mLastVelocity = pan.velocity;
529 mLastScreenVelocity = pan.screenVelocity;
534 // We update the scene object directly rather than sending a message.
535 // Sending a message could cause unnecessary delays, the scene object ensure thread safe behaviour.
536 mSceneObject->AddGesture( pan );
539 for ( PanGestureDetectorContainer::iterator iter = gestureDetectors.begin(), endIter = gestureDetectors.end(); iter != endIter; ++iter )
541 (*iter)->EmitPanGestureSignal(actorHandle, pan);
546 void PanGestureProcessor::OnGesturedActorStageDisconnection()
548 mCurrentPanEmitters.clear();
551 } // namespace Internal