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/pan-gesture/pan-gesture-recognizer.h>
25 #include <dali/devel-api/events/touch-point.h>
27 #include <dali/integration-api/events/touch-event-integ.h>
30 #include <dali/internal/event/common/scene-impl.h>
31 #include <dali/internal/event/events/gesture-requests.h>
32 #include <dali/internal/event/events/pan-gesture/pan-gesture-event.h>
39 constexpr float MINIMUM_MOTION_DISTANCE_BEFORE_PAN(15.0f);
40 constexpr float MINIMUM_MOTION_DISTANCE_BEFORE_PAN_SQUARED(MINIMUM_MOTION_DISTANCE_BEFORE_PAN* MINIMUM_MOTION_DISTANCE_BEFORE_PAN);
41 constexpr float MINIMUM_MOTION_DISTANCE_TO_THRESHOLD_ADJUSTMENTS_RATIO(2.0f / 3.0f);
42 constexpr unsigned long MINIMUM_TIME_BEFORE_THRESHOLD_ADJUSTMENTS(100);
43 constexpr unsigned int MINIMUM_MOTION_EVENTS_BEFORE_PAN(2);
45 uint32_t GetMilliSeconds()
47 // Get the time of a monotonic clock since its epoch.
48 auto epoch = std::chrono::steady_clock::now().time_since_epoch();
50 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
52 return static_cast<uint32_t>(duration.count());
54 } // unnamed namespace
56 PanGestureRecognizer::PanGestureRecognizer(Observer& observer, Vector2 screenSize, const PanGestureRequest& request, int32_t minimumDistance, int32_t minimumPanEvents)
57 : GestureRecognizer(screenSize, GestureType::PAN),
60 mThresholdAdjustmentsRemaining(0),
61 mThresholdTotalAdjustments(static_cast<unsigned int>(MINIMUM_MOTION_DISTANCE_BEFORE_PAN * MINIMUM_MOTION_DISTANCE_TO_THRESHOLD_ADJUSTMENTS_RATIO)),
62 mPrimaryTouchDownTime(0),
63 mMinimumTouchesRequired(request.minTouches),
64 mMaximumTouchesRequired(request.maxTouches),
65 mMinimumDistanceSquared(static_cast<unsigned int>(MINIMUM_MOTION_DISTANCE_BEFORE_PAN_SQUARED)),
66 mMinimumMotionEvents(MINIMUM_MOTION_EVENTS_BEFORE_PAN),
68 mMaximumMotionEventAge(request.maxMotionEventAge)
70 if(minimumDistance >= 0)
72 mMinimumDistanceSquared = minimumDistance * minimumDistance;
74 // Usually, we do not want to apply the threshold straight away, but phased over the first few pans
75 // Set our distance to threshold adjustments ratio here.
76 float fMinimumDistance = static_cast<float>(minimumDistance);
77 mThresholdTotalAdjustments = static_cast<unsigned int>(fMinimumDistance * MINIMUM_MOTION_DISTANCE_TO_THRESHOLD_ADJUSTMENTS_RATIO);
80 if(minimumPanEvents >= 1)
82 mMinimumMotionEvents = minimumPanEvents - 1; // Down is the first event
86 PanGestureRecognizer::~PanGestureRecognizer() = default;
88 void PanGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
90 PointState::Type primaryPointState(event.points[0].GetState());
91 GestureRecognizerPtr ptr(this); // To keep us from being destroyed during the life-time of this method
93 if(primaryPointState == PointState::INTERRUPTED)
95 if((mState == STARTED) || (mState == POSSIBLE))
97 // If our pan had started and we are interrupted, then tell Core that pan is cancelled.
98 mTouchEvents.push_back(event);
99 SendPan(GestureState::CANCELLED, event);
101 mState = CLEAR; // We should change our state to CLEAR.
102 mTouchEvents.clear();
110 if((primaryPointState == PointState::DOWN) || (primaryPointState == PointState::STATIONARY) || (primaryPointState == PointState::MOTION))
112 mPrimaryTouchDownLocation = event.points[0].GetScreenPosition();
113 mPrimaryTouchDownTime = event.time;
115 if(event.GetPointCount() == mMinimumTouchesRequired)
117 // We have satisfied the minimum touches required for a pan, tell core that a gesture may be possible and change our state accordingly.
119 SendPan(GestureState::POSSIBLE, event);
122 mTouchEvents.push_back(event);
129 unsigned int pointCount(event.GetPointCount());
130 if((pointCount >= mMinimumTouchesRequired) && (pointCount <= mMaximumTouchesRequired))
132 if(primaryPointState == PointState::MOTION)
134 mTouchEvents.push_back(event);
137 Vector2 delta(event.points[0].GetScreenPosition() - mPrimaryTouchDownLocation);
139 if((mMotionEvents >= mMinimumMotionEvents) &&
140 (delta.LengthSquared() >= static_cast<float>(mMinimumDistanceSquared)))
142 // If the touch point(s) have moved enough distance to be considered a pan, then tell Core that the pan gesture has started and change our state accordingly.
144 SendPan(GestureState::STARTED, event);
147 else if(primaryPointState == PointState::UP)
149 Vector2 delta(event.points[0].GetScreenPosition() - mPrimaryTouchDownLocation);
150 if(delta.LengthSquared() >= static_cast<float>(mMinimumDistanceSquared))
152 SendPan(GestureState::STARTED, event);
153 mTouchEvents.push_back(event);
154 SendPan(GestureState::FINISHED, event);
158 // If we have lifted the primary touch point then tell core the pan is cancelled and change our state to CLEAR.
159 SendPan(GestureState::CANCELLED, event);
162 mTouchEvents.clear();
167 // We do not satisfy pan conditions, tell Core our Gesture has been cancelled.
168 SendPan(GestureState::CANCELLED, event);
170 if(pointCount == 1 && primaryPointState == PointState::UP)
172 // If we have lifted the primary touch point, then change our state to CLEAR...
174 mTouchEvents.clear();
178 // ...otherwise change it to FAILED.
187 mTouchEvents.push_back(event);
189 unsigned int pointCount(event.GetPointCount());
190 if((pointCount >= mMinimumTouchesRequired) && (pointCount <= mMaximumTouchesRequired))
192 switch(primaryPointState)
194 case PointState::MOTION:
196 // Check whether this motion event is acceptable or not.
197 // If event time is too old, we should skip this event.
198 if(GetMilliSeconds() - event.time > mMaximumMotionEventAge)
200 // Too old event. Skip it.
201 mTouchEvents.pop_back();
205 // Pan is continuing, tell Core.
206 SendPan(GestureState::CONTINUING, event);
212 // Pan is finally finished when our primary point is lifted, tell Core and change our state to CLEAR.
214 SendPan(GestureState::FINISHED, event);
215 mTouchEvents.clear();
218 case PointState::STATIONARY:
219 if(pointCount == mMinimumTouchesRequired)
221 Integration::PointContainerConstIterator iter = event.points.begin() + 1; // We already know the state of the first point
222 for(; iter != event.points.end(); ++iter)
224 if(iter->GetState() == PointState::UP)
226 // The number of touch points will be less than the minimum required. Inform core and change our state to FINISHED.
227 SendPan(GestureState::FINISHED, event);
241 // We have gone outside of the pan requirements, inform Core that the gesture is finished.
242 SendPan(GestureState::FINISHED, event);
244 if(pointCount == 1 && primaryPointState == PointState::UP)
246 // If this was the primary point being released, then we change our state back to CLEAR...
248 mTouchEvents.clear();
252 // ...otherwise we change it to FINISHED.
262 if(primaryPointState == PointState::UP)
264 // Change our state back to clear when the primary touch point is released.
266 mTouchEvents.clear();
274 void PanGestureRecognizer::Update(const GestureRequest& request)
276 const PanGestureRequest& pan = static_cast<const PanGestureRequest&>(request);
278 mMinimumTouchesRequired = pan.minTouches;
279 mMaximumTouchesRequired = pan.maxTouches;
280 mMaximumMotionEventAge = pan.maxMotionEventAge;
283 void PanGestureRecognizer::SendPan(GestureState state, const Integration::TouchEvent& currentEvent)
285 PanGestureEvent gesture(state);
286 gesture.currentPosition = currentEvent.points[0].GetScreenPosition();
287 gesture.numberOfTouches = currentEvent.GetPointCount();
289 if(mTouchEvents.size() > 1)
291 // Get the second last event in the queue, the last one is the current event
292 const Integration::TouchEvent& previousEvent(*(mTouchEvents.rbegin() + 1));
294 Vector2 previousPosition(mPreviousPosition);
295 uint32_t previousTime(previousEvent.time);
297 // If we've just started then we want to remove the threshold from Core calculations.
298 if(state == GestureState::STARTED)
300 previousPosition = mPrimaryTouchDownLocation;
301 previousTime = mPrimaryTouchDownTime;
303 // If it's a slow pan, we do not want to phase in the threshold over the first few pan-events
304 // A slow pan is defined as one that starts the specified number of milliseconds after the down-event
305 if((currentEvent.time - previousTime) > MINIMUM_TIME_BEFORE_THRESHOLD_ADJUSTMENTS)
307 mThresholdAdjustmentsRemaining = mThresholdTotalAdjustments;
308 mThresholdAdjustmentPerFrame = (gesture.currentPosition - previousPosition) / static_cast<float>(mThresholdTotalAdjustments);
312 mThresholdAdjustmentsRemaining = 0;
313 mThresholdAdjustmentPerFrame = Vector2::ZERO;
317 gesture.previousPosition = previousPosition;
318 gesture.timeDelta = currentEvent.time - previousTime;
320 // Apply the threshold with a phased approach
321 if(mThresholdAdjustmentsRemaining > 0)
323 --mThresholdAdjustmentsRemaining;
324 gesture.currentPosition -= mThresholdAdjustmentPerFrame * static_cast<float>(mThresholdAdjustmentsRemaining);
327 mPreviousPosition = gesture.currentPosition;
331 gesture.previousPosition = gesture.currentPosition;
332 gesture.timeDelta = 0;
335 gesture.time = currentEvent.time;
336 gesture.sourceType = mSourceType;
337 gesture.sourceData = mSourceData;
341 // Create another handle so the recognizer cannot be destroyed during process function
342 GestureRecognizerPtr recognizerHandle = this;
344 mObserver.Process(*mScene, gesture);
348 void PanGestureRecognizer::SetMinimumDistance(int32_t minimumDistance)
350 if(minimumDistance >= 0)
352 mMinimumDistanceSquared = minimumDistance * minimumDistance;
354 // Usually, we do not want to apply the threshold straight away, but phased over the first few pans
355 // Set our distance to threshold adjustments ratio here.
356 float fMinimumDistance = static_cast<float>(minimumDistance);
357 mThresholdTotalAdjustments = static_cast<unsigned int>(fMinimumDistance * MINIMUM_MOTION_DISTANCE_TO_THRESHOLD_ADJUSTMENTS_RATIO);
361 void PanGestureRecognizer::SetMinimumPanEvents(int32_t minimumPanEvents)
363 if(minimumPanEvents >= 1)
365 mMinimumMotionEvents = minimumPanEvents - 1; // Down is the first event
369 } // namespace Internal