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