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/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/stage-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( Stage& stage )
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 );
171 DALI_ASSERT_ALWAYS( !event.points.empty() && "Empty TouchEvent sent from Integration\n" );
173 Stage& stage = mStage;
175 PRINT_HIERARCHY(gLogFilter);
177 // 1) Check if it is an interrupted event - we should inform our last primary hit actor about this
178 // and emit the stage signal as well.
180 if ( event.points[0].GetState() == PointState::INTERRUPTED )
182 Dali::Actor consumingActor;
183 Integration::Point currentPoint( event.points[0] );
185 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
186 if ( lastPrimaryHitActor )
188 Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
189 currentPoint.SetHitActor( lastPrimaryHitActorHandle );
191 consumingActor = AllocAndEmitTouchSignals( event.time, lastPrimaryHitActorHandle, currentPoint );
194 // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
195 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
196 if ( lastConsumedActor &&
197 lastConsumedActor != lastPrimaryHitActor &&
198 lastConsumedActor != consumingActor )
200 Dali::Actor lastConsumedActorHandle( lastConsumedActor );
201 currentPoint.SetHitActor( lastConsumedActorHandle );
202 AllocAndEmitTouchSignals( event.time, lastConsumedActorHandle, currentPoint );
205 // Tell the touch-down consuming actor as well, if required
206 Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
207 if ( touchDownConsumedActor &&
208 touchDownConsumedActor != lastPrimaryHitActor &&
209 touchDownConsumedActor != lastConsumedActor &&
210 touchDownConsumedActor != consumingActor )
212 Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
214 currentPoint.SetHitActor( touchDownConsumedActorHandle );
215 AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
218 mLastPrimaryHitActor.SetActor( NULL );
219 mLastConsumedActor.SetActor( NULL );
220 mTouchDownConsumedActor.SetActor( NULL );
221 mLastRenderTask.Reset();
223 currentPoint.SetHitActor( Dali::Actor() );
225 TouchEvent touchEvent( event.time );
226 TouchDataPtr touchData( new TouchData( event.time ) );
227 Dali::TouchData touchDataHandle( touchData.Get() );
229 touchEvent.points.push_back( currentPoint.GetTouchPoint() );
230 touchData->AddPoint( currentPoint );
232 mStage.EmitTouchedSignal( touchEvent, touchDataHandle );
234 return; // No need for hit testing
238 TouchEvent touchEvent( event.time );
239 TouchDataPtr touchData( new TouchData( event.time ) );
240 Dali::TouchData touchDataHandle( touchData.Get() );
242 DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
243 DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
245 Dali::RenderTask currentRenderTask;
247 for ( Integration::PointContainerConstIterator iter = event.points.begin(), beginIter = event.points.begin(), endIter = event.points.end(); iter != endIter; ++iter )
249 HitTestAlgorithm::Results hitTestResults;
250 HitTestAlgorithm::HitTest( stage, iter->GetScreenPosition(), hitTestResults );
252 Integration::Point newPoint( *iter );
253 newPoint.SetHitActor( hitTestResults.actor );
254 newPoint.SetLocalPosition( hitTestResults.actorCoordinates );
256 touchEvent.points.push_back( newPoint.GetTouchPoint() );
257 touchData->AddPoint( newPoint );
259 DALI_LOG_INFO( gLogFilter, Debug::General, " State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
260 TOUCH_POINT_STATE[iter->GetState()], iter->GetScreenPosition().x, iter->GetScreenPosition().y,
261 ( hitTestResults.actor ? (void*)&hitTestResults.actor.GetBaseObject() : NULL ),
262 ( hitTestResults.actor ? hitTestResults.actor.GetName().c_str() : "" ),
263 hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
265 // Only set the currentRenderTask for the primary hit actor.
266 if ( iter == beginIter && hitTestResults.renderTask )
268 currentRenderTask = hitTestResults.renderTask;
272 // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
274 // Emit the touch signal
275 Dali::Actor consumedActor;
276 if ( currentRenderTask )
278 consumedActor = EmitTouchSignals( touchData->GetPoint( 0 ).GetHitActor(), touchEvent, touchDataHandle );
281 Integration::Point& primaryPoint = touchData->GetPoint( 0 );
282 Dali::Actor primaryHitActor = primaryPoint.GetHitActor();
283 PointState::Type primaryPointState = primaryPoint.GetState();
285 DALI_LOG_INFO( gLogFilter, Debug::Concise, "PrimaryHitActor: (%p) %s\n", primaryHitActor ? (void*)&primaryHitActor.GetBaseObject() : NULL, primaryHitActor ? primaryHitActor.GetName().c_str() : "" );
286 DALI_LOG_INFO( gLogFilter, Debug::Concise, "ConsumedActor: (%p) %s\n", consumedActor ? (void*)&consumedActor.GetBaseObject() : NULL, consumedActor ? consumedActor.GetName().c_str() : "" );
288 if ( ( primaryPointState == PointState::DOWN ) &&
289 ( touchEvent.GetPointCount() == 1 ) &&
290 ( consumedActor && consumedActor.OnStage() ) )
292 mTouchDownConsumedActor.SetActor( &GetImplementation( consumedActor ) );
295 // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
296 // hit actor. Also process the last consumed actor in the same manner.
298 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
299 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
300 if( ( primaryPointState == PointState::MOTION ) || ( primaryPointState == PointState::UP ) || ( primaryPointState == PointState::STATIONARY ) )
302 if ( mLastRenderTask )
304 Dali::Actor leaveEventConsumer;
305 RenderTask& lastRenderTaskImpl( GetImplementation( mLastRenderTask ) );
307 if( lastPrimaryHitActor &&
308 lastPrimaryHitActor != primaryHitActor &&
309 lastPrimaryHitActor != consumedActor )
311 if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
313 if ( lastPrimaryHitActor->GetLeaveRequired() )
315 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit): (%p) %s\n", (void*)lastPrimaryHitActor, lastPrimaryHitActor->GetName().c_str() );
316 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::LEAVE );
321 // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
322 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
323 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit): (%p) %s\n", (void*)lastPrimaryHitActor, lastPrimaryHitActor->GetName().c_str() );
324 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::INTERRUPTED );
328 // Check if the motion event has been consumed by another actor's listener. In this case, the previously
329 // consumed actor's listeners may need to be informed (through a leave event).
330 // Further checks here to ensure we do not signal the same actor twice for the same event.
331 if ( lastConsumedActor &&
332 lastConsumedActor != consumedActor &&
333 lastConsumedActor != lastPrimaryHitActor &&
334 lastConsumedActor != primaryHitActor &&
335 lastConsumedActor != leaveEventConsumer )
337 if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
339 if( lastConsumedActor->GetLeaveRequired() )
341 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", (void*)lastConsumedActor, lastConsumedActor->GetName().c_str() );
342 EmitTouchSignals( lastConsumedActor, lastRenderTaskImpl, touchEvent, touchData, PointState::LEAVE );
347 // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
348 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
349 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume): (%p) %s\n", (void*)lastConsumedActor, lastConsumedActor->GetName().c_str() );
350 EmitTouchSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::INTERRUPTED );
356 // 5) If our primary point is an Up event, then the primary point (in multi-touch) will change next
357 // time so set our last primary actor to NULL. Do the same to the last consumed actor as well.
359 if ( primaryPointState == PointState::UP )
361 mLastPrimaryHitActor.SetActor( NULL );
362 mLastConsumedActor.SetActor( NULL );
363 mLastRenderTask.Reset();
367 // The primaryHitActor may have been removed from the stage so ensure it is still on the stage before setting members.
368 if ( primaryHitActor && primaryHitActor.OnStage() )
370 mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
372 // Only observe the consumed actor if we have a primaryHitActor (check if it is still on stage).
373 if ( consumedActor && consumedActor.OnStage() )
375 mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
379 mLastConsumedActor.SetActor( NULL );
382 mLastRenderTask = currentRenderTask;
386 mLastPrimaryHitActor.SetActor( NULL );
387 mLastConsumedActor.SetActor( NULL );
388 mLastRenderTask.Reset();
392 // 6) Emit an interrupted event to the touch-down actor if it hasn't consumed the up and
393 // emit the stage touched event if required.
395 if ( touchEvent.GetPointCount() == 1 ) // Only want the first touch and the last release
397 switch ( primaryPointState )
401 Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
402 if ( touchDownConsumedActor &&
403 touchDownConsumedActor != consumedActor &&
404 touchDownConsumedActor != lastPrimaryHitActor &&
405 touchDownConsumedActor != lastConsumedActor )
407 Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
409 Integration::Point currentPoint = touchData->GetPoint( 0 );
410 currentPoint.SetHitActor( touchDownConsumedActorHandle );
411 currentPoint.SetState( PointState::INTERRUPTED );
413 AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
416 mTouchDownConsumedActor.SetActor( NULL );
418 // No break, Fallthrough
420 case PointState::DOWN:
422 mStage.EmitTouchedSignal( touchEvent, touchDataHandle );
426 case PointState::MOTION:
427 case PointState::LEAVE:
428 case PointState::STATIONARY:
429 case PointState::INTERRUPTED:
438 void TouchEventProcessor::OnObservedActorDisconnected( Actor* actor )
440 if ( actor == mLastPrimaryHitActor.GetActor() )
442 Dali::Actor handle( actor );
444 Integration::Point point;
445 point.SetState( PointState::INTERRUPTED );
446 point.SetHitActor( handle );
448 TouchDataPtr touchData( new TouchData );
449 touchData->AddPoint( point );
450 Dali::TouchData touchDataHandle( touchData.Get() );
452 TouchEvent touchEvent( 0 );
453 touchEvent.points.push_back( point.GetTouchPoint() );
455 Dali::Actor eventConsumer = EmitTouchSignals( handle, touchEvent, touchDataHandle );
457 if ( mLastConsumedActor.GetActor() != eventConsumer )
459 EmitTouchSignals( Dali::Actor( mLastConsumedActor.GetActor() ), touchEvent, touchDataHandle );
462 // Do not set mLastPrimaryHitActor to NULL we may be iterating through its observers
464 mLastConsumedActor.SetActor( NULL );
465 mLastRenderTask.Reset();
469 } // namespace Internal