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