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/events/multi-point-event-util.h>
35 #include <dali/internal/event/render-tasks/render-task-impl.h>
46 #if defined(DEBUG_ENABLED)
47 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TOUCH_PROCESSOR" );
49 const char * TOUCH_POINT_STATE[TouchPoint::Last] =
59 #endif // defined(DEBUG_ENABLED)
62 * Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
64 Dali::Actor EmitTouchSignals( Dali::Actor actor, const TouchEvent& event )
66 Dali::Actor consumedActor;
70 Dali::Actor oldParent( actor.GetParent() );
72 Actor& actorImpl( GetImplementation(actor) );
74 bool consumed( false );
76 // Only emit the signal if the actor's touch signal has connections (or derived actor implementation requires touch).
77 if ( actorImpl.GetTouchRequired() )
79 consumed = actorImpl.EmitTouchEventSignal( event );
84 // One of this actor's listeners has consumed the event so set this actor as the consumed actor.
85 consumedActor = Dali::Actor( &actorImpl );
89 // The actor may have been removed/reparented during the signal callbacks.
90 Dali::Actor parent = actor.GetParent();
93 (parent == oldParent) )
95 // One of the actor's parents may consumed the event and they should be set as the consumed actor.
96 consumedActor = EmitTouchSignals( parent, event );
101 return consumedActor;
105 * Changes the state of the primary point to leave and emits the touch signals
107 Dali::Actor EmitTouchSignals( Actor* actor, RenderTask& renderTask, const TouchEvent& originalEvent, TouchPoint::State state )
109 TouchEvent touchEvent( originalEvent );
111 DALI_ASSERT_DEBUG( NULL != actor && "NULL actor pointer" );
114 TouchPoint& primaryPoint = touchEvent.points[0];
116 actor->ScreenToLocal( renderTask, primaryPoint.local.x, primaryPoint.local.y, primaryPoint.screen.x, primaryPoint.screen.y );
118 primaryPoint.hitActor = Dali::Actor(actor);
119 primaryPoint.state = state;
122 return EmitTouchSignals( Dali::Actor(actor), touchEvent );
125 } // unnamed namespace
127 TouchEventProcessor::TouchEventProcessor( Stage& stage )
129 mLastPrimaryHitActor(),
130 mLastConsumedActor(),
131 mTouchDownConsumedActor(),
134 DALI_LOG_TRACE_METHOD( gLogFilter );
137 TouchEventProcessor::~TouchEventProcessor()
139 DALI_LOG_TRACE_METHOD( gLogFilter );
142 void TouchEventProcessor::ProcessTouchEvent( const Integration::TouchEvent& event )
144 DALI_LOG_TRACE_METHOD( gLogFilter );
146 DALI_ASSERT_ALWAYS( !event.points.empty() && "Empty TouchEvent sent from Integration\n" );
148 Stage& stage = mStage;
150 PRINT_HIERARCHY(gLogFilter);
152 // Copy so we can add the results of a hit-test.
153 TouchEvent touchEvent( event.time );
155 // 1) Check if it is an interrupted event - we should inform our last primary hit actor about this
156 // and emit the stage signal as well.
158 if ( event.points[0].state == TouchPoint::Interrupted )
160 Dali::Actor consumingActor;
161 touchEvent.points.push_back(event.points[0]);
163 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
164 if ( lastPrimaryHitActor )
166 Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
167 touchEvent.points[0].hitActor = lastPrimaryHitActorHandle;
168 consumingActor = EmitTouchSignals( lastPrimaryHitActorHandle, touchEvent );
171 // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
172 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
173 if ( lastConsumedActor &&
174 lastConsumedActor != lastPrimaryHitActor &&
175 lastConsumedActor != consumingActor )
177 Dali::Actor lastConsumedActorHandle( lastConsumedActor );
178 touchEvent.points[0].hitActor = lastConsumedActorHandle;
179 EmitTouchSignals( lastConsumedActorHandle, touchEvent );
182 // Tell the touch-down consuming actor as well, if required
183 Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
184 if ( touchDownConsumedActor &&
185 touchDownConsumedActor != lastPrimaryHitActor &&
186 touchDownConsumedActor != lastConsumedActor &&
187 touchDownConsumedActor != consumingActor )
189 Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
190 touchEvent.points[0].hitActor = touchDownConsumedActorHandle;
191 EmitTouchSignals( touchDownConsumedActorHandle, touchEvent );
194 mLastPrimaryHitActor.SetActor( NULL );
195 mLastConsumedActor.SetActor( NULL );
196 mTouchDownConsumedActor.SetActor( NULL );
197 mLastRenderTask.Reset();
199 touchEvent.points[0].hitActor.Reset();
200 mStage.EmitTouchedSignal( touchEvent );
202 return; // No need for hit testing
207 DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
208 DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
210 Dali::RenderTask currentRenderTask;
212 for ( TouchPointContainerConstIterator iter = event.points.begin(), beginIter = event.points.begin(), endIter = event.points.end(); iter != endIter; ++iter )
214 HitTestAlgorithm::Results hitTestResults;
215 HitTestAlgorithm::HitTest( stage, iter->screen, hitTestResults );
217 TouchPoint newPoint( iter->deviceId, iter->state, iter->screen.x, iter->screen.y );
218 newPoint.hitActor = hitTestResults.actor;
219 newPoint.local = hitTestResults.actorCoordinates;
221 touchEvent.points.push_back( newPoint );
223 DALI_LOG_INFO( gLogFilter, Debug::General, " State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
224 TOUCH_POINT_STATE[iter->state], iter->screen.x, iter->screen.y,
225 ( hitTestResults.actor ? (void*)&hitTestResults.actor.GetBaseObject() : NULL ),
226 ( hitTestResults.actor ? hitTestResults.actor.GetName().c_str() : "" ),
227 hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
229 // Only set the currentRenderTask for the primary hit actor.
230 if ( iter == beginIter && hitTestResults.renderTask )
232 currentRenderTask = hitTestResults.renderTask;
236 // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
238 // Emit the touch signal
239 Dali::Actor consumedActor;
240 if ( currentRenderTask )
242 consumedActor = EmitTouchSignals( touchEvent.points[0].hitActor, touchEvent );
245 TouchPoint& primaryPoint = touchEvent.points[0];
246 Dali::Actor primaryHitActor = primaryPoint.hitActor;
247 TouchPoint::State primaryPointState = primaryPoint.state;
249 DALI_LOG_INFO( gLogFilter, Debug::Concise, "PrimaryHitActor: (%p) %s\n", primaryPoint.hitActor ? (void*)&primaryPoint.hitActor.GetBaseObject() : NULL, primaryPoint.hitActor ? primaryPoint.hitActor.GetName().c_str() : "" );
250 DALI_LOG_INFO( gLogFilter, Debug::Concise, "ConsumedActor: (%p) %s\n", consumedActor ? (void*)&consumedActor.GetBaseObject() : NULL, consumedActor ? consumedActor.GetName().c_str() : "" );
252 if ( ( primaryPointState == TouchPoint::Down ) &&
253 ( touchEvent.GetPointCount() == 1 ) &&
254 ( consumedActor && consumedActor.OnStage() ) )
256 mTouchDownConsumedActor.SetActor( &GetImplementation( consumedActor ) );
259 // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
260 // hit actor. Also process the last consumed actor in the same manner.
262 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
263 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
264 if( (primaryPointState == TouchPoint::Motion) || (primaryPointState == TouchPoint::Up) || (primaryPointState == TouchPoint::Stationary) )
266 if ( mLastRenderTask )
268 Dali::Actor leaveEventConsumer;
269 RenderTask& lastRenderTaskImpl( GetImplementation( mLastRenderTask ) );
271 if( lastPrimaryHitActor &&
272 lastPrimaryHitActor != primaryHitActor &&
273 lastPrimaryHitActor != consumedActor )
275 if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
277 if ( lastPrimaryHitActor->GetLeaveRequired() )
279 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit): (%p) %s\n", (void*)lastPrimaryHitActor, lastPrimaryHitActor->GetName().c_str() );
280 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, TouchPoint::Leave );
285 // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
286 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
287 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit): (%p) %s\n", (void*)lastPrimaryHitActor, lastPrimaryHitActor->GetName().c_str() );
288 leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, TouchPoint::Interrupted );
292 // Check if the motion event has been consumed by another actor's listener. In this case, the previously
293 // consumed actor's listeners may need to be informed (through a leave event).
294 // Further checks here to ensure we do not signal the same actor twice for the same event.
295 if ( lastConsumedActor &&
296 lastConsumedActor != consumedActor &&
297 lastConsumedActor != lastPrimaryHitActor &&
298 lastConsumedActor != primaryHitActor &&
299 lastConsumedActor != leaveEventConsumer )
301 if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
303 if( lastConsumedActor->GetLeaveRequired() )
305 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", (void*)lastConsumedActor, lastConsumedActor->GetName().c_str() );
306 EmitTouchSignals( lastConsumedActor, lastRenderTaskImpl, touchEvent, TouchPoint::Leave );
311 // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
312 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
313 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume): (%p) %s\n", (void*)lastConsumedActor, lastConsumedActor->GetName().c_str() );
314 EmitTouchSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, touchEvent, TouchPoint::Interrupted );
320 // 5) If our primary point is an Up event, then the primary point (in multi-touch) will change next
321 // time so set our last primary actor to NULL. Do the same to the last consumed actor as well.
323 if ( primaryPointState == TouchPoint::Up )
325 mLastPrimaryHitActor.SetActor( NULL );
326 mLastConsumedActor.SetActor( NULL );
327 mLastRenderTask.Reset();
331 // The primaryHitActor may have been removed from the stage so ensure it is still on the stage before setting members.
332 if ( primaryHitActor && primaryHitActor.OnStage() )
334 mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
336 // Only observe the consumed actor if we have a primaryHitActor (check if it is still on stage).
337 if ( consumedActor && consumedActor.OnStage() )
339 mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
343 mLastConsumedActor.SetActor( NULL );
346 mLastRenderTask = currentRenderTask;
350 mLastPrimaryHitActor.SetActor( NULL );
351 mLastConsumedActor.SetActor( NULL );
352 mLastRenderTask.Reset();
356 // 6) Emit an interrupted event to the touch-down actor if it hasn't consumed the up and
357 // emit the stage touched event if required.
359 if ( touchEvent.GetPointCount() == 1 ) // Only want the first touch and the last release
361 switch ( primaryPointState )
365 Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
366 if ( touchDownConsumedActor &&
367 touchDownConsumedActor != consumedActor &&
368 touchDownConsumedActor != lastPrimaryHitActor &&
369 touchDownConsumedActor != lastConsumedActor )
371 Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
372 touchEvent.points[0].hitActor = touchDownConsumedActorHandle;
373 touchEvent.points[0].state = TouchPoint::Interrupted;
374 EmitTouchSignals( touchDownConsumedActorHandle, touchEvent );
376 // Restore touch-event to original state
377 touchEvent.points[0].hitActor = primaryHitActor;
378 touchEvent.points[0].state = primaryPointState;
381 mTouchDownConsumedActor.SetActor( NULL );
383 // No break, Fallthrough
385 case TouchPoint::Down:
387 mStage.EmitTouchedSignal( touchEvent );
391 case TouchPoint::Motion:
392 case TouchPoint::Leave:
393 case TouchPoint::Stationary:
394 case TouchPoint::Interrupted:
395 case TouchPoint::Last:
404 } // namespace Internal