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),
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();
72 if(pointState == PointState::DOWN)
74 SetupForTouchDown(event, point);
75 mLastTouchTime = event.time;
82 uint32_t deltaBetweenTouchDownTouchUp = event.time - mTouchTime;
84 if(pointState == PointState::UP)
86 if(deltaBetweenTouchDownTouchUp < mMaximumAllowedTime)
88 mLastTapTime = event.time;
89 EmitSingleTap(event.time, point);
97 else if(pointState == PointState::INTERRUPTED)
106 if(pointState == PointState::UP)
108 uint32_t deltaBetweenLastTouchDownTouchUp = event.time - mLastTouchTime;
109 // Clear if the time between touch down and touch up is long.
110 if(deltaBetweenLastTouchDownTouchUp > mMaximumAllowedTime)
116 mLastTapTime = event.time;
117 EmitGesture(GestureState::STARTED, event.time);
120 else if(pointState == PointState::DOWN)
122 const Vector2& screen(point.GetScreenPosition());
123 Vector2 distanceDelta(std::abs(mTouchPosition.x - screen.x),
124 std::abs(mTouchPosition.y - screen.y));
126 uint32_t deltaBetweenInitTouchDownCurrentTouchDown = event.time - mTouchTime;
127 uint32_t timeDelta = event.time - mLastTapTime;
128 mLastTouchTime = event.time;
131 if(distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
132 distanceDelta.y > MAXIMUM_MOTION_ALLOWED ||
133 timeDelta > mMaximumAllowedTime || // If the time between tabs is long, it starts over from SetupForTouchDown.
134 deltaBetweenInitTouchDownCurrentTouchDown > mMaximumAllowedTime || // If it times out compared to the first touchdown time, it starts over from SetupForTouchDown.
135 mTapsRegistered > mMaximumTapsRequired) // If it is greater than MaximumTapsRequired, it starts over from SetupForTouchDown.
137 SetupForTouchDown(event, point);
141 EmitPossibleState(event);
159 // We have entered a multi-touch event so emit registered gestures if required.
160 EmitGesture(GestureState::STARTED, event.time);
164 void TapGestureRecognizer::SetupForTouchDown(const Integration::TouchEvent& event, const Integration::Point& point)
166 mTouchPosition = point.GetScreenPosition();
167 mTouchTime = event.time;
172 EmitPossibleState(event);
175 void TapGestureRecognizer::EmitPossibleState(const Integration::TouchEvent& event)
177 TapGestureEvent tapEvent(GestureState::POSSIBLE);
178 tapEvent.point = mTouchPosition;
179 tapEvent.time = event.time;
181 ProcessEvent(tapEvent);
184 void TapGestureRecognizer::Update(const GestureRequest& request)
186 const TapGestureRequest& tap = static_cast<const TapGestureRequest&>(request);
188 mMinimumTapsRequired = tap.minTaps;
189 mMaximumTapsRequired = tap.maxTaps;
192 void TapGestureRecognizer::SetMaximumAllowedTime(uint32_t time)
194 mMaximumAllowedTime = time;
197 void TapGestureRecognizer::EmitGesture(GestureState state, uint32_t time)
199 if((state == GestureState::CANCELLED) ||
200 (mTapsRegistered >= mMinimumTapsRequired && mTapsRegistered <= mMaximumTapsRequired))
203 TapGestureEvent event(state);
204 EmitTap(time, event);
208 void TapGestureRecognizer::EmitSingleTap(uint32_t time, const Integration::Point& point)
210 TapGestureEvent event(GestureState::STARTED);
211 const Vector2& screen(point.GetScreenPosition());
212 Vector2 distanceDelta(std::abs(mTouchPosition.x - screen.x),
213 std::abs(mTouchPosition.y - screen.y));
215 if(distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
216 distanceDelta.y > MAXIMUM_MOTION_ALLOWED)
218 event.state = GestureState::CANCELLED;
220 mTapsRegistered = 1u;
221 EmitTap(time, event);
224 void TapGestureRecognizer::EmitTap(uint32_t time, TapGestureEvent& event)
226 event.numberOfTaps = mTapsRegistered;
227 event.point = mTouchPosition;
233 void TapGestureRecognizer::ProcessEvent(TapGestureEvent& event)
235 event.sourceType = mSourceType;
236 event.sourceData = mSourceData;
239 // Create another handle so the recognizer cannot be destroyed during process function
240 GestureRecognizerPtr recognizerHandle = this;
241 mObserver.Process(*mScene, event);
245 } // namespace Internal