2 * Copyright (c) 2019 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/accessibility/common/accessibility-gesture-detector.h>
23 #include <dali/integration-api/events/touch-event-integ.h>
37 const float MINIMUM_MOTION_DISTANCE_BEFORE_PAN( 15.0f );
38 const float MINIMUM_MOTION_DISTANCE_BEFORE_PAN_SQUARED( MINIMUM_MOTION_DISTANCE_BEFORE_PAN * MINIMUM_MOTION_DISTANCE_BEFORE_PAN );
39 const float MINIMUM_MOTION_DISTANCE_TO_THRESHOLD_ADJUSTMENTS_RATIO( 2.0f / 3.0f );
40 const unsigned long MAXIMUM_TIME_DIFF_ALLOWED( 500 );
41 const unsigned long MINIMUM_TIME_BEFORE_THRESHOLD_ADJUSTMENTS( 100 );
42 const unsigned int MINIMUM_MOTION_EVENTS_BEFORE_PAN(2);
43 const unsigned int MINIMUM_TOUCHES_BEFORE_PAN(1);
44 const unsigned int MAXIMUM_TOUCHES_BEFORE_PAN(1);
45 } // unnamed namespace
48 AccessibilityGestureDetector::AccessibilityGestureDetector()
51 mGestureHandler(nullptr),
53 mThresholdAdjustmentsRemaining( 0 ),
54 mThresholdTotalAdjustments( MINIMUM_MOTION_DISTANCE_BEFORE_PAN * MINIMUM_MOTION_DISTANCE_TO_THRESHOLD_ADJUSTMENTS_RATIO ),
55 mPrimaryTouchDownTime( 0 ),
56 mMinimumTouchesRequired( MINIMUM_TOUCHES_BEFORE_PAN ),
57 mMaximumTouchesRequired( MAXIMUM_TOUCHES_BEFORE_PAN ),
58 mMinimumDistanceSquared( MINIMUM_MOTION_DISTANCE_BEFORE_PAN_SQUARED ),
59 mMinimumMotionEvents( MINIMUM_MOTION_EVENTS_BEFORE_PAN ),
64 AccessibilityGestureDetector::~AccessibilityGestureDetector()
68 void AccessibilityGestureDetector::SetGestureHandler(AccessibilityGestureHandler& handler)
70 mGestureHandler = &handler;
73 void AccessibilityGestureDetector::EmitPan(const AccessibilityGestureEvent gesture)
77 if(gesture.state == AccessibilityGestureEvent::Started)
84 mGestureHandler->HandlePanGesture(gesture);
86 if( (gesture.state == AccessibilityGestureEvent::Finished) ||
87 (gesture.state == AccessibilityGestureEvent::Cancelled) )
95 void AccessibilityGestureDetector::SendEvent(const Integration::TouchEvent& event)
97 PointState::Type primaryPointState(event.points[0].GetState());
99 if (primaryPointState == PointState::INTERRUPTED)
101 if ( ( mState == Started ) || ( mState == Possible ) )
103 // If our pan had started and we are interrupted, then tell Core that pan is cancelled.
104 mTouchEvents.push_back(event);
105 SendPan(AccessibilityGestureEvent::Cancelled, event);
107 mState = Clear; // We should change our state to Clear.
108 mTouchEvents.clear();
116 if ( ( primaryPointState == PointState::DOWN ) || ( primaryPointState == PointState::STATIONARY ) )
118 mPrimaryTouchDownLocation = event.points[0].GetScreenPosition();
119 mPrimaryTouchDownTime = event.time;
121 if (event.GetPointCount() == mMinimumTouchesRequired)
123 // We have satisfied the minimum touches required for a pan, tell core that a gesture may be possible and change our state accordingly.
125 SendPan(AccessibilityGestureEvent::Possible, event);
128 mTouchEvents.push_back(event);
135 unsigned int pointCount(event.GetPointCount());
136 if ( (pointCount >= mMinimumTouchesRequired)&&(pointCount <= mMaximumTouchesRequired) )
138 if (primaryPointState == PointState::MOTION)
140 mTouchEvents.push_back(event);
143 Vector2 delta(event.points[0].GetScreenPosition() - mPrimaryTouchDownLocation);
145 if ( ( mMotionEvents >= mMinimumMotionEvents ) &&
146 ( delta.LengthSquared() >= static_cast<float>( mMinimumDistanceSquared ) ) )
148 // 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.
150 SendPan(AccessibilityGestureEvent::Started, event);
153 else if (primaryPointState == PointState::UP)
155 Vector2 delta(event.points[0].GetScreenPosition() - mPrimaryTouchDownLocation);
156 if (delta.LengthSquared() >= static_cast<float>( mMinimumDistanceSquared ) )
158 SendPan(AccessibilityGestureEvent::Started, event);
159 mTouchEvents.push_back(event);
160 SendPan(AccessibilityGestureEvent::Finished, event);
164 // If we have lifted the primary touch point then tell core the pan is cancelled and change our state to Clear.
165 SendPan(AccessibilityGestureEvent::Cancelled, event);
168 mTouchEvents.clear();
173 // We do not satisfy pan conditions, tell Core our Gesture has been cancelled.
174 SendPan(AccessibilityGestureEvent::Cancelled, event);
176 if (pointCount == 1 && primaryPointState == PointState::UP)
178 // If we have lifted the primary touch point, then change our state to Clear...
180 mTouchEvents.clear();
184 // ...otherwise change it to Failed.
193 mTouchEvents.push_back(event);
195 unsigned int pointCount(event.GetPointCount());
196 if ( (pointCount >= mMinimumTouchesRequired)&&(pointCount <= mMaximumTouchesRequired) )
198 switch (primaryPointState)
200 case PointState::MOTION:
201 // Pan is continuing, tell Core.
202 SendPan(AccessibilityGestureEvent::Continuing, event);
206 // Pan is finally finished when our primary point is lifted, tell Core and change our state to Clear.
207 SendPan(AccessibilityGestureEvent::Finished, event);
209 mTouchEvents.clear();
212 case PointState::STATIONARY:
213 if (pointCount == mMinimumTouchesRequired)
215 Integration::PointContainerConstIterator iter = event.points.begin() + 1; // We already know the state of the first point
216 for(; iter != event.points.end(); ++iter)
218 if(iter->GetState() == PointState::UP)
220 // The number of touch points will be less than the minimum required. Inform core and change our state to Finished.
221 SendPan(AccessibilityGestureEvent::Finished, event);
235 // We have gone outside of the pan requirements, inform Core that the gesture is finished.
236 SendPan(AccessibilityGestureEvent::Finished, event);
238 if (pointCount == 1 && primaryPointState == PointState::UP)
240 // If this was the primary point being released, then we change our state back to Clear...
242 mTouchEvents.clear();
246 // ...otherwise we change it to Finished.
256 if (primaryPointState == PointState::UP)
258 // Change our state back to clear when the primary touch point is released.
260 mTouchEvents.clear();
268 void AccessibilityGestureDetector::SendPan(AccessibilityGestureEvent::State state, const Integration::TouchEvent& currentEvent)
270 AccessibilityGestureEvent gesture(state);
271 gesture.currentPosition = currentEvent.points[0].GetScreenPosition();
272 gesture.numberOfTouches = currentEvent.GetPointCount();
274 if ( mTouchEvents.size() > 1 )
276 // Get the second last event in the queue, the last one is the current event
277 const Integration::TouchEvent& previousEvent( *( mTouchEvents.rbegin() + 1 ) );
279 Vector2 previousPosition( mPreviousPosition );
280 uint32_t previousTime( previousEvent.time );
282 // If we've just started then we want to remove the threshold from Core calculations.
283 if ( state == AccessibilityGestureEvent::Started )
285 previousPosition = mPrimaryTouchDownLocation;
286 previousTime = mPrimaryTouchDownTime;
288 // If it's a slow pan, we do not want to phase in the threshold over the first few pan-events
289 // A slow pan is defined as one that starts the specified number of milliseconds after the down-event
290 if ( ( currentEvent.time - previousTime ) > MINIMUM_TIME_BEFORE_THRESHOLD_ADJUSTMENTS )
292 mThresholdAdjustmentsRemaining = mThresholdTotalAdjustments;
293 mThresholdAdjustmentPerFrame = ( gesture.currentPosition - previousPosition ) / static_cast<float>( mThresholdTotalAdjustments );
297 mThresholdAdjustmentsRemaining = 0;
298 mThresholdAdjustmentPerFrame = Vector2::ZERO;
302 gesture.previousPosition = previousPosition;
303 gesture.timeDelta = currentEvent.time - previousTime;
305 // Apply the threshold with a phased approach
306 if ( mThresholdAdjustmentsRemaining > 0 )
308 --mThresholdAdjustmentsRemaining;
309 gesture.currentPosition -= mThresholdAdjustmentPerFrame * static_cast<float>( mThresholdAdjustmentsRemaining );
312 mPreviousPosition = gesture.currentPosition;
316 gesture.previousPosition = gesture.currentPosition;
317 gesture.timeDelta = 0;
320 gesture.time = currentEvent.time;
325 } // namespace Adaptor
327 } // namespace Internal