2 * Copyright (c) 2014 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/actors/renderable-actor.h>
27 #include <dali/public-api/math/vector2.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/integration-api/events/touch-event-integ.h>
30 #include <dali/internal/event/actors/actor-impl.h>
31 #include <dali/internal/event/actors/layer-impl.h>
32 #include <dali/internal/event/common/stage-impl.h>
33 #include <dali/internal/event/events/hit-test-algorithm-impl.h>
34 #include <dali/internal/event/render-tasks/render-task-impl.h>
45 bool IsActuallySensitive( Actor* actor );
47 #if defined(DEBUG_ENABLED)
48 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TOUCH_PROCESSOR" );
50 const char * TOUCH_POINT_STATE[TouchPoint::Last] =
60 static const Debug::LogLevel HIERARCHY_DEBUG_LOG_LEVEL( Debug::Verbose );
62 static bool HIERARCHY_GEOMETRY( false );
63 static bool HIERARCHY_SENSITIVITY( false );
64 static bool HIERARCHY_TOUCH_REQUIRED( false );
65 static bool HIERARCHY_HITTABLE( false );
68 * Prints out all the children of the given actor when debug is enabled.
70 * @param[in] actor The actor whose children to print.
71 * @param[in] level The number of " | " to put in front of the children.
73 void PrintChildren( Dali::Actor actor, int level )
75 std::ostringstream output;
77 for ( int t = 0; t < level; ++t )
82 output << actor.GetName() << "(" << actor.GetTypeName() << ", " << actor.GetObjectPtr() << ")";
84 if ( HIERARCHY_GEOMETRY )
86 output << " Pos: " << actor.GetCurrentWorldPosition() << " Size: " << actor.GetCurrentSize() << " Scale: " << actor.GetCurrentWorldScale();
89 if ( HIERARCHY_SENSITIVITY )
91 output << " Sensitivity: " << ( IsActuallySensitive( &GetImplementation( actor ) ) ? "True " : "False " );
94 if ( HIERARCHY_TOUCH_REQUIRED )
96 output << " TouchRequired: " << ( GetImplementation(actor).GetTouchRequired() ? "True " : "False " );
99 if ( HIERARCHY_HITTABLE )
101 output << " Hittable: " << ( GetImplementation(actor).IsHittable() ? "True " : "False " );
106 DALI_LOG_INFO( gLogFilter, HIERARCHY_DEBUG_LOG_LEVEL, output.str().c_str() );
109 unsigned int numChildren=actor.GetChildCount();
110 for ( unsigned int i=0; i<numChildren; ++i )
112 PrintChildren( actor.GetChildAt(i), level );
118 * Prints the entire hierarchy of the scene.
120 void PrintHierarchy()
122 if ( gLogFilter->IsEnabledFor( HIERARCHY_DEBUG_LOG_LEVEL ) )
124 PrintChildren( Dali::Stage().GetCurrent().GetRootLayer(), 0 );
128 #define PRINT_HIERARCHY PrintHierarchy()
130 #else // defined(DEBUG_ENABLED)
132 #define PRINT_HIERARCHY
134 #endif // defined(DEBUG_ENABLED)
137 * Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
139 Dali::Actor EmitTouchSignals( Dali::Actor actor, RenderTask& renderTask, const TouchEvent& event )
141 Dali::Actor consumedActor;
145 Dali::Actor oldParent( actor.GetParent() );
147 Actor& actorImpl( GetImplementation(actor) );
149 bool consumed( false );
151 // Only emit the signal if the actor's touch signal has connections (or derived actor implementation requires touch).
152 if ( actorImpl.GetTouchRequired() )
154 consumed = actorImpl.EmitTouchEventSignal( event );
159 // One of this actor's listeners has consumed the event so set this actor as the consumed actor.
160 consumedActor = Dali::Actor( &actorImpl );
164 // The actor may have been removed/reparented during the signal callbacks.
165 Dali::Actor parent = actor.GetParent();
168 (parent == oldParent) )
170 // One of the actor's parents may consumed the event and they should be set as the consumed actor.
171 consumedActor = EmitTouchSignals( parent, renderTask, event );
176 return consumedActor;
180 * Changes the state of the primary point to leave and emits the touch signals
182 Dali::Actor EmitTouchSignals( Actor* actor, RenderTask& renderTask, const TouchEvent& originalEvent, TouchPoint::State state )
184 TouchEvent touchEvent( originalEvent );
185 TouchPoint& primaryPoint = touchEvent.points[0];
187 actor->ScreenToLocal( renderTask, primaryPoint.local.x, primaryPoint.local.y, primaryPoint.screen.x, primaryPoint.screen.y );
189 primaryPoint.hitActor = actor;
190 primaryPoint.state = state;
192 return EmitTouchSignals( Dali::Actor(actor), renderTask, touchEvent );
196 * In the hit test algorithm above we do not descend actor tree if it is insensitive, so here, we
197 * should also check if any of the actor's parents has become insensitive since we last processed
200 bool IsActuallySensitive( Actor* actor )
202 bool sensitive = true;
204 while ( actor && sensitive )
206 sensitive = actor->IsSensitive();
207 actor = actor->GetParent();
213 } // unnamed namespace
215 TouchEventProcessor::TouchEventProcessor( Stage& stage )
217 mLastPrimaryHitActor(),
218 mLastConsumedActor(),
221 DALI_LOG_TRACE_METHOD( gLogFilter );
224 TouchEventProcessor::~TouchEventProcessor()
226 DALI_LOG_TRACE_METHOD( gLogFilter );
229 void TouchEventProcessor::ProcessTouchEvent( const Integration::TouchEvent& event )
231 DALI_LOG_TRACE_METHOD( gLogFilter );
233 DALI_ASSERT_ALWAYS( !event.points.empty() && "Empty TouchEvent sent from Integration\n" );
235 Stage& stage = mStage;
239 // Copy so we can add the results of a hit-test.
240 TouchEvent touchEvent( event.time );
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].state == TouchPoint::Interrupted )
247 Dali::Actor consumingActor;
248 touchEvent.points.push_back(event.points[0]);
250 if ( mLastRenderTask )
252 RenderTask& lastRenderTaskImpl( GetImplementation( mLastRenderTask ) );
254 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
255 if ( lastPrimaryHitActor )
257 Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
258 touchEvent.points[0].hitActor = lastPrimaryHitActorHandle;
259 consumingActor = EmitTouchSignals( lastPrimaryHitActorHandle, lastRenderTaskImpl, touchEvent );
262 // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
263 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
264 if ( lastConsumedActor &&
265 lastConsumedActor != lastPrimaryHitActor &&
266 lastConsumedActor != consumingActor )
268 Dali::Actor lastConsumedActorHandle( lastConsumedActor );
269 touchEvent.points[0].hitActor = lastConsumedActorHandle;
270 EmitTouchSignals( lastConsumedActorHandle, lastRenderTaskImpl, touchEvent );
274 mLastPrimaryHitActor.SetActor( NULL );
275 mLastConsumedActor.SetActor( NULL );
276 mLastRenderTask.Reset();
278 touchEvent.points[0].hitActor = NULL;
279 mStage.EmitTouchedSignal( touchEvent );
281 return; // No need for hit testing
286 DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
287 DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
289 Dali::RenderTask currentRenderTask;
291 for ( TouchPointContainerConstIterator iter = event.points.begin(), beginIter = event.points.begin(), endIter = event.points.end(); iter != endIter; ++iter )
293 HitTestAlgorithm::Results hitTestResults;
294 HitTestAlgorithm::HitTest( stage, iter->screen, hitTestResults );
296 TouchPoint newPoint( iter->deviceId, iter->state, iter->screen.x, iter->screen.y );
297 newPoint.hitActor = hitTestResults.actor;
298 newPoint.local = hitTestResults.actorCoordinates;
300 touchEvent.points.push_back( newPoint );
302 DALI_LOG_INFO( gLogFilter, Debug::General, " State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
303 TOUCH_POINT_STATE[iter->state], iter->screen.x, iter->screen.y,
304 ( hitTestResults.actor ? (void*)&hitTestResults.actor.GetBaseObject() : NULL ),
305 ( hitTestResults.actor ? hitTestResults.actor.GetName().c_str() : "" ),
306 hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
308 // Only set the currentRenderTask for the primary hit actor.
309 if ( iter == beginIter && hitTestResults.renderTask )
311 currentRenderTask = hitTestResults.renderTask;
315 // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
317 // Emit the touch signal
318 Dali::Actor consumedActor;
319 if ( currentRenderTask )
321 consumedActor = EmitTouchSignals( touchEvent.points[0].hitActor, GetImplementation( currentRenderTask ), touchEvent );
324 TouchPoint& primaryPoint = touchEvent.points[0];
325 Dali::Actor primaryHitActor = primaryPoint.hitActor;
326 TouchPoint::State primaryPointState = primaryPoint.state;
328 DALI_LOG_INFO( gLogFilter, Debug::Concise, "PrimaryHitActor: (%p) %s\n", primaryPoint.hitActor ? (void*)&primaryPoint.hitActor.GetBaseObject() : NULL, primaryPoint.hitActor ? primaryPoint.hitActor.GetName().c_str() : "" );
329 DALI_LOG_INFO( gLogFilter, Debug::Concise, "ConsumedActor: (%p) %s\n", consumedActor ? (void*)&consumedActor.GetBaseObject() : NULL, consumedActor ? consumedActor.GetName().c_str() : "" );
331 // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
332 // hit actor. Also process the last consumed actor in the same manner.
334 if( (primaryPointState == TouchPoint::Motion) || (primaryPointState == TouchPoint::Up) || (primaryPointState == TouchPoint::Stationary) )
336 if ( mLastRenderTask )
338 Dali::Actor leaveEventConsumer;
339 RenderTask& lastRenderTaskImpl( GetImplementation( mLastRenderTask ) );
341 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
342 if( lastPrimaryHitActor &&
343 lastPrimaryHitActor != primaryHitActor &&
344 lastPrimaryHitActor != consumedActor )
346 if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
348 if ( lastPrimaryHitActor->GetLeaveRequired() )
350 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit): (%p) %s\n", (void*)lastPrimaryHitActor, lastPrimaryHitActor->GetName().c_str() );
351 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, TouchPoint::Leave );
356 // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
357 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
358 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit): (%p) %s\n", (void*)lastPrimaryHitActor, lastPrimaryHitActor->GetName().c_str() );
359 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, TouchPoint::Interrupted );
363 // Check if the motion event has been consumed by another actor's listener. In this case, the previously
364 // consumed actor's listeners may need to be informed (through a leave event).
365 // Further checks here to ensure we do not signal the same actor twice for the same event.
366 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
367 if ( lastConsumedActor &&
368 lastConsumedActor != consumedActor &&
369 lastConsumedActor != lastPrimaryHitActor &&
370 lastConsumedActor != primaryHitActor &&
371 lastConsumedActor != leaveEventConsumer )
373 if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
375 if( lastConsumedActor->GetLeaveRequired() )
377 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", (void*)lastConsumedActor, lastConsumedActor->GetName().c_str() );
378 EmitTouchSignals( lastConsumedActor, lastRenderTaskImpl, touchEvent, TouchPoint::Leave );
383 // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
384 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
385 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume): (%p) %s\n", (void*)lastConsumedActor, lastConsumedActor->GetName().c_str() );
386 EmitTouchSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, touchEvent, TouchPoint::Interrupted );
392 // 5) If our primary point is an Up event, then the primary point (in multi-touch) will change next
393 // time so set our last primary actor to NULL. Do the same to the last consumed actor as well.
395 if ( primaryPointState == TouchPoint::Up )
397 mLastPrimaryHitActor.SetActor( NULL );
398 mLastConsumedActor.SetActor( NULL );
399 mLastRenderTask.Reset();
403 // The primaryHitActor may have been removed from the stage so ensure it is still on the stage before setting members.
404 if ( primaryHitActor && primaryHitActor.OnStage() )
406 mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
408 // Only observe the consumed actor if we have a primaryHitActor (check if it is still on stage).
409 if ( consumedActor && consumedActor.OnStage() )
411 mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
415 mLastConsumedActor.SetActor( NULL );
418 mLastRenderTask = currentRenderTask;
422 mLastPrimaryHitActor.SetActor( NULL );
423 mLastConsumedActor.SetActor( NULL );
424 mLastRenderTask.Reset();
428 // 6) Emit the stage touched event if required.
429 if ( touchEvent.GetPointCount() == 1 ) // Only want the first touch and the last release
431 switch ( primaryPointState )
433 case TouchPoint::Down:
436 mStage.EmitTouchedSignal( touchEvent );
440 case TouchPoint::Motion:
441 case TouchPoint::Leave:
442 case TouchPoint::Stationary:
443 case TouchPoint::Interrupted:
444 case TouchPoint::Last:
453 TouchEventProcessor::ActorObserver::ActorObserver()
455 mActorDisconnected(false)
457 DALI_LOG_TRACE_METHOD( gLogFilter );
460 TouchEventProcessor::ActorObserver::~ActorObserver()
462 DALI_LOG_TRACE_METHOD( gLogFilter );
466 Actor* TouchEventProcessor::ActorObserver::GetActor()
468 return mActorDisconnected ? NULL : mActor;
471 void TouchEventProcessor::ActorObserver::SetActor( Actor* actor )
473 DALI_LOG_TRACE_METHOD( gLogFilter );
475 if ( mActor != actor )
483 mActor->AddObserver( *this );
484 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Start Observing: %p\n", mActor);
489 void TouchEventProcessor::ActorObserver::ResetActor()
493 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Stop Observing: %p\n", mActor);
494 mActor->RemoveObserver( *this );
496 mActorDisconnected = false;
500 void TouchEventProcessor::ActorObserver::SceneObjectRemoved( ProxyObject& proxy )
502 DALI_LOG_TRACE_METHOD( gLogFilter );
504 if ( mActor == &proxy )
506 // do not call proxy.RemoveObserver here, proxy is currently iterating through observers... you wouldnt want to upset proxy now would you?
507 mActorDisconnected = true;
511 void TouchEventProcessor::ActorObserver::ProxyDestroyed(ProxyObject& proxy)
513 DALI_LOG_TRACE_METHOD( gLogFilter );
515 if ( mActor == &proxy )
517 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Stop Observing: %p\n", mActor);
522 } // namespace Internal