2 * Copyright (c) 2021 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-recognizer.h>
24 #include <dali/public-api/math/vector2.h>
27 #include <dali/integration-api/events/touch-event-integ.h>
28 #include <dali/integration-api/platform-abstraction.h>
29 #include <dali/internal/event/common/scene-impl.h>
30 #include <dali/internal/event/common/thread-local-storage.h>
31 #include <dali/internal/event/events/gesture-requests.h>
39 // TODO: Set these according to DPI
40 const float MAXIMUM_MOTION_ALLOWED = 20.0f;
41 const unsigned long MAXIMUM_TIME_ALLOWED = 500u;
42 const uint32_t WAIT_TIME = 330u;
43 } // unnamed namespace
45 TapGestureRecognizer::TapGestureRecognizer(Observer& observer, Vector2 screenSize, const TapGestureRequest& request)
46 : GestureRecognizer(screenSize, GestureType::TAP),
49 mMinimumTapsRequired(request.minTaps),
50 mMaximumTapsRequired(request.maxTaps),
56 mGestureSourceType(GestureSourceType::INVALID),
61 TapGestureRecognizer::~TapGestureRecognizer()
63 if(mTimerId != 0 && ThreadLocalStorage::Created())
65 Dali::Integration::PlatformAbstraction& platformAbstraction = ThreadLocalStorage::Get().GetPlatformAbstraction();
66 platformAbstraction.CancelTimer(mTimerId);
70 void TapGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
72 GestureRecognizerPtr ptr(this); // To keep us from being destroyed during the life-time of this method
74 if(event.GetPointCount() == 1)
76 const Integration::Point& point = event.points[0];
77 PointState::Type pointState = point.GetState();
78 Dali::Integration::PlatformAbstraction& platformAbstraction = ThreadLocalStorage::Get().GetPlatformAbstraction();
80 MouseButton::Type mouseButton = point.GetMouseButton();
83 case MouseButton::INVALID:
85 mGestureSourceType = GestureSourceType::INVALID;
88 case MouseButton::PRIMARY:
90 mGestureSourceType = GestureSourceType::PRIMARY;
93 case MouseButton::SECONDARY:
95 mGestureSourceType = GestureSourceType::SECONDARY;
98 case MouseButton::TERTIARY:
100 mGestureSourceType = GestureSourceType::TERTIARY;
105 mGestureSourceType = GestureSourceType::INVALID;
114 if(pointState == PointState::DOWN)
116 SetupForTouchDown(event, point);
123 uint32_t deltaBetweenTouchDownTouchUp = event.time - mTouchTime;
125 if(pointState == PointState::UP)
127 if(deltaBetweenTouchDownTouchUp < MAXIMUM_TIME_ALLOWED)
129 if(mMaximumTapsRequired > mMinimumTapsRequired)
131 mEventTime = event.time;
132 mTimerId = platformAbstraction.StartTimer(WAIT_TIME, MakeCallback(this, &TapGestureRecognizer::TimerCallback));
135 mLastTapTime = mTouchTime;
136 EmitSingleTap(event.time, point);
144 else if(pointState == PointState::INTERRUPTED)
153 if(pointState == PointState::UP)
155 // This is a possible multiple tap, so has it been quick enough?
156 uint32_t timeDelta = event.time - mLastTapTime;
157 uint32_t deltaBetweenTouchDownTouchUp = event.time - mTouchTime;
158 if(timeDelta > MAXIMUM_TIME_ALLOWED) // If exceeded time between taps then just a single tap
160 mLastTapTime = event.time;
161 EmitSingleTap(event.time, point);
163 else if(deltaBetweenTouchDownTouchUp < MAXIMUM_TIME_ALLOWED)
166 if(mMaximumTapsRequired > mMinimumTapsRequired)
168 mEventTime = event.time;
169 mTimerId = platformAbstraction.StartTimer(WAIT_TIME, MakeCallback(this, &TapGestureRecognizer::TimerCallback));
173 EmitGesture(GestureState::STARTED, event.time);
176 else // Delta between touch down and touch up too long to be considered a TAP event
181 else if(pointState == PointState::DOWN)
183 const Vector2& screen(point.GetScreenPosition());
184 Vector2 distanceDelta(std::abs(mTouchPosition.x - screen.x),
185 std::abs(mTouchPosition.y - screen.y));
187 uint32_t timeDelta = event.time - mLastTapTime;
189 if(distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
190 distanceDelta.y > MAXIMUM_MOTION_ALLOWED ||
191 timeDelta > MAXIMUM_TIME_ALLOWED)
193 SetupForTouchDown(event, point);
197 EmitPossibleState(event);
202 platformAbstraction.CancelTimer(mTimerId);
221 // We have entered a multi-touch event so emit registered gestures if required.
222 EmitGesture(GestureState::STARTED, event.time);
226 bool TapGestureRecognizer::TimerCallback()
228 EmitGesture(GestureState::STARTED, mEventTime);
234 void TapGestureRecognizer::SetupForTouchDown(const Integration::TouchEvent& event, const Integration::Point& point)
236 mTouchPosition = point.GetScreenPosition();
237 mTouchTime = event.time;
242 EmitPossibleState(event);
245 void TapGestureRecognizer::EmitPossibleState(const Integration::TouchEvent& event)
247 TapGestureEvent tapEvent(GestureState::POSSIBLE);
248 tapEvent.point = mTouchPosition;
249 tapEvent.time = event.time;
250 tapEvent.gestureSourceType = mGestureSourceType;
252 ProcessEvent(tapEvent);
255 void TapGestureRecognizer::Update(const GestureRequest& request)
257 const TapGestureRequest& tap = static_cast<const TapGestureRequest&>(request);
259 mMinimumTapsRequired = tap.minTaps;
260 mMaximumTapsRequired = tap.maxTaps;
263 void TapGestureRecognizer::EmitGesture(GestureState state, uint32_t time)
265 if((state == GestureState::CANCELLED) ||
266 (mTapsRegistered >= mMinimumTapsRequired && mTapsRegistered <= mMaximumTapsRequired))
269 TapGestureEvent event(state);
270 EmitTap(time, event);
274 void TapGestureRecognizer::EmitSingleTap(uint32_t time, const Integration::Point& point)
276 TapGestureEvent event(GestureState::STARTED);
277 const Vector2& screen(point.GetScreenPosition());
278 Vector2 distanceDelta(std::abs(mTouchPosition.x - screen.x),
279 std::abs(mTouchPosition.y - screen.y));
281 mTapsRegistered = 1u;
282 if(distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
283 distanceDelta.y > MAXIMUM_MOTION_ALLOWED)
285 event.state = GestureState::CANCELLED;
288 Dali::Integration::PlatformAbstraction& platformAbstraction = ThreadLocalStorage::Get().GetPlatformAbstraction();
289 platformAbstraction.CancelTimer(mTimerId);
295 EmitTap(time, event);
299 void TapGestureRecognizer::EmitTap(uint32_t time, TapGestureEvent& event)
301 event.numberOfTaps = mTapsRegistered;
302 event.point = mTouchPosition;
304 event.gestureSourceType = mGestureSourceType;
309 void TapGestureRecognizer::ProcessEvent(TapGestureEvent& event)
313 // Create another handle so the recognizer cannot be destroyed during process function
314 GestureRecognizerPtr recognizerHandle = this;
315 mObserver.Process(*mScene, event);
319 } // namespace Internal