1 #ifndef DALI_PAN_GESTURE_DETECTOR_H
2 #define DALI_PAN_GESTURE_DETECTOR_H
5 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <cstdint> // uint32_t
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>
34 namespace Internal DALI_INTERNAL
36 class PanGestureDetector;
40 * @addtogroup dali_core_events
45 * @brief This class looks for panning (or dragging) gestures.
47 * The user will be pressing one or more fingers on an actor while they pan it.
49 * The application programmer can use this gesture detector as follows:
51 * PanGestureDetector detector = PanGestureDetector::New();
52 * detector.Attach(myActor);
53 * detector.DetectedSignal().Connect(this, &MyApplication::OnPan);
55 * // Detect pan gesture for single and double touch.
56 * detector.SetMaximumTouchesRequired(2);
63 * | %Signal Name | Method |
64 * |--------------|-----------------------|
65 * | panDetected | @ref DetectedSignal() |
67 class DALI_CORE_API PanGestureDetector : public GestureDetector
71 * @brief Enumeration for the instance of properties belonging to the PanGestureDetector class.
77 * @brief Enumeration for the instance of properties belonging to the PanGestureDetector class.
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
93 using DetectedSignalType = Signal<void(Actor, const PanGesture&)>; ///< Pan gesture detected signal type @SINCE_1_0.0
96 using AngleThresholdPair = std::pair<Radian, Radian>; ///< Range of angles for a direction @SINCE_1_0.0
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().
105 static const Radian DEFAULT_THRESHOLD; ///< The default threshold is PI * 0.25 radians (or 45 degrees).
107 public: // Creation & Destruction
109 * @brief Creates an uninitialized PanGestureDetector; this can be initialized with PanGestureDetector::New().
111 * Calling member functions with an uninitialized PanGestureDetector handle is not allowed.
114 PanGestureDetector();
117 * @brief Creates an initialized PanGestureDetector.
120 * @return A handle to a newly allocated Dali resource
122 static PanGestureDetector New();
125 * @brief Downcasts a handle to PanGestureDetector handle.
127 * If handle points to a PanGestureDetector object, the
128 * downcast produces valid handle. If not, the returned handle is left uninitialized.
130 * @param[in] handle Handle to an object
131 * @return Handle to a PanGestureDetector object or an uninitialized handle
133 static PanGestureDetector DownCast(BaseHandle handle);
138 * This is non-virtual since derived Handle types must not contain data or virtual methods.
141 ~PanGestureDetector();
144 * @brief This copy constructor is required for (smart) pointer semantics.
147 * @param[in] handle A reference to the copied handle
149 PanGestureDetector(const PanGestureDetector& handle);
152 * @brief This assignment operator is required for (smart) pointer semantics.
155 * @param[in] rhs A reference to the copied handle
156 * @return A reference to this
158 PanGestureDetector& operator=(const PanGestureDetector& rhs);
162 * @brief This is the minimum number of touches required for the pan gesture to be detected.
165 * @param[in] minimum Minimum touches required
166 * @pre The gesture detector has been initialized.
167 * @note The default minimum is '1'.
169 void SetMinimumTouchesRequired(uint32_t minimum);
172 * @brief This is the maximum number of touches required for the pan gesture to be detected.
175 * @param[in] maximum Maximum touches required
176 * @pre The gesture detector has been initialized.
177 * @note The default maximum is '1'.
179 void SetMaximumTouchesRequired(uint32_t maximum);
182 * @brief This value is a maximum duration of motion can live on the pan gesture event queue.
183 * If duration exceed it, the motion event is discarded.
186 * @param[in] maximumAge Maximum age of motion events as milliseconds
187 * @pre The gesture detector has been initialized.
188 * @note The default maximumAge is 'std::numeric_limits<uint32_t>::max()'.
190 void SetMaximumMotionEventAge(uint32_t maximumAge);
194 * @brief Retrieves the minimum number of touches required for the pan gesture to be detected.
197 * @return The minimum touches required
198 * @pre The gesture detector has been initialized.
200 uint32_t GetMinimumTouchesRequired() const;
203 * @brief Retrieves the maximum number of touches required for the pan gesture to be detected.
206 * @return The maximum touches required
207 * @pre The gesture detector has been initialized.
209 uint32_t GetMaximumTouchesRequired() const;
212 * @brief Retrieves the maximum age for the pan gesture motion as milliseconds.
215 * @return The maximum age of motion events as milliseconds
216 * @pre The gesture detector has been initialized.
218 uint32_t GetMaximumMotionEventAge() const;
220 public: // Directional Panning
222 * @brief The pan gesture is only emitted if the pan occurs in the direction specified by this method with a +/- threshold allowance.
224 * The angle is from -180 -> 0 -> 180 degrees (or -M_PI -> 0 -> M_PI in radians) i.e:
227 * -90.0f ( -0.5f * PI )
230 * 180.0f ( PI ) ------------- 0.0f ( 0.0f )
233 * 90.0f ( 0.5f * PI )
236 * If an angle of 0.0 degrees is specified and the threshold is 45 degrees then the acceptable
237 * direction range is from -45 to 45 degrees.
240 * @param[in] angle The angle that pan should be allowed
241 * @param[in] threshold The threshold around that angle
243 * @pre The gesture detector has been initialized.
244 * @note The angle added using this API is only checked when the gesture first starts, after that,
245 * this detector will emit the gesture regardless of what angle the pan is moving.
246 * @note The user can add as many angles as they require.
247 * @note If an angle outside the range above is given, then it is wrapped within the range, i.e.
248 * 190 degrees becomes -170 degrees and 370 degrees becomes 10 degrees.
249 * @note As long as you specify the type, you can also pass in a Dali::Degree to this method.
250 * @note If no threshold is provided, then the default threshold (PI * 0.25) is used.
251 * @note If the threshold is greater than PI, then PI will be used as the threshold.
254 void AddAngle(Radian angle, Radian threshold = DEFAULT_THRESHOLD);
257 * @brief A helper method for adding bi-directional angles where the pan should take place.
259 * In other words, if 0 is requested, then PI will also be added so that we have both left and
263 * @param[in] direction The direction of panning required
264 * @param[in] threshold The threshold
266 * @pre The gesture detector has been initialized.
268 * @note If a direction outside the range above is given, then it is wrapped within the range, i.e.
269 * 190 degrees becomes -170 degrees and 370 degrees becomes 10 degrees.
270 * @note If no threshold is provided, then the default threshold (PI * 0.25) is used.
271 * @note If the threshold is greater than PI, then PI will be used as the threshold.
272 * @note As long as you specify the type, you can also pass in a Dali::Degree to this method.
276 void AddDirection(Radian direction, Radian threshold = DEFAULT_THRESHOLD);
279 * @brief Returns the count of angles that this pan gesture detector emits a signal.
283 * @pre The gesture detector has been initialized.
285 size_t GetAngleCount() const;
288 * @brief Returns the angle by index that this pan gesture detector emits a signal.
291 * @param[in] index The angle's index
292 * @return An angle threshold pair, or a zero valued angle pair when index is invalid
293 * @pre The gesture detector has been initialized.
294 * @pre The index is less than GetAngleCount()
296 AngleThresholdPair GetAngle(size_t index) const;
299 * @brief Clears any directional angles that are used by the gesture detector.
301 * After this, the pan gesture
302 * will be emitted for a pan in ANY direction.
304 * @pre The gesture detector has been initialized.
309 * @brief Removes the angle specified from the container.
312 * @param[in] angle The angle to remove
313 * @pre The gesture detector has been initialized.
314 * @note This will only remove the first instance of the angle found from the container.
315 * @note If an angle outside the range in AddAngle() is given, then the value is wrapped within
316 * the range and that is removed.
318 void RemoveAngle(Radian angle);
321 * @brief Removes the two angles that make up the direction from the container.
324 * @param[in] direction The direction to remove
325 * @pre The gesture detector has been initialized.
326 * @note If a direction outside the range in AddAngle() is given, then the value is wrapped within
327 * the range and that is removed.
329 void RemoveDirection(Radian direction);
333 * @brief This signal is emitted when the pan gesture is detected on the attached actor.
335 * A callback of the following type may be connected:
337 * void YourCallbackName( Actor actor, const PanGesture& gesture );
340 * @return The signal to connect to
341 * @pre The gesture detector has been initialized.
343 DetectedSignalType& DetectedSignal();
345 public: // Pan Properties Setters
347 * @brief Allows setting of the pan properties that are returned in constraints.
350 * @param[in] pan The pan gesture to set
351 * @note If a normal pan is taking place, then any value set is ignored.
353 static void SetPanGestureProperties(const PanGesture& pan);
355 public: // Not intended for Application developers
358 * @brief This constructor is used by PanGestureDetector::New() methods.
361 * @param[in] internal A pointer to a newly allocated Dali resource
363 explicit DALI_INTERNAL PanGestureDetector(Internal::PanGestureDetector* internal);
373 #endif // DALI_PAN_GESTURE_DETECTOR_H