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