2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali/internal/event/events/hit-test-algorithm-impl.h>
21 #include <dali/integration-api/system-overlay.h>
22 #include <dali/public-api/math/vector2.h>
23 #include <dali/public-api/math/vector4.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/internal/event/actors/actor-impl.h>
26 #include <dali/internal/event/actors/camera-actor-impl.h>
27 #include <dali/internal/event/actors/layer-impl.h>
28 #include <dali/internal/event/actors/layer-list.h>
29 #include <dali/internal/event/actors/renderable-actor-impl.h>
30 #include <dali/internal/event/common/system-overlay-impl.h>
31 #include <dali/internal/event/common/stage-impl.h>
32 #include <dali/internal/event/common/projection.h>
33 #include <dali/internal/event/render-tasks/render-task-impl.h>
34 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
42 namespace HitTestAlgorithm
53 distance( std::numeric_limits<float>::max() ),
59 Dali::Actor actor; ///< the actor hit. (if actor hit, then initialised)
60 float x; ///< x position of hit (only valid if actor valid)
61 float y; ///< y position of hit (only valid if actor valid)
62 float distance; ///< distance from ray origin to hit actor
63 bool overlay; ///< true if the hit actor is an overlay
68 * The function to be used in the hit-test algorithm to check whether the actor is touchable.
69 * It is used by the touch event and gesture processor.
71 bool IsActorTouchableFunction(Dali::Actor actor, Dali::HitTestAlgorithm::TraverseType type)
73 bool hittable = false;
77 case Dali::HitTestAlgorithm::CHECK_ACTOR:
79 if( GetImplementation(actor).GetTouchRequired() && // Does the Application or derived actor type require a touch event?
80 GetImplementation(actor).IsHittable() ) // Is actor sensitive, visible and on the scene?
86 case Dali::HitTestAlgorithm::DESCEND_ACTOR_TREE:
88 if( actor.IsVisible() && // Actor is visible, if not visible then none of its children are visible.
89 actor.IsSensitive() ) // Actor is sensitive, if insensitive none of its children should be hittable either.
105 * Recursively hit test all the actors, without crossing into other layers.
106 * This algorithm performs a Depth-First-Search (DFS) on all Actors within Layer.
107 * Hit-Testing each Actor, noting the distance from the Ray-Origin (3D origin
108 * of touch vector). The closest Hit-Tested Actor is that which is returned.
109 * Exceptions to this rule are:
110 * - If the Actor is an overlay then it is considered closer than all previous
111 * overlays encountered in the hit test traversal.
112 * - When comparing against renderable parents, if Actor is the same distance
113 * or closer than it's renderable parent, then it takes priority.
115 HitActor HitTestWithinLayer( Actor& actor,
116 const Vector4& rayOrigin,
117 const Vector4& rayDir,
119 float& nearClippingPlane,
120 float& farClippingPlane,
121 Dali::HitTestAlgorithm::HitTestFunction func,
122 bool& stencilOnLayer,
124 bool parentIsStencil )
126 worldOverlay |= actor.IsOverlay();
130 // Children should inherit the stencil draw mode
131 bool isStencil = parentIsStencil;
133 if ( actor.GetDrawMode() == DrawMode::STENCIL && actor.IsVisible() )
136 stencilOnLayer = true;
139 // If we are a stencil or hittable...
140 if ( isStencil || func(Dali::Actor(&actor), Dali::HitTestAlgorithm::CHECK_ACTOR) ) // Is actor hittable
142 Vector3 size( actor.GetCurrentSize() );
144 if ( size.x > 0.0f && size.y > 0.0f && // Ensure the actor has a valid size.
145 actor.RaySphereTest( rayOrigin, rayDir ) ) // Perform quicker ray sphere test to see if our ray is close to the actor.
147 Vector4 hitPointLocal;
150 // Finally, perform a more accurate ray test to see if our ray actually hits the actor.
151 if( actor.RayActorTest( rayOrigin, rayDir, hitPointLocal, distance ) )
153 if( distance >= nearClippingPlane && distance <= farClippingPlane )
155 // If the hit has happened on a stencil then register, but don't record as hit result
162 hit.actor = Dali::Actor(&actor);
163 hit.x = hitPointLocal.x;
164 hit.y = hitPointLocal.y;
165 hit.distance = distance;
166 hit.overlay = worldOverlay;
173 // If there is a stencil on this layer and we've also registered a hit, then don't both searching any children
174 if ( stencilHit && hit.actor )
179 // Find a child hit, until we run out of actors in the current layer.
181 if( actor.GetChildCount() > 0 )
183 childHit.distance = std::numeric_limits<float>::max();
184 Dali::ActorContainer& children = actor.GetChildrenInternal();
186 // Hit test ALL children and calculate their distance.
187 bool parentIsRenderable = actor.IsRenderable();
189 for (Dali::ActorIter iter = children.begin(), endIter = children.end(); iter != endIter; ++iter)
191 // Descend tree only if...
192 if ( !iter->IsLayer() && // Child is NOT a layer, hit testing current layer only or Child is not a layer and we've inherited the stencil draw mode
193 ( isStencil || func(*iter, Dali::HitTestAlgorithm::DESCEND_ACTOR_TREE ) ) ) // Child is visible and sensitive, otherwise none of its children should be hittable.
195 HitActor currentHit( HitTestWithinLayer( GetImplementation(*iter), rayOrigin, rayDir, worldOverlay, nearClippingPlane, farClippingPlane, func, stencilOnLayer, stencilHit, isStencil ) );
197 // If Current child is an overlay, then it takes priority.
198 // If it is not an overlay, and the previously hit sibling is also not an overlay, then closest takes priority.
199 // (last overlay sibling has priority as is rendered on top)
200 if ( currentHit.distance >= 0.f && (currentHit.overlay || (!childHit.overlay && currentHit.distance < childHit.distance) ) )
202 if ( !parentIsRenderable )
204 // If our parent is not renderable, then child should be hit regardless of distance.
205 childHit = currentHit;
207 else if ( currentHit.overlay || (!hit.overlay && currentHit.distance <= hit.distance) )
209 // If our parent is renderable, then child should only be hit if it is an overlay, or if it is closer than a non-overlay.
210 // (child overlay has priority as is rendered on top of it's parent)
211 childHit = currentHit;
217 return ( childHit.actor ) ? childHit : hit;
221 * Return true if actor is sourceActor or a descendent of sourceActor
223 bool IsWithinSourceActors( const Actor& sourceActor, const Actor& actor )
225 if ( &sourceActor == &actor )
231 Actor* parent = actor.GetParent();
234 return IsWithinSourceActors( sourceActor, *parent );
238 // Not within source actors
243 * Returns true if the layer and all of the layer's parents are visible and sensitive.
245 inline bool IsActuallyHittable( Layer& layer, const Vector2& screenCoordinates, const Vector2& stageSize, Dali::HitTestAlgorithm::HitTestFunction func )
247 bool hittable( true );
249 if(layer.IsClipping())
251 ClippingBox box = layer.GetClippingBox();
253 if( screenCoordinates.x < box.x ||
254 screenCoordinates.x > box.x + box.width ||
255 screenCoordinates.y < stageSize.y - (box.y + box.height) ||
256 screenCoordinates.y > stageSize.y - box.y)
258 // Not touchable if clipping is enabled in the layer and the screen coordinate is outside the clip region.
265 Actor* actor( &layer );
266 while ( actor && hittable )
268 if ( !(func(Dali::Actor(actor), Dali::HitTestAlgorithm::DESCEND_ACTOR_TREE)) ) // Layer (or its Parent) is NOT visible and sensitive, so our layer is not either.
273 actor = actor->GetParent();
281 * Gets the near and far clipping planes of the camera from which the scene is viewed in the render task.
283 void GetCameraClippingPlane( RenderTask& renderTask, float& nearClippingPlane, float& farClippingPlane )
285 CameraActor* cameraActor = renderTask.GetCameraActor();
286 nearClippingPlane = cameraActor->GetNearClippingPlane();
287 farClippingPlane = cameraActor->GetFarClippingPlane();
291 * Hit test a RenderTask
293 bool HitTestRenderTask( LayerList& layers,
294 RenderTask& renderTask,
295 Vector2 screenCoordinates,
297 Dali::HitTestAlgorithm::HitTestFunction func )
299 if ( renderTask.IsHittable( screenCoordinates ) )
302 renderTask.GetViewport( viewport );
303 if( screenCoordinates.x < viewport.x ||
304 screenCoordinates.x > viewport.x + viewport.width ||
305 screenCoordinates.y < viewport.y ||
306 screenCoordinates.y > viewport.y + viewport.height )
308 // The screen coordinate is outside the viewport of render task. The viewport clips all layers.
312 float nearClippingPlane, farClippingPlane;
313 GetCameraClippingPlane(renderTask, nearClippingPlane, farClippingPlane);
315 // Determine the layer depth of the source actor
316 Actor* sourceActor( renderTask.GetSourceActor() );
319 Dali::Layer layer( sourceActor->GetLayer() );
322 const unsigned int sourceActorDepth( layer.GetDepth() );
324 CameraActor* cameraActor = renderTask.GetCameraActor();
325 bool pickingPossible = cameraActor->BuildPickingRay( screenCoordinates, viewport, results.rayOrigin, results.rayDirection );
326 if( !pickingPossible )
331 // Hit test starting with the top layer, working towards the bottom layer.
333 bool stencilOnLayer = false;
334 bool stencilHit = false;
335 const Vector2& stageSize = Stage::GetCurrent()->GetSize();
337 for (int i=layers.GetLayerCount()-1; i>=0 && !(hit.actor); --i)
339 Layer* layer( layers.GetLayer(i) );
341 HitActor previousHit = hit;
342 stencilOnLayer = false;
345 // Ensure layer is touchable (also checks whether ancestors are also touchable)
346 if ( IsActuallyHittable ( *layer, screenCoordinates, stageSize, func ) )
348 // Always hit-test the source actor; otherwise test whether the layer is below the source actor in the hierarchy
349 if ( sourceActorDepth == static_cast<unsigned int>(i) )
351 // Recursively hit test the source actor & children, without crossing into other layers.
352 hit = HitTestWithinLayer( *sourceActor, results.rayOrigin, results.rayDirection, false, nearClippingPlane, farClippingPlane, func, stencilOnLayer, stencilHit, false );
354 else if ( IsWithinSourceActors( *sourceActor, *layer ) )
356 // Recursively hit test all the actors, without crossing into other layers.
357 hit = HitTestWithinLayer( *layer, results.rayOrigin, results.rayDirection, false, nearClippingPlane, farClippingPlane, func, stencilOnLayer, stencilHit, false );
359 // If a stencil on this layer hasn't been hit, then discard hit results for this layer
360 if ( stencilOnLayer && !stencilHit )
368 results.renderTask = Dali::RenderTask(&renderTask);
369 results.actor = hit.actor;
370 results.actorCoordinates.x = hit.x;
371 results.actorCoordinates.y = hit.y;
372 return true; // Success
381 * Iterate through RenderTaskList and perform hit test.
384 void HitTestForEachRenderTask( LayerList& layers,
385 RenderTaskList& taskList,
386 const Vector2& screenCoordinates,
388 Dali::HitTestAlgorithm::HitTestFunction func )
390 RenderTaskList::RenderTaskContainer& tasks = taskList.GetTasks();
391 RenderTaskList::RenderTaskContainer::reverse_iterator endIter = tasks.rend();
393 // Check onscreen tasks before offscreen ones, hit test order should be reverse of draw order (see ProcessRenderTasks() where offscreen tasks are drawn first).
396 for ( RenderTaskList::RenderTaskContainer::reverse_iterator iter = tasks.rbegin(); endIter != iter; ++iter )
398 RenderTask& renderTask = GetImplementation( *iter );
399 Dali::FrameBufferImage frameBufferImage = renderTask.GetTargetFrameBuffer();
401 // Note that if frameBufferImage is NULL we are using the default (on screen) render target
404 ResourceId id = GetImplementation(frameBufferImage).GetResourceId();
414 if ( HitTestRenderTask( layers, renderTask, screenCoordinates, results, func ) )
416 // Exit when an actor is hit
417 return; // don't bother checking off screen tasks
422 for ( RenderTaskList::RenderTaskContainer::reverse_iterator iter = tasks.rbegin(); endIter != iter; ++iter )
424 RenderTask& renderTask = GetImplementation( *iter );
425 Dali::FrameBufferImage frameBufferImage = renderTask.GetTargetFrameBuffer();
427 // Note that if frameBufferImage is NULL we are using the default (on screen) render target
430 ResourceId id = GetImplementation(frameBufferImage).GetResourceId();
439 if ( HitTestRenderTask( layers, renderTask, screenCoordinates, results, func ) )
441 // Exit when an actor is hit
448 } // unnamed namespace
450 void HitTest( Stage& stage, const Vector2& screenCoordinates, Dali::HitTestAlgorithm::Results& results, Dali::HitTestAlgorithm::HitTestFunction func )
452 // Hit-test the regular on-stage actors
453 RenderTaskList& taskList = stage.GetRenderTaskList();
454 LayerList& layerList = stage.GetLayerList();
456 Results hitTestResults;
457 HitTestForEachRenderTask( layerList, taskList, screenCoordinates, hitTestResults, func );
459 results.actor = hitTestResults.actor;
460 results.actorCoordinates = hitTestResults.actorCoordinates;
463 void HitTest( Stage& stage, const Vector2& screenCoordinates, Results& results )
465 // Hit-test the system-overlay actors first
466 SystemOverlay* systemOverlay = stage.GetSystemOverlayInternal();
470 RenderTaskList& overlayTaskList = systemOverlay->GetOverlayRenderTasks();
471 LayerList& overlayLayerList = systemOverlay->GetLayerList();
473 HitTestForEachRenderTask( overlayLayerList, overlayTaskList, screenCoordinates, results, IsActorTouchableFunction );
476 // Hit-test the regular on-stage actors
477 if ( !results.actor )
479 RenderTaskList& taskList = stage.GetRenderTaskList();
480 LayerList& layerList = stage.GetLayerList();
482 HitTestForEachRenderTask( layerList, taskList, screenCoordinates, results, IsActorTouchableFunction );
486 void HitTest( Stage& stage, RenderTask& renderTask, const Vector2& screenCoordinates,
487 Dali::HitTestAlgorithm::Results& results, Dali::HitTestAlgorithm::HitTestFunction func )
489 Results hitTestResults;
490 HitTestRenderTask( stage.GetLayerList(), renderTask, screenCoordinates, hitTestResults, func );
491 results.actor = hitTestResults.actor;
492 results.actorCoordinates = hitTestResults.actorCoordinates;
495 } // namespace HitTestAlgorithm
497 } // namespace Internal