Merge "Gesture event refactor" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / events / gesture-event-processor.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/gesture-event-processor.h>
20
21 #if defined(DEBUG_ENABLED)
22 #include <sstream>
23 #endif
24
25 // INTERNAL INCLUDES
26 #include <dali/integration-api/render-controller.h>
27 #include <dali/internal/event/common/stage-impl.h>
28 #include <dali/internal/event/events/pinch-gesture-detector-impl.h>
29 #include <dali/internal/update/gestures/scene-graph-pan-gesture.h>
30 #include <dali/public-api/events/pan-gesture.h>
31 #include <dali/integration-api/debug.h>
32
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39 GestureEventProcessor::GestureEventProcessor( SceneGraph::UpdateManager& updateManager, Integration::RenderController& renderController )
40 : mLongPressGestureProcessor(),
41   mPanGestureProcessor( updateManager ),
42   mPinchGestureProcessor(),
43   mTapGestureProcessor(),
44   mRenderController( renderController ),
45   mLongPressDetectorCount(0),
46   mPanDetectorCount(0),
47   mPinchDetectorCount(0),
48   mTapDetectorCount(0),
49   envOptionMinimumPanDistance(-1),
50   envOptionMinimumPanEvents(-1)
51 {
52 }
53
54 GestureEventProcessor::~GestureEventProcessor()
55 {
56 }
57
58 void GestureEventProcessor::ProcessTouchEvent( Scene& scene, const Integration::TouchEvent& event)
59 {
60   if( mLongPressDetectorCount > 0 )
61   {
62     mLongPressGestureProcessor.ProcessTouch(scene, event);
63   }
64   if( mPanDetectorCount > 0 )
65   {
66     mPanGestureProcessor.ProcessTouch(scene, event);
67   }
68   if( mPinchDetectorCount > 0 )
69   {
70     mPinchGestureProcessor.ProcessTouch(scene, event);
71   }
72   if( mTapDetectorCount > 0 )
73   {
74     mTapGestureProcessor.ProcessTouch(scene, event);
75   }
76 }
77
78 void GestureEventProcessor::AddGestureDetector(GestureDetector* gestureDetector, Scene& scene)
79 {
80   switch (gestureDetector->GetType())
81   {
82     case Gesture::LongPress:
83     {
84       LongPressGestureDetector* longPress = static_cast<LongPressGestureDetector*>(gestureDetector);
85       mLongPressGestureProcessor.AddGestureDetector(longPress, scene);
86       mLongPressDetectorCount++;
87       break;
88     }
89
90     case Gesture::Pan:
91     {
92       PanGestureDetector* pan = static_cast<PanGestureDetector*>(gestureDetector);
93       mPanGestureProcessor.AddGestureDetector(pan, scene, envOptionMinimumPanDistance, envOptionMinimumPanEvents);
94       mPanDetectorCount++;
95       break;
96     }
97
98     case Gesture::Pinch:
99     {
100       PinchGestureDetector* pinch = static_cast<PinchGestureDetector*>(gestureDetector);
101       mPinchGestureProcessor.AddGestureDetector(pinch, scene);
102       mPinchDetectorCount++;
103       break;
104     }
105
106     case Gesture::Tap:
107     {
108       TapGestureDetector* tap = static_cast<TapGestureDetector*>(gestureDetector);
109       mTapGestureProcessor.AddGestureDetector(tap, scene);
110       mTapDetectorCount++;
111       break;
112     }
113   }
114 }
115
116 void GestureEventProcessor::RemoveGestureDetector(GestureDetector* gestureDetector)
117 {
118   switch (gestureDetector->GetType())
119   {
120     case Gesture::LongPress:
121     {
122       LongPressGestureDetector* longPress = static_cast<LongPressGestureDetector*>(gestureDetector);
123       mLongPressGestureProcessor.RemoveGestureDetector(longPress);
124       mLongPressDetectorCount--;
125       break;
126     }
127
128     case Gesture::Pan:
129     {
130       PanGestureDetector* pan = static_cast<PanGestureDetector*>(gestureDetector);
131       mPanGestureProcessor.RemoveGestureDetector(pan);
132       mPanDetectorCount--;
133       break;
134     }
135
136     case Gesture::Pinch:
137     {
138       PinchGestureDetector* pinch = static_cast<PinchGestureDetector*>(gestureDetector);
139       mPinchGestureProcessor.RemoveGestureDetector(pinch);
140       mPinchDetectorCount--;
141       break;
142     }
143
144     case Gesture::Tap:
145     {
146       TapGestureDetector* tap = static_cast<TapGestureDetector*>(gestureDetector);
147       mTapGestureProcessor.RemoveGestureDetector(tap);
148       mTapDetectorCount--;
149       break;
150     }
151   }
152 }
153
154 void GestureEventProcessor::GestureDetectorUpdated(GestureDetector* gestureDetector)
155 {
156   switch (gestureDetector->GetType())
157   {
158     case Gesture::LongPress:
159     {
160       LongPressGestureDetector* longPress = static_cast<LongPressGestureDetector*>(gestureDetector);
161       mLongPressGestureProcessor.GestureDetectorUpdated(longPress);
162       break;
163     }
164
165     case Gesture::Pan:
166     {
167       PanGestureDetector* pan = static_cast<PanGestureDetector*>(gestureDetector);
168       mPanGestureProcessor.GestureDetectorUpdated(pan);
169       break;
170     }
171
172     case Gesture::Pinch:
173     {
174       PinchGestureDetector* pinch = static_cast<PinchGestureDetector*>(gestureDetector);
175       mPinchGestureProcessor.GestureDetectorUpdated(pinch);
176       break;
177     }
178
179     case Gesture::Tap:
180     {
181       TapGestureDetector* tap = static_cast<TapGestureDetector*>(gestureDetector);
182       mTapGestureProcessor.GestureDetectorUpdated(tap);
183       break;
184     }
185   }
186 }
187
188 void GestureEventProcessor::SetGestureProperties( const Gesture& gesture )
189 {
190   bool requestUpdate = false;
191
192   switch ( gesture.type )
193   {
194     case Gesture::Pan:
195     {
196       const PanGesture& pan = static_cast< const PanGesture& >( gesture );
197       requestUpdate = mPanGestureProcessor.SetPanGestureProperties( pan );
198       break;
199     }
200
201     case Gesture::LongPress:
202     case Gesture::Pinch:
203     case Gesture::Tap:
204     {
205       DALI_ASSERT_DEBUG( false && "Gesture type does not have scene object\n" );
206       break;
207     }
208   }
209
210   if( requestUpdate )
211   {
212     // We may not be updating so we need to ask the render controller for an update.
213     mRenderController.RequestUpdate( false );
214   }
215 }
216
217 bool GestureEventProcessor::NeedsUpdate()
218 {
219   bool updateRequired = false;
220
221   updateRequired |= mLongPressGestureProcessor.NeedsUpdate();
222   updateRequired |= mPanGestureProcessor.NeedsUpdate();
223   updateRequired |= mPinchGestureProcessor.NeedsUpdate();
224   updateRequired |= mTapGestureProcessor.NeedsUpdate();
225
226   return updateRequired;
227 }
228
229 void GestureEventProcessor::EnablePanGestureProfiling()
230 {
231   mPanGestureProcessor.EnableProfiling();
232 }
233
234 void GestureEventProcessor::SetPanGesturePredictionMode(int mode)
235 {
236   mPanGestureProcessor.SetPredictionMode(mode);
237 }
238
239 void GestureEventProcessor::SetPanGesturePredictionAmount( uint32_t amount )
240 {
241   mPanGestureProcessor.SetPredictionAmount(amount);
242 }
243
244 void GestureEventProcessor::SetPanGestureMaximumPredictionAmount( uint32_t amount )
245 {
246   mPanGestureProcessor.SetMaximumPredictionAmount(amount);
247 }
248
249 void GestureEventProcessor::SetPanGestureMinimumPredictionAmount( uint32_t amount )
250 {
251   mPanGestureProcessor.SetMinimumPredictionAmount(amount);
252 }
253
254 void GestureEventProcessor::SetPanGesturePredictionAmountAdjustment( uint32_t amount )
255 {
256   mPanGestureProcessor.SetPredictionAmountAdjustment(amount);
257 }
258
259 void GestureEventProcessor::SetPanGestureSmoothingMode( int32_t mode )
260 {
261   mPanGestureProcessor.SetSmoothingMode(mode);
262 }
263
264 void GestureEventProcessor::SetPanGestureSmoothingAmount( float amount )
265 {
266   mPanGestureProcessor.SetSmoothingAmount(amount);
267 }
268
269 void GestureEventProcessor::SetPanGestureUseActualTimes( bool value )
270 {
271   mPanGestureProcessor.SetUseActualTimes( value );
272 }
273
274 void GestureEventProcessor::SetPanGestureInterpolationTimeRange( int32_t value )
275 {
276   mPanGestureProcessor.SetInterpolationTimeRange( value );
277 }
278
279 void GestureEventProcessor::SetPanGestureScalarOnlyPredictionEnabled( bool value )
280 {
281   mPanGestureProcessor.SetScalarOnlyPredictionEnabled( value );
282 }
283
284 void GestureEventProcessor::SetPanGestureTwoPointPredictionEnabled( bool value )
285 {
286   mPanGestureProcessor.SetTwoPointPredictionEnabled( value );
287 }
288
289 void GestureEventProcessor::SetPanGestureTwoPointInterpolatePastTime( int value )
290 {
291   mPanGestureProcessor.SetTwoPointInterpolatePastTime( value );
292 }
293
294 void GestureEventProcessor::SetPanGestureTwoPointVelocityBias( float value )
295 {
296   mPanGestureProcessor.SetTwoPointVelocityBias( value );
297 }
298
299 void GestureEventProcessor::SetPanGestureTwoPointAccelerationBias( float value )
300 {
301   mPanGestureProcessor.SetTwoPointAccelerationBias( value );
302 }
303
304 void GestureEventProcessor::SetPanGestureMultitapSmoothingRange( int32_t value )
305 {
306   mPanGestureProcessor.SetMultitapSmoothingRange( value );
307 }
308
309 void GestureEventProcessor::SetPanGestureMinimumDistance( int32_t value )
310 {
311   envOptionMinimumPanDistance =  value;
312 }
313
314 void GestureEventProcessor::SetPanGestureMinimumPanEvents( int32_t value )
315 {
316   envOptionMinimumPanEvents = value;
317 }
318
319 void GestureEventProcessor::SetPinchGestureMinimumDistance( float value)
320 {
321   mPinchGestureProcessor.SetMinimumPinchDistance( value );
322 }
323
324 const PanGestureProcessor& GestureEventProcessor::GetPanGestureProcessor()
325 {
326   return mPanGestureProcessor;
327 }
328
329 } // namespace Internal
330
331 } // namespace Dali