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>
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 const float MAXIMUM_MOTION_ALLOWED = 20.0f;
40 const unsigned long MAXIMUM_TIME_ALLOWED = 500u;
41 } // unnamed namespace
43 TapGestureRecognizer::TapGestureRecognizer(Observer& observer, Vector2 screenSize, const TapGestureRequest& request)
44 : GestureRecognizer(screenSize, GestureType::TAP),
47 mMinimumTapsRequired(request.minTaps),
48 mMaximumTapsRequired(request.maxTaps),
53 mGestureSourceType(GestureSourceType::INVALID)
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 < MAXIMUM_TIME_ALLOWED)
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 > MAXIMUM_TIME_ALLOWED) // If exceeded time between taps then just a single tap
142 mLastTapTime = event.time;
143 EmitSingleTap(event.time, point);
145 else if(deltaBetweenTouchDownTouchUp < MAXIMUM_TIME_ALLOWED)
148 EmitGesture(GestureState::STARTED, event.time);
150 else // Delta between touch down and touch up too long to be considered a TAP event
155 else if(pointState == PointState::DOWN)
157 const Vector2& screen(point.GetScreenPosition());
158 Vector2 distanceDelta(std::abs(mTouchPosition.x - screen.x),
159 std::abs(mTouchPosition.y - screen.y));
161 uint32_t timeDelta = event.time - mLastTapTime;
163 if(distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
164 distanceDelta.y > MAXIMUM_MOTION_ALLOWED ||
165 timeDelta > MAXIMUM_TIME_ALLOWED)
167 SetupForTouchDown(event, point);
171 EmitPossibleState(event);
189 // We have entered a multi-touch event so emit registered gestures if required.
190 EmitGesture(GestureState::STARTED, event.time);
194 void TapGestureRecognizer::SetupForTouchDown(const Integration::TouchEvent& event, const Integration::Point& point)
196 mTouchPosition = point.GetScreenPosition();
197 mTouchTime = event.time;
202 EmitPossibleState(event);
205 void TapGestureRecognizer::EmitPossibleState(const Integration::TouchEvent& event)
207 TapGestureEvent tapEvent(GestureState::POSSIBLE);
208 tapEvent.point = mTouchPosition;
209 tapEvent.time = event.time;
210 tapEvent.gestureSourceType = mGestureSourceType;
212 ProcessEvent(tapEvent);
215 void TapGestureRecognizer::Update(const GestureRequest& request)
217 const TapGestureRequest& tap = static_cast<const TapGestureRequest&>(request);
219 mMinimumTapsRequired = tap.minTaps;
220 mMaximumTapsRequired = tap.maxTaps;
223 void TapGestureRecognizer::EmitGesture(GestureState state, uint32_t time)
225 if((state == GestureState::CANCELLED) ||
226 (mTapsRegistered >= mMinimumTapsRequired && mTapsRegistered <= mMaximumTapsRequired))
229 TapGestureEvent event(state);
230 EmitTap(time, event);
234 void TapGestureRecognizer::EmitSingleTap(uint32_t time, const Integration::Point& point)
236 TapGestureEvent event(GestureState::STARTED);
237 const Vector2& screen(point.GetScreenPosition());
238 Vector2 distanceDelta(std::abs(mTouchPosition.x - screen.x),
239 std::abs(mTouchPosition.y - screen.y));
241 if(distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
242 distanceDelta.y > MAXIMUM_MOTION_ALLOWED)
244 event.state = GestureState::CANCELLED;
246 mTapsRegistered = 1u;
247 EmitTap(time, event);
250 void TapGestureRecognizer::EmitTap(uint32_t time, TapGestureEvent& event)
252 event.numberOfTaps = mTapsRegistered;
253 event.point = mTouchPosition;
255 event.gestureSourceType = mGestureSourceType;
260 void TapGestureRecognizer::ProcessEvent(TapGestureEvent& event)
264 // Create another handle so the recognizer cannot be destroyed during process function
265 GestureRecognizerPtr recognizerHandle = this;
266 mObserver.Process(*mScene, event);
270 } // namespace Internal