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/event/events/long-press-gesture/long-press-gesture-recognizer.h>
24 #include <dali/public-api/events/touch-point.h>
25 #include <dali/public-api/math/vector2.h>
27 #include <dali/internal/event/common/thread-local-storage.h>
28 #include <dali/internal/event/events/gesture-requests.h>
29 #include <dali/integration-api/events/touch-event-integ.h>
30 #include <dali/integration-api/platform-abstraction.h>
40 // TODO: Set these according to DPI
41 const float MAXIMUM_MOTION_ALLOWED = 60.0f;
43 } // unnamed namespace
45 LongPressGestureRecognizer::LongPressGestureRecognizer(Observer& observer, Vector2 screenSize, const LongPressGestureRequest& request, uint32_t minimumHoldingTime )
46 : GestureRecognizer( screenSize, Gesture::LongPress ),
47 mObserver( observer ),
49 mMinimumTouchesRequired( request.minTouches ),
50 mMaximumTouchesRequired( request.maxTouches ),
53 mMinimumHoldingTime( minimumHoldingTime )
57 LongPressGestureRecognizer::~LongPressGestureRecognizer()
61 void LongPressGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
63 unsigned int pointCount( event.GetPointCount() );
64 Dali::Integration::PlatformAbstraction& platformAbstraction = ThreadLocalStorage::Get().GetPlatformAbstraction();
68 // Clear: Wait till one point touches the screen before starting timer.
71 const Integration::Point& point = event.points[0];
73 if ( point.GetState() == PointState::DOWN )
75 mTouchPositions.clear();
76 mTouchPositions[point.GetDeviceId()] = point.GetScreenPosition();
78 mTouchTime = event.time;
80 mTimerId = platformAbstraction.StartTimer( mMinimumHoldingTime, MakeCallback( this, &LongPressGestureRecognizer::TimerCallback ) );
82 // A long press gesture may be possible, tell Core about this and change state to Touched.
84 EmitGesture( Gesture::Possible );
90 // Touched: Monitor movement and addition/removal of points.
93 if (pointCount > mMaximumTouchesRequired)
95 // A long press did not occur, tell Core that it was cancelled and change state to Failed.
96 EmitGesture( Gesture::Cancelled );
97 mTouchPositions.clear();
98 platformAbstraction.CancelTimer(mTimerId);
105 for ( Integration::PointContainerConstIterator iter = event.points.begin(), endIter = event.points.end();
106 iter != endIter && !endLoop; ++iter)
108 switch( iter->GetState() )
111 case PointState::DOWN:
113 mTouchPositions[iter->GetDeviceId()] = iter->GetScreenPosition();
119 case PointState::INTERRUPTED:
121 // System has interrupted us, long press is not possible, inform Core
122 EmitGesture( Gesture::Cancelled );
123 mTouchPositions.clear();
124 platformAbstraction.CancelTimer(mTimerId);
125 mState = ( pointCount == 1 ) ? Clear : Failed; // Change state to Clear if only one point, Failed otherwise.
130 case PointState::MOTION:
132 const Vector2 touchPosition( mTouchPositions[iter->GetDeviceId()] - iter->GetScreenPosition() );
133 float distanceSquared = touchPosition.LengthSquared();
135 if (distanceSquared > ( MAXIMUM_MOTION_ALLOWED * MAXIMUM_MOTION_ALLOWED ) )
137 // We have moved more than the allowable motion for a long press gesture. Inform Core and change state to Failed.
138 EmitGesture( Gesture::Cancelled );
139 platformAbstraction.CancelTimer(mTimerId);
146 case PointState::STATIONARY:
147 case PointState::LEAVE:
156 // Failed/Finished: Monitor the touches, waiting for all touches to be released.
160 // eventually the final touch point will be removed, marking the end of this gesture.
161 if ( pointCount == 1 )
163 PointState::Type primaryPointState = event.points[0].GetState();
165 if ( (primaryPointState == PointState::UP) || (primaryPointState == PointState::INTERRUPTED) )
167 if(mState == Finished)
169 // When the last touch point is lifted, we should inform the Core that the Long press has finished.
170 EmitGesture(Gesture::Finished);
172 mTouchPositions.clear();
173 mState = Clear; // Reset state to clear when last touch point is lifted.
181 void LongPressGestureRecognizer::Update(const GestureRequest& request)
183 const LongPressGestureRequest& longPress = static_cast<const LongPressGestureRequest&>(request);
185 mMinimumTouchesRequired = longPress.minTouches;
186 mMaximumTouchesRequired = longPress.maxTouches;
189 void LongPressGestureRecognizer::SetMinimumHoldingTime( uint32_t time )
191 mMinimumHoldingTime = time;
195 bool LongPressGestureRecognizer::TimerCallback()
197 EmitGesture(Gesture::Started);
204 void LongPressGestureRecognizer::EmitGesture(Gesture::State state)
206 unsigned int touchPoints ( static_cast<unsigned int>( mTouchPositions.size() ) );
208 // We should tell Core about the Possible and Cancelled states regardless of whether we have satisfied long press requirements.
209 if ( (state == Gesture::Possible) ||
210 (state == Gesture::Cancelled) ||
211 (touchPoints >= mMinimumTouchesRequired) )
213 LongPressGestureEvent longPress( state );
214 longPress.numberOfTouches = touchPoints;
216 for (std::map<int, Vector2>::iterator iter = mTouchPositions.begin(), endIter = mTouchPositions.end();
217 iter != endIter; ++iter)
219 longPress.point += iter->second;
221 longPress.point /= static_cast<float>( touchPoints );
223 longPress.time = mTouchTime;
224 if ( state != Gesture::Possible )
226 longPress.time += mMinimumHoldingTime;
231 // Create another handle so the recognizer cannot be destroyed during process function
232 GestureRecognizerPtr recognizerHandle = this;
234 mObserver.Process(*mScene, longPress);
239 } // namespace Internal