2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/events/touch-event-processor.h>
21 #if defined(DEBUG_ENABLED)
26 #include <dali/integration-api/debug.h>
27 #include <dali/integration-api/events/touch-event-integ.h>
28 #include <dali/integration-api/trace.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/events/touch-event-impl.h>
35 #include <dali/internal/event/render-tasks/render-task-impl.h>
36 #include <dali/public-api/events/touch-event.h>
37 #include <dali/public-api/math/vector2.h>
38 #include <dali/public-api/signals/callback.h>
46 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_PERFORMANCE_MARKER, false);
47 #if defined(DEBUG_ENABLED)
48 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_TOUCH_PROCESSOR");
50 const char* TOUCH_POINT_STATE[6] =
60 #endif // defined(DEBUG_ENABLED)
62 Dali::Actor EmitInterceptTouchSignals(Dali::Actor actor, const Dali::TouchEvent& touchEvent)
64 Dali::Actor interceptedActor;
68 Dali::Actor parent = actor.GetParent();
71 // Recursively deliver events to the actor and its parents for intercept touch event.
72 interceptedActor = EmitInterceptTouchSignals(parent, touchEvent);
77 bool intercepted = false;
78 Actor& actorImpl(GetImplementation(actor));
79 if(actorImpl.GetInterceptTouchRequired())
81 DALI_TRACE_SCOPE(gTraceFilter, "DALI_EMIT_INTERCEPT_TOUCH_EVENT_SIGNAL");
82 intercepted = actorImpl.EmitInterceptTouchEventSignal(touchEvent);
85 interceptedActor = Dali::Actor(&actorImpl);
91 return interceptedActor;
95 * Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
97 Dali::Actor EmitTouchSignals(Dali::Actor actor, const Dali::TouchEvent& touchEvent)
99 Dali::Actor consumedActor;
103 Dali::Actor oldParent(actor.GetParent());
105 Actor& actorImpl(GetImplementation(actor));
107 bool consumed(false);
109 // Only emit the signal if the actor's touch signal has connections (or derived actor implementation requires touch).
110 if(actorImpl.GetTouchRequired())
112 DALI_TRACE_SCOPE(gTraceFilter, "DALI_EMIT_TOUCH_EVENT_SIGNAL");
113 consumed = actorImpl.EmitTouchEventSignal(touchEvent);
118 // One of this actor's listeners has consumed the event so set this actor as the consumed actor.
119 consumedActor = Dali::Actor(&actorImpl);
123 // The actor may have been removed/reparented during the signal callbacks.
124 Dali::Actor parent = actor.GetParent();
127 (parent == oldParent))
129 // One of the actor's parents may consumed the event and they should be set as the consumed actor.
130 consumedActor = EmitTouchSignals(parent, touchEvent);
135 return consumedActor;
138 Dali::Actor AllocAndEmitTouchSignals(unsigned long time, Dali::Actor actor, const Integration::Point& point)
140 TouchEventPtr touchEvent(new TouchEvent(time));
141 Dali::TouchEvent touchEventHandle(touchEvent.Get());
143 touchEvent->AddPoint(point);
145 return EmitTouchSignals(actor, touchEventHandle);
149 * Changes the state of the primary point to leave and emits the touch signals
151 Dali::Actor EmitTouchSignals(Actor* actor, RenderTask& renderTask, const TouchEventPtr& originalTouchEvent, PointState::Type state)
153 Dali::Actor consumingActor;
157 TouchEventPtr touchEventImpl = TouchEvent::Clone(*originalTouchEvent.Get());
159 Integration::Point& primaryPoint = touchEventImpl->GetPoint(0);
161 const Vector2& screenPosition = primaryPoint.GetScreenPosition();
162 Vector2 localPosition;
163 actor->ScreenToLocal(renderTask, localPosition.x, localPosition.y, screenPosition.x, screenPosition.y);
165 primaryPoint.SetLocalPosition(localPosition);
166 primaryPoint.SetHitActor(Dali::Actor(actor));
167 primaryPoint.SetState(state);
169 consumingActor = EmitTouchSignals(Dali::Actor(actor), Dali::TouchEvent(touchEventImpl.Get()));
172 return consumingActor;
176 * @brief Parses the primary touch point by performing a hit-test if necessary
178 * @param[out] hitTestResults The hit test results are put into this variable
179 * @param[in/out] capturingTouchActorObserver The observer for the capturing touch actor member
180 * @param[in] lastRenderTask The last render task member
181 * @param[in] currentPoint The current point information
182 * @param[in] scene The scene that this touch is related to
184 void ParsePrimaryTouchPoint(
185 HitTestAlgorithm::Results& hitTestResults,
186 ActorObserver& capturingTouchActorObserver,
187 ActorObserver& ownTouchActorObserver,
188 const RenderTaskPtr& lastRenderTask,
189 const Integration::Point& currentPoint,
190 const Internal::Scene& scene)
192 Actor* capturingTouchActor = capturingTouchActorObserver.GetActor();
194 // We only set the capturing touch actor when the first touch-started actor captures all touch so if it's set, just use it
195 if(capturingTouchActor && lastRenderTask)
197 hitTestResults.actor = Dali::Actor(capturingTouchActor);
198 hitTestResults.renderTask = lastRenderTask;
199 const Vector2& screenPosition = currentPoint.GetScreenPosition();
200 capturingTouchActor->ScreenToLocal(*lastRenderTask, hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y, screenPosition.x, screenPosition.y);
204 Actor* ownTouchActor = ownTouchActorObserver.GetActor();
205 HitTestAlgorithm::HitTest(scene.GetSize(), scene.GetRenderTaskList(), scene.GetLayerList(), currentPoint.GetScreenPosition(), hitTestResults, ownTouchActor);
207 if(currentPoint.GetState() == PointState::STARTED && hitTestResults.actor)
209 // If we've just started touch, then check whether the actor has requested to capture all touch events
210 Actor* hitActor = &GetImplementation(hitTestResults.actor);
211 if(hitActor->CapturesAllTouchAfterStart())
213 capturingTouchActorObserver.SetActor(hitActor);
215 if(hitActor->IsAllowedOnlyOwnTouch())
217 ownTouchActorObserver.SetActor(hitActor);
223 } // unnamed namespace
225 TouchEventProcessor::TouchEventProcessor(Scene& scene)
227 mLastPrimaryHitActor(MakeCallback(this, &TouchEventProcessor::OnObservedActorDisconnected)),
228 mLastConsumedActor(),
229 mCapturingTouchActor(),
231 mTouchDownConsumedActor(),
232 mInterceptedTouchActor(),
234 mLastPrimaryPointState(PointState::FINISHED)
236 DALI_LOG_TRACE_METHOD(gLogFilter);
239 TouchEventProcessor::~TouchEventProcessor()
241 DALI_LOG_TRACE_METHOD(gLogFilter);
244 void TouchEventProcessor::Clear()
246 mLastPrimaryHitActor.SetActor(nullptr);
247 mLastConsumedActor.SetActor(nullptr);
248 mCapturingTouchActor.SetActor(nullptr);
249 mOwnTouchActor.SetActor(nullptr);
250 mInterceptedTouchActor.SetActor(nullptr);
251 mLastRenderTask.Reset();
252 mLastPrimaryPointState = PointState::FINISHED;
255 bool TouchEventProcessor::ProcessTouchEvent(const Integration::TouchEvent& event)
257 DALI_LOG_TRACE_METHOD(gLogFilter);
258 DALI_ASSERT_ALWAYS(!event.points.empty() && "Empty TouchEvent sent from Integration\n");
260 PRINT_HIERARCHY(gLogFilter);
262 DALI_TRACE_SCOPE(gTraceFilter, "DALI_PROCESS_TOUCH_EVENT");
264 // 1) Check if it is an interrupted event - we should inform our last primary hit actor about this
265 // and emit the stage signal as well.
267 if(event.points[0].GetState() == PointState::INTERRUPTED)
269 Dali::Actor consumingActor;
270 Integration::Point currentPoint(event.points[0]);
272 Actor* lastPrimaryHitActor(mLastPrimaryHitActor.GetActor());
273 if(lastPrimaryHitActor)
275 Dali::Actor lastPrimaryHitActorHandle(lastPrimaryHitActor);
276 currentPoint.SetHitActor(lastPrimaryHitActorHandle);
278 consumingActor = AllocAndEmitTouchSignals(event.time, lastPrimaryHitActorHandle, currentPoint);
281 // If the last consumed actor was different to the primary hit actor then inform it as well (if it has not already been informed).
282 Actor* lastConsumedActor(mLastConsumedActor.GetActor());
283 if(lastConsumedActor &&
284 lastConsumedActor != lastPrimaryHitActor &&
285 lastConsumedActor != consumingActor)
287 Dali::Actor lastConsumedActorHandle(lastConsumedActor);
288 currentPoint.SetHitActor(lastConsumedActorHandle);
289 AllocAndEmitTouchSignals(event.time, lastConsumedActorHandle, currentPoint);
292 // Tell the touch-down consuming actor as well, if required
293 Actor* touchDownConsumedActor(mTouchDownConsumedActor.GetActor());
294 if(touchDownConsumedActor &&
295 touchDownConsumedActor != lastPrimaryHitActor &&
296 touchDownConsumedActor != lastConsumedActor &&
297 touchDownConsumedActor != consumingActor)
299 Dali::Actor touchDownConsumedActorHandle(touchDownConsumedActor);
301 currentPoint.SetHitActor(touchDownConsumedActorHandle);
302 AllocAndEmitTouchSignals(event.time, touchDownConsumedActorHandle, currentPoint);
306 mTouchDownConsumedActor.SetActor(nullptr);
308 currentPoint.SetHitActor(Dali::Actor());
310 TouchEventPtr touchEventImpl(new TouchEvent(event.time));
311 Dali::TouchEvent touchEventHandle(touchEventImpl.Get());
313 touchEventImpl->AddPoint(currentPoint);
315 mScene.EmitTouchedSignal(touchEventHandle);
316 return false; // No need for hit testing & already an interrupted event so just return false
320 TouchEventPtr touchEventImpl(new TouchEvent(event.time));
321 Dali::TouchEvent touchEventHandle(touchEventImpl.Get());
323 DALI_LOG_INFO(gLogFilter, Debug::Concise, "\n");
324 DALI_LOG_INFO(gLogFilter, Debug::General, "Point(s): %d\n", event.GetPointCount());
326 RenderTaskPtr currentRenderTask;
327 bool firstPointParsed = false;
329 for(auto&& currentPoint : event.points)
331 HitTestAlgorithm::Results hitTestResults;
332 hitTestResults.point = currentPoint;
333 hitTestResults.eventTime = event.time;
334 if(!firstPointParsed)
336 firstPointParsed = true;
337 ParsePrimaryTouchPoint(hitTestResults, mCapturingTouchActor, mOwnTouchActor, mLastRenderTask, currentPoint, mScene);
339 // Only set the currentRenderTask for the primary hit actor.
340 currentRenderTask = hitTestResults.renderTask;
344 HitTestAlgorithm::HitTest(mScene.GetSize(), mScene.GetRenderTaskList(), mScene.GetLayerList(), currentPoint.GetScreenPosition(), hitTestResults);
347 Integration::Point newPoint(currentPoint);
348 newPoint.SetHitActor(hitTestResults.actor);
349 newPoint.SetLocalPosition(hitTestResults.actorCoordinates);
351 touchEventImpl->AddPoint(newPoint);
353 DALI_LOG_INFO(gLogFilter, Debug::General, " State(%s), Screen(%.0f, %.0f), HitActor(%p, %s), Local(%.2f, %.2f)\n", TOUCH_POINT_STATE[currentPoint.GetState()], currentPoint.GetScreenPosition().x, currentPoint.GetScreenPosition().y, (hitTestResults.actor ? reinterpret_cast<void*>(&hitTestResults.actor.GetBaseObject()) : NULL), (hitTestResults.actor ? hitTestResults.actor.GetProperty<std::string>(Dali::Actor::Property::NAME).c_str() : ""), hitTestResults.actorCoordinates.x, hitTestResults.actorCoordinates.y);
356 // 3) Recursively deliver events to the actor and its parents, until the event is consumed or the stage is reached.
358 bool consumed = false;
360 // Emit the touch signal
361 Dali::Actor consumedActor;
363 Integration::Point& primaryPoint = touchEventImpl->GetPoint(0);
364 Dali::Actor primaryHitActor = primaryPoint.GetHitActor();
365 PointState::Type primaryPointState = primaryPoint.GetState();
367 if(currentRenderTask)
369 Actor* interceptedTouchActor(mInterceptedTouchActor.GetActor());
370 if(interceptedTouchActor)
372 Dali::Actor interceptedTouchActorHandle(interceptedTouchActor);
373 consumedActor = EmitTouchSignals(interceptedTouchActorHandle, touchEventHandle);
377 // Emit the intercept touch signal
378 Dali::Actor interceptedActor = EmitInterceptTouchSignals(primaryHitActor, touchEventHandle);
381 mInterceptedTouchActor.SetActor(&GetImplementation(interceptedActor));
382 // If the child is being touched, INTERRUPTED is sent.
383 if(mLastPrimaryHitActor.GetActor() &&
384 mLastPrimaryHitActor.GetActor() != interceptedActor &&
386 mLastPrimaryPointState != PointState::FINISHED)
388 EmitTouchSignals(mLastPrimaryHitActor.GetActor(), *mLastRenderTask.Get(), touchEventImpl, PointState::INTERRUPTED);
389 mTouchDownConsumedActor.SetActor(nullptr);
391 consumedActor = EmitTouchSignals(interceptedActor, touchEventHandle);
395 consumedActor = EmitTouchSignals(primaryHitActor, touchEventHandle);
398 consumed = consumedActor ? true : false;
401 DALI_LOG_INFO(gLogFilter, Debug::Concise, "PrimaryHitActor: (%p) %s\n", primaryHitActor ? reinterpret_cast<void*>(&primaryHitActor.GetBaseObject()) : NULL, primaryHitActor ? primaryHitActor.GetProperty<std::string>(Dali::Actor::Property::NAME).c_str() : "");
402 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() : "");
404 if((primaryPointState == PointState::DOWN) &&
405 (touchEventImpl->GetPointCount() == 1) &&
406 (consumedActor && consumedActor.GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE)))
408 mTouchDownConsumedActor.SetActor(&GetImplementation(consumedActor));
411 // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
412 // hit actor. Also process the last consumed actor in the same manner.
413 Actor* lastPrimaryHitActor(nullptr);
414 if(mInterceptedTouchActor.GetActor())
416 lastPrimaryHitActor = mInterceptedTouchActor.GetActor();
420 lastPrimaryHitActor = mLastPrimaryHitActor.GetActor();
422 Actor* lastConsumedActor(mLastConsumedActor.GetActor());
423 if((primaryPointState == PointState::MOTION) || (primaryPointState == PointState::UP) || (primaryPointState == PointState::STATIONARY))
427 Dali::Actor leaveEventConsumer;
428 RenderTask& lastRenderTaskImpl = *mLastRenderTask.Get();
430 if(lastPrimaryHitActor &&
431 lastPrimaryHitActor != primaryHitActor &&
432 lastPrimaryHitActor != consumedActor)
434 if(lastPrimaryHitActor->IsHittable() && IsActuallySensitive(lastPrimaryHitActor))
436 if(lastPrimaryHitActor->GetLeaveRequired())
438 DALI_LOG_INFO(gLogFilter, Debug::Concise, "LeaveActor(Hit): (%p) %s\n", reinterpret_cast<void*>(lastPrimaryHitActor), lastPrimaryHitActor->GetName().data());
439 leaveEventConsumer = EmitTouchSignals(lastPrimaryHitActor, lastRenderTaskImpl, touchEventImpl, PointState::LEAVE);
444 // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
445 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
446 DALI_LOG_INFO(gLogFilter, Debug::Concise, "InterruptedActor(Hit): (%p) %s\n", reinterpret_cast<void*>(lastPrimaryHitActor), lastPrimaryHitActor->GetName().data());
447 leaveEventConsumer = EmitTouchSignals(lastPrimaryHitActor, lastRenderTaskImpl, touchEventImpl, PointState::INTERRUPTED);
451 consumed |= leaveEventConsumer ? true : false;
453 // Check if the motion event has been consumed by another actor's listener. In this case, the previously
454 // consumed actor's listeners may need to be informed (through a leave event).
455 // Further checks here to ensure we do not signal the same actor twice for the same event.
456 if(lastConsumedActor &&
457 lastConsumedActor != consumedActor &&
458 lastConsumedActor != lastPrimaryHitActor &&
459 lastConsumedActor != primaryHitActor &&
460 lastConsumedActor != leaveEventConsumer)
462 if(lastConsumedActor->IsHittable() && IsActuallySensitive(lastConsumedActor))
464 if(lastConsumedActor->GetLeaveRequired())
466 DALI_LOG_INFO(gLogFilter, Debug::Concise, "LeaveActor(Consume): (%p) %s\n", reinterpret_cast<void*>(lastConsumedActor), lastConsumedActor->GetName().data());
467 EmitTouchSignals(lastConsumedActor, lastRenderTaskImpl, touchEventImpl, PointState::LEAVE);
472 // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
473 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
474 DALI_LOG_INFO(gLogFilter, Debug::Concise, "InterruptedActor(Consume): (%p) %s\n", reinterpret_cast<void*>(lastConsumedActor), lastConsumedActor->GetName().data());
475 EmitTouchSignals(mLastConsumedActor.GetActor(), lastRenderTaskImpl, touchEventImpl, PointState::INTERRUPTED);
481 // 5) If our primary point is an Up event, then the primary point (in multi-touch) will change next
482 // time so set our last primary actor to NULL. Do the same to the last consumed actor as well.
483 if(primaryPointState == PointState::UP)
489 // The primaryHitActor may have been removed from the scene so ensure it is still on the scene before setting members.
490 if(primaryHitActor && GetImplementation(primaryHitActor).OnScene())
492 mLastPrimaryHitActor.SetActor(&GetImplementation(primaryHitActor));
494 // Only observe the consumed actor if we have a primaryHitActor (check if it is still on the scene).
495 if(consumedActor && GetImplementation(consumedActor).OnScene())
497 mLastConsumedActor.SetActor(&GetImplementation(consumedActor));
501 mLastConsumedActor.SetActor(nullptr);
504 mLastRenderTask = currentRenderTask;
505 mLastPrimaryPointState = primaryPointState;
513 // 6) Emit an interrupted event to the touch-down actor if it hasn't consumed the up and
514 // emit the stage touched event if required.
516 if(touchEventImpl->GetPointCount() == 1) // Only want the first touch and the last release
518 switch(primaryPointState)
522 Actor* touchDownConsumedActor(mTouchDownConsumedActor.GetActor());
523 if(touchDownConsumedActor &&
524 touchDownConsumedActor != consumedActor &&
525 touchDownConsumedActor != lastPrimaryHitActor &&
526 touchDownConsumedActor != lastConsumedActor)
528 Dali::Actor touchDownConsumedActorHandle(touchDownConsumedActor);
530 Integration::Point currentPoint = touchEventImpl->GetPoint(0);
531 currentPoint.SetHitActor(touchDownConsumedActorHandle);
532 currentPoint.SetState(PointState::INTERRUPTED);
534 AllocAndEmitTouchSignals(event.time, touchDownConsumedActorHandle, currentPoint);
537 mTouchDownConsumedActor.SetActor(nullptr);
538 mInterceptedTouchActor.SetActor(nullptr);
543 case PointState::DOWN:
545 mScene.EmitTouchedSignal(touchEventHandle);
549 case PointState::MOTION:
550 case PointState::LEAVE:
551 case PointState::STATIONARY:
552 case PointState::INTERRUPTED:
563 void TouchEventProcessor::OnObservedActorDisconnected(Actor* actor)
565 if(actor == mLastPrimaryHitActor.GetActor())
567 Dali::Actor actorHandle(actor);
569 Integration::Point point;
570 point.SetState(PointState::INTERRUPTED);
571 point.SetHitActor(actorHandle);
573 TouchEventPtr touchEventImpl(new TouchEvent);
574 touchEventImpl->AddPoint(point);
575 Dali::TouchEvent touchEventHandle(touchEventImpl.Get());
577 Dali::Actor eventConsumer = EmitTouchSignals(actorHandle, touchEventHandle);
579 if(mLastConsumedActor.GetActor() != eventConsumer)
581 EmitTouchSignals(Dali::Actor(mLastConsumedActor.GetActor()), touchEventHandle);
584 // Do not set mLastPrimaryHitActor to NULL we may be iterating through its observers
586 mLastConsumedActor.SetActor(nullptr);
587 mLastRenderTask.Reset();
588 mLastPrimaryPointState = PointState::FINISHED;
592 } // namespace Internal