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/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;
171 TouchPoint::State state = static_cast< TouchPoint::State >( event.points[0].GetState() );
173 PRINT_HIERARCHY(gLogFilter);
175 // Copy so we can add the results of a hit-test.
176 HoverEvent hoverEvent( event.time );
178 // 1) Check if it is an interrupted event - we should inform our last primary hit actor about this
179 // and emit the stage signal as well.
181 if ( state == TouchPoint::Interrupted )
183 Dali::Actor consumingActor;
184 hoverEvent.points.push_back( event.points[0].GetTouchPoint() );
186 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
187 if ( lastPrimaryHitActor )
189 Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
190 hoverEvent.points[0].hitActor = lastPrimaryHitActorHandle;
191 consumingActor = EmitHoverSignals( lastPrimaryHitActorHandle, hoverEvent );
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 hoverEvent.points[0].hitActor = lastConsumedActorHandle;
202 EmitHoverSignals( lastConsumedActorHandle, hoverEvent );
205 // Tell the hover-start consuming actor as well, if required
206 Actor* hoverStartConsumedActor( mHoverStartConsumedActor.GetActor() );
207 if ( hoverStartConsumedActor &&
208 hoverStartConsumedActor != lastPrimaryHitActor &&
209 hoverStartConsumedActor != lastConsumedActor &&
210 hoverStartConsumedActor != consumingActor )
212 Dali::Actor hoverStartConsumedActorHandle( hoverStartConsumedActor );
213 hoverEvent.points[0].hitActor = hoverStartConsumedActorHandle;
214 EmitHoverSignals( hoverStartConsumedActorHandle, hoverEvent );
217 mLastPrimaryHitActor.SetActor( NULL );
218 mLastConsumedActor.SetActor( NULL );
219 mHoverStartConsumedActor.SetActor( NULL );
220 mLastRenderTask.Reset();
222 hoverEvent.points[0].hitActor.Reset();
224 return; // No need for hit testing
229 DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
230 DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
232 RenderTaskPtr currentRenderTask;
234 for ( Integration::PointContainerConstIterator iter = event.points.begin(), beginIter = event.points.begin(), endIter = event.points.end(); iter != endIter; ++iter )
236 HitTestAlgorithm::Results hitTestResults;
237 ActorHoverableCheck actorHoverableCheck;
238 HitTestAlgorithm::HitTest( stage, iter->GetScreenPosition(), hitTestResults, actorHoverableCheck );
240 TouchPoint newPoint( iter->GetTouchPoint() );
241 newPoint.hitActor = hitTestResults.actor;
242 newPoint.local = hitTestResults.actorCoordinates;
244 hoverEvent.points.push_back( newPoint );
246 DALI_LOG_INFO( gLogFilter, Debug::General, " State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
247 TOUCH_POINT_STATE[iter->GetState()], iter->GetScreenPosition().x, iter->GetScreenPosition().y,
248 ( hitTestResults.actor ? reinterpret_cast< void* >( &hitTestResults.actor.GetBaseObject() ) : NULL ),
249 ( hitTestResults.actor ? hitTestResults.actor.GetName().c_str() : "" ),
250 hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
252 // Only set the currentRenderTask for the primary hit actor.
253 if ( iter == beginIter && hitTestResults.renderTask )
255 currentRenderTask = hitTestResults.renderTask;
259 // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
261 // Emit the touch signal
262 Dali::Actor consumedActor;
263 if ( currentRenderTask )
265 consumedActor = EmitHoverSignals( hoverEvent.points[0].hitActor, hoverEvent );
268 TouchPoint& primaryPoint = hoverEvent.points[0];
269 Dali::Actor primaryHitActor = primaryPoint.hitActor;
270 TouchPoint::State primaryPointState = primaryPoint.state;
272 DALI_LOG_INFO( gLogFilter, Debug::Concise, "PrimaryHitActor: (%p) %s\n", primaryPoint.hitActor ? reinterpret_cast< void* >( &primaryPoint.hitActor.GetBaseObject() ) : NULL, primaryPoint.hitActor ? primaryPoint.hitActor.GetName().c_str() : "" );
273 DALI_LOG_INFO( gLogFilter, Debug::Concise, "ConsumedActor: (%p) %s\n", consumedActor ? reinterpret_cast< void* >( &consumedActor.GetBaseObject() ) : NULL, consumedActor ? consumedActor.GetName().c_str() : "" );
275 if ( ( primaryPointState == TouchPoint::Started ) &&
276 ( hoverEvent.GetPointCount() == 1 ) &&
277 ( consumedActor && consumedActor.OnStage() ) )
279 mHoverStartConsumedActor.SetActor( &GetImplementation( consumedActor ) );
282 // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
283 // hit actor. Also process the last consumed actor in the same manner.
285 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
286 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
287 if( (primaryPointState == TouchPoint::Motion) || (primaryPointState == TouchPoint::Finished) || (primaryPointState == TouchPoint::Stationary) )
289 if ( mLastRenderTask )
291 Dali::Actor leaveEventConsumer;
292 RenderTask& lastRenderTaskImpl = *mLastRenderTask.Get();
294 if( lastPrimaryHitActor &&
295 lastPrimaryHitActor != primaryHitActor &&
296 lastPrimaryHitActor != consumedActor )
298 if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
300 if ( lastPrimaryHitActor->GetLeaveRequired() )
302 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit): (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
303 leaveEventConsumer = EmitHoverSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, hoverEvent, TouchPoint::Leave );
308 // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
309 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
310 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit): (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
311 leaveEventConsumer = EmitHoverSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, hoverEvent, TouchPoint::Interrupted );
315 // Check if the motion event has been consumed by another actor's listener. In this case, the previously
316 // consumed actor's listeners may need to be informed (through a leave event).
317 // Further checks here to ensure we do not signal the same actor twice for the same event.
318 if ( lastConsumedActor &&
319 lastConsumedActor != consumedActor &&
320 lastConsumedActor != lastPrimaryHitActor &&
321 lastConsumedActor != primaryHitActor &&
322 lastConsumedActor != leaveEventConsumer )
324 if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
326 if( lastConsumedActor->GetLeaveRequired() )
328 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
329 EmitHoverSignals( lastConsumedActor, lastRenderTaskImpl, hoverEvent, TouchPoint::Leave );
334 // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
335 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
336 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
337 EmitHoverSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, hoverEvent, TouchPoint::Interrupted );
343 // 5) If our primary point is an Finished event, then the primary point (in multi-touch) will change next
344 // time so set our last primary actor to NULL. Do the same to the last consumed actor as well.
346 if ( primaryPointState == TouchPoint::Finished )
348 mLastPrimaryHitActor.SetActor( NULL );
349 mLastConsumedActor.SetActor( NULL );
350 mLastRenderTask.Reset();
354 // The primaryHitActor may have been removed from the stage so ensure it is still on the stage before setting members.
355 if ( primaryHitActor && primaryHitActor.OnStage() )
357 mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
359 // Only observe the consumed actor if we have a primaryHitActor (check if it is still on stage).
360 if ( consumedActor && consumedActor.OnStage() )
362 mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
366 mLastConsumedActor.SetActor( NULL );
369 mLastRenderTask = currentRenderTask;
373 mLastPrimaryHitActor.SetActor( NULL );
374 mLastConsumedActor.SetActor( NULL );
375 mLastRenderTask.Reset();
379 // 6) Emit an interrupted event to the hover-started actor if it hasn't consumed the Finished.
381 if ( hoverEvent.GetPointCount() == 1 ) // Only want the first hover started
383 switch ( primaryPointState )
385 case TouchPoint::Finished:
387 Actor* hoverStartConsumedActor( mHoverStartConsumedActor.GetActor() );
388 if ( hoverStartConsumedActor &&
389 hoverStartConsumedActor != consumedActor &&
390 hoverStartConsumedActor != lastPrimaryHitActor &&
391 hoverStartConsumedActor != lastConsumedActor )
393 Dali::Actor hoverStartConsumedActorHandle( hoverStartConsumedActor );
394 hoverEvent.points[0].hitActor = hoverStartConsumedActorHandle;
395 hoverEvent.points[0].state = TouchPoint::Interrupted;
396 EmitHoverSignals( hoverStartConsumedActorHandle, hoverEvent );
398 // Restore hover-event to original state
399 hoverEvent.points[0].hitActor = primaryHitActor;
400 hoverEvent.points[0].state = primaryPointState;
403 mHoverStartConsumedActor.SetActor( NULL );
405 // No break, Fallthrough
407 case TouchPoint::Started:
408 case TouchPoint::Motion:
409 case TouchPoint::Leave:
410 case TouchPoint::Stationary:
411 case TouchPoint::Interrupted:
412 case TouchPoint::Last:
421 } // namespace Internal