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 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( 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, touchData );
104 return consumedActor;
107 Dali::Actor AllocAndEmitTouchSignals( unsigned long time, Dali::Actor actor, const Integration::Point& point )
109 TouchDataPtr touchData( new TouchData( time ) );
110 Dali::TouchData touchDataHandle( touchData.Get() );
112 touchData->AddPoint( point );
114 return EmitTouchSignals( actor, touchDataHandle );
119 * Changes the state of the primary point to leave and emits the touch signals
121 Dali::Actor EmitTouchSignals( Actor* actor, RenderTask& renderTask, const TouchDataPtr& originalTouchData, PointState::Type state )
123 Dali::Actor consumingActor;
127 TouchDataPtr touchData = TouchData::Clone( *originalTouchData.Get() );
129 Integration::Point& primaryPoint = touchData->GetPoint( 0 );
131 const Vector2& screenPosition = primaryPoint.GetScreenPosition();
132 Vector2 localPosition;
133 actor->ScreenToLocal( renderTask, localPosition.x, localPosition.y, screenPosition.x, screenPosition.y );
135 primaryPoint.SetLocalPosition( localPosition );
136 primaryPoint.SetHitActor( Dali::Actor( actor ) );
137 primaryPoint.SetState( state );
139 consumingActor = EmitTouchSignals( Dali::Actor(actor), Dali::TouchData( touchData.Get() ) );
142 return consumingActor;
146 * @brief Parses the primary touch point by performing a hit-test if necessary
148 * @param[out] hitTestResults The hit test results are put into this variable
149 * @param[in/out] capturingTouchActorObserver The observer for the capturing touch actor member
150 * @param[in] lastRenderTask The last render task member
151 * @param[in] currentPoint The current point information
152 * @param[in] scene The scene that this touch is related to
154 void ParsePrimaryTouchPoint(
155 HitTestAlgorithm::Results& hitTestResults,
156 ActorObserver& capturingTouchActorObserver,
157 const RenderTaskPtr& lastRenderTask,
158 const Integration::Point& currentPoint,
159 const Internal::Scene& scene )
161 Actor* capturingTouchActor = capturingTouchActorObserver.GetActor();
163 // We only set the capturing touch actor when the first touch-started actor captures all touch so if it's set, just use it
164 if( capturingTouchActor && lastRenderTask )
166 hitTestResults.actor = Dali::Actor( capturingTouchActor );
167 hitTestResults.renderTask = lastRenderTask;
168 const Vector2& screenPosition = currentPoint.GetScreenPosition();
169 capturingTouchActor->ScreenToLocal( *lastRenderTask, hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y, screenPosition.x, screenPosition.y );
173 HitTestAlgorithm::HitTest( scene.GetSize(), scene.GetRenderTaskList(), scene.GetLayerList(), currentPoint.GetScreenPosition(), hitTestResults );
175 if( currentPoint.GetState() == PointState::STARTED && hitTestResults.actor )
177 // If we've just started touch, then check whether the actor has requested to capture all touch events
178 Actor* hitActor = &GetImplementation( hitTestResults.actor );
179 if( hitActor->CapturesAllTouchAfterStart() )
181 capturingTouchActorObserver.SetActor( hitActor );
187 } // unnamed namespace
189 TouchEventProcessor::TouchEventProcessor( Scene& scene )
191 mLastPrimaryHitActor( MakeCallback( this, &TouchEventProcessor::OnObservedActorDisconnected ) ),
192 mLastConsumedActor(),
193 mCapturingTouchActor(),
194 mTouchDownConsumedActor(),
197 DALI_LOG_TRACE_METHOD( gLogFilter );
200 TouchEventProcessor::~TouchEventProcessor()
202 DALI_LOG_TRACE_METHOD( gLogFilter );
205 bool TouchEventProcessor::ProcessTouchEvent( const Integration::TouchEvent& event )
207 DALI_LOG_TRACE_METHOD( gLogFilter );
208 DALI_ASSERT_ALWAYS( !event.points.empty() && "Empty TouchEvent sent from Integration\n" );
210 PRINT_HIERARCHY(gLogFilter);
212 // 1) Check if it is an interrupted event - we should inform our last primary hit actor about this
213 // and emit the stage signal as well.
215 if ( event.points[0].GetState() == PointState::INTERRUPTED )
217 Dali::Actor consumingActor;
218 Integration::Point currentPoint( event.points[0] );
220 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
221 if ( lastPrimaryHitActor )
223 Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
224 currentPoint.SetHitActor( lastPrimaryHitActorHandle );
226 consumingActor = AllocAndEmitTouchSignals( event.time, lastPrimaryHitActorHandle, currentPoint );
229 // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
230 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
231 if ( lastConsumedActor &&
232 lastConsumedActor != lastPrimaryHitActor &&
233 lastConsumedActor != consumingActor )
235 Dali::Actor lastConsumedActorHandle( lastConsumedActor );
236 currentPoint.SetHitActor( lastConsumedActorHandle );
237 AllocAndEmitTouchSignals( event.time, lastConsumedActorHandle, currentPoint );
240 // Tell the touch-down consuming actor as well, if required
241 Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
242 if ( touchDownConsumedActor &&
243 touchDownConsumedActor != lastPrimaryHitActor &&
244 touchDownConsumedActor != lastConsumedActor &&
245 touchDownConsumedActor != consumingActor )
247 Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
249 currentPoint.SetHitActor( touchDownConsumedActorHandle );
250 AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
253 mLastPrimaryHitActor.SetActor( nullptr );
254 mLastConsumedActor.SetActor( nullptr );
255 mCapturingTouchActor.SetActor( nullptr );
256 mTouchDownConsumedActor.SetActor( nullptr );
257 mLastRenderTask.Reset();
259 currentPoint.SetHitActor( Dali::Actor() );
261 TouchDataPtr touchData( new TouchData( event.time ) );
262 Dali::TouchData touchDataHandle( touchData.Get() );
264 touchData->AddPoint( currentPoint );
266 mScene.EmitTouchedSignal( touchDataHandle );
267 return false; // No need for hit testing & already an interrupted event so just return false
271 TouchDataPtr touchData( new TouchData( event.time ) );
272 Dali::TouchData touchDataHandle( touchData.Get() );
274 DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
275 DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
277 RenderTaskPtr currentRenderTask;
278 bool firstPointParsed = false;
280 for ( auto&& currentPoint : event.points )
282 HitTestAlgorithm::Results hitTestResults;
283 if( !firstPointParsed )
285 firstPointParsed = true;
286 ParsePrimaryTouchPoint( hitTestResults, mCapturingTouchActor, mLastRenderTask, currentPoint, mScene );
288 // Only set the currentRenderTask for the primary hit actor.
289 currentRenderTask = hitTestResults.renderTask;
293 HitTestAlgorithm::HitTest( mScene.GetSize(), mScene.GetRenderTaskList(), mScene.GetLayerList(), currentPoint.GetScreenPosition(), hitTestResults );
296 Integration::Point newPoint( currentPoint );
297 newPoint.SetHitActor( hitTestResults.actor );
298 newPoint.SetLocalPosition( hitTestResults.actorCoordinates );
300 touchData->AddPoint( newPoint );
302 DALI_LOG_INFO( gLogFilter, Debug::General, " State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
303 TOUCH_POINT_STATE[currentPoint.GetState()], currentPoint.GetScreenPosition().x, currentPoint.GetScreenPosition().y,
304 ( hitTestResults.actor ? reinterpret_cast< void* >( &hitTestResults.actor.GetBaseObject() ) : NULL ),
305 ( hitTestResults.actor ? hitTestResults.actor.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() : "" ),
306 hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
310 // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
312 bool consumed = false;
314 // Emit the touch signal
315 Dali::Actor consumedActor;
316 if ( currentRenderTask )
318 consumedActor = EmitTouchSignals( touchData->GetPoint( 0 ).GetHitActor(), touchDataHandle );
319 consumed = consumedActor ? true : false;
322 Integration::Point& primaryPoint = touchData->GetPoint( 0 );
323 Dali::Actor primaryHitActor = primaryPoint.GetHitActor();
324 PointState::Type primaryPointState = primaryPoint.GetState();
326 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() : "" );
327 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() : "" );
329 if ( ( primaryPointState == PointState::DOWN ) &&
330 ( touchData->GetPointCount() == 1 ) &&
331 ( consumedActor && consumedActor.GetProperty< bool >( Dali::Actor::Property::CONNECTED_TO_SCENE ) ) )
333 mTouchDownConsumedActor.SetActor( &GetImplementation( consumedActor ) );
336 // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
337 // hit actor. Also process the last consumed actor in the same manner.
339 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
340 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
341 if( ( primaryPointState == PointState::MOTION ) || ( primaryPointState == PointState::UP ) || ( primaryPointState == PointState::STATIONARY ) )
343 if( mLastRenderTask )
345 Dali::Actor leaveEventConsumer;
346 RenderTask& lastRenderTaskImpl = *mLastRenderTask.Get();
348 if( lastPrimaryHitActor &&
349 lastPrimaryHitActor != primaryHitActor &&
350 lastPrimaryHitActor != consumedActor )
352 if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
354 if ( lastPrimaryHitActor->GetLeaveRequired() )
356 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit): (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
357 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchData, PointState::LEAVE );
362 // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
363 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
364 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit): (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
365 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchData, PointState::INTERRUPTED );
369 consumed |= leaveEventConsumer ? true : false;
371 // Check if the motion event has been consumed by another actor's listener. In this case, the previously
372 // consumed actor's listeners may need to be informed (through a leave event).
373 // Further checks here to ensure we do not signal the same actor twice for the same event.
374 if ( lastConsumedActor &&
375 lastConsumedActor != consumedActor &&
376 lastConsumedActor != lastPrimaryHitActor &&
377 lastConsumedActor != primaryHitActor &&
378 lastConsumedActor != leaveEventConsumer )
380 if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
382 if( lastConsumedActor->GetLeaveRequired() )
384 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
385 EmitTouchSignals( lastConsumedActor, lastRenderTaskImpl, touchData, PointState::LEAVE );
390 // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
391 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
392 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
393 EmitTouchSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, touchData, PointState::INTERRUPTED );
399 // 5) If our primary point is an Up event, then the primary point (in multi-touch) will change next
400 // time so set our last primary actor to NULL. Do the same to the last consumed actor as well.
402 if ( primaryPointState == PointState::UP )
404 mLastPrimaryHitActor.SetActor( nullptr );
405 mLastConsumedActor.SetActor( nullptr );
406 mCapturingTouchActor.SetActor( nullptr );
407 mLastRenderTask.Reset();
411 // The primaryHitActor may have been removed from the scene so ensure it is still on the scene before setting members.
412 if ( primaryHitActor && GetImplementation( primaryHitActor ).OnScene() )
414 mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
416 // Only observe the consumed actor if we have a primaryHitActor (check if it is still on the scene).
417 if ( consumedActor && GetImplementation( consumedActor ).OnScene() )
419 mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
423 mLastConsumedActor.SetActor( NULL );
426 mLastRenderTask = currentRenderTask;
430 mLastPrimaryHitActor.SetActor( nullptr );
431 mLastConsumedActor.SetActor( nullptr );
432 mCapturingTouchActor.SetActor( nullptr );
433 mLastRenderTask.Reset();
437 // 6) Emit an interrupted event to the touch-down actor if it hasn't consumed the up and
438 // emit the stage touched event if required.
440 if ( touchData->GetPointCount() == 1 ) // Only want the first touch and the last release
442 switch ( primaryPointState )
446 Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
447 if ( touchDownConsumedActor &&
448 touchDownConsumedActor != consumedActor &&
449 touchDownConsumedActor != lastPrimaryHitActor &&
450 touchDownConsumedActor != lastConsumedActor )
452 Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
454 Integration::Point currentPoint = touchData->GetPoint( 0 );
455 currentPoint.SetHitActor( touchDownConsumedActorHandle );
456 currentPoint.SetState( PointState::INTERRUPTED );
458 AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
461 mTouchDownConsumedActor.SetActor( NULL );
466 case PointState::DOWN:
468 mScene.EmitTouchedSignal( touchDataHandle );
472 case PointState::MOTION:
473 case PointState::LEAVE:
474 case PointState::STATIONARY:
475 case PointState::INTERRUPTED:
486 void TouchEventProcessor::OnObservedActorDisconnected( Actor* actor )
488 if ( actor == mLastPrimaryHitActor.GetActor() )
490 Dali::Actor handle( actor );
492 Integration::Point point;
493 point.SetState( PointState::INTERRUPTED );
494 point.SetHitActor( handle );
496 TouchDataPtr touchData( new TouchData );
497 touchData->AddPoint( point );
498 Dali::TouchData touchDataHandle( touchData.Get() );
500 Dali::Actor eventConsumer = EmitTouchSignals( handle, touchDataHandle );
502 if ( mLastConsumedActor.GetActor() != eventConsumer )
504 EmitTouchSignals( Dali::Actor( mLastConsumedActor.GetActor() ), touchDataHandle );
507 // Do not set mLastPrimaryHitActor to NULL we may be iterating through its observers
509 mLastConsumedActor.SetActor( NULL );
510 mLastRenderTask.Reset();
514 } // namespace Internal