Merge "Remove RenderableActor" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / events / touch-event-processor.cpp
1 /*
2  * Copyright (c) 2014 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/math/vector2.h>
27 #include <dali/public-api/signals/callback.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/integration-api/events/touch-event-integ.h>
30 #include <dali/internal/event/actors/actor-impl.h>
31 #include <dali/internal/event/actors/layer-impl.h>
32 #include <dali/internal/event/common/stage-impl.h>
33 #include <dali/internal/event/events/hit-test-algorithm-impl.h>
34 #include <dali/internal/event/events/multi-point-event-util.h>
35 #include <dali/internal/event/render-tasks/render-task-impl.h>
36
37 namespace Dali
38 {
39
40 namespace Internal
41 {
42
43 namespace
44 {
45
46 #if defined(DEBUG_ENABLED)
47 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TOUCH_PROCESSOR" );
48
49 const char * TOUCH_POINT_STATE[TouchPoint::Last] =
50 {
51   "Down",
52   "Up",
53   "Motion",
54   "Leave",
55   "Stationary",
56   "Interrupted",
57 };
58
59 #endif // defined(DEBUG_ENABLED)
60
61 /**
62  *  Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
63  */
64 Dali::Actor EmitTouchSignals( Dali::Actor actor, const TouchEvent& event )
65 {
66   Dali::Actor consumedActor;
67
68   if ( actor )
69   {
70     Dali::Actor oldParent( actor.GetParent() );
71
72     Actor& actorImpl( GetImplementation(actor) );
73
74     bool consumed( false );
75
76     // Only emit the signal if the actor's touch signal has connections (or derived actor implementation requires touch).
77     if ( actorImpl.GetTouchRequired() )
78     {
79       consumed = actorImpl.EmitTouchEventSignal( event );
80     }
81
82     if ( consumed )
83     {
84       // One of this actor's listeners has consumed the event so set this actor as the consumed actor.
85       consumedActor = Dali::Actor( &actorImpl );
86     }
87     else
88     {
89       // The actor may have been removed/reparented during the signal callbacks.
90       Dali::Actor parent = actor.GetParent();
91
92       if ( parent &&
93            (parent == oldParent) )
94       {
95         // One of the actor's parents may consumed the event and they should be set as the consumed actor.
96         consumedActor = EmitTouchSignals( parent, event );
97       }
98     }
99   }
100
101   return consumedActor;
102 }
103
104 /**
105  * Changes the state of the primary point to leave and emits the touch signals
106  */
107 Dali::Actor EmitTouchSignals( Actor* actor, RenderTask& renderTask, const TouchEvent& originalEvent, TouchPoint::State state )
108 {
109   TouchEvent touchEvent( originalEvent );
110
111   DALI_ASSERT_DEBUG( NULL != actor && "NULL actor pointer" );
112   if( actor )
113   {
114     TouchPoint& primaryPoint = touchEvent.points[0];
115
116     actor->ScreenToLocal( renderTask, primaryPoint.local.x, primaryPoint.local.y, primaryPoint.screen.x, primaryPoint.screen.y );
117
118     primaryPoint.hitActor = Dali::Actor(actor);
119     primaryPoint.state = state;
120   }
121
122   return EmitTouchSignals( Dali::Actor(actor), touchEvent );
123 }
124
125 } // unnamed namespace
126
127 TouchEventProcessor::TouchEventProcessor( Stage& stage )
128 : mStage( stage ),
129   mLastPrimaryHitActor( MakeCallback( this, &TouchEventProcessor::OnObservedActorDisconnected ) ),
130   mLastConsumedActor(),
131   mTouchDownConsumedActor(),
132   mLastRenderTask()
133 {
134   DALI_LOG_TRACE_METHOD( gLogFilter );
135 }
136
137 TouchEventProcessor::~TouchEventProcessor()
138 {
139   DALI_LOG_TRACE_METHOD( gLogFilter );
140 }
141
142 void TouchEventProcessor::ProcessTouchEvent( const Integration::TouchEvent& event )
143 {
144   DALI_LOG_TRACE_METHOD( gLogFilter );
145
146   DALI_ASSERT_ALWAYS( !event.points.empty() && "Empty TouchEvent sent from Integration\n" );
147
148   Stage& stage = mStage;
149
150   PRINT_HIERARCHY(gLogFilter);
151
152   // Copy so we can add the results of a hit-test.
153   TouchEvent touchEvent( event.time );
154
155   // 1) Check if it is an interrupted event - we should inform our last primary hit actor about this
156   //    and emit the stage signal as well.
157
158   if ( event.points[0].state == TouchPoint::Interrupted )
159   {
160     Dali::Actor consumingActor;
161     touchEvent.points.push_back(event.points[0]);
162
163     Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
164     if ( lastPrimaryHitActor )
165     {
166       Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
167       touchEvent.points[0].hitActor = lastPrimaryHitActorHandle;
168       consumingActor = EmitTouchSignals( lastPrimaryHitActorHandle, touchEvent );
169     }
170
171     // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
172     Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
173     if ( lastConsumedActor &&
174          lastConsumedActor != lastPrimaryHitActor &&
175          lastConsumedActor != consumingActor )
176     {
177       Dali::Actor lastConsumedActorHandle( lastConsumedActor );
178       touchEvent.points[0].hitActor = lastConsumedActorHandle;
179       EmitTouchSignals( lastConsumedActorHandle, touchEvent );
180     }
181
182     // Tell the touch-down consuming actor as well, if required
183     Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
184     if ( touchDownConsumedActor &&
185          touchDownConsumedActor != lastPrimaryHitActor &&
186          touchDownConsumedActor != lastConsumedActor &&
187          touchDownConsumedActor != consumingActor )
188     {
189       Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
190       touchEvent.points[0].hitActor = touchDownConsumedActorHandle;
191       EmitTouchSignals( touchDownConsumedActorHandle, touchEvent );
192     }
193
194     mLastPrimaryHitActor.SetActor( NULL );
195     mLastConsumedActor.SetActor( NULL );
196     mTouchDownConsumedActor.SetActor( NULL );
197     mLastRenderTask.Reset();
198
199     touchEvent.points[0].hitActor.Reset();
200     mStage.EmitTouchedSignal( touchEvent );
201
202     return; // No need for hit testing
203   }
204
205   // 2) Hit Testing.
206
207   DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
208   DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
209
210   Dali::RenderTask currentRenderTask;
211
212   for ( TouchPointContainerConstIterator iter = event.points.begin(), beginIter = event.points.begin(), endIter = event.points.end(); iter != endIter; ++iter )
213   {
214     HitTestAlgorithm::Results hitTestResults;
215     HitTestAlgorithm::HitTest( stage, iter->screen, hitTestResults );
216
217     TouchPoint newPoint( iter->deviceId, iter->state, iter->screen.x, iter->screen.y );
218     newPoint.hitActor = hitTestResults.actor;
219     newPoint.local = hitTestResults.actorCoordinates;
220
221     touchEvent.points.push_back( newPoint );
222
223     DALI_LOG_INFO( gLogFilter, Debug::General, "  State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
224                    TOUCH_POINT_STATE[iter->state], iter->screen.x, iter->screen.y,
225                    ( hitTestResults.actor ? (void*)&hitTestResults.actor.GetBaseObject() : NULL ),
226                    ( hitTestResults.actor ? hitTestResults.actor.GetName().c_str() : "" ),
227                    hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
228
229     // Only set the currentRenderTask for the primary hit actor.
230     if ( iter == beginIter && hitTestResults.renderTask )
231     {
232       currentRenderTask = hitTestResults.renderTask;
233     }
234   }
235
236   // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
237
238   // Emit the touch signal
239   Dali::Actor consumedActor;
240   if ( currentRenderTask )
241   {
242     consumedActor = EmitTouchSignals( touchEvent.points[0].hitActor, touchEvent );
243   }
244
245   TouchPoint& primaryPoint = touchEvent.points[0];
246   Dali::Actor primaryHitActor = primaryPoint.hitActor;
247   TouchPoint::State primaryPointState = primaryPoint.state;
248
249   DALI_LOG_INFO( gLogFilter, Debug::Concise, "PrimaryHitActor:     (%p) %s\n", primaryPoint.hitActor ? (void*)&primaryPoint.hitActor.GetBaseObject() : NULL, primaryPoint.hitActor ? primaryPoint.hitActor.GetName().c_str() : "" );
250   DALI_LOG_INFO( gLogFilter, Debug::Concise, "ConsumedActor:       (%p) %s\n", consumedActor ? (void*)&consumedActor.GetBaseObject() : NULL, consumedActor ? consumedActor.GetName().c_str() : "" );
251
252   if ( ( primaryPointState == TouchPoint::Down ) &&
253        ( touchEvent.GetPointCount() == 1 ) &&
254        ( consumedActor && consumedActor.OnStage() ) )
255   {
256     mTouchDownConsumedActor.SetActor( &GetImplementation( consumedActor ) );
257   }
258
259   // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
260   //    hit actor.  Also process the last consumed actor in the same manner.
261
262   Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
263   Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
264   if( (primaryPointState == TouchPoint::Motion) || (primaryPointState == TouchPoint::Up) || (primaryPointState == TouchPoint::Stationary) )
265   {
266     if ( mLastRenderTask )
267     {
268       Dali::Actor leaveEventConsumer;
269       RenderTask& lastRenderTaskImpl( GetImplementation( mLastRenderTask ) );
270
271       if( lastPrimaryHitActor &&
272           lastPrimaryHitActor != primaryHitActor &&
273           lastPrimaryHitActor != consumedActor )
274       {
275         if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
276         {
277           if ( lastPrimaryHitActor->GetLeaveRequired() )
278           {
279             DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit):     (%p) %s\n", (void*)lastPrimaryHitActor, lastPrimaryHitActor->GetName().c_str() );
280             leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, TouchPoint::Leave );
281           }
282         }
283         else
284         {
285           // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
286           // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
287           DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit):     (%p) %s\n", (void*)lastPrimaryHitActor, lastPrimaryHitActor->GetName().c_str() );
288           leaveEventConsumer = EmitTouchSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, touchEvent, TouchPoint::Interrupted );
289         }
290       }
291
292       // Check if the motion event has been consumed by another actor's listener.  In this case, the previously
293       // consumed actor's listeners may need to be informed (through a leave event).
294       // Further checks here to ensure we do not signal the same actor twice for the same event.
295       if ( lastConsumedActor &&
296            lastConsumedActor != consumedActor &&
297            lastConsumedActor != lastPrimaryHitActor &&
298            lastConsumedActor != primaryHitActor &&
299            lastConsumedActor != leaveEventConsumer )
300       {
301         if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
302         {
303           if( lastConsumedActor->GetLeaveRequired() )
304           {
305             DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", (void*)lastConsumedActor, lastConsumedActor->GetName().c_str() );
306             EmitTouchSignals( lastConsumedActor, lastRenderTaskImpl, touchEvent, TouchPoint::Leave );
307           }
308         }
309         else
310         {
311           // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
312           // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
313           DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume):     (%p) %s\n", (void*)lastConsumedActor, lastConsumedActor->GetName().c_str() );
314           EmitTouchSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, touchEvent, TouchPoint::Interrupted );
315         }
316       }
317     }
318   }
319
320   // 5) If our primary point is an Up event, then the primary point (in multi-touch) will change next
321   //    time so set our last primary actor to NULL.  Do the same to the last consumed actor as well.
322
323   if ( primaryPointState == TouchPoint::Up )
324   {
325     mLastPrimaryHitActor.SetActor( NULL );
326     mLastConsumedActor.SetActor( NULL );
327     mLastRenderTask.Reset();
328   }
329   else
330   {
331     // The primaryHitActor may have been removed from the stage so ensure it is still on the stage before setting members.
332     if ( primaryHitActor && primaryHitActor.OnStage() )
333     {
334       mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
335
336       // Only observe the consumed actor if we have a primaryHitActor (check if it is still on stage).
337       if ( consumedActor && consumedActor.OnStage() )
338       {
339         mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
340       }
341       else
342       {
343         mLastConsumedActor.SetActor( NULL );
344       }
345
346       mLastRenderTask = currentRenderTask;
347     }
348     else
349     {
350       mLastPrimaryHitActor.SetActor( NULL );
351       mLastConsumedActor.SetActor( NULL );
352       mLastRenderTask.Reset();
353     }
354   }
355
356   // 6) Emit an interrupted event to the touch-down actor if it hasn't consumed the up and
357   //    emit the stage touched event if required.
358
359   if ( touchEvent.GetPointCount() == 1 ) // Only want the first touch and the last release
360   {
361     switch ( primaryPointState )
362     {
363       case TouchPoint::Up:
364       {
365         Actor* touchDownConsumedActor( mTouchDownConsumedActor.GetActor() );
366         if ( touchDownConsumedActor &&
367              touchDownConsumedActor != consumedActor &&
368              touchDownConsumedActor != lastPrimaryHitActor &&
369              touchDownConsumedActor != lastConsumedActor )
370         {
371           Dali::Actor touchDownConsumedActorHandle( touchDownConsumedActor );
372           touchEvent.points[0].hitActor = touchDownConsumedActorHandle;
373           touchEvent.points[0].state = TouchPoint::Interrupted;
374           EmitTouchSignals( touchDownConsumedActorHandle, touchEvent );
375
376           // Restore touch-event to original state
377           touchEvent.points[0].hitActor = primaryHitActor;
378           touchEvent.points[0].state = primaryPointState;
379         }
380
381         mTouchDownConsumedActor.SetActor( NULL );
382       }
383       // No break, Fallthrough
384
385       case TouchPoint::Down:
386       {
387         mStage.EmitTouchedSignal( touchEvent );
388         break;
389       }
390
391       case TouchPoint::Motion:
392       case TouchPoint::Leave:
393       case TouchPoint::Stationary:
394       case TouchPoint::Interrupted:
395       case TouchPoint::Last:
396       {
397         // Ignore
398         break;
399       }
400     }
401   }
402 }
403
404 void TouchEventProcessor::OnObservedActorDisconnected( Actor* actor )
405 {
406   if ( actor == mLastPrimaryHitActor.GetActor() )
407   {
408     Dali::Actor handle( actor );
409     TouchEvent touchEvent( 0 );
410     touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Interrupted, 0.0f, 0.0f ) );
411     touchEvent.points[0].hitActor = handle;
412
413     Dali::Actor eventConsumer = EmitTouchSignals( handle, touchEvent );
414
415     if ( mLastConsumedActor.GetActor() != eventConsumer )
416     {
417       EmitTouchSignals( Dali::Actor( mLastConsumedActor.GetActor() ), touchEvent );
418     }
419
420     // Do not set mLastPrimaryHitActor to NULL we may be iterating through its observers
421
422     mLastConsumedActor.SetActor( NULL );
423     mLastRenderTask.Reset();
424   }
425 }
426
427 } // namespace Internal
428
429 } // namespace Dali