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);
72 if(gTraceFilter && gTraceFilter->IsTraceEnabled())
74 std::ostringstream stream;
75 stream << "[" << gestureDetectors.size() << "]";
76 DALI_TRACE_BEGIN_WITH_MESSAGE(gTraceFilter, "DALI_EMIT_TAP_GESTURE_SIGNAL", stream.str().c_str());
80 Dali::Actor actorHandle(actor);
81 const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
82 for(GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter)
84 static_cast<TapGestureDetector*>(*iter)->EmitTapGestureSignal(actorHandle, Dali::TapGesture(tap.Get()));
88 if(gTraceFilter && gTraceFilter->IsTraceEnabled())
90 std::ostringstream stream;
91 stream << "[" << gestureDetectors.size() << "]";
92 DALI_TRACE_END_WITH_MESSAGE(gTraceFilter, "DALI_EMIT_TAP_GESTURE_SIGNAL", stream.str().c_str());
97 } // unnamed namespace
99 TapGestureProcessor::TapGestureProcessor()
100 : GestureProcessor(GestureType::TAP),
101 mTapGestureDetectors(),
102 mMinTouchesRequired(1),
103 mMaxTouchesRequired(1),
104 mCurrentTapEvent(nullptr),
105 mPossibleProcessed(false),
106 mMaximumAllowedTime(DEFAULT_MAXIMUM_ALLOWED_TIME),
107 mRecognizerTime(DEFAULT_RECOGNIZER_TIME)
111 TapGestureProcessor::~TapGestureProcessor() = default;
113 void TapGestureProcessor::Process(Scene& scene, const TapGestureEvent& tapEvent)
115 DALI_TRACE_SCOPE(gTraceFilter, "DALI_PROCESS_TAP_GESTURE");
116 switch(tapEvent.state)
118 case GestureState::POSSIBLE:
120 // 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 )
121 HitTestAlgorithm::Results hitTestResults;
122 if(HitTest(scene, tapEvent.point, hitTestResults))
124 SetActor(&GetImplementation(hitTestResults.actor));
125 mCurrentTapActor.SetActor(GetCurrentGesturedActor());
127 // Indicate that we've processed a touch down. Bool should be sufficient as a change in actor will result in a cancellation
128 mPossibleProcessed = true;
137 case GestureState::STARTED:
139 // Ensure that we're processing a hit on the current actor and that we've already processed a touch down
140 HitTestAlgorithm::Results hitTestResults;
141 if(GetCurrentGesturedActor() && HitTest(scene, tapEvent.point, hitTestResults) && mPossibleProcessed)
143 // Check that this actor is still the one that was used for the last touch down ?
144 if(mCurrentTapActor.GetActor() == &GetImplementation(hitTestResults.actor))
146 mCurrentTapEvent = &tapEvent;
147 ProcessAndEmit(hitTestResults);
149 mCurrentTapEvent = nullptr;
150 mPossibleProcessed = false;
155 case GestureState::CANCELLED:
157 mPossibleProcessed = false;
162 case GestureState::CONTINUING:
164 DALI_ABORT("Incorrect state received from Integration layer: CONTINUING\n");
167 case GestureState::FINISHED:
169 DALI_ABORT("Incorrect state received from Integration layer: FINISHED\n");
172 case GestureState::CLEAR:
174 DALI_ABORT("Incorrect state received from Integration layer: CLEAR\n");
180 void TapGestureProcessor::AddGestureDetector(TapGestureDetector* gestureDetector, Scene& scene)
182 bool firstRegistration(mTapGestureDetectors.empty());
184 mTapGestureDetectors.push_back(gestureDetector);
186 const unsigned int touchesRequired = gestureDetector->GetTouchesRequired();
188 if(firstRegistration)
190 // If this is the first tap gesture detector that has been added, then our minimum and maximum
191 // requirements are the same as each other.
193 mMinTouchesRequired = mMaxTouchesRequired = touchesRequired;
195 TapGestureRequest request;
196 request.minTouches = mMinTouchesRequired;
197 request.maxTouches = mMaxTouchesRequired;
199 Size size = scene.GetSize();
200 mGestureRecognizer = new TapGestureRecognizer(*this, Vector2(size.width, size.height), static_cast<const TapGestureRequest&>(request), mMaximumAllowedTime, mRecognizerTime);
204 // If we have already registered for tap gesture detection before then we need to check our
205 // minimum and maximums and see if our gesture detection requirements have changed, if they
206 // have, then we should ask the adaptor to update its detection policy.
208 // This is quicker than calling UpdateDetection as there is no need to iterate through the container
210 unsigned int minTouches = mMinTouchesRequired < touchesRequired ? mMinTouchesRequired : touchesRequired;
211 unsigned int maxTouches = mMaxTouchesRequired > touchesRequired ? mMaxTouchesRequired : touchesRequired;
213 if((minTouches != mMinTouchesRequired) || (maxTouches != mMaxTouchesRequired))
215 TapGestureRequest request;
216 request.minTouches = mMinTouchesRequired = minTouches;
217 request.maxTouches = mMaxTouchesRequired = maxTouches;
219 mGestureRecognizer->Update(request);
224 void TapGestureProcessor::RemoveGestureDetector(TapGestureDetector* gestureDetector)
227 TapGestureDetectorContainer::iterator endIter = std::remove(mTapGestureDetectors.begin(), mTapGestureDetectors.end(), gestureDetector);
228 DALI_ASSERT_DEBUG(endIter != mTapGestureDetectors.end());
231 mTapGestureDetectors.erase(endIter, mTapGestureDetectors.end());
233 if(mTapGestureDetectors.empty())
235 mGestureRecognizer = nullptr;
245 void TapGestureProcessor::GestureDetectorUpdated(TapGestureDetector* gestureDetector)
250 void TapGestureProcessor::SetMaximumAllowedTime(uint32_t time)
254 DALI_LOG_WARNING("MaximumAllowedTime must be greater than zero.");
257 if(mMaximumAllowedTime != time)
259 mMaximumAllowedTime = time;
261 if(mGestureRecognizer)
263 TapGestureRecognizer* tapRecognizer = dynamic_cast<TapGestureRecognizer*>(mGestureRecognizer.Get());
266 tapRecognizer->SetMaximumAllowedTime(time);
272 uint32_t TapGestureProcessor::GetMaximumAllowedTime() const
274 return mMaximumAllowedTime;
277 void TapGestureProcessor::SetRecognizerTime(uint32_t time)
281 DALI_LOG_WARNING("RecognizerTime must be greater than zero.");
284 if(mRecognizerTime != time)
286 mRecognizerTime = time;
288 if(mGestureRecognizer)
290 TapGestureRecognizer* tapRecognizer = dynamic_cast<TapGestureRecognizer*>(mGestureRecognizer.Get());
293 tapRecognizer->SetRecognizerTime(time);
299 void TapGestureProcessor::UpdateDetection()
301 DALI_ASSERT_DEBUG(!mTapGestureDetectors.empty());
303 unsigned int minTouches = UINT_MAX;
304 unsigned int maxTouches = 0;
306 for(TapGestureDetectorContainer::iterator iter = mTapGestureDetectors.begin(), endIter = mTapGestureDetectors.end(); iter != endIter; ++iter)
308 TapGestureDetector* detector(*iter);
312 const unsigned int touchesRequired = detector->GetTouchesRequired();
314 minTouches = touchesRequired < minTouches ? touchesRequired : minTouches;
315 maxTouches = touchesRequired > maxTouches ? touchesRequired : maxTouches;
319 if((minTouches != mMinTouchesRequired) || (maxTouches != mMaxTouchesRequired))
321 TapGestureRequest request;
322 request.minTouches = mMinTouchesRequired = minTouches;
323 request.maxTouches = mMaxTouchesRequired = maxTouches;
325 mGestureRecognizer->Update(request);
329 bool TapGestureProcessor::CheckGestureDetector(GestureDetector* detector, Actor* actor)
331 DALI_ASSERT_DEBUG(mCurrentTapEvent);
333 TapGestureDetector* tapDetector(static_cast<TapGestureDetector*>(detector));
335 return (tapDetector->GetMinimumTapsRequired() <= mCurrentTapEvent->numberOfTaps) && (tapDetector->GetTouchesRequired() == mCurrentTapEvent->numberOfTouches);
338 void TapGestureProcessor::EmitGestureSignal(Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates)
340 DALI_ASSERT_DEBUG(mCurrentTapEvent);
342 EmitTapSignal(actor, gestureDetectors, *mCurrentTapEvent, actorCoordinates);
345 } // namespace Internal