Improved pan gesture prediction
[platform/core/uifw/dali-core.git] / dali / internal / update / gestures / scene-graph-pan-gesture.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_PAN_GESTURE_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_PAN_GESTURE_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/public-api/events/pan-gesture.h>
24 #include <dali/internal/update/common/property-owner.h>
25 #include <dali/internal/update/gestures/gesture-properties.h>
26
27 namespace Dali
28 {
29
30 struct PanGesture;
31
32 namespace Internal
33 {
34
35 struct PanGestureProfiling;
36
37 namespace SceneGraph
38 {
39
40 /**
41  * The latest pan gesture information is stored in this scene object.
42  */
43 class PanGesture : public PropertyOwner
44 {
45 public:
46
47   enum PredictionMode
48   {
49     PREDICTION_NONE = 0,
50     PREDICTION_1
51   };
52
53   enum SmoothingMode
54   {
55     SMOOTHING_NONE,           // no smoothing
56     SMOOTHING_LAST_VALUE,     // smooth between last value and latest value
57   };
58
59   static const PredictionMode DEFAULT_PREDICTION_MODE;
60   static const int NUM_PREDICTION_MODES;
61
62   static const SmoothingMode DEFAULT_SMOOTHING_MODE;
63   static const int NUM_SMOOTHING_MODES;
64
65   // Latest Pan Information
66
67   /**
68    * Only stores the information we actually require from Dali::PanGesture
69    */
70   struct PanInfo
71   {
72     /**
73      * Stores the velocity, displacement and position.
74      */
75     struct Info
76     {
77       Info()
78       {
79       }
80
81       /**
82        * Copy constructor
83        */
84       Info( const Info& rhs )
85       : velocity( rhs.velocity ),
86         displacement( rhs.displacement ),
87         position( rhs.position )
88       {
89       }
90
91       /**
92        * Assignment operator
93        */
94       Info& operator=( const Info& rhs )
95       {
96         velocity = rhs.velocity;
97         displacement = rhs.displacement;
98         position = rhs.position;
99
100         return *this;
101       }
102
103       // Data
104
105       Vector2 velocity;
106       Vector2 displacement;
107       Vector2 position;
108     };
109
110     /**
111      * Constructor
112      */
113     PanInfo()
114     : time( 0u ),
115       state( Gesture::Clear ),
116       read( true )
117     {
118     }
119
120     /**
121      * Copy constructor
122      */
123     PanInfo( const PanInfo& rhs )
124     : time( rhs.time ),
125       state( rhs.state ),
126       local( rhs.local ),
127       screen( rhs.screen ),
128       read( true )
129     {
130     }
131
132     /**
133      * Assignment operator
134      */
135     PanInfo& operator=( const PanInfo& rhs )
136     {
137       time = rhs.time;
138       state = rhs.state;
139       local = rhs.local;
140       screen = rhs.screen;
141
142       return *this;
143     }
144
145     /**
146      * Assignment operator
147      * @param[in] gesture A Dali::Gesture
148      */
149     PanInfo& operator=( const Dali::PanGesture& rhs )
150     {
151       time = rhs.time;
152       state = rhs.state;
153
154       local.velocity = rhs.velocity;
155       local.displacement = rhs.displacement;
156       local.position = rhs.position;
157
158       screen.velocity = rhs.screenVelocity;
159       screen.displacement = rhs.screenDisplacement;
160       screen.position = rhs.screenPosition;
161
162       return *this;
163     }
164
165     // Data
166     unsigned int time;
167     Gesture::State state;
168     Info local;
169     Info screen;
170     volatile bool read;
171   };
172
173   typedef std::vector<PanInfo> PanInfoHistory;
174   typedef PanInfoHistory::iterator PanInfoHistoryIter;
175   typedef PanInfoHistory::const_iterator PanInfoHistoryConstIter;
176
177 private:
178   static const unsigned int PAN_GESTURE_HISTORY = 10u;
179
180 public:
181
182   /**
183    * Create a new PanGesture
184    */
185   static PanGesture* New();
186
187   /**
188    * Virtual destructor
189    */
190   virtual ~PanGesture();
191
192   /**
193    * Adds a PanGesture to the internal circular-buffer waiting to be handled by UpdateProperties.
194    * @param[in]  gesture  The latest pan gesture.
195    */
196   void AddGesture( const Dali::PanGesture& gesture );
197
198   /**
199    * @brief Removes pan events from the history that are older than maxAge, leaving at least minEvents
200    *
201    * @param[in] panHistory The pan event history container
202    * @param[in] currentTime The current frame time
203    * @param[in] maxAge Maximum age of an event before removing (in millis)
204    * @param[in] minEvents The minimum number of events to leave in history, oldest events are removed before newest
205    */
206   void RemoveOldHistory(PanInfoHistory& panHistory, unsigned int currentTime, unsigned int maxAge, unsigned int minEvents);
207
208   /**
209    * Uses elapsed time and time stamps
210    */
211   void PredictiveAlgorithm1(int eventsThisFrame, PanInfo& gestureOut, PanInfoHistory& panHistory, unsigned int lastVSyncTime, unsigned int nextVSyncTime);
212
213   /**
214    * Uses last two gestures
215    *
216    * @param[in]  justStarted Whether the pan has just started.
217    * @param[out] gestureOut Output gesture using average values from last two gestures
218    * @param[in]  lastVSyncTime The time to set on gestureOut.
219    */
220   void SmoothingAlgorithm1(bool justStarted, PanInfo& gestureOut, unsigned int lastVSyncTime);
221
222   /**
223    * Future smoothing method, implementation not complete
224    */
225   void SmoothingAlgorithm2(bool justStarted, PanInfo& gestureOut, unsigned int lastVSyncTime);
226
227   /**
228    * Called by the update manager so that we can update the value of our properties.
229    * @param[in]  nextRenderTime  The estimated time of the next render (in milliseconds).
230    * @return true, if properties were updated.
231    */
232   virtual bool UpdateProperties( unsigned int lastRenderTime, unsigned int nextRenderTime );
233
234   /**
235    * Retrieves a reference to the panning flag property.
236    * @return The panning flag property.
237    */
238   const GesturePropertyBool& GetPanningProperty() const;
239
240   /**
241    * Retrieves a reference to the screen position property.
242    * @return The screen position property.
243    */
244   const GesturePropertyVector2& GetScreenPositionProperty() const;
245
246   /**
247    * Retrieves a reference to the screen velocity property.
248    * @return The screen velocity property.
249    */
250   const GesturePropertyVector2& GetScreenVelocityProperty() const;
251
252   /**
253    * Retrieves a reference to the screen displacement property.
254    * @return The screen displacement property.
255    */
256   const GesturePropertyVector2& GetScreenDisplacementProperty() const;
257
258   /**
259    * Retrieves a reference to the local position property.
260    * @return The local position property.
261    */
262   const GesturePropertyVector2& GetLocalPositionProperty() const;
263
264   /**
265    * Retrieves a reference to the local displacement property.
266    * @return The local displacement property.
267    */
268   const GesturePropertyVector2& GetLocalDisplacementProperty() const;
269
270   /**
271    * Retrieves a reference to the local velocity property.
272    * @return The local velocity property.
273    */
274   const GesturePropertyVector2& GetLocalVelocityProperty() const;
275
276   /**
277    * @brief Sets the prediction mode of the pan gesture
278    *
279    * @param[in] mode The prediction mode
280    */
281   void SetPredictionMode(PredictionMode mode);
282
283   /**
284    * @brief Sets the prediction amount of the pan gesture
285    *
286    * @param[in] amount The prediction amount in milliseconds
287    */
288   void SetPredictionAmount(unsigned int amount);
289
290   /**
291    * @brief Sets the upper bound of the prediction amount for clamping
292    *
293    * @param[in] amount The prediction amount in milliseconds
294    */
295   void SetMaximumPredictionAmount(unsigned int amount);
296
297   /**
298    * @brief Sets the lower bound of the prediction amount for clamping
299    *
300    * @param[in] amount The prediction amount in milliseconds
301    */
302   void SetMinimumPredictionAmount(unsigned int amount);
303
304   /**
305    * @brief Sets the amount of prediction interpolation to adjust when the pan velocity is changed
306    *
307    * @param[in] amount The prediction amount in milliseconds
308    */
309   void SetPredictionAmountAdjustment(unsigned int amount);
310
311   /**
312    * @brief Sets the prediction mode of the pan gesture
313    *
314    * @param[in] mode The prediction mode
315    */
316   void SetSmoothingMode(SmoothingMode mode);
317
318   /**
319    * @brief Sets the amount of smoothing to apply for the current smoothing mode
320    *
321    * @param[in] amount The amount of smoothing [0.0f,1.0f]
322    */
323   void SetSmoothingAmount(float amount);
324
325   /**
326    * Called to provide pan-gesture profiling information.
327    */
328   void EnableProfiling();
329
330 private:
331
332   /**
333    * Protected constructor.
334    */
335   PanGesture();
336
337   // Undefined
338   PanGesture(const PanGesture&);
339
340   // Undefined
341   PanGesture& operator=(const PanGesture&);
342
343   // PropertyOwner
344   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
345
346 private:
347
348   // Properties
349   GesturePropertyBool    mPanning;            ///< panning flag
350   GesturePropertyVector2 mScreenPosition;     ///< screen-position
351   GesturePropertyVector2 mScreenDisplacement; ///< screen-displacement
352   GesturePropertyVector2 mScreenVelocity;     ///< screen-velocity
353   GesturePropertyVector2 mLocalPosition;      ///< local-position
354   GesturePropertyVector2 mLocalDisplacement;  ///< local-displacement
355   GesturePropertyVector2 mLocalVelocity;      ///< local-velocity
356
357   PanInfo mGestures[PAN_GESTURE_HISTORY];         ///< Circular buffer storing the 4 most recent gestures.
358   PanInfoHistory mPanHistory;
359   PanInfoHistory mPredictionHistory;
360   unsigned int mWritePosition;  ///< The next PanInfo buffer to write to. (starts at 0)
361   unsigned int mReadPosition;   ///< The next PanInfo buffer to read. (starts at 0)
362
363   PanInfo mEventGesture;        ///< Result of all pan events received this frame
364   PanInfo mLastEventGesture;    ///< The last frame's event gesture.
365   PanInfo mLastGesture;         ///< The latest gesture. (this update frame)
366   PanInfo mLatestGesture;       ///< The latest gesture. (this update frame)
367   bool mInGesture;              ///< True if the gesture is currently being handled i.e. between Started <-> Finished/Cancelled
368
369   PredictionMode mPredictionMode;  ///< The pan gesture prediction mode
370   unsigned int mPredictionAmount;  ///< how far into future to predict in milliseconds
371   unsigned int mCurrentPredictionAmount;  ///< the current prediction amount used by the prediction algorithm
372   unsigned int mMaxPredictionAmount;  ///< the maximum prediction amount used by the prediction algorithm
373   unsigned int mMinPredictionAmount;  ///< the minimum prediction amount used by the prediction algorithm
374   unsigned int mPredictionAmountAdjustment;  ///< the prediction amount to adjust in milliseconds when pan velocity changes
375   SmoothingMode mSmoothingMode;    ///< The pan gesture prediction mode
376   float         mSmoothingAmount;  ///< How much smoothing to apply [0.0f,1.0f]
377   PanGestureProfiling* mProfiling; ///< NULL unless pan-gesture profiling information is required.
378 };
379
380 } // namespace SceneGraph
381
382 } // namespace Internal
383
384 } // namespace Dali
385
386 #endif // __DALI_INTERNAL_SCENE_GRAPH_PAN_GESTURE_H__