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),
56 TapGestureRecognizer::~TapGestureRecognizer() = default;
58 void TapGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
60 GestureRecognizerPtr ptr(this); // To keep us from being destroyed during the life-time of this method
62 if(event.GetPointCount() == 1)
64 const Integration::Point& point = event.points[0];
65 PointState::Type pointState = point.GetState();
71 if(pointState == PointState::DOWN)
73 SetupForTouchDown(event, point);
80 uint32_t deltaBetweenTouchDownTouchUp = event.time - mTouchTime;
82 if(pointState == PointState::UP)
84 if(deltaBetweenTouchDownTouchUp < MAXIMUM_TIME_ALLOWED)
86 mLastTapTime = mTouchTime;
87 EmitSingleTap(event.time, point);
95 else if(pointState == PointState::INTERRUPTED)
104 if(pointState == PointState::UP)
106 // This is a possible multiple tap, so has it been quick enough?
107 uint32_t timeDelta = event.time - mLastTapTime;
108 uint32_t deltaBetweenTouchDownTouchUp = event.time - mTouchTime;
109 if(timeDelta > MAXIMUM_TIME_ALLOWED) // If exceeded time between taps then just a single tap
111 mLastTapTime = event.time;
112 EmitSingleTap(event.time, point);
114 else if(deltaBetweenTouchDownTouchUp < MAXIMUM_TIME_ALLOWED)
117 EmitGesture(GestureState::STARTED, event.time);
119 else // Delta between touch down and touch up too long to be considered a TAP event
124 else if(pointState == PointState::DOWN)
126 const Vector2& screen(point.GetScreenPosition());
127 Vector2 distanceDelta(std::abs(mTouchPosition.x - screen.x),
128 std::abs(mTouchPosition.y - screen.y));
130 uint32_t timeDelta = event.time - mLastTapTime;
132 if(distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
133 distanceDelta.y > MAXIMUM_MOTION_ALLOWED ||
134 timeDelta > MAXIMUM_TIME_ALLOWED)
136 SetupForTouchDown(event, point);
140 EmitPossibleState(event);
158 // We have entered a multi-touch event so emit registered gestures if required.
159 EmitGesture(GestureState::STARTED, event.time);
163 void TapGestureRecognizer::SetupForTouchDown(const Integration::TouchEvent& event, const Integration::Point& point)
165 mTouchPosition = point.GetScreenPosition();
166 mTouchTime = event.time;
170 EmitPossibleState(event);
173 void TapGestureRecognizer::EmitPossibleState(const Integration::TouchEvent& event)
175 TapGestureEvent tapEvent(GestureState::POSSIBLE);
176 tapEvent.point = mTouchPosition;
177 tapEvent.time = event.time;
179 ProcessEvent(tapEvent);
182 void TapGestureRecognizer::Update(const GestureRequest& request)
184 const TapGestureRequest& tap = static_cast<const TapGestureRequest&>(request);
186 mMinimumTapsRequired = tap.minTaps;
187 mMaximumTapsRequired = tap.maxTaps;
190 void TapGestureRecognizer::EmitGesture(GestureState state, uint32_t time)
192 if((state == GestureState::CANCELLED) ||
193 (mTapsRegistered >= mMinimumTapsRequired && mTapsRegistered <= mMaximumTapsRequired))
196 TapGestureEvent event(state);
197 EmitTap(time, event);
201 void TapGestureRecognizer::EmitSingleTap(uint32_t time, const Integration::Point& point)
203 TapGestureEvent event(GestureState::STARTED);
204 const Vector2& screen(point.GetScreenPosition());
205 Vector2 distanceDelta(std::abs(mTouchPosition.x - screen.x),
206 std::abs(mTouchPosition.y - screen.y));
208 if(distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
209 distanceDelta.y > MAXIMUM_MOTION_ALLOWED)
211 event.state = GestureState::CANCELLED;
213 mTapsRegistered = 1u;
214 EmitTap(time, event);
217 void TapGestureRecognizer::EmitTap(uint32_t time, TapGestureEvent& event)
219 event.numberOfTaps = mTapsRegistered;
220 event.point = mTouchPosition;
226 void TapGestureRecognizer::ProcessEvent(TapGestureEvent& event)
230 // Create another handle so the recognizer cannot be destroyed during process function
231 GestureRecognizerPtr recognizerHandle = this;
232 mObserver.Process(*mScene, event);
236 } // namespace Internal