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/tap-gesture/tap-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/tap-gesture/tap-gesture-event.h>
31 #include <dali/internal/event/events/tap-gesture/tap-gesture-impl.h>
32 #include <dali/internal/event/events/tap-gesture/tap-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>
36 #include <dali/public-api/events/tap-gesture.h>
37 #include <dali/public-api/math/vector2.h>
45 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_PERFORMANCE_MARKER, false);
46 constexpr uint32_t DEFAULT_MAXIMUM_ALLOWED_TIME = 330u;
47 constexpr uint32_t DEFAULT_RECOGNIZER_TIME = 330u;
50 * Creates a TapGesture and asks the specified detector to emit its detected signal.
51 * @param[in] actor The actor on which a tap has occurred.
52 * @param[in] gestureDetectors A reference to gesture detectors that should emit the signal.
53 * @param[in] tapEvent The tapEvent received from the adaptor.
54 * @param[in] localPoint Relative to the actor attached to the detector.
58 const GestureDetectorContainer& gestureDetectors,
59 const TapGestureEvent& tapEvent,
62 Internal::TapGesturePtr tap(new Internal::TapGesture(tapEvent.state));
63 tap->SetTime(tapEvent.time);
64 tap->SetNumberOfTaps(tapEvent.numberOfTaps);
65 tap->SetNumberOfTouches(tapEvent.numberOfTouches);
66 tap->SetScreenPoint(tapEvent.point);
67 tap->SetLocalPoint(localPoint);
68 tap->SetSourceType(tapEvent.sourceType);
69 tap->SetSourceData(tapEvent.sourceData);
71 DALI_TRACE_BEGIN_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_EMIT_TAP_GESTURE_SIGNAL", [&](std::ostringstream& oss) {
72 oss << "[" << gestureDetectors.size() << "]";
75 Dali::Actor actorHandle(actor);
76 const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
77 for(GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter)
79 static_cast<TapGestureDetector*>(*iter)->EmitTapGestureSignal(actorHandle, Dali::TapGesture(tap.Get()));
82 DALI_TRACE_END_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_EMIT_TAP_GESTURE_SIGNAL", [&](std::ostringstream& oss) {
83 oss << "[" << gestureDetectors.size() << "]";
87 } // unnamed namespace
89 TapGestureProcessor::TapGestureProcessor()
90 : GestureProcessor(GestureType::TAP),
91 mTapGestureDetectors(),
92 mMinTouchesRequired(1),
93 mMaxTouchesRequired(1),
94 mCurrentTapEvent(nullptr),
95 mPossibleProcessed(false),
96 mMaximumAllowedTime(DEFAULT_MAXIMUM_ALLOWED_TIME),
97 mRecognizerTime(DEFAULT_RECOGNIZER_TIME)
101 TapGestureProcessor::~TapGestureProcessor() = default;
103 void TapGestureProcessor::Process(Scene& scene, const TapGestureEvent& tapEvent)
105 DALI_TRACE_SCOPE(gTraceFilter, "DALI_PROCESS_TAP_GESTURE");
106 switch(tapEvent.state)
108 case GestureState::POSSIBLE:
110 // Do a hit test and if an actor has been hit then save to see if tap event is still valid on a tap( same actor being hit )
111 HitTestAlgorithm::Results hitTestResults;
112 if(HitTest(scene, tapEvent.point, hitTestResults))
114 SetActor(&GetImplementation(hitTestResults.actor));
115 mCurrentTapActor.SetActor(GetCurrentGesturedActor());
117 // Indicate that we've processed a touch down. Bool should be sufficient as a change in actor will result in a cancellation
118 mPossibleProcessed = true;
127 case GestureState::STARTED:
129 // Ensure that we're processing a hit on the current actor and that we've already processed a touch down
130 HitTestAlgorithm::Results hitTestResults;
131 if(GetCurrentGesturedActor() && HitTest(scene, tapEvent.point, hitTestResults) && mPossibleProcessed)
133 // Check that this actor is still the one that was used for the last touch down ?
134 if(mCurrentTapActor.GetActor() == &GetImplementation(hitTestResults.actor))
136 mCurrentTapEvent = &tapEvent;
137 ProcessAndEmit(hitTestResults);
139 mCurrentTapEvent = nullptr;
140 mPossibleProcessed = false;
145 case GestureState::CANCELLED:
147 mPossibleProcessed = false;
152 case GestureState::CONTINUING:
154 DALI_ABORT("Incorrect state received from Integration layer: CONTINUING\n");
157 case GestureState::FINISHED:
159 DALI_ABORT("Incorrect state received from Integration layer: FINISHED\n");
162 case GestureState::CLEAR:
164 DALI_ABORT("Incorrect state received from Integration layer: CLEAR\n");
170 void TapGestureProcessor::AddGestureDetector(TapGestureDetector* gestureDetector, Scene& scene)
172 bool firstRegistration(mTapGestureDetectors.empty());
174 mTapGestureDetectors.push_back(gestureDetector);
176 const unsigned int touchesRequired = gestureDetector->GetTouchesRequired();
178 if(firstRegistration)
180 // If this is the first tap gesture detector that has been added, then our minimum and maximum
181 // requirements are the same as each other.
183 mMinTouchesRequired = mMaxTouchesRequired = touchesRequired;
185 TapGestureRequest request;
186 request.minTouches = mMinTouchesRequired;
187 request.maxTouches = mMaxTouchesRequired;
189 Size size = scene.GetSize();
190 mGestureRecognizer = new TapGestureRecognizer(*this, Vector2(size.width, size.height), static_cast<const TapGestureRequest&>(request), mMaximumAllowedTime, mRecognizerTime);
194 // If we have already registered for tap gesture detection before then we need to check our
195 // minimum and maximums and see if our gesture detection requirements have changed, if they
196 // have, then we should ask the adaptor to update its detection policy.
198 // This is quicker than calling UpdateDetection as there is no need to iterate through the container
200 unsigned int minTouches = mMinTouchesRequired < touchesRequired ? mMinTouchesRequired : touchesRequired;
201 unsigned int maxTouches = mMaxTouchesRequired > touchesRequired ? mMaxTouchesRequired : touchesRequired;
203 if((minTouches != mMinTouchesRequired) || (maxTouches != mMaxTouchesRequired))
205 TapGestureRequest request;
206 request.minTouches = mMinTouchesRequired = minTouches;
207 request.maxTouches = mMaxTouchesRequired = maxTouches;
209 mGestureRecognizer->Update(request);
214 void TapGestureProcessor::RemoveGestureDetector(TapGestureDetector* gestureDetector)
217 TapGestureDetectorContainer::iterator endIter = std::remove(mTapGestureDetectors.begin(), mTapGestureDetectors.end(), gestureDetector);
218 DALI_ASSERT_DEBUG(endIter != mTapGestureDetectors.end());
221 mTapGestureDetectors.erase(endIter, mTapGestureDetectors.end());
223 if(mTapGestureDetectors.empty())
225 mGestureRecognizer = nullptr;
235 void TapGestureProcessor::GestureDetectorUpdated(TapGestureDetector* gestureDetector)
240 void TapGestureProcessor::SetMaximumAllowedTime(uint32_t time)
244 DALI_LOG_WARNING("MaximumAllowedTime must be greater than zero.");
247 if(mMaximumAllowedTime != time)
249 mMaximumAllowedTime = time;
251 if(mGestureRecognizer)
253 TapGestureRecognizer* tapRecognizer = dynamic_cast<TapGestureRecognizer*>(mGestureRecognizer.Get());
256 tapRecognizer->SetMaximumAllowedTime(time);
262 uint32_t TapGestureProcessor::GetMaximumAllowedTime() const
264 return mMaximumAllowedTime;
267 void TapGestureProcessor::SetRecognizerTime(uint32_t time)
271 DALI_LOG_WARNING("RecognizerTime must be greater than zero.");
274 if(mRecognizerTime != time)
276 mRecognizerTime = time;
278 if(mGestureRecognizer)
280 TapGestureRecognizer* tapRecognizer = dynamic_cast<TapGestureRecognizer*>(mGestureRecognizer.Get());
283 tapRecognizer->SetRecognizerTime(time);
289 void TapGestureProcessor::UpdateDetection()
291 DALI_ASSERT_DEBUG(!mTapGestureDetectors.empty());
293 unsigned int minTouches = UINT_MAX;
294 unsigned int maxTouches = 0;
296 for(TapGestureDetectorContainer::iterator iter = mTapGestureDetectors.begin(), endIter = mTapGestureDetectors.end(); iter != endIter; ++iter)
298 TapGestureDetector* detector(*iter);
302 const unsigned int touchesRequired = detector->GetTouchesRequired();
304 minTouches = touchesRequired < minTouches ? touchesRequired : minTouches;
305 maxTouches = touchesRequired > maxTouches ? touchesRequired : maxTouches;
309 if((minTouches != mMinTouchesRequired) || (maxTouches != mMaxTouchesRequired))
311 TapGestureRequest request;
312 request.minTouches = mMinTouchesRequired = minTouches;
313 request.maxTouches = mMaxTouchesRequired = maxTouches;
315 mGestureRecognizer->Update(request);
319 bool TapGestureProcessor::CheckGestureDetector(GestureDetector* detector, Actor* actor)
321 DALI_ASSERT_DEBUG(mCurrentTapEvent);
323 TapGestureDetector* tapDetector(static_cast<TapGestureDetector*>(detector));
325 return (tapDetector->GetMinimumTapsRequired() <= mCurrentTapEvent->numberOfTaps) && (tapDetector->GetTouchesRequired() == mCurrentTapEvent->numberOfTouches);
328 void TapGestureProcessor::EmitGestureSignal(Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates)
330 DALI_ASSERT_DEBUG(mCurrentTapEvent);
332 EmitTapSignal(actor, gestureDetectors, *mCurrentTapEvent, actorCoordinates);
335 } // namespace Internal