[Tizen] Revert "Use touch consumed return to set whether we process a gesture or...
[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 void 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; // No need for hit testing
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   // Emit the touch signal
322   Dali::Actor consumedActor;
323   if ( currentRenderTask )
324   {
325     consumedActor = EmitTouchSignals( touchData->GetPoint( 0 ).GetHitActor(), touchEvent, touchDataHandle );
326   }
327
328   Integration::Point& primaryPoint = touchData->GetPoint( 0 );
329   Dali::Actor primaryHitActor = primaryPoint.GetHitActor();
330   PointState::Type primaryPointState = primaryPoint.GetState();
331
332   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() : "" );
333   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() : "" );
334
335   if ( ( primaryPointState == PointState::DOWN ) &&
336        ( touchEvent.GetPointCount() == 1 ) &&
337        ( consumedActor && consumedActor.GetProperty< bool >( Dali::Actor::Property::CONNECTED_TO_SCENE ) ) )
338   {
339     mTouchDownConsumedActor.SetActor( &GetImplementation( consumedActor ) );
340   }
341
342   // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
343   //    hit actor.  Also process the last consumed actor in the same manner.
344
345   Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
346   Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
347   if( ( primaryPointState == PointState::MOTION ) || ( primaryPointState == PointState::UP ) || ( primaryPointState == PointState::STATIONARY ) )
348   {
349     if( mLastRenderTask )
350     {
351       Dali::Actor leaveEventConsumer;
352       RenderTask& lastRenderTaskImpl = *mLastRenderTask.Get();
353
354       if( lastPrimaryHitActor &&
355           lastPrimaryHitActor != primaryHitActor &&
356           lastPrimaryHitActor != consumedActor )
357       {
358         if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
359         {
360           if ( lastPrimaryHitActor->GetLeaveRequired() )
361           {
362             DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit):     (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
363             leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::LEAVE );
364           }
365         }
366         else
367         {
368           // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
369           // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
370           DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit):     (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
371           leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::INTERRUPTED );
372         }
373       }
374
375       // Check if the motion event has been consumed by another actor's listener.  In this case, the previously
376       // consumed actor's listeners may need to be informed (through a leave event).
377       // Further checks here to ensure we do not signal the same actor twice for the same event.
378       if ( lastConsumedActor &&
379            lastConsumedActor != consumedActor &&
380            lastConsumedActor != lastPrimaryHitActor &&
381            lastConsumedActor != primaryHitActor &&
382            lastConsumedActor != leaveEventConsumer )
383       {
384         if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
385         {
386           if( lastConsumedActor->GetLeaveRequired() )
387           {
388             DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
389             EmitTouchSignals( lastConsumedActor, lastRenderTaskImpl, touchEvent, touchData, PointState::LEAVE );
390           }
391         }
392         else
393         {
394           // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
395           // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
396           DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume):     (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
397           EmitTouchSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::INTERRUPTED );
398         }
399       }
400     }
401   }
402
403   // 5) If our primary point is an Up event, then the primary point (in multi-touch) will change next
404   //    time so set our last primary actor to NULL.  Do the same to the last consumed actor as well.
405
406   if ( primaryPointState == PointState::UP )
407   {
408     mLastPrimaryHitActor.SetActor( nullptr );
409     mLastConsumedActor.SetActor( nullptr );
410     mCapturingTouchActor.SetActor( nullptr );
411     mLastRenderTask.Reset();
412   }
413   else
414   {
415     // The primaryHitActor may have been removed from the scene so ensure it is still on the scene before setting members.
416     if ( primaryHitActor && GetImplementation( primaryHitActor ).OnScene() )
417     {
418       mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
419
420       // Only observe the consumed actor if we have a primaryHitActor (check if it is still on the scene).
421       if ( consumedActor && GetImplementation( consumedActor ).OnScene() )
422       {
423         mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
424       }
425       else
426       {
427         mLastConsumedActor.SetActor( NULL );
428       }
429
430       mLastRenderTask = currentRenderTask;
431     }
432     else
433     {
434       mLastPrimaryHitActor.SetActor( nullptr );
435       mLastConsumedActor.SetActor( nullptr );
436       mCapturingTouchActor.SetActor( nullptr );
437       mLastRenderTask.Reset();
438     }
439   }
440
441   // 6) Emit an interrupted event to the touch-down actor if it hasn't consumed the up and
442   //    emit the stage touched event if required.
443
444   if ( touchEvent.GetPointCount() == 1 ) // Only want the first touch and the last release
445   {
446     switch ( primaryPointState )
447     {
448       case PointState::UP:
449       {
450         Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
451         if ( touchDownConsumedActor &&
452              touchDownConsumedActor != consumedActor &&
453              touchDownConsumedActor != lastPrimaryHitActor &&
454              touchDownConsumedActor != lastConsumedActor )
455         {
456           Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
457
458           Integration::Point currentPoint = touchData->GetPoint( 0 );
459           currentPoint.SetHitActor( touchDownConsumedActorHandle );
460           currentPoint.SetState( PointState::INTERRUPTED );
461
462           AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
463         }
464
465         mTouchDownConsumedActor.SetActor( NULL );
466
467         DALI_FALLTHROUGH;
468       }
469
470       case PointState::DOWN:
471       {
472         mScene.EmitTouchedSignal( touchEvent, touchDataHandle );
473         break;
474       }
475
476       case PointState::MOTION:
477       case PointState::LEAVE:
478       case PointState::STATIONARY:
479       case PointState::INTERRUPTED:
480       {
481         // Ignore
482         break;
483       }
484     }
485   }
486 }
487
488 void TouchEventProcessor::OnObservedActorDisconnected( Actor* actor )
489 {
490   if ( actor == mLastPrimaryHitActor.GetActor() )
491   {
492     Dali::Actor handle( actor );
493
494     Integration::Point point;
495     point.SetState( PointState::INTERRUPTED );
496     point.SetHitActor( handle );
497
498     TouchDataPtr touchData( new TouchData );
499     touchData->AddPoint( point );
500     Dali::TouchData touchDataHandle( touchData.Get() );
501
502     TouchEvent touchEvent( 0 );
503     touchEvent.points.push_back( point.GetTouchPoint() );
504
505     Dali::Actor eventConsumer = EmitTouchSignals( handle, touchEvent, touchDataHandle );
506
507     if ( mLastConsumedActor.GetActor() != eventConsumer )
508     {
509       EmitTouchSignals( Dali::Actor( mLastConsumedActor.GetActor() ), touchEvent, touchDataHandle );
510     }
511
512     // Do not set mLastPrimaryHitActor to NULL we may be iterating through its observers
513
514     mLastConsumedActor.SetActor( NULL );
515     mLastRenderTask.Reset();
516   }
517 }
518
519 } // namespace Internal
520
521 } // namespace Dali