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/long-press-gesture/long-press-gesture-processor.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/trace.h>
27 #include <dali/internal/event/actors/actor-impl.h>
28 #include <dali/internal/event/common/scene-impl.h>
29 #include <dali/internal/event/events/gesture-requests.h>
30 #include <dali/internal/event/events/long-press-gesture/long-press-gesture-event.h>
31 #include <dali/internal/event/events/long-press-gesture/long-press-gesture-impl.h>
32 #include <dali/internal/event/events/long-press-gesture/long-press-gesture-recognizer.h>
33 #include <dali/internal/event/render-tasks/render-task-impl.h>
34 #include <dali/public-api/actors/actor.h>
35 #include <dali/public-api/common/dali-common.h>
43 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_PERFORMANCE_MARKER, false);
44 const unsigned long DEFAULT_MINIMUM_HOLDING_TIME = 500u;
47 * Creates a LongPressGesture and asks the specified detector to emit its detected signal.
48 * @param[in] actor The actor on which the long press gesture has occurred.
49 * @param[in] gestureDetectors A reference to gesture detectors that should emit the signal.
50 * @param[in] longPressEvent The longPressEvent received from the adaptor.
51 * @param[in] localPoint Relative to the actor attached to the detector.
53 void EmitLongPressSignal(
55 const GestureDetectorContainer& gestureDetectors,
56 const LongPressGestureEvent& longPressEvent,
59 Internal::LongPressGesturePtr longPress(new Internal::LongPressGesture(longPressEvent.state));
60 longPress->SetTime(longPressEvent.time);
61 longPress->SetNumberOfTouches(longPressEvent.numberOfTouches);
62 longPress->SetScreenPoint(longPressEvent.point);
63 longPress->SetLocalPoint(localPoint);
64 longPress->SetSourceType(longPressEvent.sourceType);
65 longPress->SetSourceData(longPressEvent.sourceData);
67 DALI_TRACE_BEGIN_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_EMIT_LONG_PRESS_GESTURE_SIGNAL", [&](std::ostringstream& oss) {
68 oss << "[" << gestureDetectors.size() << "]";
71 Dali::Actor actorHandle(actor);
72 const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
73 for(GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter)
75 static_cast<LongPressGestureDetector*>(*iter)->EmitLongPressGestureSignal(actorHandle, Dali::LongPressGesture(longPress.Get()));
78 DALI_TRACE_END_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_EMIT_LONG_PRESS_GESTURE_SIGNAL", [&](std::ostringstream& oss) {
79 oss << "[" << gestureDetectors.size() << "]";
84 * Functor which checks whether the specified actor is attached to the gesture detector.
85 * It returns true if it is no longer attached. This can be used in remove_if functions.
87 struct IsNotAttachedFunctor
91 * @param[in] actor The actor to check whether it is attached.
93 IsNotAttachedFunctor(Actor* actor)
99 * Returns true if not attached, false if it is still attached.
100 * @param[in] detector The detector to check.
101 * @return true, if not attached, false otherwise.
103 bool operator()(const GestureDetector* detector) const
105 return !detector->IsAttached(*actorToCheck);
108 Actor* actorToCheck; ///< The actor to check whether it is attached or not.
111 } // unnamed namespace
113 LongPressGestureProcessor::LongPressGestureProcessor()
114 : GestureProcessor(GestureType::LONG_PRESS),
115 mLongPressGestureDetectors(),
117 mCurrentRenderTask(),
118 mMinTouchesRequired(1),
119 mMaxTouchesRequired(1),
120 mCurrentLongPressEvent(nullptr),
121 mMinimumHoldingTime(DEFAULT_MINIMUM_HOLDING_TIME)
125 LongPressGestureProcessor::~LongPressGestureProcessor() = default;
127 void LongPressGestureProcessor::Process(Scene& scene, const LongPressGestureEvent& longPressEvent, Actor* actor)
129 DALI_TRACE_SCOPE(gTraceFilter, "DALI_PROCESS_LONG_PRESS_GESTURE");
130 switch(longPressEvent.state)
132 case GestureState::POSSIBLE:
134 mCurrentEmitters.clear();
137 HitTestAlgorithm::Results hitTestResults;
142 else if(HitTest(scene, longPressEvent.point, hitTestResults))
144 SetActor(&GetImplementation(hitTestResults.actor));
149 case GestureState::STARTED:
151 Actor* currentGesturedActor = GetCurrentGesturedActor();
152 if(currentGesturedActor)
154 HitTestAlgorithm::Results hitTestResults;
157 hitTestResults.actor = Dali::Actor(actor);
158 hitTestResults.renderTask = longPressEvent.renderTask;
161 currentGesturedActor->ScreenToLocal(*hitTestResults.renderTask.Get(), actorCoords.x, actorCoords.y, longPressEvent.point.x, longPressEvent.point.y);
162 hitTestResults.actorCoordinates = actorCoords;
166 HitTest(scene, longPressEvent.point, hitTestResults);
169 if(hitTestResults.actor && (currentGesturedActor == &GetImplementation(hitTestResults.actor)))
171 // Record the current render-task for Screen->Actor coordinate conversions
172 mCurrentRenderTask = hitTestResults.renderTask;
174 // Set mCurrentLongPressEvent to use inside overridden methods called from ProcessAndEmit()
175 mCurrentLongPressEvent = &longPressEvent;
178 ProcessAndEmitActor(hitTestResults);
182 ProcessAndEmit(hitTestResults);
184 mCurrentLongPressEvent = nullptr;
188 mCurrentEmitters.clear();
195 case GestureState::FINISHED:
197 // The gesture should only be sent to the gesture detector which first received it so that it
198 // can be told when the gesture ends as well.
200 // Only send subsequent long press gesture signals if we processed the gesture when it started.
201 // Check if actor is still touchable.
203 Actor* currentGesturedActor = GetCurrentGesturedActor();
204 if(currentGesturedActor)
206 if(currentGesturedActor->IsHittable() && !mCurrentEmitters.empty() && mCurrentRenderTask)
208 // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
209 GestureDetectorContainer::iterator endIter = std::remove_if(mCurrentEmitters.begin(), mCurrentEmitters.end(), IsNotAttachedFunctor(currentGesturedActor));
210 mCurrentEmitters.erase(endIter, mCurrentEmitters.end());
212 if(!mCurrentEmitters.empty())
215 RenderTask& renderTaskImpl = *mCurrentRenderTask.Get();
216 currentGesturedActor->ScreenToLocal(renderTaskImpl, actorCoords.x, actorCoords.y, longPressEvent.point.x, longPressEvent.point.y);
218 EmitLongPressSignal(currentGesturedActor, mCurrentEmitters, longPressEvent, actorCoords);
222 // Clear current emitters and emitted actor
223 mCurrentEmitters.clear();
229 case GestureState::CANCELLED:
231 mCurrentEmitters.clear();
236 case GestureState::CONTINUING:
238 DALI_ABORT("Incorrect state received from Integration layer: CONTINUING\n");
242 case GestureState::CLEAR:
244 DALI_ABORT("Incorrect state received from Integration layer: CLEAR\n");
250 void LongPressGestureProcessor::AddGestureDetector(LongPressGestureDetector* gestureDetector, Scene& scene)
252 bool firstRegistration(mLongPressGestureDetectors.empty());
254 mLongPressGestureDetectors.push_back(gestureDetector);
256 if(firstRegistration)
258 mMinTouchesRequired = gestureDetector->GetMinimumTouchesRequired();
259 mMaxTouchesRequired = gestureDetector->GetMaximumTouchesRequired();
261 LongPressGestureRequest request;
262 request.minTouches = mMinTouchesRequired;
263 request.maxTouches = mMaxTouchesRequired;
265 Size size = scene.GetSize();
267 mGestureRecognizer = new LongPressGestureRecognizer(*this, Vector2(size.width, size.height), static_cast<const LongPressGestureRequest&>(request), mMinimumHoldingTime);
275 void LongPressGestureProcessor::RemoveGestureDetector(LongPressGestureDetector* gestureDetector)
278 LongPressGestureDetectorContainer::iterator endIter = std::remove(mLongPressGestureDetectors.begin(), mLongPressGestureDetectors.end(), gestureDetector);
279 DALI_ASSERT_DEBUG(endIter != mLongPressGestureDetectors.end());
282 mLongPressGestureDetectors.erase(endIter, mLongPressGestureDetectors.end());
284 if(mLongPressGestureDetectors.empty())
286 mGestureRecognizer = nullptr;
294 void LongPressGestureProcessor::GestureDetectorUpdated(LongPressGestureDetector* gestureDetector)
296 DALI_ASSERT_DEBUG(find(mLongPressGestureDetectors.begin(), mLongPressGestureDetectors.end(), gestureDetector) != mLongPressGestureDetectors.end());
301 void LongPressGestureProcessor::SetMinimumHoldingTime(uint32_t time)
303 if(time > 0u && mMinimumHoldingTime != time)
305 mMinimumHoldingTime = time;
307 if(mGestureRecognizer)
309 LongPressGestureRecognizer* longPressRecognizer = dynamic_cast<LongPressGestureRecognizer*>(mGestureRecognizer.Get());
310 if(longPressRecognizer)
312 longPressRecognizer->SetMinimumHoldingTime(time);
318 uint32_t LongPressGestureProcessor::GetMinimumHoldingTime() const
320 return mMinimumHoldingTime;
323 void LongPressGestureProcessor::UpdateDetection()
325 DALI_ASSERT_DEBUG(!mLongPressGestureDetectors.empty());
327 unsigned int minimumRequired = UINT_MAX;
328 unsigned int maximumRequired = 0;
330 for(LongPressGestureDetectorContainer::iterator iter = mLongPressGestureDetectors.begin(), endIter = mLongPressGestureDetectors.end(); iter != endIter; ++iter)
332 LongPressGestureDetector* current(*iter);
336 unsigned int minimum = current->GetMinimumTouchesRequired();
337 if(minimum < minimumRequired)
339 minimumRequired = minimum;
342 unsigned int maximum = current->GetMaximumTouchesRequired();
343 if(maximum > maximumRequired)
345 maximumRequired = maximum;
350 if((minimumRequired != mMinTouchesRequired) || (maximumRequired != mMaxTouchesRequired))
352 mMinTouchesRequired = minimumRequired;
353 mMaxTouchesRequired = maximumRequired;
355 LongPressGestureRequest request;
356 request.minTouches = mMinTouchesRequired;
357 request.maxTouches = mMaxTouchesRequired;
358 mGestureRecognizer->Update(request);
362 void LongPressGestureProcessor::OnGesturedActorStageDisconnection()
364 mCurrentEmitters.clear();
367 bool LongPressGestureProcessor::CheckGestureDetector(GestureDetector* detector, Actor* actor)
369 DALI_ASSERT_DEBUG(mCurrentLongPressEvent);
371 LongPressGestureDetector* longPressDetector(static_cast<LongPressGestureDetector*>(detector));
373 return (longPressDetector->GetMinimumTouchesRequired() <= mCurrentLongPressEvent->numberOfTouches) &&
374 (longPressDetector->GetMaximumTouchesRequired() >= mCurrentLongPressEvent->numberOfTouches);
377 void LongPressGestureProcessor::EmitGestureSignal(Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates)
379 DALI_ASSERT_DEBUG(mCurrentLongPressEvent);
381 mCurrentEmitters.clear();
384 EmitLongPressSignal(actor, gestureDetectors, *mCurrentLongPressEvent, actorCoordinates);
388 mCurrentEmitters = gestureDetectors;
393 } // namespace Internal