ec83a3887b3fe2e59802fcac381120c9c671ca22
[platform/core/uifw/dali-core.git] / dali / public-api / events / pan-gesture-detector.h
1 #ifndef __DALI_PAN_GESTURE_DETECTOR_H__
2 #define __DALI_PAN_GESTURE_DETECTOR_H__
3
4 /*
5  * Copyright (c) 2018 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/events/gesture-detector.h>
23 #include <dali/public-api/object/property-index-ranges.h>
24 #include <dali/public-api/signals/dali-signal.h>
25
26 namespace Dali
27 {
28
29 struct Radian;
30
31 namespace Internal DALI_INTERNAL
32 {
33 class PanGestureDetector;
34 }
35
36 struct PanGesture;
37
38 /**
39  * @addtogroup dali_core_events
40  * @{
41  */
42
43 /**
44  * @brief This class looks for panning (or dragging) gestures.
45  *
46  * The user will be pressing one or more fingers on an actor while they pan it.
47  *
48  * The application programmer can use this gesture detector as follows:
49  * @code
50  * PanGestureDetector detector = PanGestureDetector::New();
51  * detector.Attach(myActor);
52  * detector.DetectedSignal().Connect(this, &MyApplication::OnPan);
53  *
54  * // Detect pan gesture for single and double touch.
55  * detector.SetMaximumTouchesRequired(2);
56  * @endcode
57  *
58  * @SINCE_1_0.0
59  * @see PanGesture
60  *
61  * Signals
62  * | %Signal Name | Method                |
63  * |--------------|-----------------------|
64  * | panDetected  | @ref DetectedSignal() |
65  */
66 class DALI_CORE_API PanGestureDetector : public GestureDetector
67 {
68 public:
69
70   /**
71    * @brief Enumeration for the instance of properties belonging to the PanGestureDetector class.
72    * @SINCE_1_0.0
73    */
74   struct Property
75   {
76     /**
77      * @brief Enumeration for the instance of properties belonging to the PanGestureDetector class.
78      * @SINCE_1_0.0
79      */
80     enum
81     {
82       SCREEN_POSITION = DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX, ///< name "screenPosition",      type Vector2 @SINCE_1_0.0
83       SCREEN_DISPLACEMENT,                                             ///< name "screenDisplacement",  type Vector2 @SINCE_1_0.0
84       SCREEN_VELOCITY,                                                 ///< name "screenVelocity",      type Vector2 @SINCE_1_0.0
85       LOCAL_POSITION,                                                  ///< name "localPosition",       type Vector2 @SINCE_1_0.0
86       LOCAL_DISPLACEMENT,                                              ///< name "localDisplacement",   type Vector2 @SINCE_1_0.0
87       LOCAL_VELOCITY,                                                  ///< name "localVelocity",       type Vector2 @SINCE_1_0.0
88       PANNING,                                                         ///< name "panning",             type bool @SINCE_1_0.0
89     };
90   };
91
92   // Typedefs
93   typedef Signal< void ( Actor, const PanGesture& ) > DetectedSignalType; ///< Pan gesture detected signal type @SINCE_1_0.0
94
95   // Directional Pan
96   typedef std::pair< Radian, Radian > AngleThresholdPair; ///< Range of angles for a direction @SINCE_1_0.0
97
98   static const Radian DIRECTION_LEFT;       ///< For a left pan (-PI Radians).
99   static const Radian DIRECTION_RIGHT;      ///< For a right pan (0 Radians).
100   static const Radian DIRECTION_UP;         ///< For an up pan (-0.5 * PI Radians).
101   static const Radian DIRECTION_DOWN;       ///< For a down pan (0.5 * PI Radians).
102   static const Radian DIRECTION_HORIZONTAL; ///< For a left and right pan (PI Radians). Useful for AddDirection().
103   static const Radian DIRECTION_VERTICAL;   ///< For an up and down pan (-0.5 * PI Radians). Useful for AddDirection().
104
105   static const Radian DEFAULT_THRESHOLD;    ///< The default threshold is PI * 0.25 radians (or 45 degrees).
106
107 public: // Creation & Destruction
108
109   /**
110    * @brief Creates an uninitialized PanGestureDetector; this can be initialized with PanGestureDetector::New().
111    *
112    * Calling member functions with an uninitialized PanGestureDetector handle is not allowed.
113    * @SINCE_1_0.0
114    */
115   PanGestureDetector();
116
117   /**
118    * @brief Creates an initialized PanGestureDetector.
119    *
120    * @SINCE_1_0.0
121    * @return A handle to a newly allocated Dali resource
122    */
123   static PanGestureDetector New();
124
125   /**
126    * @brief Downcasts a handle to PanGestureDetector handle.
127    *
128    * If handle points to a PanGestureDetector object, the
129    * downcast produces valid handle. If not, the returned handle is left uninitialized.
130    * @SINCE_1_0.0
131    * @param[in] handle Handle to an object
132    * @return Handle to a PanGestureDetector object or an uninitialized handle
133    */
134   static PanGestureDetector DownCast( BaseHandle handle );
135
136   /**
137    * @brief Destructor.
138    *
139    * This is non-virtual since derived Handle types must not contain data or virtual methods.
140    * @SINCE_1_0.0
141    */
142   ~PanGestureDetector();
143
144   /**
145    * @brief This copy constructor is required for (smart) pointer semantics.
146    *
147    * @SINCE_1_0.0
148    * @param[in] handle A reference to the copied handle
149    */
150   PanGestureDetector(const PanGestureDetector& handle);
151
152   /**
153    * @brief This assignment operator is required for (smart) pointer semantics.
154    *
155    * @SINCE_1_0.0
156    * @param[in] rhs A reference to the copied handle
157    * @return A reference to this
158    */
159   PanGestureDetector& operator=(const PanGestureDetector& rhs);
160
161 public: // Setters
162
163   /**
164    * @brief This is the minimum number of touches required for the pan gesture to be detected.
165    *
166    * @SINCE_1_0.0
167    * @param[in] minimum Minimum touches required
168    * @pre The gesture detector has been initialized.
169    * @note The default minimum is '1'.
170    */
171   void SetMinimumTouchesRequired(unsigned int minimum);
172
173   /**
174    * @brief This is the maximum number of touches required for the pan gesture to be detected.
175    *
176    * @SINCE_1_0.0
177    * @param[in] maximum Maximum touches required
178    * @pre The gesture detector has been initialized.
179    * @note The default maximum is '1'.
180    */
181   void SetMaximumTouchesRequired(unsigned int maximum);
182
183 public: // Getters
184
185   /**
186    * @brief Retrieves the minimum number of touches required for the pan gesture to be detected.
187    *
188    * @SINCE_1_0.0
189    * @return The minimum touches required
190    * @pre The gesture detector has been initialized.
191    */
192   unsigned int GetMinimumTouchesRequired() const;
193
194   /**
195    * @brief Retrieves the maximum number of touches required for the pan gesture to be detected.
196    *
197    * @SINCE_1_0.0
198    * @return The maximum touches required
199    * @pre The gesture detector has been initialized.
200    */
201   unsigned int GetMaximumTouchesRequired() const;
202
203 public: // Directional Panning
204
205   /**
206    * @brief The pan gesture is only emitted if the pan occurs in the direction specified by this method with a +/- threshold allowance.
207    *
208    * The angle is from -180 -> 0 -> 180 degrees (or -M_PI -> 0 -> M_PI in radians) i.e:
209    *
210    * @code
211    *           -90.0f ( -0.5f * PI )
212    *                     |
213    *                     |
214    * 180.0f ( PI ) ------------- 0.0f ( 0.0f )
215    *                     |
216    *                     |
217    *            90.0f ( 0.5f * PI )
218    * @endcode
219    *
220    * If an angle of 0.0 degrees is specified and the threshold is 45 degrees then the acceptable
221    * direction range is from -45 to 45 degrees.
222    *
223    * @SINCE_1_0.0
224    * @param[in] angle     The angle that pan should be allowed
225    * @param[in] threshold The threshold around that angle
226    *
227    * @pre The gesture detector has been initialized.
228    * @note The angle added using this API is only checked when the gesture first starts, after that,
229    *       this detector will emit the gesture regardless of what angle the pan is moving.
230    * @note The user can add as many angles as they require.
231    * @note If an angle outside the range above is given, then it is wrapped within the range, i.e.
232    *       190 degrees becomes -170 degrees and 370 degrees becomes 10 degrees.
233    * @note As long as you specify the type, you can also pass in a Dali::Degree to this method.
234    * @note If no threshold is provided, then the default threshold (PI * 0.25) is used.
235    * @note If the threshold is greater than PI, then PI will be used as the threshold.
236    *
237    */
238   void AddAngle( Radian angle, Radian threshold = DEFAULT_THRESHOLD );
239
240   /**
241    * @brief A helper method for adding bi-directional angles where the pan should take place.
242    *
243    * In other words, if 0 is requested, then PI will also be added so that we have both left and
244    * right scrolling.
245    *
246    * @SINCE_1_0.0
247    * @param[in] direction The direction of panning required
248    * @param[in] threshold The threshold
249    *
250    * @pre The gesture detector has been initialized.
251    *
252    * @note If a direction outside the range above is given, then it is wrapped within the range, i.e.
253    *       190 degrees becomes -170 degrees and 370 degrees becomes 10 degrees.
254    * @note If no threshold is provided, then the default threshold (PI * 0.25) is used.
255    * @note If the threshold is greater than PI, then PI will be used as the threshold.
256    * @note As long as you specify the type, you can also pass in a Dali::Degree to this method.
257    *
258    * @see AddAngle
259    */
260   void AddDirection( Radian direction, Radian threshold = DEFAULT_THRESHOLD );
261
262   /**
263    * @brief Returns the count of angles that this pan gesture detector emits a signal.
264    *
265    * @SINCE_1_0.0
266    * @return The count
267    * @pre The gesture detector has been initialized.
268    */
269   size_t GetAngleCount() const;
270
271   /**
272    * @brief Returns the angle by index that this pan gesture detector emits a signal.
273    *
274    * @SINCE_1_0.0
275    * @param[in] index The angle's index
276    * @return An angle threshold pair, or a zero valued angle pair when index is invalid
277    * @pre The gesture detector has been initialized.
278    * @pre The index is less than GetAngleCount()
279    */
280   AngleThresholdPair GetAngle(size_t index) const;
281
282   /**
283    * @brief Clears any directional angles that are used by the gesture detector.
284    *
285    * After this, the pan gesture
286    * will be emitted for a pan in ANY direction.
287    * @SINCE_1_0.0
288    * @pre The gesture detector has been initialized.
289    */
290   void ClearAngles();
291
292   /**
293    * @brief Removes the angle specified from the container.
294    *
295    * @SINCE_1_0.0
296    * @param[in] angle The angle to remove
297    * @pre The gesture detector has been initialized.
298    * @note This will only remove the first instance of the angle found from the container.
299    * @note If an angle outside the range in AddAngle() is given, then the value is wrapped within
300    *       the range and that is removed.
301    */
302   void RemoveAngle( Radian angle );
303
304   /**
305    * @brief Removes the two angles that make up the direction from the container.
306    *
307    * @SINCE_1_0.0
308    * @param[in] direction The direction to remove
309    * @pre The gesture detector has been initialized.
310    * @note If a direction outside the range in AddAngle() is given, then the value is wrapped within
311    *       the range and that is removed.
312    */
313   void RemoveDirection( Radian direction );
314
315 public: // Signals
316
317   /**
318    * @brief This signal is emitted when the pan gesture is detected on the attached actor.
319    *
320    * A callback of the following type may be connected:
321    * @code
322    *   void YourCallbackName( Actor actor, const PanGesture& gesture );
323    * @endcode
324    * @SINCE_1_0.0
325    * @return The signal to connect to
326    * @pre The gesture detector has been initialized.
327    */
328   DetectedSignalType& DetectedSignal();
329
330 public: // Pan Properties Setters
331
332   /**
333    * @brief Allows setting of the pan properties that are returned in constraints.
334    *
335    * @SINCE_1_0.0
336    * @param[in] pan The pan gesture to set
337    * @note If a normal pan is taking place, then any value set is ignored.
338    */
339   static void SetPanGestureProperties( const PanGesture& pan );
340
341 public: // Not intended for Application developers
342
343   /// @cond internal
344   /**
345    * @brief This constructor is used by PanGestureDetector::New() methods.
346    *
347    * @SINCE_1_0.0
348    * @param[in] internal A pointer to a newly allocated Dali resource
349    */
350   explicit DALI_INTERNAL PanGestureDetector(Internal::PanGestureDetector* internal);
351   /// @endcond
352
353 };
354
355 /**
356  * @}
357  */
358
359 } // namespace Dali
360
361 #endif // __DALI_PAN_GESTURE_DETECTOR_H__