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/hover-event-processor.h>
21 #if defined(DEBUG_ENABLED)
26 #include <dali/public-api/math/vector2.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/integration-api/events/hover-event-integ.h>
29 #include <dali/internal/event/actors/actor-impl.h>
30 #include <dali/internal/event/actors/layer-impl.h>
31 #include <dali/internal/event/common/stage-impl.h>
32 #include <dali/internal/event/events/hit-test-algorithm-impl.h>
33 #include <dali/internal/event/events/multi-point-event-util.h>
34 #include <dali/internal/event/render-tasks/render-task-impl.h>
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_HOVER_PROCESSOR" );
48 const char * TOUCH_POINT_STATE[TouchPoint::Last] =
58 #endif // defined(DEBUG_ENABLED)
61 * Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
63 Dali::Actor EmitHoverSignals( Dali::Actor actor, const HoverEvent& event )
65 Dali::Actor consumedActor;
69 Dali::Actor oldParent( actor.GetParent() );
71 Actor& actorImpl( GetImplementation(actor) );
73 bool consumed( false );
75 // Only emit the signal if the actor's hover signal has connections (or derived actor implementation requires hover).
76 if ( actorImpl.GetHoverRequired() )
78 consumed = actorImpl.EmitHoverEventSignal( event );
83 // One of this actor's listeners has consumed the event so set this actor as the consumed actor.
84 consumedActor = Dali::Actor( &actorImpl );
88 // The actor may have been removed/reparented during the signal callbacks.
89 Dali::Actor parent = actor.GetParent();
92 (parent == oldParent) )
94 // One of the actor's parents may consumed the event and they should be set as the consumed actor.
95 consumedActor = EmitHoverSignals( parent, event );
100 return consumedActor;
104 * Changes the state of the primary point to leave and emits the hover signals
106 Dali::Actor EmitHoverSignals( Actor* actor, RenderTask& renderTask, const HoverEvent& originalEvent, TouchPoint::State state )
108 HoverEvent hoverEvent( originalEvent );
110 DALI_ASSERT_DEBUG( NULL != actor && "NULL actor pointer" );
113 TouchPoint& primaryPoint = hoverEvent.points[0];
115 actor->ScreenToLocal( renderTask, primaryPoint.local.x, primaryPoint.local.y, primaryPoint.screen.x, primaryPoint.screen.y );
117 primaryPoint.hitActor = Dali::Actor(actor);
118 primaryPoint.state = state;
121 return EmitHoverSignals( Dali::Actor(actor), hoverEvent );
125 * Used in the hit-test algorithm to check whether the actor is hoverable.
127 struct ActorHoverableCheck : public HitTestAlgorithm::HitTestInterface
129 bool IsActorHittable( Actor* actor )
131 return actor->GetHoverRequired() && // Does the Application or derived actor type require a hover event?
132 actor->IsHittable(); // Is actor sensitive, visible and on the scene?
135 bool DescendActorHierarchy( Actor* actor )
137 return actor->IsVisible() && // Actor is visible, if not visible then none of its children are visible.
138 actor->IsSensitive(); // Actor is sensitive, if insensitive none of its children should be hittable either.
141 bool DoesLayerConsumeHit( Layer* layer )
143 return layer->IsHoverConsumed();
147 } // unnamed namespace
149 HoverEventProcessor::HoverEventProcessor( Stage& stage )
151 mLastPrimaryHitActor(),
152 mLastConsumedActor(),
153 mHoverStartConsumedActor(),
156 DALI_LOG_TRACE_METHOD( gLogFilter );
159 HoverEventProcessor::~HoverEventProcessor()
161 DALI_LOG_TRACE_METHOD( gLogFilter );
164 void HoverEventProcessor::ProcessHoverEvent( const Integration::HoverEvent& event )
166 DALI_LOG_TRACE_METHOD( gLogFilter );
168 DALI_ASSERT_ALWAYS( !event.points.empty() && "Empty HoverEvent sent from Integration\n" );
170 Stage& stage = mStage;
172 PRINT_HIERARCHY(gLogFilter);
174 // Copy so we can add the results of a hit-test.
175 HoverEvent hoverEvent( event.time );
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].state == TouchPoint::Interrupted )
182 Dali::Actor consumingActor;
183 hoverEvent.points.push_back(event.points[0]);
185 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
186 if ( lastPrimaryHitActor )
188 Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
189 hoverEvent.points[0].hitActor = lastPrimaryHitActorHandle;
190 consumingActor = EmitHoverSignals( lastPrimaryHitActorHandle, hoverEvent );
193 // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
194 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
195 if ( lastConsumedActor &&
196 lastConsumedActor != lastPrimaryHitActor &&
197 lastConsumedActor != consumingActor )
199 Dali::Actor lastConsumedActorHandle( lastConsumedActor );
200 hoverEvent.points[0].hitActor = lastConsumedActorHandle;
201 EmitHoverSignals( lastConsumedActorHandle, hoverEvent );
204 // Tell the hover-start consuming actor as well, if required
205 Actor* hoverStartConsumedActor( mHoverStartConsumedActor.GetActor() );
206 if ( hoverStartConsumedActor &&
207 hoverStartConsumedActor != lastPrimaryHitActor &&
208 hoverStartConsumedActor != lastConsumedActor &&
209 hoverStartConsumedActor != consumingActor )
211 Dali::Actor hoverStartConsumedActorHandle( hoverStartConsumedActor );
212 hoverEvent.points[0].hitActor = hoverStartConsumedActorHandle;
213 EmitHoverSignals( hoverStartConsumedActorHandle, hoverEvent );
216 mLastPrimaryHitActor.SetActor( NULL );
217 mLastConsumedActor.SetActor( NULL );
218 mHoverStartConsumedActor.SetActor( NULL );
219 mLastRenderTask.Reset();
221 hoverEvent.points[0].hitActor.Reset();
223 return; // No need for hit testing
228 DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
229 DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
231 Dali::RenderTask currentRenderTask;
233 for ( TouchPointContainerConstIterator iter = event.points.begin(), beginIter = event.points.begin(), endIter = event.points.end(); iter != endIter; ++iter )
235 HitTestAlgorithm::Results hitTestResults;
236 ActorHoverableCheck actorHoverableCheck;
237 HitTestAlgorithm::HitTest( stage, iter->screen, hitTestResults, actorHoverableCheck );
239 TouchPoint newPoint( iter->deviceId, iter->state, iter->screen.x, iter->screen.y );
240 newPoint.hitActor = hitTestResults.actor;
241 newPoint.local = hitTestResults.actorCoordinates;
243 hoverEvent.points.push_back( newPoint );
245 DALI_LOG_INFO( gLogFilter, Debug::General, " State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
246 TOUCH_POINT_STATE[iter->state], iter->screen.x, iter->screen.y,
247 ( hitTestResults.actor ? (void*)&hitTestResults.actor.GetBaseObject() : NULL ),
248 ( hitTestResults.actor ? hitTestResults.actor.GetName().c_str() : "" ),
249 hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
251 // Only set the currentRenderTask for the primary hit actor.
252 if ( iter == beginIter && hitTestResults.renderTask )
254 currentRenderTask = hitTestResults.renderTask;
258 // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
260 // Emit the touch signal
261 Dali::Actor consumedActor;
262 if ( currentRenderTask )
264 consumedActor = EmitHoverSignals( hoverEvent.points[0].hitActor, hoverEvent );
267 TouchPoint& primaryPoint = hoverEvent.points[0];
268 Dali::Actor primaryHitActor = primaryPoint.hitActor;
269 TouchPoint::State primaryPointState = primaryPoint.state;
271 DALI_LOG_INFO( gLogFilter, Debug::Concise, "PrimaryHitActor: (%p) %s\n", primaryPoint.hitActor ? (void*)&primaryPoint.hitActor.GetBaseObject() : NULL, primaryPoint.hitActor ? primaryPoint.hitActor.GetName().c_str() : "" );
272 DALI_LOG_INFO( gLogFilter, Debug::Concise, "ConsumedActor: (%p) %s\n", consumedActor ? (void*)&consumedActor.GetBaseObject() : NULL, consumedActor ? consumedActor.GetName().c_str() : "" );
274 if ( ( primaryPointState == TouchPoint::Started ) &&
275 ( hoverEvent.GetPointCount() == 1 ) &&
276 ( consumedActor && consumedActor.OnStage() ) )
278 mHoverStartConsumedActor.SetActor( &GetImplementation( consumedActor ) );
281 // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
282 // hit actor. Also process the last consumed actor in the same manner.
284 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
285 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
286 if( (primaryPointState == TouchPoint::Motion) || (primaryPointState == TouchPoint::Finished) || (primaryPointState == TouchPoint::Stationary) )
288 if ( mLastRenderTask )
290 Dali::Actor leaveEventConsumer;
291 RenderTask& lastRenderTaskImpl( GetImplementation( mLastRenderTask ) );
293 if( lastPrimaryHitActor &&
294 lastPrimaryHitActor != primaryHitActor &&
295 lastPrimaryHitActor != consumedActor )
297 if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
299 if ( lastPrimaryHitActor->GetLeaveRequired() )
301 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit): (%p) %s\n", (void*)lastPrimaryHitActor, lastPrimaryHitActor->GetName().c_str() );
302 leaveEventConsumer = EmitHoverSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, hoverEvent, TouchPoint::Leave );
307 // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
308 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
309 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit): (%p) %s\n", (void*)lastPrimaryHitActor, lastPrimaryHitActor->GetName().c_str() );
310 leaveEventConsumer = EmitHoverSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, hoverEvent, TouchPoint::Interrupted );
314 // Check if the motion event has been consumed by another actor's listener. In this case, the previously
315 // consumed actor's listeners may need to be informed (through a leave event).
316 // Further checks here to ensure we do not signal the same actor twice for the same event.
317 if ( lastConsumedActor &&
318 lastConsumedActor != consumedActor &&
319 lastConsumedActor != lastPrimaryHitActor &&
320 lastConsumedActor != primaryHitActor &&
321 lastConsumedActor != leaveEventConsumer )
323 if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
325 if( lastConsumedActor->GetLeaveRequired() )
327 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", (void*)lastConsumedActor, lastConsumedActor->GetName().c_str() );
328 EmitHoverSignals( lastConsumedActor, lastRenderTaskImpl, hoverEvent, TouchPoint::Leave );
333 // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
334 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
335 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume): (%p) %s\n", (void*)lastConsumedActor, lastConsumedActor->GetName().c_str() );
336 EmitHoverSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, hoverEvent, TouchPoint::Interrupted );
342 // 5) If our primary point is an Finished event, then the primary point (in multi-touch) will change next
343 // time so set our last primary actor to NULL. Do the same to the last consumed actor as well.
345 if ( primaryPointState == TouchPoint::Finished )
347 mLastPrimaryHitActor.SetActor( NULL );
348 mLastConsumedActor.SetActor( NULL );
349 mLastRenderTask.Reset();
353 // The primaryHitActor may have been removed from the stage so ensure it is still on the stage before setting members.
354 if ( primaryHitActor && primaryHitActor.OnStage() )
356 mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
358 // Only observe the consumed actor if we have a primaryHitActor (check if it is still on stage).
359 if ( consumedActor && consumedActor.OnStage() )
361 mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
365 mLastConsumedActor.SetActor( NULL );
368 mLastRenderTask = currentRenderTask;
372 mLastPrimaryHitActor.SetActor( NULL );
373 mLastConsumedActor.SetActor( NULL );
374 mLastRenderTask.Reset();
378 // 6) Emit an interrupted event to the hover-started actor if it hasn't consumed the Finished.
380 if ( hoverEvent.GetPointCount() == 1 ) // Only want the first hover started
382 switch ( primaryPointState )
384 case TouchPoint::Finished:
386 Actor* hoverStartConsumedActor( mHoverStartConsumedActor.GetActor() );
387 if ( hoverStartConsumedActor &&
388 hoverStartConsumedActor != consumedActor &&
389 hoverStartConsumedActor != lastPrimaryHitActor &&
390 hoverStartConsumedActor != lastConsumedActor )
392 Dali::Actor hoverStartConsumedActorHandle( hoverStartConsumedActor );
393 hoverEvent.points[0].hitActor = hoverStartConsumedActorHandle;
394 hoverEvent.points[0].state = TouchPoint::Interrupted;
395 EmitHoverSignals( hoverStartConsumedActorHandle, hoverEvent );
397 // Restore hover-event to original state
398 hoverEvent.points[0].hitActor = primaryHitActor;
399 hoverEvent.points[0].state = primaryPointState;
402 mHoverStartConsumedActor.SetActor( NULL );
404 // No break, Fallthrough
406 case TouchPoint::Started:
407 case TouchPoint::Motion:
408 case TouchPoint::Leave:
409 case TouchPoint::Stationary:
410 case TouchPoint::Interrupted:
411 case TouchPoint::Last:
420 } // namespace Internal