2 * Copyright (c) 2018 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;
150 } // unnamed namespace
152 TouchEventProcessor::TouchEventProcessor( Scene& scene )
154 mLastPrimaryHitActor( MakeCallback( this, &TouchEventProcessor::OnObservedActorDisconnected ) ),
155 mLastConsumedActor(),
156 mTouchDownConsumedActor(),
159 DALI_LOG_TRACE_METHOD( gLogFilter );
162 TouchEventProcessor::~TouchEventProcessor()
164 DALI_LOG_TRACE_METHOD( gLogFilter );
167 void TouchEventProcessor::ProcessTouchEvent( const Integration::TouchEvent& event )
169 DALI_LOG_TRACE_METHOD( gLogFilter );
170 DALI_ASSERT_ALWAYS( !event.points.empty() && "Empty TouchEvent sent from Integration\n" );
172 PRINT_HIERARCHY(gLogFilter);
174 // 1) Check if it is an interrupted event - we should inform our last primary hit actor about this
175 // and emit the stage signal as well.
177 if ( event.points[0].GetState() == PointState::INTERRUPTED )
179 Dali::Actor consumingActor;
180 Integration::Point currentPoint( event.points[0] );
182 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
183 if ( lastPrimaryHitActor )
185 Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
186 currentPoint.SetHitActor( lastPrimaryHitActorHandle );
188 consumingActor = AllocAndEmitTouchSignals( event.time, lastPrimaryHitActorHandle, currentPoint );
191 // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
192 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
193 if ( lastConsumedActor &&
194 lastConsumedActor != lastPrimaryHitActor &&
195 lastConsumedActor != consumingActor )
197 Dali::Actor lastConsumedActorHandle( lastConsumedActor );
198 currentPoint.SetHitActor( lastConsumedActorHandle );
199 AllocAndEmitTouchSignals( event.time, lastConsumedActorHandle, currentPoint );
202 // Tell the touch-down consuming actor as well, if required
203 Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
204 if ( touchDownConsumedActor &&
205 touchDownConsumedActor != lastPrimaryHitActor &&
206 touchDownConsumedActor != lastConsumedActor &&
207 touchDownConsumedActor != consumingActor )
209 Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
211 currentPoint.SetHitActor( touchDownConsumedActorHandle );
212 AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
215 mLastPrimaryHitActor.SetActor( NULL );
216 mLastConsumedActor.SetActor( NULL );
217 mTouchDownConsumedActor.SetActor( NULL );
218 mLastRenderTask.Reset();
220 currentPoint.SetHitActor( Dali::Actor() );
222 TouchEvent touchEvent( event.time );
223 TouchDataPtr touchData( new TouchData( event.time ) );
224 Dali::TouchData touchDataHandle( touchData.Get() );
226 touchEvent.points.push_back( currentPoint.GetTouchPoint() );
227 touchData->AddPoint( currentPoint );
229 mScene.EmitTouchedSignal( touchEvent, touchDataHandle );
230 return; // No need for hit testing
234 TouchEvent touchEvent( event.time );
235 TouchDataPtr touchData( new TouchData( event.time ) );
236 Dali::TouchData touchDataHandle( touchData.Get() );
238 DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
239 DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
241 RenderTaskPtr currentRenderTask;
243 for ( Integration::PointContainerConstIterator iter = event.points.begin(), beginIter = event.points.begin(), endIter = event.points.end(); iter != endIter; ++iter )
245 HitTestAlgorithm::Results hitTestResults;
246 HitTestAlgorithm::HitTest( mScene.GetSize(), mScene.GetRenderTaskList(), mScene.GetLayerList(), iter->GetScreenPosition(), hitTestResults );
248 Integration::Point newPoint( *iter );
249 newPoint.SetHitActor( hitTestResults.actor );
250 newPoint.SetLocalPosition( hitTestResults.actorCoordinates );
252 touchEvent.points.push_back( newPoint.GetTouchPoint() );
253 touchData->AddPoint( newPoint );
255 DALI_LOG_INFO( gLogFilter, Debug::General, " State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
256 TOUCH_POINT_STATE[iter->GetState()], iter->GetScreenPosition().x, iter->GetScreenPosition().y,
257 ( hitTestResults.actor ? reinterpret_cast< void* >( &hitTestResults.actor.GetBaseObject() ) : NULL ),
258 ( hitTestResults.actor ? hitTestResults.actor.GetName().c_str() : "" ),
259 hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
261 // Only set the currentRenderTask for the primary hit actor.
262 if ( iter == beginIter && hitTestResults.renderTask )
264 currentRenderTask = hitTestResults.renderTask;
268 // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
270 // Emit the touch signal
271 Dali::Actor consumedActor;
272 if ( currentRenderTask )
274 consumedActor = EmitTouchSignals( touchData->GetPoint( 0 ).GetHitActor(), touchEvent, touchDataHandle );
277 Integration::Point& primaryPoint = touchData->GetPoint( 0 );
278 Dali::Actor primaryHitActor = primaryPoint.GetHitActor();
279 PointState::Type primaryPointState = primaryPoint.GetState();
281 DALI_LOG_INFO( gLogFilter, Debug::Concise, "PrimaryHitActor: (%p) %s\n", primaryHitActor ? reinterpret_cast< void* >( &primaryHitActor.GetBaseObject() ) : NULL, primaryHitActor ? primaryHitActor.GetName().c_str() : "" );
282 DALI_LOG_INFO( gLogFilter, Debug::Concise, "ConsumedActor: (%p) %s\n", consumedActor ? reinterpret_cast< void* >( &consumedActor.GetBaseObject() ) : NULL, consumedActor ? consumedActor.GetName().c_str() : "" );
284 if ( ( primaryPointState == PointState::DOWN ) &&
285 ( touchEvent.GetPointCount() == 1 ) &&
286 ( consumedActor && consumedActor.OnStage() ) )
288 mTouchDownConsumedActor.SetActor( &GetImplementation( consumedActor ) );
291 // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
292 // hit actor. Also process the last consumed actor in the same manner.
294 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
295 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
296 if( ( primaryPointState == PointState::MOTION ) || ( primaryPointState == PointState::UP ) || ( primaryPointState == PointState::STATIONARY ) )
298 if( mLastRenderTask )
300 Dali::Actor leaveEventConsumer;
301 RenderTask& lastRenderTaskImpl = *mLastRenderTask.Get();
303 if( lastPrimaryHitActor &&
304 lastPrimaryHitActor != primaryHitActor &&
305 lastPrimaryHitActor != consumedActor )
307 if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
309 if ( lastPrimaryHitActor->GetLeaveRequired() )
311 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit): (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
312 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::LEAVE );
317 // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
318 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
319 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit): (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
320 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::INTERRUPTED );
324 // Check if the motion event has been consumed by another actor's listener. In this case, the previously
325 // consumed actor's listeners may need to be informed (through a leave event).
326 // Further checks here to ensure we do not signal the same actor twice for the same event.
327 if ( lastConsumedActor &&
328 lastConsumedActor != consumedActor &&
329 lastConsumedActor != lastPrimaryHitActor &&
330 lastConsumedActor != primaryHitActor &&
331 lastConsumedActor != leaveEventConsumer )
333 if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
335 if( lastConsumedActor->GetLeaveRequired() )
337 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
338 EmitTouchSignals( lastConsumedActor, lastRenderTaskImpl, touchEvent, touchData, PointState::LEAVE );
343 // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
344 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
345 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
346 EmitTouchSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::INTERRUPTED );
352 // 5) If our primary point is an Up event, then the primary point (in multi-touch) will change next
353 // time so set our last primary actor to NULL. Do the same to the last consumed actor as well.
355 if ( primaryPointState == PointState::UP )
357 mLastPrimaryHitActor.SetActor( NULL );
358 mLastConsumedActor.SetActor( NULL );
359 mLastRenderTask.Reset();
363 // The primaryHitActor may have been removed from the stage so ensure it is still on the stage before setting members.
364 if ( primaryHitActor && primaryHitActor.OnStage() )
366 mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
368 // Only observe the consumed actor if we have a primaryHitActor (check if it is still on stage).
369 if ( consumedActor && consumedActor.OnStage() )
371 mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
375 mLastConsumedActor.SetActor( NULL );
378 mLastRenderTask = currentRenderTask;
382 mLastPrimaryHitActor.SetActor( NULL );
383 mLastConsumedActor.SetActor( NULL );
384 mLastRenderTask.Reset();
388 // 6) Emit an interrupted event to the touch-down actor if it hasn't consumed the up and
389 // emit the stage touched event if required.
391 if ( touchEvent.GetPointCount() == 1 ) // Only want the first touch and the last release
393 switch ( primaryPointState )
397 Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
398 if ( touchDownConsumedActor &&
399 touchDownConsumedActor != consumedActor &&
400 touchDownConsumedActor != lastPrimaryHitActor &&
401 touchDownConsumedActor != lastConsumedActor )
403 Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
405 Integration::Point currentPoint = touchData->GetPoint( 0 );
406 currentPoint.SetHitActor( touchDownConsumedActorHandle );
407 currentPoint.SetState( PointState::INTERRUPTED );
409 AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
412 mTouchDownConsumedActor.SetActor( NULL );
414 // No break, Fallthrough
416 case PointState::DOWN:
418 mScene.EmitTouchedSignal( touchEvent, touchDataHandle );
422 case PointState::MOTION:
423 case PointState::LEAVE:
424 case PointState::STATIONARY:
425 case PointState::INTERRUPTED:
434 void TouchEventProcessor::OnObservedActorDisconnected( Actor* actor )
436 if ( actor == mLastPrimaryHitActor.GetActor() )
438 Dali::Actor handle( actor );
440 Integration::Point point;
441 point.SetState( PointState::INTERRUPTED );
442 point.SetHitActor( handle );
444 TouchDataPtr touchData( new TouchData );
445 touchData->AddPoint( point );
446 Dali::TouchData touchDataHandle( touchData.Get() );
448 TouchEvent touchEvent( 0 );
449 touchEvent.points.push_back( point.GetTouchPoint() );
451 Dali::Actor eventConsumer = EmitTouchSignals( handle, touchEvent, touchDataHandle );
453 if ( mLastConsumedActor.GetActor() != eventConsumer )
455 EmitTouchSignals( Dali::Actor( mLastConsumedActor.GetActor() ), touchEvent, touchDataHandle );
458 // Do not set mLastPrimaryHitActor to NULL we may be iterating through its observers
460 mLastConsumedActor.SetActor( NULL );
461 mLastRenderTask.Reset();
465 } // namespace Internal