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-data.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-data-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)
65 * Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
67 Dali::Actor EmitTouchSignals( Dali::Actor actor, const TouchEvent& event, const Dali::TouchData& touchData )
69 Dali::Actor consumedActor;
73 Dali::Actor oldParent( actor.GetParent() );
75 Actor& actorImpl( GetImplementation(actor) );
77 bool consumed( false );
79 // Only emit the signal if the actor's touch signal has connections (or derived actor implementation requires touch).
80 if ( actorImpl.GetTouchRequired() )
82 consumed = actorImpl.EmitTouchEventSignal( event, touchData );
87 // One of this actor's listeners has consumed the event so set this actor as the consumed actor.
88 consumedActor = Dali::Actor( &actorImpl );
92 // The actor may have been removed/reparented during the signal callbacks.
93 Dali::Actor parent = actor.GetParent();
96 (parent == oldParent) )
98 // One of the actor's parents may consumed the event and they should be set as the consumed actor.
99 consumedActor = EmitTouchSignals( parent, event, touchData );
104 return consumedActor;
107 Dali::Actor AllocAndEmitTouchSignals( unsigned long time, Dali::Actor actor, const Integration::Point& point )
109 TouchEvent touchEvent( time );
110 TouchDataPtr touchData( new TouchData( time ) );
111 Dali::TouchData touchDataHandle( touchData.Get() );
113 touchEvent.points.push_back( point.GetTouchPoint() );
114 touchData->AddPoint( point );
116 return EmitTouchSignals( actor, touchEvent, touchDataHandle );
121 * Changes the state of the primary point to leave and emits the touch signals
123 Dali::Actor EmitTouchSignals( Actor* actor, RenderTask& renderTask, const TouchEvent& originalEvent, const TouchDataPtr& originalTouchData, PointState::Type state )
125 Dali::Actor consumingActor;
129 TouchDataPtr touchData = TouchData::Clone( *originalTouchData.Get() );
131 Integration::Point& primaryPoint = touchData->GetPoint( 0 );
133 const Vector2& screenPosition = primaryPoint.GetScreenPosition();
134 Vector2 localPosition;
135 actor->ScreenToLocal( renderTask, localPosition.x, localPosition.y, screenPosition.x, screenPosition.y );
137 primaryPoint.SetLocalPosition( localPosition );
138 primaryPoint.SetHitActor( Dali::Actor( actor ) );
139 primaryPoint.SetState( state );
141 TouchEvent touchEvent( originalEvent );
142 touchEvent.points[0] = primaryPoint.GetTouchPoint();
144 consumingActor = EmitTouchSignals( Dali::Actor(actor), touchEvent, Dali::TouchData( touchData.Get() ) );
147 return consumingActor;
151 * @brief Parses the primary touch point by performing a hit-test if necessary
153 * @param[out] hitTestResults The hit test results are put into this variable
154 * @param[in/out] capturingTouchActorObserver The observer for the capturing touch actor member
155 * @param[in] lastRenderTask The last render task member
156 * @param[in] currentPoint The current point information
157 * @param[in] scene The scene that this touch is related to
159 void ParsePrimaryTouchPoint(
160 HitTestAlgorithm::Results& hitTestResults,
161 ActorObserver& capturingTouchActorObserver,
162 const RenderTaskPtr& lastRenderTask,
163 const Integration::Point& currentPoint,
164 const Internal::Scene& scene )
166 Actor* capturingTouchActor = capturingTouchActorObserver.GetActor();
168 // We only set the capturing touch actor when the first touch-started actor captures all touch so if it's set, just use it
169 if( capturingTouchActor && lastRenderTask )
171 hitTestResults.actor = Dali::Actor( capturingTouchActor );
172 hitTestResults.renderTask = lastRenderTask;
173 const Vector2& screenPosition = currentPoint.GetScreenPosition();
174 capturingTouchActor->ScreenToLocal( *lastRenderTask, hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y, screenPosition.x, screenPosition.y );
178 HitTestAlgorithm::HitTest( scene.GetSize(), scene.GetRenderTaskList(), scene.GetLayerList(), currentPoint.GetScreenPosition(), hitTestResults );
180 if( currentPoint.GetState() == PointState::STARTED && hitTestResults.actor )
182 // If we've just started touch, then check whether the actor has requested to capture all touch events
183 Actor* hitActor = &GetImplementation( hitTestResults.actor );
184 if( hitActor->CapturesAllTouchAfterStart() )
186 capturingTouchActorObserver.SetActor( hitActor );
192 } // unnamed namespace
194 TouchEventProcessor::TouchEventProcessor( Scene& scene )
196 mLastPrimaryHitActor( MakeCallback( this, &TouchEventProcessor::OnObservedActorDisconnected ) ),
197 mLastConsumedActor(),
198 mCapturingTouchActor(),
199 mTouchDownConsumedActor(),
202 DALI_LOG_TRACE_METHOD( gLogFilter );
205 TouchEventProcessor::~TouchEventProcessor()
207 DALI_LOG_TRACE_METHOD( gLogFilter );
210 bool TouchEventProcessor::ProcessTouchEvent( const Integration::TouchEvent& event )
212 DALI_LOG_TRACE_METHOD( gLogFilter );
213 DALI_ASSERT_ALWAYS( !event.points.empty() && "Empty TouchEvent sent from Integration\n" );
215 PRINT_HIERARCHY(gLogFilter);
217 // 1) Check if it is an interrupted event - we should inform our last primary hit actor about this
218 // and emit the stage signal as well.
220 if ( event.points[0].GetState() == PointState::INTERRUPTED )
222 Dali::Actor consumingActor;
223 Integration::Point currentPoint( event.points[0] );
225 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
226 if ( lastPrimaryHitActor )
228 Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
229 currentPoint.SetHitActor( lastPrimaryHitActorHandle );
231 consumingActor = AllocAndEmitTouchSignals( event.time, lastPrimaryHitActorHandle, currentPoint );
234 // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
235 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
236 if ( lastConsumedActor &&
237 lastConsumedActor != lastPrimaryHitActor &&
238 lastConsumedActor != consumingActor )
240 Dali::Actor lastConsumedActorHandle( lastConsumedActor );
241 currentPoint.SetHitActor( lastConsumedActorHandle );
242 AllocAndEmitTouchSignals( event.time, lastConsumedActorHandle, currentPoint );
245 // Tell the touch-down consuming actor as well, if required
246 Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
247 if ( touchDownConsumedActor &&
248 touchDownConsumedActor != lastPrimaryHitActor &&
249 touchDownConsumedActor != lastConsumedActor &&
250 touchDownConsumedActor != consumingActor )
252 Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
254 currentPoint.SetHitActor( touchDownConsumedActorHandle );
255 AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
258 mLastPrimaryHitActor.SetActor( nullptr );
259 mLastConsumedActor.SetActor( nullptr );
260 mCapturingTouchActor.SetActor( nullptr );
261 mTouchDownConsumedActor.SetActor( nullptr );
262 mLastRenderTask.Reset();
264 currentPoint.SetHitActor( Dali::Actor() );
266 TouchEvent touchEvent( event.time );
267 TouchDataPtr touchData( new TouchData( event.time ) );
268 Dali::TouchData touchDataHandle( touchData.Get() );
270 touchEvent.points.push_back( currentPoint.GetTouchPoint() );
271 touchData->AddPoint( currentPoint );
273 mScene.EmitTouchedSignal( touchEvent, touchDataHandle );
274 return false; // No need for hit testing & already an interrupted event so just return false
278 TouchEvent touchEvent( event.time );
279 TouchDataPtr touchData( new TouchData( event.time ) );
280 Dali::TouchData touchDataHandle( touchData.Get() );
282 DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
283 DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
285 RenderTaskPtr currentRenderTask;
286 bool firstPointParsed = false;
288 for ( auto&& currentPoint : event.points )
290 HitTestAlgorithm::Results hitTestResults;
291 if( !firstPointParsed )
293 firstPointParsed = true;
294 ParsePrimaryTouchPoint( hitTestResults, mCapturingTouchActor, mLastRenderTask, currentPoint, mScene );
296 // Only set the currentRenderTask for the primary hit actor.
297 currentRenderTask = hitTestResults.renderTask;
301 HitTestAlgorithm::HitTest( mScene.GetSize(), mScene.GetRenderTaskList(), mScene.GetLayerList(), currentPoint.GetScreenPosition(), hitTestResults );
304 Integration::Point newPoint( currentPoint );
305 newPoint.SetHitActor( hitTestResults.actor );
306 newPoint.SetLocalPosition( hitTestResults.actorCoordinates );
308 touchEvent.points.push_back( newPoint.GetTouchPoint() );
309 touchData->AddPoint( newPoint );
311 DALI_LOG_INFO( gLogFilter, Debug::General, " State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
312 TOUCH_POINT_STATE[currentPoint.GetState()], currentPoint.GetScreenPosition().x, currentPoint.GetScreenPosition().y,
313 ( hitTestResults.actor ? reinterpret_cast< void* >( &hitTestResults.actor.GetBaseObject() ) : NULL ),
314 ( hitTestResults.actor ? hitTestResults.actor.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() : "" ),
315 hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
319 // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
321 bool consumed = false;
323 // Emit the touch signal
324 Dali::Actor consumedActor;
325 if ( currentRenderTask )
327 consumedActor = EmitTouchSignals( touchData->GetPoint( 0 ).GetHitActor(), touchEvent, touchDataHandle );
328 consumed = consumedActor ? true : false;
331 Integration::Point& primaryPoint = touchData->GetPoint( 0 );
332 Dali::Actor primaryHitActor = primaryPoint.GetHitActor();
333 PointState::Type primaryPointState = primaryPoint.GetState();
335 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() : "" );
336 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() : "" );
338 if ( ( primaryPointState == PointState::DOWN ) &&
339 ( touchEvent.GetPointCount() == 1 ) &&
340 ( consumedActor && consumedActor.GetProperty< bool >( Dali::Actor::Property::CONNECTED_TO_SCENE ) ) )
342 mTouchDownConsumedActor.SetActor( &GetImplementation( consumedActor ) );
345 // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
346 // hit actor. Also process the last consumed actor in the same manner.
348 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
349 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
350 if( ( primaryPointState == PointState::MOTION ) || ( primaryPointState == PointState::UP ) || ( primaryPointState == PointState::STATIONARY ) )
352 if( mLastRenderTask )
354 Dali::Actor leaveEventConsumer;
355 RenderTask& lastRenderTaskImpl = *mLastRenderTask.Get();
357 if( lastPrimaryHitActor &&
358 lastPrimaryHitActor != primaryHitActor &&
359 lastPrimaryHitActor != consumedActor )
361 if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
363 if ( lastPrimaryHitActor->GetLeaveRequired() )
365 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit): (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
366 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::LEAVE );
371 // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
372 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
373 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit): (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
374 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::INTERRUPTED );
378 consumed |= leaveEventConsumer ? true : false;
380 // Check if the motion event has been consumed by another actor's listener. In this case, the previously
381 // consumed actor's listeners may need to be informed (through a leave event).
382 // Further checks here to ensure we do not signal the same actor twice for the same event.
383 if ( lastConsumedActor &&
384 lastConsumedActor != consumedActor &&
385 lastConsumedActor != lastPrimaryHitActor &&
386 lastConsumedActor != primaryHitActor &&
387 lastConsumedActor != leaveEventConsumer )
389 if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
391 if( lastConsumedActor->GetLeaveRequired() )
393 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
394 EmitTouchSignals( lastConsumedActor, lastRenderTaskImpl, touchEvent, touchData, PointState::LEAVE );
399 // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
400 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
401 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
402 EmitTouchSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::INTERRUPTED );
408 // 5) If our primary point is an Up event, then the primary point (in multi-touch) will change next
409 // time so set our last primary actor to NULL. Do the same to the last consumed actor as well.
411 if ( primaryPointState == PointState::UP )
413 mLastPrimaryHitActor.SetActor( nullptr );
414 mLastConsumedActor.SetActor( nullptr );
415 mCapturingTouchActor.SetActor( nullptr );
416 mLastRenderTask.Reset();
420 // The primaryHitActor may have been removed from the scene so ensure it is still on the scene before setting members.
421 if ( primaryHitActor && GetImplementation( primaryHitActor ).OnScene() )
423 mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
425 // Only observe the consumed actor if we have a primaryHitActor (check if it is still on the scene).
426 if ( consumedActor && GetImplementation( consumedActor ).OnScene() )
428 mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
432 mLastConsumedActor.SetActor( NULL );
435 mLastRenderTask = currentRenderTask;
439 mLastPrimaryHitActor.SetActor( nullptr );
440 mLastConsumedActor.SetActor( nullptr );
441 mCapturingTouchActor.SetActor( nullptr );
442 mLastRenderTask.Reset();
446 // 6) Emit an interrupted event to the touch-down actor if it hasn't consumed the up and
447 // emit the stage touched event if required.
449 if ( touchEvent.GetPointCount() == 1 ) // Only want the first touch and the last release
451 switch ( primaryPointState )
455 Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
456 if ( touchDownConsumedActor &&
457 touchDownConsumedActor != consumedActor &&
458 touchDownConsumedActor != lastPrimaryHitActor &&
459 touchDownConsumedActor != lastConsumedActor )
461 Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
463 Integration::Point currentPoint = touchData->GetPoint( 0 );
464 currentPoint.SetHitActor( touchDownConsumedActorHandle );
465 currentPoint.SetState( PointState::INTERRUPTED );
467 AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
470 mTouchDownConsumedActor.SetActor( NULL );
475 case PointState::DOWN:
477 mScene.EmitTouchedSignal( touchEvent, touchDataHandle );
481 case PointState::MOTION:
482 case PointState::LEAVE:
483 case PointState::STATIONARY:
484 case PointState::INTERRUPTED:
495 void TouchEventProcessor::OnObservedActorDisconnected( Actor* actor )
497 if ( actor == mLastPrimaryHitActor.GetActor() )
499 Dali::Actor handle( actor );
501 Integration::Point point;
502 point.SetState( PointState::INTERRUPTED );
503 point.SetHitActor( handle );
505 TouchDataPtr touchData( new TouchData );
506 touchData->AddPoint( point );
507 Dali::TouchData touchDataHandle( touchData.Get() );
509 TouchEvent touchEvent( 0 );
510 touchEvent.points.push_back( point.GetTouchPoint() );
512 Dali::Actor eventConsumer = EmitTouchSignals( handle, touchEvent, touchDataHandle );
514 if ( mLastConsumedActor.GetActor() != eventConsumer )
516 EmitTouchSignals( Dali::Actor( mLastConsumedActor.GetActor() ), touchEvent, touchDataHandle );
519 // Do not set mLastPrimaryHitActor to NULL we may be iterating through its observers
521 mLastConsumedActor.SetActor( NULL );
522 mLastRenderTask.Reset();
526 } // namespace Internal