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/scene-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( Scene& scene )
152 DALI_LOG_TRACE_METHOD( gLogFilter );
155 HoverEventProcessor::~HoverEventProcessor()
157 DALI_LOG_TRACE_METHOD( gLogFilter );
160 void HoverEventProcessor::ProcessHoverEvent( const Integration::HoverEvent& event )
162 DALI_LOG_TRACE_METHOD( gLogFilter );
163 DALI_ASSERT_ALWAYS( !event.points.empty() && "Empty HoverEvent sent from Integration\n" );
165 TouchPoint::State state = static_cast< TouchPoint::State >( event.points[0].GetState() );
167 PRINT_HIERARCHY(gLogFilter);
169 // Copy so we can add the results of a hit-test.
170 HoverEvent hoverEvent( event.time );
172 // 1) Check if it is an interrupted event - we should inform our last primary hit actor about this
173 // and emit the stage signal as well.
175 if ( state == TouchPoint::Interrupted )
177 Dali::Actor consumingActor;
178 hoverEvent.points.push_back( event.points[0].GetTouchPoint() );
180 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
181 if ( lastPrimaryHitActor )
183 Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
184 hoverEvent.points[0].hitActor = lastPrimaryHitActorHandle;
185 consumingActor = EmitHoverSignals( lastPrimaryHitActorHandle, hoverEvent );
188 // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
189 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
190 if ( lastConsumedActor &&
191 lastConsumedActor != lastPrimaryHitActor &&
192 lastConsumedActor != consumingActor )
194 Dali::Actor lastConsumedActorHandle( lastConsumedActor );
195 hoverEvent.points[0].hitActor = lastConsumedActorHandle;
196 EmitHoverSignals( lastConsumedActorHandle, hoverEvent );
199 // Tell the hover-start consuming actor as well, if required
200 Actor* hoverStartConsumedActor( mHoverStartConsumedActor.GetActor() );
201 if ( hoverStartConsumedActor &&
202 hoverStartConsumedActor != lastPrimaryHitActor &&
203 hoverStartConsumedActor != lastConsumedActor &&
204 hoverStartConsumedActor != consumingActor )
206 Dali::Actor hoverStartConsumedActorHandle( hoverStartConsumedActor );
207 hoverEvent.points[0].hitActor = hoverStartConsumedActorHandle;
208 EmitHoverSignals( hoverStartConsumedActorHandle, hoverEvent );
211 mLastPrimaryHitActor.SetActor( NULL );
212 mLastConsumedActor.SetActor( NULL );
213 mHoverStartConsumedActor.SetActor( NULL );
214 mLastRenderTask.Reset();
216 hoverEvent.points[0].hitActor.Reset();
218 return; // No need for hit testing
223 DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
224 DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
226 RenderTaskPtr currentRenderTask;
228 for ( Integration::PointContainerConstIterator iter = event.points.begin(), beginIter = event.points.begin(), endIter = event.points.end(); iter != endIter; ++iter )
230 HitTestAlgorithm::Results hitTestResults;
231 ActorHoverableCheck actorHoverableCheck;
232 HitTestAlgorithm::HitTest( mScene.GetSize(), mScene.GetRenderTaskList(), mScene.GetLayerList(), iter->GetScreenPosition(), hitTestResults, actorHoverableCheck );
234 TouchPoint newPoint( iter->GetTouchPoint() );
235 newPoint.hitActor = hitTestResults.actor;
236 newPoint.local = hitTestResults.actorCoordinates;
238 hoverEvent.points.push_back( newPoint );
240 DALI_LOG_INFO( gLogFilter, Debug::General, " State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
241 TOUCH_POINT_STATE[iter->GetState()], iter->GetScreenPosition().x, iter->GetScreenPosition().y,
242 ( hitTestResults.actor ? reinterpret_cast< void* >( &hitTestResults.actor.GetBaseObject() ) : NULL ),
243 ( hitTestResults.actor ? hitTestResults.actor.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() : "" ),
244 hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
246 // Only set the currentRenderTask for the primary hit actor.
247 if ( iter == beginIter && hitTestResults.renderTask )
249 currentRenderTask = hitTestResults.renderTask;
253 // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
255 // Emit the touch signal
256 Dali::Actor consumedActor;
257 if ( currentRenderTask )
259 consumedActor = EmitHoverSignals( hoverEvent.points[0].hitActor, hoverEvent );
262 TouchPoint& primaryPoint = hoverEvent.points[0];
263 Dali::Actor primaryHitActor = primaryPoint.hitActor;
264 TouchPoint::State primaryPointState = primaryPoint.state;
266 DALI_LOG_INFO( gLogFilter, Debug::Concise, "PrimaryHitActor: (%p) %s\n", primaryPoint.hitActor ? reinterpret_cast< void* >( &primaryPoint.hitActor.GetBaseObject() ) : NULL, primaryPoint.hitActor ? primaryPoint.hitActor.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() : "" );
267 DALI_LOG_INFO( gLogFilter, Debug::Concise, "ConsumedActor: (%p) %s\n", consumedActor ? reinterpret_cast< void* >( &consumedActor.GetBaseObject() ) : NULL, consumedActor ? consumedActor.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() : "" );
269 if ( ( primaryPointState == TouchPoint::Started ) &&
270 ( hoverEvent.GetPointCount() == 1 ) &&
271 ( consumedActor && consumedActor.OnStage() ) )
273 mHoverStartConsumedActor.SetActor( &GetImplementation( consumedActor ) );
276 // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
277 // hit actor. Also process the last consumed actor in the same manner.
279 Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
280 Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
281 if( (primaryPointState == TouchPoint::Motion) || (primaryPointState == TouchPoint::Finished) || (primaryPointState == TouchPoint::Stationary) )
283 if ( mLastRenderTask )
285 Dali::Actor leaveEventConsumer;
286 RenderTask& lastRenderTaskImpl = *mLastRenderTask.Get();
288 if( lastPrimaryHitActor &&
289 lastPrimaryHitActor != primaryHitActor &&
290 lastPrimaryHitActor != consumedActor )
292 if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
294 if ( lastPrimaryHitActor->GetLeaveRequired() )
296 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit): (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
297 leaveEventConsumer = EmitHoverSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, hoverEvent, TouchPoint::Leave );
302 // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
303 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
304 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit): (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
305 leaveEventConsumer = EmitHoverSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, hoverEvent, TouchPoint::Interrupted );
309 // Check if the motion event has been consumed by another actor's listener. In this case, the previously
310 // consumed actor's listeners may need to be informed (through a leave event).
311 // Further checks here to ensure we do not signal the same actor twice for the same event.
312 if ( lastConsumedActor &&
313 lastConsumedActor != consumedActor &&
314 lastConsumedActor != lastPrimaryHitActor &&
315 lastConsumedActor != primaryHitActor &&
316 lastConsumedActor != leaveEventConsumer )
318 if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
320 if( lastConsumedActor->GetLeaveRequired() )
322 DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
323 EmitHoverSignals( lastConsumedActor, lastRenderTaskImpl, hoverEvent, TouchPoint::Leave );
328 // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
329 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
330 DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
331 EmitHoverSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, hoverEvent, TouchPoint::Interrupted );
337 // 5) If our primary point is an Finished event, then the primary point (in multi-touch) will change next
338 // time so set our last primary actor to NULL. Do the same to the last consumed actor as well.
340 if ( primaryPointState == TouchPoint::Finished )
342 mLastPrimaryHitActor.SetActor( NULL );
343 mLastConsumedActor.SetActor( NULL );
344 mLastRenderTask.Reset();
348 // The primaryHitActor may have been removed from the stage so ensure it is still on the stage before setting members.
349 if ( primaryHitActor && primaryHitActor.OnStage() )
351 mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
353 // Only observe the consumed actor if we have a primaryHitActor (check if it is still on stage).
354 if ( consumedActor && consumedActor.OnStage() )
356 mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
360 mLastConsumedActor.SetActor( NULL );
363 mLastRenderTask = currentRenderTask;
367 mLastPrimaryHitActor.SetActor( NULL );
368 mLastConsumedActor.SetActor( NULL );
369 mLastRenderTask.Reset();
373 // 6) Emit an interrupted event to the hover-started actor if it hasn't consumed the Finished.
375 if ( hoverEvent.GetPointCount() == 1 ) // Only want the first hover started
377 switch ( primaryPointState )
379 case TouchPoint::Finished:
381 Actor* hoverStartConsumedActor( mHoverStartConsumedActor.GetActor() );
382 if ( hoverStartConsumedActor &&
383 hoverStartConsumedActor != consumedActor &&
384 hoverStartConsumedActor != lastPrimaryHitActor &&
385 hoverStartConsumedActor != lastConsumedActor )
387 Dali::Actor hoverStartConsumedActorHandle( hoverStartConsumedActor );
388 hoverEvent.points[0].hitActor = hoverStartConsumedActorHandle;
389 hoverEvent.points[0].state = TouchPoint::Interrupted;
390 EmitHoverSignals( hoverStartConsumedActorHandle, hoverEvent );
392 // Restore hover-event to original state
393 hoverEvent.points[0].hitActor = primaryHitActor;
394 hoverEvent.points[0].state = primaryPointState;
397 mHoverStartConsumedActor.SetActor( NULL );
399 // No break, Fallthrough
401 case TouchPoint::Started:
402 case TouchPoint::Motion:
403 case TouchPoint::Leave:
404 case TouchPoint::Stationary:
405 case TouchPoint::Interrupted:
406 case TouchPoint::Last:
415 } // namespace Internal