(PanGesture) Added pan velocities as a property.
[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[in]  justStarted Whether the pan has just started.
205    * @param[out] gestureOut Output gesture using average values from last two gestures
206    * @param[in]  lastVSyncTime The time to set on gestureOut.
207    */
208   void SimpleAverageAlgorithm(bool justStarted, PanInfo& gestureOut, unsigned int lastVSyncTime);
209
210   /**
211    * Uses elapsed time and time stamps
212    */
213   void PredictiveAlgorithm1(int eventsThisFrame, PanInfo& gestureOut, PanInfoHistory& panHistory, unsigned int lastVSyncTime, unsigned int nextVSyncTime);
214
215   /**
216    * Uses elapsed time, time stamps and future render time
217    */
218   void PredictiveAlgorithm2(int eventsThisFrame, PanInfo& gestureOut, PanInfoHistory& panHistory, unsigned int lastVSyncTime, unsigned int nextVSyncTime);
219
220   /**
221    * Called by the update manager so that we can update the value of our properties.
222    * @param[in]  nextRenderTime  The estimated time of the next render (in milliseconds).
223    * @return true, if properties were updated.
224    */
225   virtual bool UpdateProperties( unsigned int lastRenderTime, unsigned int nextRenderTime );
226
227   /**
228    * Retrieves a reference to the panning flag property.
229    * @return The panning flag property.
230    */
231   const GesturePropertyBool& GetPanningProperty() const;
232
233   /**
234    * Retrieves a reference to the screen position property.
235    * @return The screen position property.
236    */
237   const GesturePropertyVector2& GetScreenPositionProperty() const;
238
239   /**
240    * Retrieves a reference to the screen velocity property.
241    * @return The screen velocity property.
242    */
243   const GesturePropertyVector2& GetScreenVelocityProperty() const;
244
245   /**
246    * Retrieves a reference to the screen displacement property.
247    * @return The screen displacement property.
248    */
249   const GesturePropertyVector2& GetScreenDisplacementProperty() const;
250
251   /**
252    * Retrieves a reference to the local position property.
253    * @return The local position property.
254    */
255   const GesturePropertyVector2& GetLocalPositionProperty() const;
256
257   /**
258    * Retrieves a reference to the local displacement property.
259    * @return The local displacement property.
260    */
261   const GesturePropertyVector2& GetLocalDisplacementProperty() const;
262
263   /**
264    * Retrieves a reference to the local velocity property.
265    * @return The local velocity property.
266    */
267   const GesturePropertyVector2& GetLocalVelocityProperty() const;
268
269   /**
270    * @brief Sets the prediction mode of the pan gesture
271    *
272    * @param[in] mode The prediction mode
273    */
274   void SetPredictionMode(PredictionMode mode);
275
276   /**
277    * @brief Sets the prediction amount of the pan gesture
278    *
279    * @param[in] amount The prediction amount in milliseconds
280    */
281   void SetPredictionAmount(unsigned int amount);
282
283   /**
284    * Called to provide pan-gesture profiling information.
285    */
286   void EnableProfiling();
287
288 private:
289
290   /**
291    * Protected constructor.
292    */
293   PanGesture();
294
295   // Undefined
296   PanGesture(const PanGesture&);
297
298   // Undefined
299   PanGesture& operator=(const PanGesture&);
300
301   // PropertyOwner
302   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
303
304 private:
305
306   // Properties
307   GesturePropertyBool    mPanning;            ///< panning flag
308   GesturePropertyVector2 mScreenPosition;     ///< screen-position
309   GesturePropertyVector2 mScreenDisplacement; ///< screen-displacement
310   GesturePropertyVector2 mScreenVelocity;     ///< screen-velocity
311   GesturePropertyVector2 mLocalPosition;      ///< local-position
312   GesturePropertyVector2 mLocalDisplacement;  ///< local-displacement
313   GesturePropertyVector2 mLocalVelocity;      ///< local-velocity
314
315   PanInfo mGestures[PAN_GESTURE_HISTORY];         ///< Circular buffer storing the 4 most recent gestures.
316   PanInfoHistory mPanHistory;
317   unsigned int mWritePosition;  ///< The next PanInfo buffer to write to. (starts at 0)
318   unsigned int mReadPosition;   ///< The next PanInfo buffer to read. (starts at 0)
319
320   PanInfo mEventGesture;        ///< Result of all pan events received this frame
321   PanInfo mLastEventGesture;    ///< The last frame's event gesture.
322   PanInfo mLatestGesture;       ///< The latest gesture. (this update frame)
323   bool mInGesture;              ///< True if the gesture is currently being handled i.e. between Started <-> Finished/Cancelled
324
325   PredictionMode mPredictionMode;  ///< The pan gesture prediction mode
326   unsigned int mPredictionAmount;  ///< how far into future to predict in milliseconds
327   PanGestureProfiling* mProfiling; ///< NULL unless pan-gesture profiling information is required.
328 };
329
330 } // namespace SceneGraph
331
332 } // namespace Internal
333
334 } // namespace Dali
335
336 #endif // __DALI_INTERNAL_SCENE_GRAPH_PAN_GESTURE_H__