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>
44 inline float GetAngle( const Integration::Point& point1, const Integration::Point& point2 )
46 const Vector2& point1ScreenPosition = point1.GetScreenPosition();
47 const Vector2& point2ScreenPosition = point2.GetScreenPosition();
48 return atan2( point2ScreenPosition.y - point1ScreenPosition.y, point2ScreenPosition.x - point1ScreenPosition.x );
51 inline Vector2 GetCenterPoint( const Integration::Point& point1, const Integration::Point& point2 )
53 return Vector2( point1.GetScreenPosition() + point2.GetScreenPosition() ) * 0.5f;
56 } // unnamed namespace
58 RotationGestureRecognizer::RotationGestureRecognizer( Observer& observer, uint32_t minimumTouchEvents, uint32_t minimumTouchEventsAfterStart )
59 : GestureRecognizer( DevelGesture::Rotation ),
60 mObserver( observer ),
63 mStartingAngle( 0.0f ),
64 mMinimumTouchEvents( minimumTouchEvents ),
65 mMinimumTouchEventsAfterStart( minimumTouchEventsAfterStart )
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() >= 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( 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() >= mMinimumTouchEventsAfterStart )
170 // Send rotation continuing
171 SendRotation( Gesture::Continuing, event );
173 mTouchEvents.clear();
182 void RotationGestureRecognizer::SetMinimumTouchEvents( uint32_t value )
184 mMinimumTouchEvents = value;
187 void RotationGestureRecognizer::SetMinimumTouchEventsAfterStart( uint32_t value )
189 mMinimumTouchEventsAfterStart = value;
192 void RotationGestureRecognizer::SendRotation( Gesture::State state, const Integration::TouchEvent& currentEvent )
194 RotationGestureEvent gesture( state );
196 if( !mTouchEvents.empty() )
198 // Assert if we have been holding TouchEvents that do not have 2 points
199 DALI_ASSERT_DEBUG( mTouchEvents[0].GetPointCount() == 2 );
201 // We should use the current event in our calculations unless it does not have two points.
202 // If it does not have two points, then we should use the last point in mTouchEvents.
203 Integration::TouchEvent event( currentEvent );
204 if( event.GetPointCount() != 2 )
206 event = *mTouchEvents.rbegin();
209 const Integration::Point& currentPoint1( event.points[0] );
210 const Integration::Point& currentPoint2( event.points[1] );
212 gesture.rotation = GetAngle( currentPoint1, currentPoint2 ) - mStartingAngle;
213 gesture.centerPoint = GetCenterPoint( currentPoint1, currentPoint2 );
217 // Something has gone wrong, just cancel the gesture.
218 gesture.state = Gesture::Cancelled;
221 gesture.time = currentEvent.time;
225 // Create another handle so the recognizer cannot be destroyed during process function
226 GestureRecognizerPtr recognizerHandle = this;
228 mObserver.Process( *mScene, gesture );
232 } // namespace Internal