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/rotation-gesture/rotation-gesture-recognizer.h>
25 #include <dali/devel-api/events/touch-point.h>
26 #include <dali/public-api/math/vector2.h>
27 #include <dali/integration-api/events/touch-event-integ.h>
28 #include <dali/internal/event/events/rotation-gesture/rotation-gesture-event.h>
29 #include <dali/internal/event/common/scene-impl.h>
43 inline float GetAngle( const Integration::Point& point1, const Integration::Point& point2 )
45 const Vector2& point1ScreenPosition = point1.GetScreenPosition();
46 const Vector2& point2ScreenPosition = point2.GetScreenPosition();
47 return atan2( point2ScreenPosition.y - point1ScreenPosition.y, point2ScreenPosition.x - point1ScreenPosition.x );
50 inline Vector2 GetCenterPoint( const Integration::Point& point1, const Integration::Point& point2 )
52 return Vector2( point1.GetScreenPosition() + point2.GetScreenPosition() ) * 0.5f;
55 } // unnamed namespace
57 RotationGestureRecognizer::RotationGestureRecognizer( Observer& observer, uint32_t minimumTouchEvents, uint32_t minimumTouchEventsAfterStart )
58 : GestureRecognizer( GestureType::ROTATION ),
59 mObserver( observer ),
62 mStartingAngle( 0.0f ),
63 mMinimumTouchEvents( minimumTouchEvents ),
64 mMinimumTouchEventsAfterStart( minimumTouchEventsAfterStart )
68 void RotationGestureRecognizer::SendEvent( const Integration::TouchEvent& event )
70 int pointCount = event.GetPointCount();
71 GestureRecognizerPtr ptr(this); // To keep us from being destroyed during the life-time of this method
79 // Change state to possible as we have two touch points.
81 mTouchEvents.push_back( event );
88 if ( pointCount != 2 )
90 // We no longer have two touch points so change state back to CLEAR.
96 const Integration::Point& currentPoint1 = event.points[0];
97 const Integration::Point& currentPoint2 = event.points[1];
99 if( currentPoint1.GetState() == PointState::UP || currentPoint2.GetState() == PointState::UP || currentPoint1.GetState() == PointState::INTERRUPTED )
101 // One of our touch points has an Up event so change our state back to CLEAR.
103 mTouchEvents.clear();
107 mTouchEvents.push_back( event );
109 // We can only determine a rotation after a certain number of touch points have been collected.
110 if( mTouchEvents.size() >= mMinimumTouchEvents )
112 // Remove the first few events from the vector otherwise values are exaggerated
113 mTouchEvents.erase( mTouchEvents.begin(), mTouchEvents.end() - mMinimumTouchEvents );
115 if( !mTouchEvents.empty() )
117 mStartingAngle = GetAngle( mTouchEvents.begin()->points[0], mTouchEvents.begin()->points[1] );
119 // Send rotation started
120 SendRotation( GestureState::STARTED, event );
125 mTouchEvents.clear();
127 if( mState == POSSIBLE )
129 // No rotation, so restart detection
131 mTouchEvents.clear();
141 if(event.points[0].GetState() == PointState::INTERRUPTED)
143 // System interruption occurred, rotation should be cancelled
144 mTouchEvents.clear();
145 SendRotation(GestureState::CANCELLED, event);
147 mTouchEvents.clear();
149 else if( pointCount != 2 )
151 // Send rotation finished event
152 SendRotation( GestureState::FINISHED, event );
155 mTouchEvents.clear();
159 const Integration::Point& currentPoint1 = event.points[0];
160 const Integration::Point& currentPoint2 = event.points[1];
162 if( ( currentPoint1.GetState() == PointState::UP ) ||
163 ( currentPoint2.GetState() == PointState::UP ) )
165 mTouchEvents.push_back( event );
166 // Send rotation finished event
167 SendRotation( GestureState::FINISHED, event );
170 mTouchEvents.clear();
174 mTouchEvents.push_back( event );
176 if( mTouchEvents.size() >= mMinimumTouchEventsAfterStart )
178 // Send rotation continuing
179 SendRotation( GestureState::CONTINUING, event );
181 mTouchEvents.clear();
190 void RotationGestureRecognizer::SetMinimumTouchEvents( uint32_t value )
192 mMinimumTouchEvents = value;
195 void RotationGestureRecognizer::SetMinimumTouchEventsAfterStart( uint32_t value )
197 mMinimumTouchEventsAfterStart = value;
200 void RotationGestureRecognizer::SendRotation( GestureState state, const Integration::TouchEvent& currentEvent )
202 RotationGestureEvent gesture( state );
203 if( !mTouchEvents.empty() )
205 // Assert if we have been holding TouchEvents that do not have 2 points
206 DALI_ASSERT_DEBUG( mTouchEvents[0].GetPointCount() == 2 );
208 // We should use the current event in our calculations unless it does not have two points.
209 // If it does not have two points, then we should use the last point in mTouchEvents.
210 Integration::TouchEvent event( currentEvent );
211 if( event.GetPointCount() != 2 )
213 event = *mTouchEvents.rbegin();
216 const Integration::Point& currentPoint1( event.points[0] );
217 const Integration::Point& currentPoint2( event.points[1] );
219 gesture.rotation = GetAngle( currentPoint1, currentPoint2 ) - mStartingAngle;
220 gesture.centerPoint = GetCenterPoint( currentPoint1, currentPoint2 );
224 // Something has gone wrong, just cancel the gesture.
225 gesture.state = GestureState::CANCELLED;
228 gesture.time = currentEvent.time;
232 // Create another handle so the recognizer cannot be destroyed during process function
233 GestureRecognizerPtr recognizerHandle = this;
235 mObserver.Process( *mScene, gesture );
239 } // namespace Internal