2 * Copyright (c) 2020 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/touch-event-processor.h>
21 #if defined(DEBUG_ENABLED)
26 #include <dali/public-api/events/touch-event.h>
27 #include <dali/public-api/math/vector2.h>
28 #include <dali/public-api/signals/callback.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/integration-api/events/touch-event-integ.h>
31 #include <dali/internal/event/actors/actor-impl.h>
32 #include <dali/internal/event/actors/layer-impl.h>
33 #include <dali/internal/event/common/scene-impl.h>
34 #include <dali/internal/event/events/hit-test-algorithm-impl.h>
35 #include <dali/internal/event/events/multi-point-event-util.h>
36 #include <dali/internal/event/events/touch-event-impl.h>
37 #include <dali/internal/event/render-tasks/render-task-impl.h>
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TOUCH_PROCESSOR" );
51 const char * TOUCH_POINT_STATE[ 6 ] =
61 #endif // defined(DEBUG_ENABLED)
63 Dali::Actor EmitInterceptTouchSignals( Dali::Actor actor, const Dali::TouchEvent& touchEvent )
65 Dali::Actor interceptedActor;
69 Dali::Actor parent = actor.GetParent();
72 // Recursively deliver events to the actor and its parents for intercept touch event.
73 interceptedActor = EmitInterceptTouchSignals( parent, touchEvent );
76 if( !interceptedActor )
78 bool intercepted = false;
79 Actor& actorImpl( GetImplementation(actor) );
80 if( actorImpl.GetInterceptTouchRequired() )
82 intercepted = actorImpl.EmitInterceptTouchEventSignal( touchEvent );
85 interceptedActor = Dali::Actor( &actorImpl );
91 return interceptedActor;
95 * Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
97 Dali::Actor EmitTouchSignals( Dali::Actor actor, const Dali::TouchEvent& touchEvent )
99 Dali::Actor consumedActor;
103 Dali::Actor oldParent( actor.GetParent() );
105 Actor& actorImpl( GetImplementation(actor) );
107 bool consumed( false );
109 // Only emit the signal if the actor's touch signal has connections (or derived actor implementation requires touch).
110 if ( actorImpl.GetTouchRequired() )
112 consumed = actorImpl.EmitTouchEventSignal( touchEvent );
117 // One of this actor's listeners has consumed the event so set this actor as the consumed actor.
118 consumedActor = Dali::Actor( &actorImpl );
122 // The actor may have been removed/reparented during the signal callbacks.
123 Dali::Actor parent = actor.GetParent();
126 (parent == oldParent) )
128 // One of the actor's parents may consumed the event and they should be set as the consumed actor.
129 consumedActor = EmitTouchSignals( parent, touchEvent );
134 return consumedActor;
137 Dali::Actor AllocAndEmitTouchSignals( unsigned long time, Dali::Actor actor, const Integration::Point& point )
139 TouchEventPtr touchEvent( new TouchEvent( time ) );
140 Dali::TouchEvent touchEventHandle( touchEvent.Get() );
142 touchEvent->AddPoint( point );
144 return EmitTouchSignals( actor, touchEventHandle );
149 * Changes the state of the primary point to leave and emits the touch signals
151 Dali::Actor EmitTouchSignals( Actor* actor, RenderTask& renderTask, const TouchEventPtr& originalTouchEvent, PointState::Type state )
153 Dali::Actor consumingActor;
157 TouchEventPtr touchEventImpl = TouchEvent::Clone( *originalTouchEvent.Get() );
159 Integration::Point& primaryPoint = touchEventImpl->GetPoint( 0 );
161 const Vector2& screenPosition = primaryPoint.GetScreenPosition();
162 Vector2 localPosition;
163 actor->ScreenToLocal( renderTask, localPosition.x, localPosition.y, screenPosition.x, screenPosition.y );
165 primaryPoint.SetLocalPosition( localPosition );
166 primaryPoint.SetHitActor( Dali::Actor( actor ) );
167 primaryPoint.SetState( state );
169 consumingActor = EmitTouchSignals( Dali::Actor(actor), Dali::TouchEvent( touchEventImpl.Get() ) );
172 return consumingActor;
176 * @brief Parses the primary touch point by performing a hit-test if necessary
178 * @param[out] hitTestResults The hit test results are put into this variable
179 * @param[in/out] capturingTouchActorObserver The observer for the capturing touch actor member
180 * @param[in] lastRenderTask The last render task member
181 * @param[in] currentPoint The current point information
182 * @param[in] scene The scene that this touch is related to
184 void ParsePrimaryTouchPoint(
185 HitTestAlgorithm::Results& hitTestResults,
186 ActorObserver& capturingTouchActorObserver,
187 const RenderTaskPtr& lastRenderTask,
188 const Integration::Point& currentPoint,
189 const Internal::Scene& scene )
191 Actor* capturingTouchActor = capturingTouchActorObserver.GetActor();
193 // We only set the capturing touch actor when the first touch-started actor captures all touch so if it's set, just use it
194 if( capturingTouchActor && lastRenderTask )
196 hitTestResults.actor = Dali::Actor( capturingTouchActor );
197 hitTestResults.renderTask = lastRenderTask;
198 const Vector2& screenPosition = currentPoint.GetScreenPosition();
199 capturingTouchActor->ScreenToLocal( *lastRenderTask, hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y, screenPosition.x, screenPosition.y );
203 HitTestAlgorithm::HitTest( scene.GetSize(), scene.GetRenderTaskList(), scene.GetLayerList(), currentPoint.GetScreenPosition(), hitTestResults );
205 if( currentPoint.GetState() == PointState::STARTED && hitTestResults.actor )
207 // If we've just started touch, then check whether the actor has requested to capture all touch events
208 Actor* hitActor = &GetImplementation( hitTestResults.actor );
209 if( hitActor->CapturesAllTouchAfterStart() )
211 capturingTouchActorObserver.SetActor( hitActor );
217 } // unnamed namespace
219 TouchEventProcessor::TouchEventProcessor( Scene& scene )
221 mLastPrimaryHitActor( MakeCallback( this, &TouchEventProcessor::OnObservedActorDisconnected ) ),
222 mLastConsumedActor(),
223 mCapturingTouchActor(),
224 mTouchDownConsumedActor(),
227 DALI_LOG_TRACE_METHOD( gLogFilter );
230 TouchEventProcessor::~TouchEventProcessor()
232 DALI_LOG_TRACE_METHOD( gLogFilter );
235 bool TouchEventProcessor::ProcessTouchEvent( const Integration::TouchEvent& event )
237 DALI_LOG_TRACE_METHOD( gLogFilter );
238 DALI_ASSERT_ALWAYS( !event.points.empty() && "Empty TouchEvent sent from Integration\n" );
240 PRINT_HIERARCHY(gLogFilter);
242 // 1) Check if it is an interrupted event - we should inform our last primary hit actor about this
243 // and emit the stage signal as well.
245 if ( event.points[0].GetState() == PointState::INTERRUPTED )
247 Dali::Actor consumingActor;
248 Integration::Point currentPoint( event.points[0] );
250 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
251 if ( lastPrimaryHitActor )
253 Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
254 currentPoint.SetHitActor( lastPrimaryHitActorHandle );
256 consumingActor = AllocAndEmitTouchSignals( event.time, lastPrimaryHitActorHandle, currentPoint );
259 // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
260 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
261 if ( lastConsumedActor &&
262 lastConsumedActor != lastPrimaryHitActor &&
263 lastConsumedActor != consumingActor )
265 Dali::Actor lastConsumedActorHandle( lastConsumedActor );
266 currentPoint.SetHitActor( lastConsumedActorHandle );
267 AllocAndEmitTouchSignals( event.time, lastConsumedActorHandle, currentPoint );
270 // Tell the touch-down consuming actor as well, if required
271 Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
272 if ( touchDownConsumedActor &&
273 touchDownConsumedActor != lastPrimaryHitActor &&
274 touchDownConsumedActor != lastConsumedActor &&
275 touchDownConsumedActor != consumingActor )
277 Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
279 currentPoint.SetHitActor( touchDownConsumedActorHandle );
280 AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
283 mLastPrimaryHitActor.SetActor( nullptr );
284 mLastConsumedActor.SetActor( nullptr );
285 mCapturingTouchActor.SetActor( nullptr );
286 mTouchDownConsumedActor.SetActor( nullptr );
287 mLastRenderTask.Reset();
289 currentPoint.SetHitActor( Dali::Actor() );
291 TouchEventPtr touchEventImpl( new TouchEvent( event.time ) );
292 Dali::TouchEvent touchEventHandle( touchEventImpl.Get() );
294 touchEventImpl->AddPoint( currentPoint );
296 mScene.EmitTouchedSignal( touchEventHandle );
297 return false; // No need for hit testing & already an interrupted event so just return false
301 TouchEventPtr touchEventImpl( new TouchEvent( event.time ) );
302 Dali::TouchEvent touchEventHandle( touchEventImpl.Get() );
304 DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
305 DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
307 RenderTaskPtr currentRenderTask;
308 bool firstPointParsed = false;
310 for ( auto&& currentPoint : event.points )
312 HitTestAlgorithm::Results hitTestResults;
313 if( !firstPointParsed )
315 firstPointParsed = true;
316 ParsePrimaryTouchPoint( hitTestResults, mCapturingTouchActor, mLastRenderTask, currentPoint, mScene );
318 // Only set the currentRenderTask for the primary hit actor.
319 currentRenderTask = hitTestResults.renderTask;
323 HitTestAlgorithm::HitTest( mScene.GetSize(), mScene.GetRenderTaskList(), mScene.GetLayerList(), currentPoint.GetScreenPosition(), hitTestResults );
326 Integration::Point newPoint( currentPoint );
327 newPoint.SetHitActor( hitTestResults.actor );
328 newPoint.SetLocalPosition( hitTestResults.actorCoordinates );
330 touchEventImpl->AddPoint( newPoint );
332 DALI_LOG_INFO( gLogFilter, Debug::General, " State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
333 TOUCH_POINT_STATE[currentPoint.GetState()], currentPoint.GetScreenPosition().x, currentPoint.GetScreenPosition().y,
334 ( hitTestResults.actor ? reinterpret_cast< void* >( &hitTestResults.actor.GetBaseObject() ) : NULL ),
335 ( hitTestResults.actor ? hitTestResults.actor.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() : "" ),
336 hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
340 // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
342 bool consumed = false;
344 // Emit the touch signal
345 Dali::Actor consumedActor;
346 if ( currentRenderTask )
348 // Emit the intercept touch signal
349 Dali::Actor interceptedActor = EmitInterceptTouchSignals( touchEventImpl->GetPoint( 0 ).GetHitActor(), touchEventHandle );
350 if( interceptedActor )
352 consumedActor = EmitTouchSignals( interceptedActor, touchEventHandle );
356 consumedActor = EmitTouchSignals( touchEventImpl->GetPoint( 0 ).GetHitActor(), touchEventHandle );
358 consumed = consumedActor ? true : false;
361 Integration::Point& primaryPoint = touchEventImpl->GetPoint( 0 );
362 Dali::Actor primaryHitActor = primaryPoint.GetHitActor();
363 PointState::Type primaryPointState = primaryPoint.GetState();
365 DALI_LOG_INFO( gLogFilter, Debug::Concise, "PrimaryHitActor: (%p) %s\n", primaryHitActor ? reinterpret_cast< void* >( &primaryHitActor.GetBaseObject() ) : NULL, primaryHitActor ? primaryHitActor.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() : "" );
366 DALI_LOG_INFO( gLogFilter, Debug::Concise, "ConsumedActor: (%p) %s\n", consumedActor ? reinterpret_cast< void* >( &consumedActor.GetBaseObject() ) : NULL, consumedActor ? consumedActor.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() : "" );
368 if ( ( primaryPointState == PointState::DOWN ) &&
369 ( touchEventImpl->GetPointCount() == 1 ) &&
370 ( consumedActor && consumedActor.GetProperty< bool >( Dali::Actor::Property::CONNECTED_TO_SCENE ) ) )
372 mTouchDownConsumedActor.SetActor( &GetImplementation( consumedActor ) );
375 // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
376 // hit actor. Also process the last consumed actor in the same manner.
378 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
379 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
380 if( ( primaryPointState == PointState::MOTION ) || ( primaryPointState == PointState::UP ) || ( primaryPointState == PointState::STATIONARY ) )
382 if( mLastRenderTask )
384 Dali::Actor leaveEventConsumer;
385 RenderTask& lastRenderTaskImpl = *mLastRenderTask.Get();
387 if( lastPrimaryHitActor &&
388 lastPrimaryHitActor != primaryHitActor &&
389 lastPrimaryHitActor != consumedActor )
391 if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
393 if ( lastPrimaryHitActor->GetLeaveRequired() )
395 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit): (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().data() );
396 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEventImpl, PointState::LEAVE );
401 // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
402 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
403 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit): (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().data() );
404 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEventImpl, PointState::INTERRUPTED );
408 consumed |= leaveEventConsumer ? true : false;
410 // Check if the motion event has been consumed by another actor's listener. In this case, the previously
411 // consumed actor's listeners may need to be informed (through a leave event).
412 // Further checks here to ensure we do not signal the same actor twice for the same event.
413 if ( lastConsumedActor &&
414 lastConsumedActor != consumedActor &&
415 lastConsumedActor != lastPrimaryHitActor &&
416 lastConsumedActor != primaryHitActor &&
417 lastConsumedActor != leaveEventConsumer )
419 if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
421 if( lastConsumedActor->GetLeaveRequired() )
423 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().data() );
424 EmitTouchSignals( lastConsumedActor, lastRenderTaskImpl, touchEventImpl, PointState::LEAVE );
429 // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
430 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
431 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().data() );
432 EmitTouchSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, touchEventImpl, PointState::INTERRUPTED );
438 // 5) If our primary point is an Up event, then the primary point (in multi-touch) will change next
439 // time so set our last primary actor to NULL. Do the same to the last consumed actor as well.
441 if ( primaryPointState == PointState::UP )
443 mLastPrimaryHitActor.SetActor( nullptr );
444 mLastConsumedActor.SetActor( nullptr );
445 mCapturingTouchActor.SetActor( nullptr );
446 mLastRenderTask.Reset();
450 // The primaryHitActor may have been removed from the scene so ensure it is still on the scene before setting members.
451 if ( primaryHitActor && GetImplementation( primaryHitActor ).OnScene() )
453 mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
455 // Only observe the consumed actor if we have a primaryHitActor (check if it is still on the scene).
456 if ( consumedActor && GetImplementation( consumedActor ).OnScene() )
458 mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
462 mLastConsumedActor.SetActor( nullptr );
465 mLastRenderTask = currentRenderTask;
469 mLastPrimaryHitActor.SetActor( nullptr );
470 mLastConsumedActor.SetActor( nullptr );
471 mCapturingTouchActor.SetActor( nullptr );
472 mLastRenderTask.Reset();
476 // 6) Emit an interrupted event to the touch-down actor if it hasn't consumed the up and
477 // emit the stage touched event if required.
479 if ( touchEventImpl->GetPointCount() == 1 ) // Only want the first touch and the last release
481 switch ( primaryPointState )
485 Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
486 if ( touchDownConsumedActor &&
487 touchDownConsumedActor != consumedActor &&
488 touchDownConsumedActor != lastPrimaryHitActor &&
489 touchDownConsumedActor != lastConsumedActor )
491 Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
493 Integration::Point currentPoint = touchEventImpl->GetPoint( 0 );
494 currentPoint.SetHitActor( touchDownConsumedActorHandle );
495 currentPoint.SetState( PointState::INTERRUPTED );
497 AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
500 mTouchDownConsumedActor.SetActor( nullptr );
505 case PointState::DOWN:
507 mScene.EmitTouchedSignal( touchEventHandle );
511 case PointState::MOTION:
512 case PointState::LEAVE:
513 case PointState::STATIONARY:
514 case PointState::INTERRUPTED:
525 void TouchEventProcessor::OnObservedActorDisconnected( Actor* actor )
527 if ( actor == mLastPrimaryHitActor.GetActor() )
529 Dali::Actor actorHandle( actor );
531 Integration::Point point;
532 point.SetState( PointState::INTERRUPTED );
533 point.SetHitActor( actorHandle );
535 TouchEventPtr touchEventImpl( new TouchEvent );
536 touchEventImpl->AddPoint( point );
537 Dali::TouchEvent touchEventHandle( touchEventImpl.Get() );
539 Dali::Actor eventConsumer = EmitTouchSignals( actorHandle, touchEventHandle );
541 if ( mLastConsumedActor.GetActor() != eventConsumer )
543 EmitTouchSignals( Dali::Actor( mLastConsumedActor.GetActor() ), touchEventHandle );
546 // Do not set mLastPrimaryHitActor to NULL we may be iterating through its observers
548 mLastConsumedActor.SetActor( nullptr );
549 mLastRenderTask.Reset();
553 } // namespace Internal