Pan Gesture Prediction - Added DALI_PAN_GESTURE_PREDICTION_AMOUNT environment variable
[platform/core/uifw/dali-core.git] / dali / internal / event / events / gesture-event-processor.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/event/events/gesture-event-processor.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/integration-api/events/gesture-event.h>
22 #include <dali/integration-api/events/long-press-gesture-event.h>
23 #include <dali/integration-api/events/pan-gesture-event.h>
24 #include <dali/integration-api/events/pinch-gesture-event.h>
25 #include <dali/integration-api/events/tap-gesture-event.h>
26 #include <dali/integration-api/gesture-manager.h>
27 #include <dali/integration-api/render-controller.h>
28 #include <dali/internal/event/common/stage-impl.h>
29 #include <dali/internal/event/events/pinch-gesture-detector-impl.h>
30 #include <dali/internal/update/gestures/scene-graph-pan-gesture.h>
31 #include <dali/public-api/events/pan-gesture.h>
32
33 namespace Dali
34 {
35
36 namespace Internal
37 {
38
39 GestureEventProcessor::GestureEventProcessor(Stage& stage, Integration::GestureManager& gestureManager, Integration::RenderController& renderController)
40 : mStage(stage),
41   mGestureManager(gestureManager),
42   mLongPressGestureProcessor(stage, gestureManager),
43   mPanGestureProcessor(stage, gestureManager),
44   mPinchGestureProcessor(stage, gestureManager),
45   mTapGestureProcessor(stage, gestureManager),
46   mRenderController(renderController),
47   mUpdateRequired( false )
48 {
49 }
50
51 GestureEventProcessor::~GestureEventProcessor()
52 {
53 }
54
55 void GestureEventProcessor::ProcessGestureEvent(const Integration::GestureEvent& event)
56 {
57   if( Gesture::Started == event.state || Gesture::Continuing == event.state )
58   {
59     SetUpdateRequired();
60   }
61
62   switch(event.gestureType)
63   {
64     case Gesture::LongPress:
65       mLongPressGestureProcessor.Process(static_cast<const Integration::LongPressGestureEvent&>(event));
66       break;
67
68     case Gesture::Pan:
69       mPanGestureProcessor.Process(static_cast<const Integration::PanGestureEvent&>(event));
70       break;
71
72     case Gesture::Pinch:
73       mPinchGestureProcessor.Process(static_cast<const Integration::PinchGestureEvent&>(event));
74       break;
75
76     case Gesture::Tap:
77       mTapGestureProcessor.Process(static_cast<const Integration::TapGestureEvent&>(event));
78       break;
79   }
80 }
81
82 void GestureEventProcessor::AddGestureDetector(GestureDetector* gestureDetector)
83 {
84   switch (gestureDetector->GetType())
85   {
86     case Gesture::LongPress:
87     {
88       LongPressGestureDetector* longPress = static_cast<LongPressGestureDetector*>(gestureDetector);
89       mLongPressGestureProcessor.AddGestureDetector(longPress);
90       break;
91     }
92
93     case Gesture::Pan:
94     {
95       PanGestureDetector* pan = static_cast<PanGestureDetector*>(gestureDetector);
96       mPanGestureProcessor.AddGestureDetector(pan);
97       break;
98     }
99
100     case Gesture::Pinch:
101     {
102       PinchGestureDetector* pinch = static_cast<PinchGestureDetector*>(gestureDetector);
103       mPinchGestureProcessor.AddGestureDetector(pinch);
104       break;
105     }
106
107     case Gesture::Tap:
108     {
109       TapGestureDetector* tap = static_cast<TapGestureDetector*>(gestureDetector);
110       mTapGestureProcessor.AddGestureDetector(tap);
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       break;
125     }
126
127     case Gesture::Pan:
128     {
129       PanGestureDetector* pan = static_cast<PanGestureDetector*>(gestureDetector);
130       mPanGestureProcessor.RemoveGestureDetector(pan);
131       break;
132     }
133
134     case Gesture::Pinch:
135     {
136       PinchGestureDetector* pinch = static_cast<PinchGestureDetector*>(gestureDetector);
137       mPinchGestureProcessor.RemoveGestureDetector(pinch);
138       break;
139     }
140
141     case Gesture::Tap:
142     {
143       TapGestureDetector* tap = static_cast<TapGestureDetector*>(gestureDetector);
144       mTapGestureProcessor.RemoveGestureDetector(tap);
145       break;
146     }
147   }
148 }
149
150 void GestureEventProcessor::GestureDetectorUpdated(GestureDetector* gestureDetector)
151 {
152   switch (gestureDetector->GetType())
153   {
154     case Gesture::LongPress:
155     {
156       LongPressGestureDetector* longPress = static_cast<LongPressGestureDetector*>(gestureDetector);
157       mLongPressGestureProcessor.GestureDetectorUpdated(longPress);
158       break;
159     }
160
161     case Gesture::Pan:
162     {
163       PanGestureDetector* pan = static_cast<PanGestureDetector*>(gestureDetector);
164       mPanGestureProcessor.GestureDetectorUpdated(pan);
165       break;
166     }
167
168     case Gesture::Pinch:
169     {
170       PinchGestureDetector* pinch = static_cast<PinchGestureDetector*>(gestureDetector);
171       mPinchGestureProcessor.GestureDetectorUpdated(pinch);
172       break;
173     }
174
175     case Gesture::Tap:
176     {
177       TapGestureDetector* tap = static_cast<TapGestureDetector*>(gestureDetector);
178       mTapGestureProcessor.GestureDetectorUpdated(tap);
179       break;
180     }
181   }
182 }
183
184 void GestureEventProcessor::SetUpdateRequired()
185 {
186   mUpdateRequired = true;
187 }
188
189 void GestureEventProcessor::SetGestureProperties( const Gesture& gesture )
190 {
191   if( Gesture::Started == gesture.state || Gesture::Continuing == gesture.state )
192   {
193     SetUpdateRequired();
194
195     // We may not be updating so we need to ask the render controller for an update.
196     mRenderController.RequestUpdate();
197   }
198
199   switch ( gesture.type )
200   {
201     case Gesture::Pan:
202     {
203       const PanGesture& pan = static_cast< const PanGesture& >( gesture );
204       mPanGestureProcessor.SetPanGestureProperties( pan );
205       break;
206     }
207
208     case Gesture::LongPress:
209     case Gesture::Pinch:
210     case Gesture::Tap:
211     {
212       DALI_ASSERT_DEBUG( false && "Gesture type does not have scene object\n" );
213       break;
214     }
215   }
216 }
217
218 bool GestureEventProcessor::NeedsUpdate()
219 {
220   bool updateRequired( mUpdateRequired );
221
222   mUpdateRequired = false;
223
224   return updateRequired;
225 }
226
227 void GestureEventProcessor::EnablePanGestureProfiling()
228 {
229   mPanGestureProcessor.EnableProfiling();
230 }
231
232 void GestureEventProcessor::SetPanGesturePredictionMode(int mode)
233 {
234   mPanGestureProcessor.SetPredictionMode(mode);
235 }
236
237 void GestureEventProcessor::SetPanGesturePredictionAmount( float amount )
238 {
239   mPanGestureProcessor.SetPredictionAmount(amount);
240 }
241
242 } // namespace Internal
243
244 } // namespace Dali