[dali_1.9.23] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / events / touch-event-processor.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/events/touch-event-processor.h>
20
21 #if defined(DEBUG_ENABLED)
22 #include <sstream>
23 #endif
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/events/touch-data.h>
27 #include <dali/public-api/math/vector2.h>
28 #include <dali/public-api/signals/callback.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/integration-api/events/touch-event-integ.h>
31 #include <dali/internal/event/actors/actor-impl.h>
32 #include <dali/internal/event/actors/layer-impl.h>
33 #include <dali/internal/event/common/scene-impl.h>
34 #include <dali/internal/event/events/hit-test-algorithm-impl.h>
35 #include <dali/internal/event/events/multi-point-event-util.h>
36 #include <dali/internal/event/events/touch-data-impl.h>
37 #include <dali/internal/event/render-tasks/render-task-impl.h>
38
39 namespace Dali
40 {
41
42 namespace Internal
43 {
44
45 namespace
46 {
47
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TOUCH_PROCESSOR" );
50
51 const char * TOUCH_POINT_STATE[ 6 ] =
52 {
53   "DOWN",
54   "UP",
55   "MOTION",
56   "LEAVE",
57   "STATIONARY",
58   "INTERRUPTED",
59 };
60
61 #endif // defined(DEBUG_ENABLED)
62
63
64 /**
65  *  Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
66  */
67 Dali::Actor EmitTouchSignals( Dali::Actor actor, const TouchEvent& event, const Dali::TouchData& touchData )
68 {
69   Dali::Actor consumedActor;
70
71   if ( actor )
72   {
73     Dali::Actor oldParent( actor.GetParent() );
74
75     Actor& actorImpl( GetImplementation(actor) );
76
77     bool consumed( false );
78
79     // Only emit the signal if the actor's touch signal has connections (or derived actor implementation requires touch).
80     if ( actorImpl.GetTouchRequired() )
81     {
82       consumed = actorImpl.EmitTouchEventSignal( event, touchData );
83     }
84
85     if ( consumed )
86     {
87       // One of this actor's listeners has consumed the event so set this actor as the consumed actor.
88       consumedActor = Dali::Actor( &actorImpl );
89     }
90     else
91     {
92       // The actor may have been removed/reparented during the signal callbacks.
93       Dali::Actor parent = actor.GetParent();
94
95       if ( parent &&
96            (parent == oldParent) )
97       {
98         // One of the actor's parents may consumed the event and they should be set as the consumed actor.
99         consumedActor = EmitTouchSignals( parent, event, touchData );
100       }
101     }
102   }
103
104   return consumedActor;
105 }
106
107 Dali::Actor AllocAndEmitTouchSignals( unsigned long time,  Dali::Actor actor, const Integration::Point& point )
108 {
109   TouchEvent touchEvent( time );
110   TouchDataPtr touchData( new TouchData( time ) );
111   Dali::TouchData touchDataHandle( touchData.Get() );
112
113   touchEvent.points.push_back( point.GetTouchPoint() );
114   touchData->AddPoint( point );
115
116   return EmitTouchSignals( actor, touchEvent, touchDataHandle );
117 }
118
119
120 /**
121  * Changes the state of the primary point to leave and emits the touch signals
122  */
123 Dali::Actor EmitTouchSignals( Actor* actor, RenderTask& renderTask, const TouchEvent& originalEvent, const TouchDataPtr& originalTouchData, PointState::Type state )
124 {
125   Dali::Actor consumingActor;
126
127   if( actor )
128   {
129     TouchDataPtr touchData = TouchData::Clone( *originalTouchData.Get() );
130
131     Integration::Point& primaryPoint = touchData->GetPoint( 0 );
132
133     const Vector2& screenPosition = primaryPoint.GetScreenPosition();
134     Vector2 localPosition;
135     actor->ScreenToLocal( renderTask, localPosition.x, localPosition.y, screenPosition.x, screenPosition.y );
136
137     primaryPoint.SetLocalPosition( localPosition );
138     primaryPoint.SetHitActor( Dali::Actor( actor ) );
139     primaryPoint.SetState( state );
140
141     TouchEvent touchEvent( originalEvent );
142     touchEvent.points[0] = primaryPoint.GetTouchPoint();
143
144     consumingActor = EmitTouchSignals( Dali::Actor(actor), touchEvent, Dali::TouchData( touchData.Get() ) );
145   }
146
147   return consumingActor;
148 }
149
150 /**
151  * @brief Parses the primary touch point by performing a hit-test if necessary
152  *
153  * @param[out] hitTestResults The hit test results are put into this variable
154  * @param[in/out] capturingTouchActorObserver The observer for the capturing touch actor member
155  * @param[in] lastRenderTask The last render task member
156  * @param[in] currentPoint The current point information
157  * @param[in] scene The scene that this touch is related to
158  */
159 void ParsePrimaryTouchPoint(
160     HitTestAlgorithm::Results& hitTestResults,
161     ActorObserver& capturingTouchActorObserver,
162     const RenderTaskPtr& lastRenderTask,
163     const Integration::Point& currentPoint,
164     const Internal::Scene& scene )
165 {
166   Actor* capturingTouchActor = capturingTouchActorObserver.GetActor();
167
168   // We only set the capturing touch actor when the first touch-started actor captures all touch so if it's set, just use it
169   if( capturingTouchActor && lastRenderTask )
170   {
171     hitTestResults.actor = Dali::Actor( capturingTouchActor );
172     hitTestResults.renderTask = lastRenderTask;
173     const Vector2& screenPosition = currentPoint.GetScreenPosition();
174     capturingTouchActor->ScreenToLocal( *lastRenderTask, hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y, screenPosition.x, screenPosition.y );
175   }
176   else
177   {
178     HitTestAlgorithm::HitTest( scene.GetSize(), scene.GetRenderTaskList(), scene.GetLayerList(), currentPoint.GetScreenPosition(), hitTestResults );
179
180     if( currentPoint.GetState() == PointState::STARTED && hitTestResults.actor )
181     {
182       // If we've just started touch, then check whether the actor has requested to capture all touch events
183       Actor* hitActor = &GetImplementation( hitTestResults.actor );
184       if( hitActor->CapturesAllTouchAfterStart() )
185       {
186         capturingTouchActorObserver.SetActor( hitActor );
187       }
188     }
189   }
190 }
191
192 } // unnamed namespace
193
194 TouchEventProcessor::TouchEventProcessor( Scene& scene )
195 : mScene( scene ),
196   mLastPrimaryHitActor( MakeCallback( this, &TouchEventProcessor::OnObservedActorDisconnected ) ),
197   mLastConsumedActor(),
198   mCapturingTouchActor(),
199   mTouchDownConsumedActor(),
200   mLastRenderTask()
201 {
202   DALI_LOG_TRACE_METHOD( gLogFilter );
203 }
204
205 TouchEventProcessor::~TouchEventProcessor()
206 {
207   DALI_LOG_TRACE_METHOD( gLogFilter );
208 }
209
210 bool TouchEventProcessor::ProcessTouchEvent( const Integration::TouchEvent& event )
211 {
212   DALI_LOG_TRACE_METHOD( gLogFilter );
213   DALI_ASSERT_ALWAYS( !event.points.empty() && "Empty TouchEvent sent from Integration\n" );
214
215   PRINT_HIERARCHY(gLogFilter);
216
217   // 1) Check if it is an interrupted event - we should inform our last primary hit actor about this
218   //    and emit the stage signal as well.
219
220   if ( event.points[0].GetState() == PointState::INTERRUPTED )
221   {
222     Dali::Actor consumingActor;
223     Integration::Point currentPoint( event.points[0] );
224
225     Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
226     if ( lastPrimaryHitActor )
227     {
228       Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
229       currentPoint.SetHitActor( lastPrimaryHitActorHandle );
230
231       consumingActor = AllocAndEmitTouchSignals( event.time, lastPrimaryHitActorHandle, currentPoint );
232     }
233
234     // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
235     Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
236     if ( lastConsumedActor &&
237          lastConsumedActor != lastPrimaryHitActor &&
238          lastConsumedActor != consumingActor )
239     {
240       Dali::Actor lastConsumedActorHandle( lastConsumedActor );
241       currentPoint.SetHitActor( lastConsumedActorHandle );
242       AllocAndEmitTouchSignals( event.time, lastConsumedActorHandle, currentPoint );
243     }
244
245     // Tell the touch-down consuming actor as well, if required
246     Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
247     if ( touchDownConsumedActor &&
248          touchDownConsumedActor != lastPrimaryHitActor &&
249          touchDownConsumedActor != lastConsumedActor &&
250          touchDownConsumedActor != consumingActor )
251     {
252       Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
253
254       currentPoint.SetHitActor( touchDownConsumedActorHandle );
255       AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
256     }
257
258     mLastPrimaryHitActor.SetActor( nullptr );
259     mLastConsumedActor.SetActor( nullptr );
260     mCapturingTouchActor.SetActor( nullptr );
261     mTouchDownConsumedActor.SetActor( nullptr );
262     mLastRenderTask.Reset();
263
264     currentPoint.SetHitActor( Dali::Actor() );
265
266     TouchEvent touchEvent( event.time );
267     TouchDataPtr touchData( new TouchData( event.time ) );
268     Dali::TouchData touchDataHandle( touchData.Get() );
269
270     touchEvent.points.push_back( currentPoint.GetTouchPoint() );
271     touchData->AddPoint( currentPoint );
272
273     mScene.EmitTouchedSignal( touchEvent, touchDataHandle );
274     return false; // No need for hit testing & already an interrupted event so just return false
275   }
276
277   // 2) Hit Testing.
278   TouchEvent touchEvent( event.time );
279   TouchDataPtr touchData( new TouchData( event.time ) );
280   Dali::TouchData touchDataHandle( touchData.Get() );
281
282   DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
283   DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
284
285   RenderTaskPtr currentRenderTask;
286   bool firstPointParsed = false;
287
288   for ( auto&& currentPoint : event.points )
289   {
290     HitTestAlgorithm::Results hitTestResults;
291     if( !firstPointParsed )
292     {
293       firstPointParsed = true;
294       ParsePrimaryTouchPoint( hitTestResults, mCapturingTouchActor, mLastRenderTask, currentPoint, mScene );
295
296       // Only set the currentRenderTask for the primary hit actor.
297       currentRenderTask = hitTestResults.renderTask;
298     }
299     else
300     {
301       HitTestAlgorithm::HitTest( mScene.GetSize(), mScene.GetRenderTaskList(), mScene.GetLayerList(), currentPoint.GetScreenPosition(), hitTestResults );
302     }
303
304     Integration::Point newPoint( currentPoint );
305     newPoint.SetHitActor( hitTestResults.actor );
306     newPoint.SetLocalPosition( hitTestResults.actorCoordinates );
307
308     touchEvent.points.push_back( newPoint.GetTouchPoint() );
309     touchData->AddPoint( newPoint );
310
311     DALI_LOG_INFO( gLogFilter, Debug::General, "  State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
312                    TOUCH_POINT_STATE[currentPoint.GetState()], currentPoint.GetScreenPosition().x, currentPoint.GetScreenPosition().y,
313                    ( hitTestResults.actor ? reinterpret_cast< void* >( &hitTestResults.actor.GetBaseObject() ) : NULL ),
314                    ( hitTestResults.actor ? hitTestResults.actor.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() : "" ),
315                    hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
316
317   }
318
319   // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
320
321   bool consumed = false;
322
323   // Emit the touch signal
324   Dali::Actor consumedActor;
325   if ( currentRenderTask )
326   {
327     consumedActor = EmitTouchSignals( touchData->GetPoint( 0 ).GetHitActor(), touchEvent, touchDataHandle );
328     consumed = consumedActor ? true : false;
329   }
330
331   Integration::Point& primaryPoint = touchData->GetPoint( 0 );
332   Dali::Actor primaryHitActor = primaryPoint.GetHitActor();
333   PointState::Type primaryPointState = primaryPoint.GetState();
334
335   DALI_LOG_INFO( gLogFilter, Debug::Concise, "PrimaryHitActor:     (%p) %s\n", primaryHitActor ? reinterpret_cast< void* >( &primaryHitActor.GetBaseObject() ) : NULL, primaryHitActor ? primaryHitActor.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() : "" );
336   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() : "" );
337
338   if ( ( primaryPointState == PointState::DOWN ) &&
339        ( touchEvent.GetPointCount() == 1 ) &&
340        ( consumedActor && consumedActor.GetProperty< bool >( Dali::Actor::Property::CONNECTED_TO_SCENE ) ) )
341   {
342     mTouchDownConsumedActor.SetActor( &GetImplementation( consumedActor ) );
343   }
344
345   // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
346   //    hit actor.  Also process the last consumed actor in the same manner.
347
348   Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
349   Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
350   if( ( primaryPointState == PointState::MOTION ) || ( primaryPointState == PointState::UP ) || ( primaryPointState == PointState::STATIONARY ) )
351   {
352     if( mLastRenderTask )
353     {
354       Dali::Actor leaveEventConsumer;
355       RenderTask& lastRenderTaskImpl = *mLastRenderTask.Get();
356
357       if( lastPrimaryHitActor &&
358           lastPrimaryHitActor != primaryHitActor &&
359           lastPrimaryHitActor != consumedActor )
360       {
361         if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
362         {
363           if ( lastPrimaryHitActor->GetLeaveRequired() )
364           {
365             DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit):     (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
366             leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::LEAVE );
367           }
368         }
369         else
370         {
371           // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
372           // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
373           DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit):     (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
374           leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::INTERRUPTED );
375         }
376       }
377
378       consumed |= leaveEventConsumer ? true : false;
379
380       // Check if the motion event has been consumed by another actor's listener.  In this case, the previously
381       // consumed actor's listeners may need to be informed (through a leave event).
382       // Further checks here to ensure we do not signal the same actor twice for the same event.
383       if ( lastConsumedActor &&
384            lastConsumedActor != consumedActor &&
385            lastConsumedActor != lastPrimaryHitActor &&
386            lastConsumedActor != primaryHitActor &&
387            lastConsumedActor != leaveEventConsumer )
388       {
389         if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
390         {
391           if( lastConsumedActor->GetLeaveRequired() )
392           {
393             DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
394             EmitTouchSignals( lastConsumedActor, lastRenderTaskImpl, touchEvent, touchData, PointState::LEAVE );
395           }
396         }
397         else
398         {
399           // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
400           // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
401           DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume):     (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
402           EmitTouchSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::INTERRUPTED );
403         }
404       }
405     }
406   }
407
408   // 5) If our primary point is an Up event, then the primary point (in multi-touch) will change next
409   //    time so set our last primary actor to NULL.  Do the same to the last consumed actor as well.
410
411   if ( primaryPointState == PointState::UP )
412   {
413     mLastPrimaryHitActor.SetActor( nullptr );
414     mLastConsumedActor.SetActor( nullptr );
415     mCapturingTouchActor.SetActor( nullptr );
416     mLastRenderTask.Reset();
417   }
418   else
419   {
420     // The primaryHitActor may have been removed from the stage so ensure it is still on the stage before setting members.
421     if ( primaryHitActor && GetImplementation( primaryHitActor ).OnStage() )
422     {
423       mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
424
425       // Only observe the consumed actor if we have a primaryHitActor (check if it is still on stage).
426       if ( consumedActor && GetImplementation( consumedActor ).OnStage() )
427       {
428         mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
429       }
430       else
431       {
432         mLastConsumedActor.SetActor( NULL );
433       }
434
435       mLastRenderTask = currentRenderTask;
436     }
437     else
438     {
439       mLastPrimaryHitActor.SetActor( nullptr );
440       mLastConsumedActor.SetActor( nullptr );
441       mCapturingTouchActor.SetActor( nullptr );
442       mLastRenderTask.Reset();
443     }
444   }
445
446   // 6) Emit an interrupted event to the touch-down actor if it hasn't consumed the up and
447   //    emit the stage touched event if required.
448
449   if ( touchEvent.GetPointCount() == 1 ) // Only want the first touch and the last release
450   {
451     switch ( primaryPointState )
452     {
453       case PointState::UP:
454       {
455         Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
456         if ( touchDownConsumedActor &&
457              touchDownConsumedActor != consumedActor &&
458              touchDownConsumedActor != lastPrimaryHitActor &&
459              touchDownConsumedActor != lastConsumedActor )
460         {
461           Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
462
463           Integration::Point currentPoint = touchData->GetPoint( 0 );
464           currentPoint.SetHitActor( touchDownConsumedActorHandle );
465           currentPoint.SetState( PointState::INTERRUPTED );
466
467           AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
468         }
469
470         mTouchDownConsumedActor.SetActor( NULL );
471
472         DALI_FALLTHROUGH;
473       }
474
475       case PointState::DOWN:
476       {
477         mScene.EmitTouchedSignal( touchEvent, touchDataHandle );
478         break;
479       }
480
481       case PointState::MOTION:
482       case PointState::LEAVE:
483       case PointState::STATIONARY:
484       case PointState::INTERRUPTED:
485       {
486         // Ignore
487         break;
488       }
489     }
490   }
491
492   return consumed;
493 }
494
495 void TouchEventProcessor::OnObservedActorDisconnected( Actor* actor )
496 {
497   if ( actor == mLastPrimaryHitActor.GetActor() )
498   {
499     Dali::Actor handle( actor );
500
501     Integration::Point point;
502     point.SetState( PointState::INTERRUPTED );
503     point.SetHitActor( handle );
504
505     TouchDataPtr touchData( new TouchData );
506     touchData->AddPoint( point );
507     Dali::TouchData touchDataHandle( touchData.Get() );
508
509     TouchEvent touchEvent( 0 );
510     touchEvent.points.push_back( point.GetTouchPoint() );
511
512     Dali::Actor eventConsumer = EmitTouchSignals( handle, touchEvent, touchDataHandle );
513
514     if ( mLastConsumedActor.GetActor() != eventConsumer )
515     {
516       EmitTouchSignals( Dali::Actor( mLastConsumedActor.GetActor() ), touchEvent, touchDataHandle );
517     }
518
519     // Do not set mLastPrimaryHitActor to NULL we may be iterating through its observers
520
521     mLastConsumedActor.SetActor( NULL );
522     mLastRenderTask.Reset();
523   }
524 }
525
526 } // namespace Internal
527
528 } // namespace Dali