[dali_1.4.57] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / events / rotation-gesture / rotation-gesture-recognizer.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/events/rotation-gesture/rotation-gesture-recognizer.h>
20
21 // EXTERNAL INCLUDES
22 #include <cmath>
23
24 // INTERNAL INCLUDES
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>
31
32 // INTERNAL INCLUDES
33
34
35 namespace Dali
36 {
37
38 namespace Internal
39 {
40
41 namespace
42 {
43
44 inline float GetAngle( const Integration::Point& point1, const Integration::Point& point2 )
45 {
46   const Vector2& point1ScreenPosition = point1.GetScreenPosition();
47   const Vector2& point2ScreenPosition = point2.GetScreenPosition();
48   return atan2( point2ScreenPosition.y - point1ScreenPosition.y, point2ScreenPosition.x - point1ScreenPosition.x );
49 }
50
51 inline Vector2 GetCenterPoint( const Integration::Point& point1, const Integration::Point& point2 )
52 {
53   return Vector2( point1.GetScreenPosition() + point2.GetScreenPosition() ) * 0.5f;
54 }
55
56 } // unnamed namespace
57
58 RotationGestureRecognizer::RotationGestureRecognizer( Observer& observer, uint32_t minimumTouchEvents, uint32_t minimumTouchEventsAfterStart )
59 : GestureRecognizer( DevelGesture::Rotation ),
60   mObserver( observer ),
61   mState( Clear ),
62   mTouchEvents(),
63   mStartingAngle( 0.0f ),
64   mMinimumTouchEvents( minimumTouchEvents ),
65   mMinimumTouchEventsAfterStart( minimumTouchEventsAfterStart )
66 {
67 }
68
69 void RotationGestureRecognizer::SendEvent( const Integration::TouchEvent& event )
70 {
71   int pointCount = event.GetPointCount();
72
73   switch( mState )
74   {
75     case Clear:
76     {
77       if( pointCount == 2 )
78       {
79         // Change state to possible as we have two touch points.
80         mState = Possible;
81         mTouchEvents.push_back( event );
82       }
83       break;
84     }
85
86     case Possible:
87     {
88       if ( pointCount != 2 )
89       {
90         // We no longer have two touch points so change state back to Clear.
91         mState = Clear;
92         mTouchEvents.clear();
93       }
94       else
95       {
96         const Integration::Point& currentPoint1 = event.points[0];
97         const Integration::Point& currentPoint2 = event.points[1];
98
99         if( currentPoint1.GetState() == PointState::UP || currentPoint2.GetState() == PointState::UP )
100         {
101           // One of our touch points has an Up event so change our state back to Clear.
102           mState = Clear;
103           mTouchEvents.clear();
104         }
105         else
106         {
107           mTouchEvents.push_back( event );
108
109           // We can only determine a rotation after a certain number of touch points have been collected.
110           if( mTouchEvents.size() >= mMinimumTouchEvents )
111           {
112             // Remove the first few events from the vector otherwise values are exaggerated
113             mTouchEvents.erase( mTouchEvents.begin(), mTouchEvents.end() - mMinimumTouchEvents );
114
115             if( !mTouchEvents.empty() )
116             {
117               mStartingAngle = GetAngle( mTouchEvents.begin()->points[0], mTouchEvents.begin()->points[1] );
118
119               // Send rotation started
120               SendRotation( Gesture::Started, event );
121
122               mState = Started;
123             }
124
125             mTouchEvents.clear();
126
127             if( mState == Possible )
128             {
129               // No rotation, so restart detection
130               mState = Clear;
131               mTouchEvents.clear();
132             }
133           }
134         }
135       }
136       break;
137     }
138
139     case Started:
140     {
141       if( pointCount != 2 )
142       {
143         // Send rotation finished event
144         SendRotation( Gesture::Finished, event );
145
146         mState = Clear;
147         mTouchEvents.clear();
148       }
149       else
150       {
151         const Integration::Point& currentPoint1 = event.points[0];
152         const Integration::Point& currentPoint2 = event.points[1];
153
154         if( ( currentPoint1.GetState() == PointState::UP ) ||
155             ( currentPoint2.GetState() == PointState::UP ) )
156         {
157           mTouchEvents.push_back( event );
158           // Send rotation finished event
159           SendRotation( Gesture::Finished, event );
160
161           mState = Clear;
162           mTouchEvents.clear();
163         }
164         else
165         {
166           mTouchEvents.push_back( event );
167
168           if( mTouchEvents.size() >= mMinimumTouchEventsAfterStart )
169           {
170             // Send rotation continuing
171             SendRotation( Gesture::Continuing, event );
172
173             mTouchEvents.clear();
174           }
175         }
176       }
177       break;
178     }
179   }
180 }
181
182 void RotationGestureRecognizer::SetMinimumTouchEvents( uint32_t value )
183 {
184   mMinimumTouchEvents = value;
185 }
186
187 void RotationGestureRecognizer::SetMinimumTouchEventsAfterStart( uint32_t value )
188 {
189   mMinimumTouchEventsAfterStart = value;
190 }
191
192 void RotationGestureRecognizer::SendRotation( Gesture::State state, const Integration::TouchEvent& currentEvent )
193 {
194   RotationGestureEvent gesture( state );
195
196   if( !mTouchEvents.empty() )
197   {
198     // Assert if we have been holding TouchEvents that do not have 2 points
199     DALI_ASSERT_DEBUG( mTouchEvents[0].GetPointCount() == 2 );
200
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 )
205     {
206       event = *mTouchEvents.rbegin();
207     }
208
209     const Integration::Point& currentPoint1( event.points[0] );
210     const Integration::Point& currentPoint2( event.points[1] );
211
212     gesture.rotation = GetAngle( currentPoint1, currentPoint2 ) - mStartingAngle;
213     gesture.centerPoint = GetCenterPoint( currentPoint1, currentPoint2 );
214   }
215   else
216   {
217     // Something has gone wrong, just cancel the gesture.
218     gesture.state = Gesture::Cancelled;
219   }
220
221   gesture.time = currentEvent.time;
222
223   if( mScene )
224   {
225     // Create another handle so the recognizer cannot be destroyed during process function
226     GestureRecognizerPtr recognizerHandle = this;
227
228     mObserver.Process( *mScene, gesture );
229   }
230 }
231
232 } // namespace Internal
233
234 } // namespace Dali