Pan Gesture Prediction - Fixed issue where initial displacement could be close to...
[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     NONE,
50     AVERAGE,
51     PREDICTION_1,
52     PREDICTION_2
53   };
54
55   static const PredictionMode DEFAULT_PREDICTION_MODE;
56   static const int NUM_PREDICTION_MODES;
57
58   // Latest Pan Information
59
60   /**
61    * Only stores the information we actually require from Dali::PanGesture
62    */
63   struct PanInfo
64   {
65     /**
66      * Stores the velocity, displacement and position.
67      */
68     struct Info
69     {
70       Info()
71       {
72       }
73
74       /**
75        * Copy constructor
76        */
77       Info( const Info& rhs )
78       : velocity( rhs.velocity ),
79         displacement( rhs.displacement ),
80         position( rhs.position )
81       {
82       }
83
84       /**
85        * Assignment operator
86        */
87       Info& operator=( const Info& rhs )
88       {
89         velocity = rhs.velocity;
90         displacement = rhs.displacement;
91         position = rhs.position;
92
93         return *this;
94       }
95
96       // Data
97
98       Vector2 velocity;
99       Vector2 displacement;
100       Vector2 position;
101     };
102
103     /**
104      * Constructor
105      */
106     PanInfo()
107     : time( 0u ),
108       state( Gesture::Clear ),
109       read( true )
110     {
111     }
112
113     /**
114      * Copy constructor
115      */
116     PanInfo( const PanInfo& rhs )
117     : time( rhs.time ),
118       state( rhs.state ),
119       local( rhs.local ),
120       screen( rhs.screen ),
121       read( true )
122     {
123     }
124
125     /**
126      * Assignment operator
127      */
128     PanInfo& operator=( const PanInfo& rhs )
129     {
130       time = rhs.time;
131       state = rhs.state;
132       local = rhs.local;
133       screen = rhs.screen;
134
135       return *this;
136     }
137
138     /**
139      * Assignment operator
140      * @param[in] gesture A Dali::Gesture
141      */
142     PanInfo& operator=( const Dali::PanGesture& rhs )
143     {
144       time = rhs.time;
145       state = rhs.state;
146
147       local.velocity = rhs.velocity;
148       local.displacement = rhs.displacement;
149       local.position = rhs.position;
150
151       screen.velocity = rhs.screenVelocity;
152       screen.displacement = rhs.screenDisplacement;
153       screen.position = rhs.screenPosition;
154
155       return *this;
156     }
157
158     // Data
159     unsigned int time;
160     Gesture::State state;
161     Info local;
162     Info screen;
163     volatile bool read;
164   };
165
166   typedef std::vector<PanInfo> PanInfoHistory;
167   typedef PanInfoHistory::iterator PanInfoHistoryIter;
168   typedef PanInfoHistory::const_iterator PanInfoHistoryConstIter;
169
170 private:
171   static const unsigned int PAN_GESTURE_HISTORY = 10u;
172
173 public:
174
175   /**
176    * Create a new PanGesture
177    */
178   static PanGesture* New();
179
180   /**
181    * Virtual destructor
182    */
183   virtual ~PanGesture();
184
185   /**
186    * Adds a PanGesture to the internal circular-buffer waiting to be handled by UpdateProperties.
187    * @param[in]  gesture  The latest pan gesture.
188    */
189   void AddGesture( const Dali::PanGesture& gesture );
190
191   /**
192    * @brief Removes pan events from the history that are older than maxAge, leaving at least minEvents
193    *
194    * @param[in] panHistory The pan event history container
195    * @param[in] currentTime The current frame time
196    * @param[in] maxAge Maximum age of an event before removing (in millis)
197    * @param[in] minEvents The minimum number of events to leave in history, oldest events are removed before newest
198    */
199   void RemoveOldHistory(PanInfoHistory& panHistory, unsigned int currentTime, unsigned int maxAge, unsigned int minEvents);
200
201   /**
202    * USes last two gestures
203    *
204    * @param[out] gestureOut Output gesture using average values from last two gestures
205    */
206   void SimpleAverageAlgorithm(bool justStarted, PanInfo& gestureOut);
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 elapsed time, time stamps and future render time
215    */
216   void PredictiveAlgorithm2(int eventsThisFrame, PanInfo& gestureOut, PanInfoHistory& panHistory, unsigned int lastVSyncTime, unsigned int nextVSyncTime);
217
218   /**
219    * Called by the update manager so that we can update the value of our properties.
220    * @param[in]  nextRenderTime  The estimated time of the next render (in milliseconds).
221    * @return true, if properties were updated.
222    */
223   virtual bool UpdateProperties( unsigned int lastRenderTime, unsigned int nextRenderTime );
224
225   /**
226    * Retrieves a reference to the panning flag property.
227    * @return The panning flag property.
228    */
229   const GesturePropertyBool& GetPanningProperty() const;
230
231   /**
232    * Retrieves a reference to the screen position property.
233    * @return The screen position property.
234    */
235   const GesturePropertyVector2& GetScreenPositionProperty() const;
236
237   /**
238    * Retrieves a reference to the screen displacement property.
239    * @return The screen displacement property.
240    */
241   const GesturePropertyVector2& GetScreenDisplacementProperty() const;
242
243   /**
244    * Retrieves a reference to the local position property.
245    * @return The local position property.
246    */
247   const GesturePropertyVector2& GetLocalPositionProperty() const;
248
249   /**
250    * Retrieves a reference to the local displacement property.
251    * @return The local displacement property.
252    */
253   const GesturePropertyVector2& GetLocalDisplacementProperty() const;
254
255   /**
256    * @brief Sets the prediction mode of the pan gesture
257    *
258    * @param[in] mode The prediction mode
259    */
260   void SetPredictionMode(PredictionMode mode);
261
262   /**
263    * @brief Sets the prediction amount of the pan gesture
264    *
265    * @param[in] amount The prediction amount in milliseconds
266    */
267   void SetPredictionAmount(unsigned int amount);
268
269   /**
270    * Called to provide pan-gesture profiling information.
271    */
272   void EnableProfiling();
273
274 private:
275
276   /**
277    * Protected constructor.
278    */
279   PanGesture();
280
281   // Undefined
282   PanGesture(const PanGesture&);
283
284   // Undefined
285   PanGesture& operator=(const PanGesture&);
286
287   // PropertyOwner
288   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
289
290 private:
291
292   // Properties
293   GesturePropertyBool    mPanning;            ///< panning flag
294   GesturePropertyVector2 mScreenPosition;     ///< screen-position
295   GesturePropertyVector2 mScreenDisplacement; ///< screen-displacement
296   GesturePropertyVector2 mLocalPosition;      ///< local-position
297   GesturePropertyVector2 mLocalDisplacement;  ///< local-displacement
298
299   PanInfo mGestures[PAN_GESTURE_HISTORY];         ///< Circular buffer storing the 4 most recent gestures.
300   PanInfoHistory mPanHistory;
301   unsigned int mWritePosition;  ///< The next PanInfo buffer to write to. (starts at 0)
302   unsigned int mReadPosition;   ///< The next PanInfo buffer to read. (starts at 0)
303
304   PanInfo mEventGesture;        ///< Result of all pan events received this frame
305   PanInfo mLastEventGesture;    ///< The last frame's event gesture.
306   PanInfo mLatestGesture;       ///< The latest gesture. (this update frame)
307   bool mInGesture;              ///< True if the gesture is currently being handled i.e. between Started <-> Finished/Cancelled
308
309   PredictionMode mPredictionMode;  ///< The pan gesture prediction mode
310   unsigned int mPredictionAmount;  ///< how far into future to predict in milliseconds
311   PanGestureProfiling* mProfiling; ///< NULL unless pan-gesture profiling information is required.
312 };
313
314 } // namespace SceneGraph
315
316 } // namespace Internal
317
318 } // namespace Dali
319
320 #endif // __DALI_INTERNAL_SCENE_GRAPH_PAN_GESTURE_H__