GCC 9.2 Support
[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 } // unnamed namespace
151
152 TouchEventProcessor::TouchEventProcessor( Scene& scene )
153 : mScene( scene ),
154   mLastPrimaryHitActor( MakeCallback( this, &TouchEventProcessor::OnObservedActorDisconnected ) ),
155   mLastConsumedActor(),
156   mTouchDownConsumedActor(),
157   mLastRenderTask()
158 {
159   DALI_LOG_TRACE_METHOD( gLogFilter );
160 }
161
162 TouchEventProcessor::~TouchEventProcessor()
163 {
164   DALI_LOG_TRACE_METHOD( gLogFilter );
165 }
166
167 void TouchEventProcessor::ProcessTouchEvent( const Integration::TouchEvent& event )
168 {
169   DALI_LOG_TRACE_METHOD( gLogFilter );
170   DALI_ASSERT_ALWAYS( !event.points.empty() && "Empty TouchEvent sent from Integration\n" );
171
172   PRINT_HIERARCHY(gLogFilter);
173
174   // 1) Check if it is an interrupted event - we should inform our last primary hit actor about this
175   //    and emit the stage signal as well.
176
177   if ( event.points[0].GetState() == PointState::INTERRUPTED )
178   {
179     Dali::Actor consumingActor;
180     Integration::Point currentPoint( event.points[0] );
181
182     Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
183     if ( lastPrimaryHitActor )
184     {
185       Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
186       currentPoint.SetHitActor( lastPrimaryHitActorHandle );
187
188       consumingActor = AllocAndEmitTouchSignals( event.time, lastPrimaryHitActorHandle, currentPoint );
189     }
190
191     // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
192     Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
193     if ( lastConsumedActor &&
194          lastConsumedActor != lastPrimaryHitActor &&
195          lastConsumedActor != consumingActor )
196     {
197       Dali::Actor lastConsumedActorHandle( lastConsumedActor );
198       currentPoint.SetHitActor( lastConsumedActorHandle );
199       AllocAndEmitTouchSignals( event.time, lastConsumedActorHandle, currentPoint );
200     }
201
202     // Tell the touch-down consuming actor as well, if required
203     Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
204     if ( touchDownConsumedActor &&
205          touchDownConsumedActor != lastPrimaryHitActor &&
206          touchDownConsumedActor != lastConsumedActor &&
207          touchDownConsumedActor != consumingActor )
208     {
209       Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
210
211       currentPoint.SetHitActor( touchDownConsumedActorHandle );
212       AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
213     }
214
215     mLastPrimaryHitActor.SetActor( NULL );
216     mLastConsumedActor.SetActor( NULL );
217     mTouchDownConsumedActor.SetActor( NULL );
218     mLastRenderTask.Reset();
219
220     currentPoint.SetHitActor( Dali::Actor() );
221
222     TouchEvent touchEvent( event.time );
223     TouchDataPtr touchData( new TouchData( event.time ) );
224     Dali::TouchData touchDataHandle( touchData.Get() );
225
226     touchEvent.points.push_back( currentPoint.GetTouchPoint() );
227     touchData->AddPoint( currentPoint );
228
229     mScene.EmitTouchedSignal( touchEvent, touchDataHandle );
230     return; // No need for hit testing
231   }
232
233   // 2) Hit Testing.
234   TouchEvent touchEvent( event.time );
235   TouchDataPtr touchData( new TouchData( event.time ) );
236   Dali::TouchData touchDataHandle( touchData.Get() );
237
238   DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
239   DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
240
241   RenderTaskPtr currentRenderTask;
242
243   for ( Integration::PointContainerConstIterator iter = event.points.begin(), beginIter = event.points.begin(), endIter = event.points.end(); iter != endIter; ++iter )
244   {
245     HitTestAlgorithm::Results hitTestResults;
246     HitTestAlgorithm::HitTest( mScene.GetSize(), mScene.GetRenderTaskList(), mScene.GetLayerList(), iter->GetScreenPosition(), hitTestResults );
247
248     Integration::Point newPoint( *iter );
249     newPoint.SetHitActor( hitTestResults.actor );
250     newPoint.SetLocalPosition( hitTestResults.actorCoordinates );
251
252     touchEvent.points.push_back( newPoint.GetTouchPoint() );
253     touchData->AddPoint( newPoint );
254
255     DALI_LOG_INFO( gLogFilter, Debug::General, "  State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
256                    TOUCH_POINT_STATE[iter->GetState()], iter->GetScreenPosition().x, iter->GetScreenPosition().y,
257                    ( hitTestResults.actor ? reinterpret_cast< void* >( &hitTestResults.actor.GetBaseObject() ) : NULL ),
258                    ( hitTestResults.actor ? hitTestResults.actor.GetName().c_str() : "" ),
259                    hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
260
261     // Only set the currentRenderTask for the primary hit actor.
262     if ( iter == beginIter && hitTestResults.renderTask )
263     {
264       currentRenderTask = hitTestResults.renderTask;
265     }
266   }
267
268   // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
269
270   // Emit the touch signal
271   Dali::Actor consumedActor;
272   if ( currentRenderTask )
273   {
274     consumedActor = EmitTouchSignals( touchData->GetPoint( 0 ).GetHitActor(), touchEvent, touchDataHandle );
275   }
276
277   Integration::Point& primaryPoint = touchData->GetPoint( 0 );
278   Dali::Actor primaryHitActor = primaryPoint.GetHitActor();
279   PointState::Type primaryPointState = primaryPoint.GetState();
280
281   DALI_LOG_INFO( gLogFilter, Debug::Concise, "PrimaryHitActor:     (%p) %s\n", primaryHitActor ? reinterpret_cast< void* >( &primaryHitActor.GetBaseObject() ) : NULL, primaryHitActor ? primaryHitActor.GetName().c_str() : "" );
282   DALI_LOG_INFO( gLogFilter, Debug::Concise, "ConsumedActor:       (%p) %s\n", consumedActor ? reinterpret_cast< void* >( &consumedActor.GetBaseObject() ) : NULL, consumedActor ? consumedActor.GetName().c_str() : "" );
283
284   if ( ( primaryPointState == PointState::DOWN ) &&
285        ( touchEvent.GetPointCount() == 1 ) &&
286        ( consumedActor && consumedActor.OnStage() ) )
287   {
288     mTouchDownConsumedActor.SetActor( &GetImplementation( consumedActor ) );
289   }
290
291   // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
292   //    hit actor.  Also process the last consumed actor in the same manner.
293
294   Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
295   Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
296   if( ( primaryPointState == PointState::MOTION ) || ( primaryPointState == PointState::UP ) || ( primaryPointState == PointState::STATIONARY ) )
297   {
298     if( mLastRenderTask )
299     {
300       Dali::Actor leaveEventConsumer;
301       RenderTask& lastRenderTaskImpl = *mLastRenderTask.Get();
302
303       if( lastPrimaryHitActor &&
304           lastPrimaryHitActor != primaryHitActor &&
305           lastPrimaryHitActor != consumedActor )
306       {
307         if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
308         {
309           if ( lastPrimaryHitActor->GetLeaveRequired() )
310           {
311             DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit):     (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
312             leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::LEAVE );
313           }
314         }
315         else
316         {
317           // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
318           // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
319           DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit):     (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
320           leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::INTERRUPTED );
321         }
322       }
323
324       // Check if the motion event has been consumed by another actor's listener.  In this case, the previously
325       // consumed actor's listeners may need to be informed (through a leave event).
326       // Further checks here to ensure we do not signal the same actor twice for the same event.
327       if ( lastConsumedActor &&
328            lastConsumedActor != consumedActor &&
329            lastConsumedActor != lastPrimaryHitActor &&
330            lastConsumedActor != primaryHitActor &&
331            lastConsumedActor != leaveEventConsumer )
332       {
333         if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
334         {
335           if( lastConsumedActor->GetLeaveRequired() )
336           {
337             DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
338             EmitTouchSignals( lastConsumedActor, lastRenderTaskImpl, touchEvent, touchData, PointState::LEAVE );
339           }
340         }
341         else
342         {
343           // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
344           // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
345           DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume):     (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
346           EmitTouchSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, touchEvent, touchData, PointState::INTERRUPTED );
347         }
348       }
349     }
350   }
351
352   // 5) If our primary point is an Up event, then the primary point (in multi-touch) will change next
353   //    time so set our last primary actor to NULL.  Do the same to the last consumed actor as well.
354
355   if ( primaryPointState == PointState::UP )
356   {
357     mLastPrimaryHitActor.SetActor( NULL );
358     mLastConsumedActor.SetActor( NULL );
359     mLastRenderTask.Reset();
360   }
361   else
362   {
363     // The primaryHitActor may have been removed from the stage so ensure it is still on the stage before setting members.
364     if ( primaryHitActor && primaryHitActor.OnStage() )
365     {
366       mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
367
368       // Only observe the consumed actor if we have a primaryHitActor (check if it is still on stage).
369       if ( consumedActor && consumedActor.OnStage() )
370       {
371         mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
372       }
373       else
374       {
375         mLastConsumedActor.SetActor( NULL );
376       }
377
378       mLastRenderTask = currentRenderTask;
379     }
380     else
381     {
382       mLastPrimaryHitActor.SetActor( NULL );
383       mLastConsumedActor.SetActor( NULL );
384       mLastRenderTask.Reset();
385     }
386   }
387
388   // 6) Emit an interrupted event to the touch-down actor if it hasn't consumed the up and
389   //    emit the stage touched event if required.
390
391   if ( touchEvent.GetPointCount() == 1 ) // Only want the first touch and the last release
392   {
393     switch ( primaryPointState )
394     {
395       case PointState::UP:
396       {
397         Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
398         if ( touchDownConsumedActor &&
399              touchDownConsumedActor != consumedActor &&
400              touchDownConsumedActor != lastPrimaryHitActor &&
401              touchDownConsumedActor != lastConsumedActor )
402         {
403           Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
404
405           Integration::Point currentPoint = touchData->GetPoint( 0 );
406           currentPoint.SetHitActor( touchDownConsumedActorHandle );
407           currentPoint.SetState( PointState::INTERRUPTED );
408
409           AllocAndEmitTouchSignals( event.time, touchDownConsumedActorHandle, currentPoint );
410         }
411
412         mTouchDownConsumedActor.SetActor( NULL );
413
414         DALI_FALLTHROUGH;
415       }
416
417       case PointState::DOWN:
418       {
419         mScene.EmitTouchedSignal( touchEvent, touchDataHandle );
420         break;
421       }
422
423       case PointState::MOTION:
424       case PointState::LEAVE:
425       case PointState::STATIONARY:
426       case PointState::INTERRUPTED:
427       {
428         // Ignore
429         break;
430       }
431     }
432   }
433 }
434
435 void TouchEventProcessor::OnObservedActorDisconnected( Actor* actor )
436 {
437   if ( actor == mLastPrimaryHitActor.GetActor() )
438   {
439     Dali::Actor handle( actor );
440
441     Integration::Point point;
442     point.SetState( PointState::INTERRUPTED );
443     point.SetHitActor( handle );
444
445     TouchDataPtr touchData( new TouchData );
446     touchData->AddPoint( point );
447     Dali::TouchData touchDataHandle( touchData.Get() );
448
449     TouchEvent touchEvent( 0 );
450     touchEvent.points.push_back( point.GetTouchPoint() );
451
452     Dali::Actor eventConsumer = EmitTouchSignals( handle, touchEvent, touchDataHandle );
453
454     if ( mLastConsumedActor.GetActor() != eventConsumer )
455     {
456       EmitTouchSignals( Dali::Actor( mLastConsumedActor.GetActor() ), touchEvent, touchDataHandle );
457     }
458
459     // Do not set mLastPrimaryHitActor to NULL we may be iterating through its observers
460
461     mLastConsumedActor.SetActor( NULL );
462     mLastRenderTask.Reset();
463   }
464 }
465
466 } // namespace Internal
467
468 } // namespace Dali