[dali_1.2.40] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / events / hover-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/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   TouchPoint::State state = static_cast< TouchPoint::State >( event.points[0].GetState() );
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 ( state == TouchPoint::Interrupted )
182   {
183     Dali::Actor consumingActor;
184     hoverEvent.points.push_back( event.points[0].GetTouchPoint() );
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 ( Integration::PointContainerConstIterator 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->GetScreenPosition(), hitTestResults, actorHoverableCheck );
239
240     TouchPoint newPoint( iter->GetTouchPoint() );
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->GetState()], iter->GetScreenPosition().x, iter->GetScreenPosition().y,
248                    ( hitTestResults.actor ? reinterpret_cast< 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 ? reinterpret_cast< void* >( &primaryPoint.hitActor.GetBaseObject() ) : NULL, primaryPoint.hitActor ? primaryPoint.hitActor.GetName().c_str() : "" );
273   DALI_LOG_INFO( gLogFilter, Debug::Concise, "ConsumedActor:       (%p) %s\n", consumedActor ? reinterpret_cast< 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", reinterpret_cast< 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", reinterpret_cast< 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", reinterpret_cast< 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", reinterpret_cast< 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