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_SCOPE(gTraceFilter, "DALI_EMIT_LONG_PRESS_GESTURE_SIGNAL");
69 Dali::Actor actorHandle(actor);
70 const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
71 for(GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter)
73 static_cast<LongPressGestureDetector*>(*iter)->EmitLongPressGestureSignal(actorHandle, Dali::LongPressGesture(longPress.Get()));
78 * Functor which checks whether the specified actor is attached to the gesture detector.
79 * It returns true if it is no longer attached. This can be used in remove_if functions.
81 struct IsNotAttachedFunctor
85 * @param[in] actor The actor to check whether it is attached.
87 IsNotAttachedFunctor(Actor* actor)
93 * Returns true if not attached, false if it is still attached.
94 * @param[in] detector The detector to check.
95 * @return true, if not attached, false otherwise.
97 bool operator()(const GestureDetector* detector) const
99 return !detector->IsAttached(*actorToCheck);
102 Actor* actorToCheck; ///< The actor to check whether it is attached or not.
105 } // unnamed namespace
107 LongPressGestureProcessor::LongPressGestureProcessor()
108 : GestureProcessor(GestureType::LONG_PRESS),
109 mLongPressGestureDetectors(),
111 mCurrentRenderTask(),
112 mMinTouchesRequired(1),
113 mMaxTouchesRequired(1),
114 mCurrentLongPressEvent(nullptr),
115 mMinimumHoldingTime(DEFAULT_MINIMUM_HOLDING_TIME)
119 LongPressGestureProcessor::~LongPressGestureProcessor() = default;
121 void LongPressGestureProcessor::Process(Scene& scene, const LongPressGestureEvent& longPressEvent)
123 DALI_TRACE_SCOPE(gTraceFilter, "DALI_PROCESS_LONG_PRESS_GESTURE");
124 switch(longPressEvent.state)
126 case GestureState::POSSIBLE:
128 mCurrentEmitters.clear();
131 HitTestAlgorithm::Results hitTestResults;
132 if(HitTest(scene, longPressEvent.point, hitTestResults))
134 SetActor(&GetImplementation(hitTestResults.actor));
139 case GestureState::STARTED:
141 Actor* currentGesturedActor = GetCurrentGesturedActor();
142 if(currentGesturedActor)
144 HitTestAlgorithm::Results hitTestResults;
145 HitTest(scene, longPressEvent.point, hitTestResults);
147 if(hitTestResults.actor && (currentGesturedActor == &GetImplementation(hitTestResults.actor)))
149 // Record the current render-task for Screen->Actor coordinate conversions
150 mCurrentRenderTask = hitTestResults.renderTask;
152 // Set mCurrentLongPressEvent to use inside overridden methods called from ProcessAndEmit()
153 mCurrentLongPressEvent = &longPressEvent;
154 ProcessAndEmit(hitTestResults);
155 mCurrentLongPressEvent = nullptr;
159 mCurrentEmitters.clear();
166 case GestureState::FINISHED:
168 // The gesture should only be sent to the gesture detector which first received it so that it
169 // can be told when the gesture ends as well.
171 // Only send subsequent long press gesture signals if we processed the gesture when it started.
172 // Check if actor is still touchable.
174 Actor* currentGesturedActor = GetCurrentGesturedActor();
175 if(currentGesturedActor)
177 if(currentGesturedActor->IsHittable() && !mCurrentEmitters.empty() && mCurrentRenderTask)
179 // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
180 GestureDetectorContainer::iterator endIter = std::remove_if(mCurrentEmitters.begin(), mCurrentEmitters.end(), IsNotAttachedFunctor(currentGesturedActor));
181 mCurrentEmitters.erase(endIter, mCurrentEmitters.end());
183 if(!mCurrentEmitters.empty())
186 RenderTask& renderTaskImpl = *mCurrentRenderTask.Get();
187 currentGesturedActor->ScreenToLocal(renderTaskImpl, actorCoords.x, actorCoords.y, longPressEvent.point.x, longPressEvent.point.y);
189 EmitLongPressSignal(currentGesturedActor, mCurrentEmitters, longPressEvent, actorCoords);
193 // Clear current emitters and emitted actor
194 mCurrentEmitters.clear();
200 case GestureState::CANCELLED:
202 mCurrentEmitters.clear();
207 case GestureState::CONTINUING:
209 DALI_ABORT("Incorrect state received from Integration layer: CONTINUING\n");
213 case GestureState::CLEAR:
215 DALI_ABORT("Incorrect state received from Integration layer: CLEAR\n");
221 void LongPressGestureProcessor::AddGestureDetector(LongPressGestureDetector* gestureDetector, Scene& scene)
223 bool firstRegistration(mLongPressGestureDetectors.empty());
225 mLongPressGestureDetectors.push_back(gestureDetector);
227 if(firstRegistration)
229 mMinTouchesRequired = gestureDetector->GetMinimumTouchesRequired();
230 mMaxTouchesRequired = gestureDetector->GetMaximumTouchesRequired();
232 LongPressGestureRequest request;
233 request.minTouches = mMinTouchesRequired;
234 request.maxTouches = mMaxTouchesRequired;
236 Size size = scene.GetSize();
238 mGestureRecognizer = new LongPressGestureRecognizer(*this, Vector2(size.width, size.height), static_cast<const LongPressGestureRequest&>(request), mMinimumHoldingTime);
246 void LongPressGestureProcessor::RemoveGestureDetector(LongPressGestureDetector* gestureDetector)
249 LongPressGestureDetectorContainer::iterator endIter = std::remove(mLongPressGestureDetectors.begin(), mLongPressGestureDetectors.end(), gestureDetector);
250 DALI_ASSERT_DEBUG(endIter != mLongPressGestureDetectors.end());
253 mLongPressGestureDetectors.erase(endIter, mLongPressGestureDetectors.end());
255 if(mLongPressGestureDetectors.empty())
257 mGestureRecognizer = nullptr;
265 void LongPressGestureProcessor::GestureDetectorUpdated(LongPressGestureDetector* gestureDetector)
267 DALI_ASSERT_DEBUG(find(mLongPressGestureDetectors.begin(), mLongPressGestureDetectors.end(), gestureDetector) != mLongPressGestureDetectors.end());
272 void LongPressGestureProcessor::SetMinimumHoldingTime(uint32_t time)
274 if(time > 0u && mMinimumHoldingTime != time)
276 mMinimumHoldingTime = time;
278 if(mGestureRecognizer)
280 LongPressGestureRecognizer* longPressRecognizer = dynamic_cast<LongPressGestureRecognizer*>(mGestureRecognizer.Get());
281 if(longPressRecognizer)
283 longPressRecognizer->SetMinimumHoldingTime(time);
289 uint32_t LongPressGestureProcessor::GetMinimumHoldingTime() const
291 return mMinimumHoldingTime;
294 void LongPressGestureProcessor::UpdateDetection()
296 DALI_ASSERT_DEBUG(!mLongPressGestureDetectors.empty());
298 unsigned int minimumRequired = UINT_MAX;
299 unsigned int maximumRequired = 0;
301 for(LongPressGestureDetectorContainer::iterator iter = mLongPressGestureDetectors.begin(), endIter = mLongPressGestureDetectors.end(); iter != endIter; ++iter)
303 LongPressGestureDetector* current(*iter);
307 unsigned int minimum = current->GetMinimumTouchesRequired();
308 if(minimum < minimumRequired)
310 minimumRequired = minimum;
313 unsigned int maximum = current->GetMaximumTouchesRequired();
314 if(maximum > maximumRequired)
316 maximumRequired = maximum;
321 if((minimumRequired != mMinTouchesRequired) || (maximumRequired != mMaxTouchesRequired))
323 mMinTouchesRequired = minimumRequired;
324 mMaxTouchesRequired = maximumRequired;
326 LongPressGestureRequest request;
327 request.minTouches = mMinTouchesRequired;
328 request.maxTouches = mMaxTouchesRequired;
329 mGestureRecognizer->Update(request);
333 void LongPressGestureProcessor::OnGesturedActorStageDisconnection()
335 mCurrentEmitters.clear();
338 bool LongPressGestureProcessor::CheckGestureDetector(GestureDetector* detector, Actor* actor)
340 DALI_ASSERT_DEBUG(mCurrentLongPressEvent);
342 LongPressGestureDetector* longPressDetector(static_cast<LongPressGestureDetector*>(detector));
344 return (longPressDetector->GetMinimumTouchesRequired() <= mCurrentLongPressEvent->numberOfTouches) &&
345 (longPressDetector->GetMaximumTouchesRequired() >= mCurrentLongPressEvent->numberOfTouches);
348 void LongPressGestureProcessor::EmitGestureSignal(Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates)
350 DALI_ASSERT_DEBUG(mCurrentLongPressEvent);
352 mCurrentEmitters.clear();
355 EmitLongPressSignal(actor, gestureDetectors, *mCurrentLongPressEvent, actorCoordinates);
359 mCurrentEmitters = gestureDetectors;
364 } // namespace Internal