Revert "[Tizen] Revert "Use touch consumed return to set whether we process a gesture...
[platform/core/uifw/dali-core.git] / dali / internal / event / events / hover-event-processor.cpp
1 /*
2  * Copyright (c) 2020 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/scene-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( Scene& scene )
150 : mScene( scene )
151 {
152   DALI_LOG_TRACE_METHOD( gLogFilter );
153 }
154
155 HoverEventProcessor::~HoverEventProcessor()
156 {
157   DALI_LOG_TRACE_METHOD( gLogFilter );
158 }
159
160 void HoverEventProcessor::ProcessHoverEvent( const Integration::HoverEvent& event )
161 {
162   DALI_LOG_TRACE_METHOD( gLogFilter );
163   DALI_ASSERT_ALWAYS( !event.points.empty() && "Empty HoverEvent sent from Integration\n" );
164
165   TouchPoint::State state = static_cast< TouchPoint::State >( event.points[0].GetState() );
166
167   PRINT_HIERARCHY(gLogFilter);
168
169   // Copy so we can add the results of a hit-test.
170   HoverEvent hoverEvent( event.time );
171
172   // 1) Check if it is an interrupted event - we should inform our last primary hit actor about this
173   //    and emit the stage signal as well.
174
175   if ( state == TouchPoint::Interrupted )
176   {
177     Dali::Actor consumingActor;
178     hoverEvent.points.push_back( event.points[0].GetTouchPoint() );
179
180     Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
181     if ( lastPrimaryHitActor )
182     {
183       Dali::Actor lastPrimaryHitActorHandle( lastPrimaryHitActor );
184       hoverEvent.points[0].hitActor = lastPrimaryHitActorHandle;
185       consumingActor = EmitHoverSignals( lastPrimaryHitActorHandle, hoverEvent );
186     }
187
188     // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
189     Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
190     if ( lastConsumedActor &&
191          lastConsumedActor != lastPrimaryHitActor &&
192          lastConsumedActor != consumingActor )
193     {
194       Dali::Actor lastConsumedActorHandle( lastConsumedActor );
195       hoverEvent.points[0].hitActor = lastConsumedActorHandle;
196       EmitHoverSignals( lastConsumedActorHandle, hoverEvent );
197     }
198
199     // Tell the hover-start consuming actor as well, if required
200     Actor* hoverStartConsumedActor( mHoverStartConsumedActor.GetActor() );
201     if ( hoverStartConsumedActor &&
202          hoverStartConsumedActor != lastPrimaryHitActor &&
203          hoverStartConsumedActor != lastConsumedActor &&
204          hoverStartConsumedActor != consumingActor )
205     {
206       Dali::Actor hoverStartConsumedActorHandle( hoverStartConsumedActor );
207       hoverEvent.points[0].hitActor = hoverStartConsumedActorHandle;
208       EmitHoverSignals( hoverStartConsumedActorHandle, hoverEvent );
209     }
210
211     mLastPrimaryHitActor.SetActor( NULL );
212     mLastConsumedActor.SetActor( NULL );
213     mHoverStartConsumedActor.SetActor( NULL );
214     mLastRenderTask.Reset();
215
216     hoverEvent.points[0].hitActor.Reset();
217
218     return; // No need for hit testing
219   }
220
221   // 2) Hit Testing.
222
223   DALI_LOG_INFO( gLogFilter, Debug::Concise, "\n" );
224   DALI_LOG_INFO( gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount() );
225
226   RenderTaskPtr currentRenderTask;
227
228   for ( Integration::PointContainerConstIterator iter = event.points.begin(), beginIter = event.points.begin(), endIter = event.points.end(); iter != endIter; ++iter )
229   {
230     HitTestAlgorithm::Results hitTestResults;
231     ActorHoverableCheck actorHoverableCheck;
232     HitTestAlgorithm::HitTest( mScene.GetSize(), mScene.GetRenderTaskList(), mScene.GetLayerList(), iter->GetScreenPosition(), hitTestResults, actorHoverableCheck );
233
234     TouchPoint newPoint( iter->GetTouchPoint() );
235     newPoint.hitActor = hitTestResults.actor;
236     newPoint.local = hitTestResults.actorCoordinates;
237
238     hoverEvent.points.push_back( newPoint );
239
240     DALI_LOG_INFO( gLogFilter, Debug::General, "  State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n",
241                    TOUCH_POINT_STATE[iter->GetState()], iter->GetScreenPosition().x, iter->GetScreenPosition().y,
242                    ( hitTestResults.actor ? reinterpret_cast< void* >( &hitTestResults.actor.GetBaseObject() ) : NULL ),
243                    ( hitTestResults.actor ? hitTestResults.actor.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() : "" ),
244                    hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y );
245
246     // Only set the currentRenderTask for the primary hit actor.
247     if ( iter == beginIter && hitTestResults.renderTask )
248     {
249       currentRenderTask = hitTestResults.renderTask;
250     }
251   }
252
253   // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
254
255   // Emit the touch signal
256   Dali::Actor consumedActor;
257   if ( currentRenderTask )
258   {
259     consumedActor = EmitHoverSignals( hoverEvent.points[0].hitActor, hoverEvent );
260   }
261
262   TouchPoint& primaryPoint = hoverEvent.points[0];
263   Dali::Actor primaryHitActor = primaryPoint.hitActor;
264   TouchPoint::State primaryPointState = primaryPoint.state;
265
266   DALI_LOG_INFO( gLogFilter, Debug::Concise, "PrimaryHitActor:     (%p) %s\n", primaryPoint.hitActor ? reinterpret_cast< void* >( &primaryPoint.hitActor.GetBaseObject() ) : NULL, primaryPoint.hitActor ? primaryPoint.hitActor.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() : "" );
267   DALI_LOG_INFO( gLogFilter, Debug::Concise, "ConsumedActor:       (%p) %s\n", consumedActor ? reinterpret_cast< void* >( &consumedActor.GetBaseObject() ) : NULL, consumedActor ? consumedActor.GetProperty< std::string >( Dali::Actor::Property::NAME ).c_str() : "" );
268
269   if ( ( primaryPointState == TouchPoint::Started ) &&
270        ( hoverEvent.GetPointCount() == 1 ) &&
271        ( consumedActor && GetImplementation( consumedActor ).OnScene() ) )
272   {
273     mHoverStartConsumedActor.SetActor( &GetImplementation( consumedActor ) );
274   }
275
276   // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
277   //    hit actor.  Also process the last consumed actor in the same manner.
278
279   Actor* lastPrimaryHitActor( mLastPrimaryHitActor.GetActor() );
280   Actor* lastConsumedActor( mLastConsumedActor.GetActor() );
281   if( (primaryPointState == TouchPoint::Motion) || (primaryPointState == TouchPoint::Finished) || (primaryPointState == TouchPoint::Stationary) )
282   {
283     if ( mLastRenderTask )
284     {
285       Dali::Actor leaveEventConsumer;
286       RenderTask& lastRenderTaskImpl = *mLastRenderTask.Get();
287
288       if( lastPrimaryHitActor &&
289           lastPrimaryHitActor != primaryHitActor &&
290           lastPrimaryHitActor != consumedActor )
291       {
292         if( lastPrimaryHitActor->IsHittable() && IsActuallySensitive( lastPrimaryHitActor ) )
293         {
294           if ( lastPrimaryHitActor->GetLeaveRequired() )
295           {
296             DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Hit):     (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
297             leaveEventConsumer = EmitHoverSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, hoverEvent, TouchPoint::Leave );
298           }
299         }
300         else
301         {
302           // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
303           // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
304           DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Hit):     (%p) %s\n", reinterpret_cast< void* >( lastPrimaryHitActor ), lastPrimaryHitActor->GetName().c_str() );
305           leaveEventConsumer = EmitHoverSignals( mLastPrimaryHitActor.GetActor(), lastRenderTaskImpl, hoverEvent, TouchPoint::Interrupted );
306         }
307       }
308
309       // Check if the motion event has been consumed by another actor's listener.  In this case, the previously
310       // consumed actor's listeners may need to be informed (through a leave event).
311       // Further checks here to ensure we do not signal the same actor twice for the same event.
312       if ( lastConsumedActor &&
313            lastConsumedActor != consumedActor &&
314            lastConsumedActor != lastPrimaryHitActor &&
315            lastConsumedActor != primaryHitActor &&
316            lastConsumedActor != leaveEventConsumer )
317       {
318         if( lastConsumedActor->IsHittable() && IsActuallySensitive( lastConsumedActor ) )
319         {
320           if( lastConsumedActor->GetLeaveRequired() )
321           {
322             DALI_LOG_INFO( gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
323             EmitHoverSignals( lastConsumedActor, lastRenderTaskImpl, hoverEvent, TouchPoint::Leave );
324           }
325         }
326         else
327         {
328           // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
329           // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
330           DALI_LOG_INFO( gLogFilter, Debug::Concise, "InterruptedActor(Consume):     (%p) %s\n", reinterpret_cast< void* >( lastConsumedActor ), lastConsumedActor->GetName().c_str() );
331           EmitHoverSignals( mLastConsumedActor.GetActor(), lastRenderTaskImpl, hoverEvent, TouchPoint::Interrupted );
332         }
333       }
334     }
335   }
336
337   // 5) If our primary point is an Finished event, then the primary point (in multi-touch) will change next
338   //    time so set our last primary actor to NULL.  Do the same to the last consumed actor as well.
339
340   if ( primaryPointState == TouchPoint::Finished )
341   {
342     mLastPrimaryHitActor.SetActor( NULL );
343     mLastConsumedActor.SetActor( NULL );
344     mLastRenderTask.Reset();
345   }
346   else
347   {
348     // The primaryHitActor may have been removed from the scene so ensure it is still on the scene before setting members.
349     if ( primaryHitActor && GetImplementation( primaryHitActor ).OnScene() )
350     {
351       mLastPrimaryHitActor.SetActor( &GetImplementation( primaryHitActor ) );
352
353       // Only observe the consumed actor if we have a primaryHitActor (check if it is still on the scene).
354       if ( consumedActor && GetImplementation( consumedActor ).OnScene() )
355       {
356         mLastConsumedActor.SetActor( &GetImplementation( consumedActor ) );
357       }
358       else
359       {
360         mLastConsumedActor.SetActor( NULL );
361       }
362
363       mLastRenderTask = currentRenderTask;
364     }
365     else
366     {
367       mLastPrimaryHitActor.SetActor( NULL );
368       mLastConsumedActor.SetActor( NULL );
369       mLastRenderTask.Reset();
370     }
371   }
372
373   // 6) Emit an interrupted event to the hover-started actor if it hasn't consumed the Finished.
374
375   if ( hoverEvent.GetPointCount() == 1 ) // Only want the first hover started
376   {
377     switch ( primaryPointState )
378     {
379       case TouchPoint::Finished:
380       {
381         Actor* hoverStartConsumedActor( mHoverStartConsumedActor.GetActor() );
382         if ( hoverStartConsumedActor &&
383              hoverStartConsumedActor != consumedActor &&
384              hoverStartConsumedActor != lastPrimaryHitActor &&
385              hoverStartConsumedActor != lastConsumedActor )
386         {
387           Dali::Actor hoverStartConsumedActorHandle( hoverStartConsumedActor );
388           hoverEvent.points[0].hitActor = hoverStartConsumedActorHandle;
389           hoverEvent.points[0].state = TouchPoint::Interrupted;
390           EmitHoverSignals( hoverStartConsumedActorHandle, hoverEvent );
391
392           // Restore hover-event to original state
393           hoverEvent.points[0].hitActor = primaryHitActor;
394           hoverEvent.points[0].state = primaryPointState;
395         }
396
397         mHoverStartConsumedActor.SetActor( NULL );
398       }
399       // No break, Fallthrough
400
401       case TouchPoint::Started:
402       case TouchPoint::Motion:
403       case TouchPoint::Leave:
404       case TouchPoint::Stationary:
405       case TouchPoint::Interrupted:
406       case TouchPoint::Last:
407       {
408         // Ignore
409         break;
410       }
411     }
412   }
413 }
414
415 } // namespace Internal
416
417 } // namespace Dali