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