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");
49 #endif // defined(DEBUG_ENABLED)
51 const char* TOUCH_POINT_STATE[6] =
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;
400 if(primaryPointState == PointState::MOTION)
402 DALI_LOG_INFO(gLogFilter, Debug::Concise, "PrimaryHitActor: (%p) id(%d), name(%s), state(%s), screenPosition(%f, %f)\n", primaryHitActor ? reinterpret_cast<void*>(&primaryHitActor.GetBaseObject()) : NULL, primaryHitActor ? primaryHitActor.GetProperty<int32_t>(Dali::Actor::Property::ID) : -1, primaryHitActor ? primaryHitActor.GetProperty<std::string>(Dali::Actor::Property::NAME).c_str() : "", TOUCH_POINT_STATE[primaryPointState], primaryPoint.GetScreenPosition().x, primaryPoint.GetScreenPosition().y);
403 DALI_LOG_INFO(gLogFilter, Debug::Concise, "ConsumedActor: (%p) id(%d), name(%s), state(%s)\n", consumedActor ? reinterpret_cast<void*>(&consumedActor.GetBaseObject()) : NULL, consumedActor ? consumedActor.GetProperty<int32_t>(Dali::Actor::Property::ID) : -1, consumedActor ? consumedActor.GetProperty<std::string>(Dali::Actor::Property::NAME).c_str() : "", TOUCH_POINT_STATE[primaryPointState]);
407 DALI_LOG_RELEASE_INFO("PrimaryHitActor:(%p), id(%d), name(%s), state(%s)\n", primaryHitActor ? reinterpret_cast<void*>(&primaryHitActor.GetBaseObject()) : NULL, primaryHitActor ? primaryHitActor.GetProperty<int32_t>(Dali::Actor::Property::ID) : -1, primaryHitActor ? primaryHitActor.GetProperty<std::string>(Dali::Actor::Property::NAME).c_str() : "", TOUCH_POINT_STATE[primaryPointState]);
408 DALI_LOG_RELEASE_INFO("ConsumedActor: (%p), id(%d), name(%s), state(%s)\n", consumedActor ? reinterpret_cast<void*>(&consumedActor.GetBaseObject()) : NULL, consumedActor ? consumedActor.GetProperty<int32_t>(Dali::Actor::Property::ID) : -1, consumedActor ? consumedActor.GetProperty<std::string>(Dali::Actor::Property::NAME).c_str() : "", TOUCH_POINT_STATE[primaryPointState]);
412 if((primaryPointState == PointState::DOWN) &&
413 (touchEventImpl->GetPointCount() == 1) &&
414 (consumedActor && consumedActor.GetProperty<bool>(Dali::Actor::Property::CONNECTED_TO_SCENE)))
416 mTouchDownConsumedActor.SetActor(&GetImplementation(consumedActor));
419 // 4) Check if the last primary hit actor requires a leave event and if it was different to the current primary
420 // hit actor. Also process the last consumed actor in the same manner.
421 Actor* lastPrimaryHitActor(nullptr);
422 if(mInterceptedTouchActor.GetActor())
424 lastPrimaryHitActor = mInterceptedTouchActor.GetActor();
428 lastPrimaryHitActor = mLastPrimaryHitActor.GetActor();
430 Actor* lastConsumedActor(mLastConsumedActor.GetActor());
431 if((primaryPointState == PointState::MOTION) || (primaryPointState == PointState::UP) || (primaryPointState == PointState::STATIONARY))
435 Dali::Actor leaveEventConsumer;
436 RenderTask& lastRenderTaskImpl = *mLastRenderTask.Get();
438 if(lastPrimaryHitActor &&
439 lastPrimaryHitActor != primaryHitActor &&
440 lastPrimaryHitActor != consumedActor)
442 if(lastPrimaryHitActor->IsHittable() && IsActuallySensitive(lastPrimaryHitActor))
444 if(lastPrimaryHitActor->GetLeaveRequired())
446 DALI_LOG_RELEASE_INFO("LeaveActor(Hit): (%p) %d %s\n", reinterpret_cast<void*>(lastPrimaryHitActor), lastPrimaryHitActor->GetId(), lastPrimaryHitActor->GetName().data());
447 leaveEventConsumer = EmitTouchSignals(lastPrimaryHitActor, lastRenderTaskImpl, touchEventImpl, PointState::LEAVE);
452 // At this point mLastPrimaryHitActor was touchable and sensitive in the previous touch event process but is not in the current one.
453 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
454 DALI_LOG_RELEASE_INFO("InterruptedActor(Hit): (%p) %d %s\n", reinterpret_cast<void*>(lastPrimaryHitActor), lastPrimaryHitActor->GetId(), lastPrimaryHitActor->GetName().data());
455 leaveEventConsumer = EmitTouchSignals(lastPrimaryHitActor, lastRenderTaskImpl, touchEventImpl, PointState::INTERRUPTED);
459 consumed |= leaveEventConsumer ? true : false;
461 // Check if the motion event has been consumed by another actor's listener. In this case, the previously
462 // consumed actor's listeners may need to be informed (through a leave event).
463 // Further checks here to ensure we do not signal the same actor twice for the same event.
464 if(lastConsumedActor &&
465 lastConsumedActor != consumedActor &&
466 lastConsumedActor != lastPrimaryHitActor &&
467 lastConsumedActor != primaryHitActor &&
468 lastConsumedActor != leaveEventConsumer)
470 if(lastConsumedActor->IsHittable() && IsActuallySensitive(lastConsumedActor))
472 if(lastConsumedActor->GetLeaveRequired())
474 DALI_LOG_RELEASE_INFO("LeaveActor(Consume): (%p) %d %s\n", reinterpret_cast<void*>(lastConsumedActor), lastConsumedActor->GetId(), lastConsumedActor->GetName().data());
475 EmitTouchSignals(lastConsumedActor, lastRenderTaskImpl, touchEventImpl, PointState::LEAVE);
480 // At this point mLastConsumedActor was touchable and sensitive in the previous touch event process but is not in the current one.
481 // An interrupted event is send to allow some actors to go back to their original state (i.e. Button controls)
482 DALI_LOG_RELEASE_INFO("InterruptedActor(Consume): (%p) %d %s\n", reinterpret_cast<void*>(lastConsumedActor), lastConsumedActor->GetId(), lastConsumedActor->GetName().data());
483 EmitTouchSignals(mLastConsumedActor.GetActor(), lastRenderTaskImpl, touchEventImpl, PointState::INTERRUPTED);
489 // 5) If our primary point is an Up event, then the primary point (in multi-touch) will change next
490 // time so set our last primary actor to NULL. Do the same to the last consumed actor as well.
491 if(primaryPointState == PointState::UP)
497 // The primaryHitActor may have been removed from the scene so ensure it is still on the scene before setting members.
498 if(primaryHitActor && GetImplementation(primaryHitActor).OnScene())
500 mLastPrimaryHitActor.SetActor(&GetImplementation(primaryHitActor));
502 // Only observe the consumed actor if we have a primaryHitActor (check if it is still on the scene).
503 if(consumedActor && GetImplementation(consumedActor).OnScene())
505 mLastConsumedActor.SetActor(&GetImplementation(consumedActor));
509 mLastConsumedActor.SetActor(nullptr);
512 mLastRenderTask = currentRenderTask;
513 mLastPrimaryPointState = primaryPointState;
521 // 6) Emit an interrupted event to the touch-down actor if it hasn't consumed the up and
522 // emit the stage touched event if required.
524 if(touchEventImpl->GetPointCount() == 1) // Only want the first touch and the last release
526 switch(primaryPointState)
530 Actor* touchDownConsumedActor(mTouchDownConsumedActor.GetActor());
531 if(touchDownConsumedActor &&
532 touchDownConsumedActor != consumedActor &&
533 touchDownConsumedActor != lastPrimaryHitActor &&
534 touchDownConsumedActor != lastConsumedActor)
536 Dali::Actor touchDownConsumedActorHandle(touchDownConsumedActor);
538 Integration::Point currentPoint = touchEventImpl->GetPoint(0);
539 currentPoint.SetHitActor(touchDownConsumedActorHandle);
540 currentPoint.SetState(PointState::INTERRUPTED);
542 AllocAndEmitTouchSignals(event.time, touchDownConsumedActorHandle, currentPoint);
545 mTouchDownConsumedActor.SetActor(nullptr);
546 mInterceptedTouchActor.SetActor(nullptr);
551 case PointState::DOWN:
553 mScene.EmitTouchedSignal(touchEventHandle);
557 case PointState::MOTION:
558 case PointState::LEAVE:
559 case PointState::STATIONARY:
560 case PointState::INTERRUPTED:
571 void TouchEventProcessor::OnObservedActorDisconnected(Actor* actor)
573 if(actor == mLastPrimaryHitActor.GetActor())
575 Dali::Actor actorHandle(actor);
577 Integration::Point point;
578 point.SetState(PointState::INTERRUPTED);
579 point.SetHitActor(actorHandle);
581 TouchEventPtr touchEventImpl(new TouchEvent);
582 touchEventImpl->AddPoint(point);
583 Dali::TouchEvent touchEventHandle(touchEventImpl.Get());
585 Dali::Actor eventConsumer = EmitTouchSignals(actorHandle, touchEventHandle);
587 if(mLastConsumedActor.GetActor() != eventConsumer)
589 EmitTouchSignals(Dali::Actor(mLastConsumedActor.GetActor()), touchEventHandle);
592 // Do not set mLastPrimaryHitActor to NULL we may be iterating through its observers
594 mLastConsumedActor.SetActor(nullptr);
595 mLastRenderTask.Reset();
596 mLastPrimaryPointState = PointState::FINISHED;
600 } // namespace Internal