2 * Copyright (c) 2016 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.
21 #include <dali/public-api/dali-core.h>
22 #include <dali/devel-api/events/hit-test-algorithm.h>
23 #include <dali/integration-api/events/touch-event-integ.h>
24 #include <dali-test-suite-utils.h>
32 * The functor to be used in the hit-test algorithm to check whether the actor is hittable.
34 bool IsActorHittableFunction(Actor actor, Dali::HitTestAlgorithm::TraverseType type)
36 bool hittable = false;
40 case Dali::HitTestAlgorithm::CHECK_ACTOR:
42 // Check whether the actor is visible and not fully transparent.
44 && actor.GetCurrentWorldColor().a > 0.01f) // not FULLY_TRANSPARENT
46 // Check whether the actor has the specific name "HittableActor"
47 if(actor.GetName() == "HittableActor")
54 case Dali::HitTestAlgorithm::DESCEND_ACTOR_TREE:
56 if( actor.IsVisible() ) // Actor is visible, if not visible then none of its children are visible.
72 bool DefaultIsActorTouchableFunction(Dali::Actor actor, Dali::HitTestAlgorithm::TraverseType type)
74 bool hittable = false;
78 case Dali::HitTestAlgorithm::CHECK_ACTOR:
80 if( actor.IsVisible() &&
81 actor.IsSensitive() &&
82 actor.GetCurrentWorldColor().a > 0.01f)
88 case Dali::HitTestAlgorithm::DESCEND_ACTOR_TREE:
90 if( actor.IsVisible() && // Actor is visible, if not visible then none of its children are visible.
91 actor.IsSensitive()) // Actor is sensitive, if insensitive none of its children should be hittable either.
106 } // anonymous namespace
109 // Positive test case for a method
110 int UtcDaliHitTestAlgorithmWithFunctor(void)
112 TestApplication application;
113 tet_infoline("Testing Dali::HitTestAlgorithm functor");
115 Stage stage = Stage::GetCurrent();
117 Actor actor = Actor::New();
118 actor.SetSize(100.0f, 100.0f);
119 actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
120 actor.SetName("NonHittableActor");
124 application.SendNotification();
125 application.Render();
127 Vector2 screenCoordinates( 10.0f, 10.0f );
128 Vector2 localCoordinates;
129 actor.ScreenToLocal( localCoordinates.x, localCoordinates.y, screenCoordinates.x, screenCoordinates.y );
131 // Perform a hit-test at the given screen coordinates
132 Dali::HitTestAlgorithm::Results results;
133 Dali::HitTestAlgorithm::HitTest( stage, screenCoordinates, results, IsActorHittableFunction );
134 DALI_TEST_CHECK( results.actor != actor );
136 actor.SetName("HittableActor");
138 results.actor = Actor();
139 results.actorCoordinates = Vector2::ZERO;
141 // Perform a hit-test at the given screen coordinates
142 Dali::HitTestAlgorithm::HitTest( stage, screenCoordinates, results, IsActorHittableFunction );
143 DALI_TEST_CHECK( results.actor == actor );
144 DALI_TEST_EQUALS( localCoordinates, results.actorCoordinates, 0.1f, TEST_LOCATION );
148 int UtcDaliHitTestAlgorithmOrtho01(void)
150 TestApplication application;
151 tet_infoline("Testing Dali::HitTestAlgorithm with parallel Ortho camera()");
153 Stage stage = Stage::GetCurrent();
154 RenderTaskList renderTaskList = stage.GetRenderTaskList();
155 RenderTask defaultRenderTask = renderTaskList.GetTask(0u);
156 Dali::CameraActor cameraActor = defaultRenderTask.GetCameraActor();
158 Vector2 stageSize ( stage.GetSize() );
159 cameraActor.SetOrthographicProjection( stageSize );
160 cameraActor.SetPosition(0.0f, 0.0f, 1600.0f);
162 Vector2 actorSize( stageSize * 0.5f );
163 // Create two actors with half the size of the stage and set them to be partially overlapping
164 Actor blue = Actor::New();
165 blue.SetName( "Blue" );
166 blue.SetAnchorPoint( AnchorPoint::CENTER );
167 blue.SetParentOrigin( Vector3(1.0f/3.0f, 1.0f/3.0f, 0.5f) );
168 blue.SetSize( actorSize );
171 Actor green = Actor::New( );
172 green.SetName( "Green" );
173 green.SetAnchorPoint( AnchorPoint::CENTER );
174 green.SetParentOrigin( Vector3(2.0f/3.0f, 2.0f/3.0f, 0.5f) );
175 green.SetSize( actorSize );
177 // Add the actors to the view
182 application.SendNotification();
183 application.Render(0);
184 application.Render(10);
186 HitTestAlgorithm::Results results;
187 HitTest(stage, stageSize / 2.0f, results, &DefaultIsActorTouchableFunction);
188 DALI_TEST_CHECK( results.actor == green );
189 DALI_TEST_EQUALS( results.actorCoordinates, actorSize * 1.0f/6.0f, TEST_LOCATION );
191 HitTest(stage, stageSize / 3.0f, results, &DefaultIsActorTouchableFunction);
192 DALI_TEST_CHECK( results.actor == blue );
193 DALI_TEST_EQUALS( results.actorCoordinates, actorSize * 0.5f, TEST_LOCATION );
195 HitTest(stage, stageSize * 2.0f / 3.0f, results, &DefaultIsActorTouchableFunction);
196 DALI_TEST_CHECK( results.actor == green );
197 DALI_TEST_EQUALS( results.actorCoordinates, actorSize * 0.5f, TEST_LOCATION );
202 int UtcDaliHitTestAlgorithmOrtho02(void)
204 TestApplication application;
205 tet_infoline("Testing Dali::HitTestAlgorithm with offset Ortho camera()");
207 Stage stage = Stage::GetCurrent();
208 RenderTaskList renderTaskList = stage.GetRenderTaskList();
209 RenderTask defaultRenderTask = renderTaskList.GetTask(0u);
210 Dali::CameraActor cameraActor = defaultRenderTask.GetCameraActor();
212 Vector2 stageSize ( stage.GetSize() );
213 cameraActor.SetOrthographicProjection(-stageSize.x * 0.3f, stageSize.x * 0.7f,
214 stageSize.y * 0.3f, -stageSize.y * 0.7f,
216 cameraActor.SetPosition(0.0f, 0.0f, 1600.0f);
218 Vector2 actorSize( stageSize * 0.5f );
219 // Create two actors with half the size of the stage and set them to be partially overlapping
220 Actor blue = Actor::New();
221 blue.SetName( "Blue" );
222 blue.SetAnchorPoint( AnchorPoint::TOP_LEFT );
223 blue.SetParentOrigin( Vector3(0.2f, 0.2f, 0.5f) );
224 blue.SetSize( actorSize );
227 Actor green = Actor::New( );
228 green.SetName( "Green" );
229 green.SetAnchorPoint( AnchorPoint::TOP_LEFT );
230 green.SetParentOrigin( Vector3(0.4f, 0.4f, 0.5f) );
231 green.SetSize( actorSize );
233 // Add the actors to the view
238 application.SendNotification();
239 application.Render(0);
240 application.Render(10);
243 HitTestAlgorithm::Results results;
244 HitTest(stage, Vector2( 240.0f, 400.0f ), results, &DefaultIsActorTouchableFunction);
245 DALI_TEST_CHECK( results.actor == green );
246 DALI_TEST_EQUALS( results.actorCoordinates, actorSize * 0.6f, 0.01f, TEST_LOCATION );
250 HitTestAlgorithm::Results results;
251 HitTest(stage, Vector2( 0.001f, 0.001f ), results, &DefaultIsActorTouchableFunction);
252 DALI_TEST_CHECK( results.actor == blue );
253 DALI_TEST_EQUALS( results.actorCoordinates, Vector2( 0.001f, 0.001f ), 0.001f, TEST_LOCATION );
257 HitTestAlgorithm::Results results;
258 HitTest(stage, stageSize, results, &DefaultIsActorTouchableFunction);
259 DALI_TEST_CHECK( ! results.actor );
260 DALI_TEST_EQUALS( results.actorCoordinates, Vector2::ZERO, TEST_LOCATION );
265 HitTestAlgorithm::Results results;
266 HitTest(stage, stageSize*0.69f, results, &DefaultIsActorTouchableFunction);
267 DALI_TEST_CHECK( results.actor == green );
268 DALI_TEST_EQUALS( results.actorCoordinates, actorSize * 0.98f, 0.01f, TEST_LOCATION );
274 int UtcDaliHitTestAlgorithmClippingActor(void)
276 TestApplication application;
277 tet_infoline("Testing Dali::HitTestAlgorithm with a stencil");
279 Stage stage = Stage::GetCurrent();
280 Actor rootLayer = stage.GetRootLayer();
281 rootLayer.SetName( "RootLayer" );
284 Layer layer = Layer::New();
285 layer.SetAnchorPoint( AnchorPoint::TOP_LEFT );
286 layer.SetParentOrigin( ParentOrigin::TOP_LEFT );
287 layer.SetName( "layer" );
290 // Create a clipping actor and add it to the layer.
291 Actor clippingActor = CreateRenderableActor( Dali::BufferImage::WHITE() );
292 clippingActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
293 clippingActor.SetParentOrigin( ParentOrigin::TOP_LEFT );
294 clippingActor.SetSize( 50.0f, 50.0f );
295 clippingActor.SetProperty( Actor::Property::CLIPPING_MODE, ClippingMode::CLIP_CHILDREN );
296 clippingActor.SetName( "clippingActor" );
297 layer.Add( clippingActor );
299 // Create a renderable actor and add it to the clipping actor.
300 Actor childActor = CreateRenderableActor( Dali::BufferImage::WHITE() );
301 childActor.SetSize( 100.0f, 100.0f );
302 childActor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
303 childActor.SetParentOrigin( ParentOrigin::TOP_LEFT );
304 childActor.SetName( "childActor" );
305 clippingActor.Add( childActor );
308 application.SendNotification();
309 application.Render();
311 // Hit within clippingActor and childActor.
312 HitTestAlgorithm::Results results;
313 HitTest( stage, Vector2( 10.0f, 10.0f ), results, &DefaultIsActorTouchableFunction );
314 DALI_TEST_CHECK( results.actor == childActor );
315 tet_printf( "Hit: %s\n", ( results.actor ? results.actor.GetName().c_str() : "NULL" ) );
317 // Hit within childActor but outside of clippingActor, should hit the root-layer instead.
318 HitTest( stage, Vector2( 60.0f, 60.0f ), results, &DefaultIsActorTouchableFunction);
319 DALI_TEST_CHECK( results.actor == rootLayer );
320 tet_printf( "Hit: %s\n", ( results.actor ? results.actor.GetName().c_str() : "NULL" ) );
325 int UtcDaliHitTestAlgorithmOverlay(void)
327 TestApplication application;
328 tet_infoline("Testing Dali::HitTestAlgorithm with overlay actors");
330 Stage stage = Stage::GetCurrent();
331 RenderTaskList renderTaskList = stage.GetRenderTaskList();
332 RenderTask defaultRenderTask = renderTaskList.GetTask(0u);
333 Dali::CameraActor cameraActor = defaultRenderTask.GetCameraActor();
335 Vector2 stageSize ( stage.GetSize() );
336 cameraActor.SetOrthographicProjection( stageSize );
337 cameraActor.SetPosition(0.0f, 0.0f, 1600.0f);
339 Vector2 actorSize( stageSize * 0.5f );
340 // Create two actors with half the size of the stage and set them to be partially overlapping
341 Actor blue = Actor::New();
342 blue.SetDrawMode( DrawMode::OVERLAY_2D );
343 blue.SetName( "Blue" );
344 blue.SetAnchorPoint( AnchorPoint::CENTER );
345 blue.SetParentOrigin( Vector3(1.0f/3.0f, 1.0f/3.0f, 0.5f) );
346 blue.SetSize( actorSize );
349 Actor green = Actor::New( );
350 green.SetName( "Green" );
351 green.SetAnchorPoint( AnchorPoint::CENTER );
352 green.SetParentOrigin( Vector3(2.0f/3.0f, 2.0f/3.0f, 0.5f) );
353 green.SetSize( actorSize );
355 // Add the actors to the view
360 application.SendNotification();
361 application.Render(0);
362 application.Render(10);
364 HitTestAlgorithm::Results results;
366 //Hit in the intersection. Should pick the blue actor since it is an overlay.
367 HitTest(stage, stageSize / 2.0f, results, &DefaultIsActorTouchableFunction);
368 DALI_TEST_CHECK( results.actor == blue );
369 DALI_TEST_EQUALS( results.actorCoordinates, actorSize * 5.0f/6.0f, TEST_LOCATION );
371 //Hit in the blue actor
372 HitTest(stage, stageSize / 3.0f, results, &DefaultIsActorTouchableFunction);
373 DALI_TEST_CHECK( results.actor == blue );
374 DALI_TEST_EQUALS( results.actorCoordinates, actorSize * 0.5f, TEST_LOCATION );
376 //Hit in the green actor
377 HitTest(stage, stageSize * 2.0f / 3.0f, results, &DefaultIsActorTouchableFunction);
378 DALI_TEST_CHECK( results.actor == green );
379 DALI_TEST_EQUALS( results.actorCoordinates, actorSize * 0.5f, TEST_LOCATION );