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/rotation-gesture/rotation-gesture-recognizer.h>
25 #include <dali/public-api/events/touch-point.h>
26 #include <dali/public-api/math/vector2.h>
27 #include <dali/devel-api/events/gesture-devel.h>
28 #include <dali/integration-api/events/touch-event-integ.h>
29 #include <dali/internal/event/events/rotation-gesture/rotation-gesture-event.h>
30 #include <dali/internal/event/common/scene-impl.h>
43 const unsigned int MINIMUM_TOUCH_EVENTS_REQUIRED = 4;
44 const unsigned int MINIMUM_TOUCH_EVENTS_REQUIRED_AFTER_START = 4;
46 inline float GetAngle( const Integration::Point& point1, const Integration::Point& point2 )
48 const Vector2& point1ScreenPosition = point1.GetScreenPosition();
49 const Vector2& point2ScreenPosition = point2.GetScreenPosition();
50 return atan2( point2ScreenPosition.y - point1ScreenPosition.y, point2ScreenPosition.x - point1ScreenPosition.x );
53 inline Vector2 GetCenterPoint( const Integration::Point& point1, const Integration::Point& point2 )
55 return Vector2( point1.GetScreenPosition() + point2.GetScreenPosition() ) * 0.5f;
58 } // unnamed namespace
60 RotationGestureRecognizer::RotationGestureRecognizer( Observer& observer )
61 : GestureRecognizer( DevelGesture::Rotation ),
62 mObserver( observer ),
65 mStartingAngle( 0.0f )
69 void RotationGestureRecognizer::SendEvent( const Integration::TouchEvent& event )
71 int pointCount = event.GetPointCount();
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 )
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() >= MINIMUM_TOUCH_EVENTS_REQUIRED )
112 // Remove the first few events from the vector otherwise values are exaggerated
113 mTouchEvents.erase( mTouchEvents.begin(), mTouchEvents.end() - MINIMUM_TOUCH_EVENTS_REQUIRED_AFTER_START );
115 if( !mTouchEvents.empty() )
117 mStartingAngle = GetAngle( mTouchEvents.begin()->points[0], mTouchEvents.begin()->points[1] );
119 // Send rotation started
120 SendRotation( Gesture::Started, event );
125 mTouchEvents.clear();
127 if( mState == Possible )
129 // No rotation, so restart detection
131 mTouchEvents.clear();
141 if( pointCount != 2 )
143 // Send rotation finished event
144 SendRotation( Gesture::Finished, event );
147 mTouchEvents.clear();
151 const Integration::Point& currentPoint1 = event.points[0];
152 const Integration::Point& currentPoint2 = event.points[1];
154 if( ( currentPoint1.GetState() == PointState::UP ) ||
155 ( currentPoint2.GetState() == PointState::UP ) )
157 mTouchEvents.push_back( event );
158 // Send rotation finished event
159 SendRotation( Gesture::Finished, event );
162 mTouchEvents.clear();
166 mTouchEvents.push_back( event );
168 if( mTouchEvents.size() >= MINIMUM_TOUCH_EVENTS_REQUIRED_AFTER_START )
170 // Send rotation continuing
171 SendRotation( Gesture::Continuing, event );
173 mTouchEvents.clear();
182 void RotationGestureRecognizer::SendRotation( Gesture::State state, const Integration::TouchEvent& currentEvent )
184 RotationGestureEvent gesture( state );
186 if( !mTouchEvents.empty() )
188 // Assert if we have been holding TouchEvents that do not have 2 points
189 DALI_ASSERT_DEBUG( mTouchEvents[0].GetPointCount() == 2 );
191 // We should use the current event in our calculations unless it does not have two points.
192 // If it does not have two points, then we should use the last point in mTouchEvents.
193 Integration::TouchEvent event( currentEvent );
194 if( event.GetPointCount() != 2 )
196 event = *mTouchEvents.rbegin();
199 const Integration::Point& currentPoint1( event.points[0] );
200 const Integration::Point& currentPoint2( event.points[1] );
202 gesture.rotation = GetAngle( currentPoint1, currentPoint2 ) - mStartingAngle;
203 gesture.centerPoint = GetCenterPoint( currentPoint1, currentPoint2 );
207 // Something has gone wrong, just cancel the gesture.
208 gesture.state = Gesture::Cancelled;
211 gesture.time = currentEvent.time;
215 // Create another handle so the recognizer cannot be destroyed during process function
216 GestureRecognizerPtr recognizerHandle = this;
218 mObserver.Process( *mScene, gesture );
222 } // namespace Internal