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