2 * Copyright (c) 2020 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>
24 #include <dali/devel-api/events/touch-point.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/pan-gesture/pan-gesture-event.h>
31 #include <dali/internal/event/events/gesture-requests.h>
40 const float MINIMUM_MOTION_DISTANCE_BEFORE_PAN( 15.0f );
41 const float MINIMUM_MOTION_DISTANCE_BEFORE_PAN_SQUARED( MINIMUM_MOTION_DISTANCE_BEFORE_PAN * MINIMUM_MOTION_DISTANCE_BEFORE_PAN );
42 const float MINIMUM_MOTION_DISTANCE_TO_THRESHOLD_ADJUSTMENTS_RATIO( 2.0f / 3.0f );
43 const unsigned long MAXIMUM_TIME_DIFF_ALLOWED( 500 );
44 const unsigned long MINIMUM_TIME_BEFORE_THRESHOLD_ADJUSTMENTS( 100 );
45 const unsigned int MINIMUM_MOTION_EVENTS_BEFORE_PAN(2);
46 } // unnamed namespace
48 PanGestureRecognizer::PanGestureRecognizer( Observer& observer, Vector2 screenSize, const PanGestureRequest& request, int32_t minimumDistance, int32_t minimumPanEvents )
49 : GestureRecognizer( screenSize, Gesture::Pan ),
50 mObserver( observer ),
52 mThresholdAdjustmentsRemaining( 0 ),
53 mThresholdTotalAdjustments( static_cast<unsigned int>( MINIMUM_MOTION_DISTANCE_BEFORE_PAN * MINIMUM_MOTION_DISTANCE_TO_THRESHOLD_ADJUSTMENTS_RATIO ) ),
54 mPrimaryTouchDownTime( 0 ),
55 mMinimumTouchesRequired( request.minTouches ),
56 mMaximumTouchesRequired( request.maxTouches ),
57 mMinimumDistanceSquared( static_cast<unsigned int>( MINIMUM_MOTION_DISTANCE_BEFORE_PAN_SQUARED ) ),
58 mMinimumMotionEvents( MINIMUM_MOTION_EVENTS_BEFORE_PAN ),
61 if ( minimumDistance >= 0 )
63 mMinimumDistanceSquared = minimumDistance * minimumDistance;
65 // Usually, we do not want to apply the threshold straight away, but phased over the first few pans
66 // Set our distance to threshold adjustments ratio here.
67 float fMinimumDistance = static_cast<float>( minimumDistance );
68 mThresholdTotalAdjustments = static_cast<unsigned int>( fMinimumDistance * MINIMUM_MOTION_DISTANCE_TO_THRESHOLD_ADJUSTMENTS_RATIO );
71 if ( minimumPanEvents >= 1 )
73 mMinimumMotionEvents = minimumPanEvents - 1; // Down is the first event
77 PanGestureRecognizer::~PanGestureRecognizer()
81 void PanGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
83 PointState::Type primaryPointState(event.points[0].GetState());
84 GestureRecognizerPtr ptr(this); // To keep us from being destroyed during the life-time of this method
86 if (primaryPointState == PointState::INTERRUPTED)
88 if ( ( mState == Started ) || ( mState == Possible ) )
90 // If our pan had started and we are interrupted, then tell Core that pan is cancelled.
91 mTouchEvents.push_back(event);
92 SendPan(Gesture::Cancelled, event);
94 mState = Clear; // We should change our state to Clear.
103 if ( ( primaryPointState == PointState::DOWN ) || ( primaryPointState == PointState::STATIONARY ) || ( primaryPointState == PointState::MOTION ))
105 mPrimaryTouchDownLocation = event.points[0].GetScreenPosition();
106 mPrimaryTouchDownTime = event.time;
108 if (event.GetPointCount() == mMinimumTouchesRequired)
110 // We have satisfied the minimum touches required for a pan, tell core that a gesture may be possible and change our state accordingly.
112 SendPan(Gesture::Possible, event);
115 mTouchEvents.push_back(event);
122 unsigned int pointCount(event.GetPointCount());
123 if ( (pointCount >= mMinimumTouchesRequired)&&(pointCount <= mMaximumTouchesRequired) )
125 if (primaryPointState == PointState::MOTION)
127 mTouchEvents.push_back(event);
130 Vector2 delta(event.points[0].GetScreenPosition() - mPrimaryTouchDownLocation);
132 if ( ( mMotionEvents >= mMinimumMotionEvents ) &&
133 ( delta.LengthSquared() >= static_cast<float>( mMinimumDistanceSquared ) ) )
135 // 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.
137 SendPan(Gesture::Started, event);
140 else if (primaryPointState == PointState::UP)
142 Vector2 delta(event.points[0].GetScreenPosition() - mPrimaryTouchDownLocation);
143 if (delta.LengthSquared() >= static_cast<float>( mMinimumDistanceSquared ) )
145 SendPan(Gesture::Started, event);
146 mTouchEvents.push_back(event);
147 SendPan(Gesture::Finished, event);
151 // If we have lifted the primary touch point then tell core the pan is cancelled and change our state to Clear.
152 SendPan(Gesture::Cancelled, event);
155 mTouchEvents.clear();
160 // We do not satisfy pan conditions, tell Core our Gesture has been cancelled.
161 SendPan(Gesture::Cancelled, event);
163 if (pointCount == 1 && primaryPointState == PointState::UP)
165 // If we have lifted the primary touch point, then change our state to Clear...
167 mTouchEvents.clear();
171 // ...otherwise change it to Failed.
180 mTouchEvents.push_back(event);
182 unsigned int pointCount(event.GetPointCount());
183 if ( (pointCount >= mMinimumTouchesRequired)&&(pointCount <= mMaximumTouchesRequired) )
185 switch (primaryPointState)
187 case PointState::MOTION:
188 // Pan is continuing, tell Core.
189 SendPan(Gesture::Continuing, event);
193 // Pan is finally finished when our primary point is lifted, tell Core and change our state to Clear.
195 SendPan(Gesture::Finished, event);
196 mTouchEvents.clear();
199 case PointState::STATIONARY:
200 if (pointCount == mMinimumTouchesRequired)
202 Integration::PointContainerConstIterator iter = event.points.begin() + 1; // We already know the state of the first point
203 for(; iter != event.points.end(); ++iter)
205 if(iter->GetState() == PointState::UP)
207 // The number of touch points will be less than the minimum required. Inform core and change our state to Finished.
208 SendPan(Gesture::Finished, event);
222 // We have gone outside of the pan requirements, inform Core that the gesture is finished.
223 SendPan(Gesture::Finished, event);
225 if (pointCount == 1 && primaryPointState == PointState::UP)
227 // If this was the primary point being released, then we change our state back to Clear...
229 mTouchEvents.clear();
233 // ...otherwise we change it to Finished.
243 if (primaryPointState == PointState::UP)
245 // Change our state back to clear when the primary touch point is released.
247 mTouchEvents.clear();
255 void PanGestureRecognizer::Update(const GestureRequest& request)
257 const PanGestureRequest& pan = static_cast<const PanGestureRequest&>(request);
259 mMinimumTouchesRequired = pan.minTouches;
260 mMaximumTouchesRequired = pan.maxTouches;
263 void PanGestureRecognizer::SendPan(Gesture::State state, const Integration::TouchEvent& currentEvent)
265 PanGestureEvent gesture(state);
266 gesture.currentPosition = currentEvent.points[0].GetScreenPosition();
267 gesture.numberOfTouches = currentEvent.GetPointCount();
269 if ( mTouchEvents.size() > 1 )
271 // Get the second last event in the queue, the last one is the current event
272 const Integration::TouchEvent& previousEvent( *( mTouchEvents.rbegin() + 1 ) );
274 Vector2 previousPosition( mPreviousPosition );
275 uint32_t previousTime( previousEvent.time );
277 // If we've just started then we want to remove the threshold from Core calculations.
278 if ( state == Gesture::Started )
280 previousPosition = mPrimaryTouchDownLocation;
281 previousTime = mPrimaryTouchDownTime;
283 // If it's a slow pan, we do not want to phase in the threshold over the first few pan-events
284 // A slow pan is defined as one that starts the specified number of milliseconds after the down-event
285 if ( ( currentEvent.time - previousTime ) > MINIMUM_TIME_BEFORE_THRESHOLD_ADJUSTMENTS )
287 mThresholdAdjustmentsRemaining = mThresholdTotalAdjustments;
288 mThresholdAdjustmentPerFrame = ( gesture.currentPosition - previousPosition ) / static_cast<float>( mThresholdTotalAdjustments );
292 mThresholdAdjustmentsRemaining = 0;
293 mThresholdAdjustmentPerFrame = Vector2::ZERO;
297 gesture.previousPosition = previousPosition;
298 gesture.timeDelta = currentEvent.time - previousTime;
300 // Apply the threshold with a phased approach
301 if ( mThresholdAdjustmentsRemaining > 0 )
303 --mThresholdAdjustmentsRemaining;
304 gesture.currentPosition -= mThresholdAdjustmentPerFrame * static_cast<float>( mThresholdAdjustmentsRemaining );
307 mPreviousPosition = gesture.currentPosition;
311 gesture.previousPosition = gesture.currentPosition;
312 gesture.timeDelta = 0;
315 gesture.time = currentEvent.time;
319 // Create another handle so the recognizer cannot be destroyed during process function
320 GestureRecognizerPtr recognizerHandle = this;
322 mObserver.Process(*mScene, gesture);
326 } // namespace Internal