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