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