Revert "License conversion from Flora to Apache 2.0"
[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    * @return true, if properties were updated.
221    */
222   virtual bool UpdateProperties( unsigned int lastRenderTime, unsigned int nextRenderTime );
223
224   /**
225    * Retrieves a reference to the screen position property.
226    * @return The screen position property.
227    */
228   const GesturePropertyVector2& GetScreenPositionProperty() const;
229
230   /**
231    * Retrieves a reference to the screen displacement property.
232    * @return The screen displacement property.
233    */
234   const GesturePropertyVector2& GetScreenDisplacementProperty() const;
235
236   /**
237    * Retrieves a reference to the local position property.
238    * @return The local position property.
239    */
240   const GesturePropertyVector2& GetLocalPositionProperty() const;
241
242   /**
243    * Retrieves a reference to the local displacement property.
244    * @return The local displacement property.
245    */
246   const GesturePropertyVector2& GetLocalDisplacementProperty() const;
247
248   /**
249    * @brief Sets the prediction mode of the pan gesture
250    *
251    * @param[in] mode The prediction mode
252    */
253   void SetPredictionMode(PredictionMode mode) { mPredictionMode = mode; }
254
255   /**
256    * Called to provide pan-gesture profiling information.
257    */
258   void EnableProfiling();
259
260 private:
261
262   /**
263    * Protected constructor.
264    */
265   PanGesture();
266
267   // Undefined
268   PanGesture(const PanGesture&);
269
270   // Undefined
271   PanGesture& operator=(const PanGesture&);
272
273   // PropertyOwner
274   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
275
276 private:
277
278   // Properties
279   GesturePropertyVector2 mScreenPosition;     ///< screen-position
280   GesturePropertyVector2 mScreenDisplacement; ///< screen-displacement
281   GesturePropertyVector2 mLocalPosition;      ///< local-position
282   GesturePropertyVector2 mLocalDisplacement;  ///< local-displacement
283
284   PanInfo mGestures[PAN_GESTURE_HISTORY];         ///< Circular buffer storing the 4 most recent gestures.
285   PanInfoHistory mPanHistory;
286   unsigned int mWritePosition;  ///< The next PanInfo buffer to write to. (starts at 0)
287   unsigned int mReadPosition;   ///< The next PanInfo buffer to read. (starts at 0)
288
289   PanInfo mEventGesture;        ///< Result of all pan events received since last frame
290   PanInfo mLatestGesture;       ///< The latest gesture. (this update frame)
291   bool mInGesture;              ///< True if the gesture is currently being handled i.e. between Started <-> Finished/Cancelled
292
293   PredictionMode mPredictionMode;  ///< The pan gesture prediction mode
294   PanGestureProfiling* mProfiling; ///< NULL unless pan-gesture profiling information is required.
295 };
296
297 } // namespace SceneGraph
298
299 } // namespace Internal
300
301 } // namespace Dali
302
303 #endif // __DALI_INTERNAL_SCENE_GRAPH_PAN_GESTURE_H__