2 * Copyright (c) 2022 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>
26 #include <dali/integration-api/events/touch-event-integ.h>
29 #include <dali/internal/event/common/scene-impl.h>
30 #include <dali/internal/event/events/gesture-requests.h>
38 // TODO: Set these according to DPI
39 constexpr float MAXIMUM_MOTION_ALLOWED = 20.0f;
40 } // unnamed namespace
42 TapGestureRecognizer::TapGestureRecognizer(Observer& observer, Vector2 screenSize, const TapGestureRequest& request, uint32_t maximumAllowedTime)
43 : GestureRecognizer(screenSize, GestureType::TAP),
46 mMinimumTapsRequired(request.minTaps),
47 mMaximumTapsRequired(request.maxTaps),
52 mGestureSourceType(GestureSourceType::INVALID),
53 mMaximumAllowedTime(maximumAllowedTime)
57 TapGestureRecognizer::~TapGestureRecognizer() = default;
59 void TapGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
61 GestureRecognizerPtr ptr(this); // To keep us from being destroyed during the life-time of this method
63 if(event.GetPointCount() == 1)
65 const Integration::Point& point = event.points[0];
66 PointState::Type pointState = point.GetState();
68 MouseButton::Type mouseButton = point.GetMouseButton();
71 case MouseButton::INVALID:
73 mGestureSourceType = GestureSourceType::INVALID;
76 case MouseButton::PRIMARY:
78 mGestureSourceType = GestureSourceType::PRIMARY;
81 case MouseButton::SECONDARY:
83 mGestureSourceType = GestureSourceType::SECONDARY;
86 case MouseButton::TERTIARY:
88 mGestureSourceType = GestureSourceType::TERTIARY;
93 mGestureSourceType = GestureSourceType::INVALID;
102 if(pointState == PointState::DOWN)
104 SetupForTouchDown(event, point);
111 uint32_t deltaBetweenTouchDownTouchUp = event.time - mTouchTime;
113 if(pointState == PointState::UP)
115 if(deltaBetweenTouchDownTouchUp < mMaximumAllowedTime)
117 mLastTapTime = mTouchTime;
118 EmitSingleTap(event.time, point);
126 else if(pointState == PointState::INTERRUPTED)
135 if(pointState == PointState::UP)
137 // This is a possible multiple tap, so has it been quick enough?
138 uint32_t timeDelta = event.time - mLastTapTime;
139 uint32_t deltaBetweenTouchDownTouchUp = event.time - mTouchTime;
140 if(timeDelta > mMaximumAllowedTime || // If exceeded time between taps then just a single tap
141 mMaximumTapsRequired == 1u) // If MaximumTapsRequired is 1, it is not waiting for a multi-tap, so a Tap gesture send a single tap.
143 mLastTapTime = event.time;
144 EmitSingleTap(event.time, point);
146 else if(deltaBetweenTouchDownTouchUp < mMaximumAllowedTime)
149 EmitGesture(GestureState::STARTED, event.time);
151 else // Delta between touch down and touch up too long to be considered a TAP event
156 else if(pointState == PointState::DOWN)
158 const Vector2& screen(point.GetScreenPosition());
159 Vector2 distanceDelta(std::abs(mTouchPosition.x - screen.x),
160 std::abs(mTouchPosition.y - screen.y));
162 uint32_t timeDelta = event.time - mLastTapTime;
164 if(distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
165 distanceDelta.y > MAXIMUM_MOTION_ALLOWED ||
166 timeDelta > mMaximumAllowedTime)
168 SetupForTouchDown(event, point);
172 EmitPossibleState(event);
190 // We have entered a multi-touch event so emit registered gestures if required.
191 EmitGesture(GestureState::STARTED, event.time);
195 void TapGestureRecognizer::SetupForTouchDown(const Integration::TouchEvent& event, const Integration::Point& point)
197 mTouchPosition = point.GetScreenPosition();
198 mTouchTime = event.time;
203 EmitPossibleState(event);
206 void TapGestureRecognizer::EmitPossibleState(const Integration::TouchEvent& event)
208 TapGestureEvent tapEvent(GestureState::POSSIBLE);
209 tapEvent.point = mTouchPosition;
210 tapEvent.time = event.time;
211 tapEvent.gestureSourceType = mGestureSourceType;
213 ProcessEvent(tapEvent);
216 void TapGestureRecognizer::Update(const GestureRequest& request)
218 const TapGestureRequest& tap = static_cast<const TapGestureRequest&>(request);
220 mMinimumTapsRequired = tap.minTaps;
221 mMaximumTapsRequired = tap.maxTaps;
224 void TapGestureRecognizer::SetMaximumAllowedTime(uint32_t time)
226 mMaximumAllowedTime = time;
229 void TapGestureRecognizer::EmitGesture(GestureState state, uint32_t time)
231 if((state == GestureState::CANCELLED) ||
232 (mTapsRegistered >= mMinimumTapsRequired && mTapsRegistered <= mMaximumTapsRequired))
235 TapGestureEvent event(state);
236 EmitTap(time, event);
240 void TapGestureRecognizer::EmitSingleTap(uint32_t time, const Integration::Point& point)
242 TapGestureEvent event(GestureState::STARTED);
243 const Vector2& screen(point.GetScreenPosition());
244 Vector2 distanceDelta(std::abs(mTouchPosition.x - screen.x),
245 std::abs(mTouchPosition.y - screen.y));
247 if(distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
248 distanceDelta.y > MAXIMUM_MOTION_ALLOWED)
250 event.state = GestureState::CANCELLED;
252 mTapsRegistered = 1u;
253 EmitTap(time, event);
256 void TapGestureRecognizer::EmitTap(uint32_t time, TapGestureEvent& event)
258 event.numberOfTaps = mTapsRegistered;
259 event.point = mTouchPosition;
261 event.gestureSourceType = mGestureSourceType;
266 void TapGestureRecognizer::ProcessEvent(TapGestureEvent& event)
270 // Create another handle so the recognizer cannot be destroyed during process function
271 GestureRecognizerPtr recognizerHandle = this;
272 mObserver.Process(*mScene, event);
276 } // namespace Internal