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/pinch-gesture/pinch-gesture-recognizer.h>
24 #include <dali/devel-api/events/touch-point.h>
25 #include <dali/public-api/math/vector2.h>
27 #include <dali/internal/event/events/pinch-gesture/pinch-gesture-event.h>
28 #include <dali/integration-api/events/touch-event-integ.h>
29 #include <dali/internal/event/common/scene-impl.h>
42 const float MINIMUM_DISTANCE_IN_MILLIINCH = 45.0f; // This value is used for measuring minimum pinch distance in pixel.
43 const float MINIMUM_DISTANCE_IN_PIXEL = 10.0f; // This value is for devices that do not provide a valid dpi value. (assumes 220dpi)
45 inline float GetDistance(const Integration::Point& point1, const Integration::Point& point2)
47 Vector2 vector(point1.GetScreenPosition() - point2.GetScreenPosition());
48 return vector.Length();
51 inline Vector2 GetCenterPoint(const Integration::Point& point1, const Integration::Point& point2)
53 return Vector2(point1.GetScreenPosition() + point2.GetScreenPosition()) * 0.5f;
56 inline bool IsValidDpi( const Vector2& dpi )
58 return dpi.x > 0.f && dpi.y > 0.f;
61 inline float GetDefaultMinimumPinchDistance( const Vector2& dpi )
63 return IsValidDpi( dpi ) ? ( ( MINIMUM_DISTANCE_IN_MILLIINCH * std::min( dpi.x, dpi.y ) ) / 1000.f ) : MINIMUM_DISTANCE_IN_PIXEL;
66 } // unnamed namespace
68 PinchGestureRecognizer::PinchGestureRecognizer( Observer& observer, Vector2 screenSize, Vector2 screenDpi, float minimumPinchDistance, uint32_t minimumTouchEvents, uint32_t minimumTouchEventsAfterStart )
69 : GestureRecognizer( screenSize, GestureType::PINCH ),
70 mObserver( observer ),
73 mDefaultMinimumDistanceDelta( GetDefaultMinimumPinchDistance( screenDpi ) ),
74 mStartingDistance( 0.0f ),
75 mMinimumTouchEvents( minimumTouchEvents ),
76 mMinimumTouchEventsAfterStart( minimumTouchEventsAfterStart )
78 SetMinimumPinchDistance( minimumPinchDistance );
81 PinchGestureRecognizer::~PinchGestureRecognizer()
85 void PinchGestureRecognizer::SetMinimumPinchDistance(float value)
87 mMinimumDistanceDelta = value >= 0.0f ? value : mDefaultMinimumDistanceDelta;
90 void PinchGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
92 int pointCount = event.GetPointCount();
93 GestureRecognizerPtr ptr(this); // To keep us from being destroyed during the life-time of this method
101 // Change state to possible as we have two touch points.
103 mTouchEvents.push_back(event);
112 // We no longer have two touch points so change state back to CLEAR.
114 mTouchEvents.clear();
118 const Integration::Point& currentPoint1 = event.points[0];
119 const Integration::Point& currentPoint2 = event.points[1];
121 if (currentPoint1.GetState() == PointState::UP || currentPoint2.GetState() == PointState::UP)
123 // One of our touch points has an Up event so change our state back to CLEAR.
125 mTouchEvents.clear();
129 mTouchEvents.push_back(event);
131 // We can only determine a pinch after a certain number of touch points have been collected.
132 if( mTouchEvents.size() >= mMinimumTouchEvents )
134 const Integration::Point& firstPoint1 = mTouchEvents[0].points[0];
135 const Integration::Point& firstPoint2 = mTouchEvents[0].points[1];
137 float firstDistance = GetDistance(firstPoint1, firstPoint2);
138 float currentDistance = GetDistance(currentPoint1, currentPoint2);
139 float distanceChanged = firstDistance - currentDistance;
141 // Check if distance has changed enough
142 if (fabsf(distanceChanged) > mMinimumDistanceDelta)
144 // Remove the first few events from the vector otherwise values are exaggerated
145 mTouchEvents.erase( mTouchEvents.begin(), mTouchEvents.end() - mMinimumTouchEvents );
147 if ( !mTouchEvents.empty() )
149 mStartingDistance = GetDistance(mTouchEvents.begin()->points[0], mTouchEvents.begin()->points[1]);
151 // Send pinch started
152 SendPinch(GestureState::STARTED, event);
157 mTouchEvents.clear();
160 if (mState == POSSIBLE)
162 // No pinch, so restart detection
164 mTouchEvents.clear();
174 if(event.points[0].GetState() == PointState::INTERRUPTED)
176 // System interruption occurred, pinch should be cancelled
177 mTouchEvents.clear();
178 SendPinch(GestureState::CANCELLED, event);
180 mTouchEvents.clear();
182 else if (pointCount != 2)
184 // Send pinch finished event
185 SendPinch(GestureState::FINISHED, event);
188 mTouchEvents.clear();
192 const Integration::Point& currentPoint1 = event.points[0];
193 const Integration::Point& currentPoint2 = event.points[1];
195 if (currentPoint1.GetState() == PointState::UP || currentPoint2.GetState() == PointState::UP)
197 mTouchEvents.push_back(event);
198 // Send pinch finished event
199 SendPinch(GestureState::FINISHED, event);
202 mTouchEvents.clear();
206 mTouchEvents.push_back(event);
208 if( mTouchEvents.size() >= mMinimumTouchEventsAfterStart )
210 // Send pinch continuing
211 SendPinch(GestureState::CONTINUING, event);
213 mTouchEvents.clear();
222 void PinchGestureRecognizer::Update(const GestureRequest& request)
227 void PinchGestureRecognizer::SendPinch(GestureState state, const Integration::TouchEvent& currentEvent)
229 PinchGestureEvent gesture(state);
231 if ( !mTouchEvents.empty() )
233 const Integration::TouchEvent& firstEvent = mTouchEvents[0];
235 // Assert if we have been holding TouchEvents that do not have 2 points
236 DALI_ASSERT_DEBUG( firstEvent.GetPointCount() == 2 );
238 // We should use the current event in our calculations unless it does not have two points.
239 // If it does not have two points, then we should use the last point in mTouchEvents.
240 Integration::TouchEvent event( currentEvent );
241 if ( event.GetPointCount() != 2 )
243 event = *mTouchEvents.rbegin();
246 const Integration::Point& firstPoint1( firstEvent.points[0] );
247 const Integration::Point& firstPoint2( firstEvent.points[1] );
248 const Integration::Point& currentPoint1( event.points[0] );
249 const Integration::Point& currentPoint2( event.points[1] );
251 float firstDistance = GetDistance(firstPoint1, firstPoint2);
252 float currentDistance = GetDistance(currentPoint1, currentPoint2);
253 gesture.scale = currentDistance / mStartingDistance;
255 float distanceDelta = fabsf(firstDistance - currentDistance);
256 float timeDelta = static_cast<float> ( currentEvent.time - firstEvent.time );
257 gesture.speed = ( distanceDelta / timeDelta ) * 1000.0f;
259 gesture.centerPoint = GetCenterPoint(currentPoint1, currentPoint2);
263 // Something has gone wrong, just cancel the gesture.
264 gesture.state = GestureState::CANCELLED;
267 gesture.time = currentEvent.time;
271 // Create another handle so the recognizer cannot be destroyed during process function
272 GestureRecognizerPtr recognizerHandle = this;
274 mObserver.Process(*mScene, gesture);
278 void PinchGestureRecognizer::SetMinimumTouchEvents( uint32_t value )
280 mMinimumTouchEvents = value;
283 void PinchGestureRecognizer::SetMinimumTouchEventsAfterStart( uint32_t value )
285 mMinimumTouchEventsAfterStart = value;
288 } // namespace Internal