Allows updating MinimumDistance and MinimumPanEvents of pan gestures.
[platform/core/uifw/dali-core.git] / dali / internal / event / events / pan-gesture / pan-gesture-processor.h
1 #ifndef DALI_INTERNAL_EVENT_PAN_GESTURE_EVENT_PROCESSOR_H
2 #define DALI_INTERNAL_EVENT_PAN_GESTURE_EVENT_PROCESSOR_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 // INTERNAL INCLUDES
22 #include <dali/internal/event/events/gesture-processor.h>
23 #include <dali/internal/event/events/pan-gesture/pan-gesture-detector-impl.h>
24 #include <dali/internal/event/render-tasks/render-task-impl.h>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 class Stage;
31 class Scene;
32 struct GestureEvent;
33 struct PanGestureEvent;
34
35 namespace SceneGraph
36 {
37 class PanGesture;
38 class UpdateManager;
39 } // namespace SceneGraph
40
41 /**
42  * Pan Gesture Event Processing:
43  *
44  * When we receive a pan gesture event, we do the following:
45  * - Find the actor that requires a pan where the pan started from (i.e. the down position).
46  * - Emit the gesture if the event satisfies the detector conditions.
47  *
48  * The above is only checked when our gesture starts.  We continue sending the pan gesture to the
49  * same actor and detector until the pan ends or is cancelled.
50  */
51 class PanGestureProcessor : public GestureProcessor, public RecognizerObserver<PanGestureEvent>
52 {
53 public:
54   /**
55    * Create a pan gesture processor.
56    * @param[in] updateManager The Update Manager
57    */
58   PanGestureProcessor(SceneGraph::UpdateManager& updateManager);
59
60   /**
61    * Destructor
62    */
63   ~PanGestureProcessor() override;
64
65 public: // To be called by GestureEventProcessor
66   /**
67    * This method is called whenever a pan gesture event occurs.
68    * @param[in] scene The scene the pan gesture event occurs in.
69    * @param[in] panEvent The event that has occurred.
70    */
71   void Process(Scene& scene, const PanGestureEvent& panEvent) override;
72
73   /**
74    * Adds a gesture detector to this gesture processor.
75    * If this is the first gesture detector being added, then this method registers the required
76    * gesture with the adaptor.
77    * @param[in] gestureDetector The gesture detector being added.
78    * @param[in] scene           The scene the pan gesture event occurs in.
79    * @param[in] minDistance     The minimum required motion distance to start pan gesture. If this value is less than 0, we use default setuped distance.
80    * @param[in] minPanEvents    The minimum required motion event number to start pan gesture. If this value is less than 1, we use default setuped number.
81    */
82   void AddGestureDetector(PanGestureDetector* gestureDetector, Scene& scene, int32_t minDistance, int32_t minPanEvents);
83
84   /**
85    * Removes the specified gesture detector from this gesture processor.  If, after removing this
86    * gesture detector, there are no more gesture detectors registered, then this method unregisters
87    * the gesture from the adaptor.
88    * @param[in]  gestureDetector  The gesture detector being removed.
89    */
90   void RemoveGestureDetector(PanGestureDetector* gestureDetector);
91
92   /**
93    * This method updates the gesture detection parameters.
94    * @param[in]  gestureDetector  The gesture detector that has been updated.
95    */
96   void GestureDetectorUpdated(PanGestureDetector* gestureDetector);
97
98   /**
99    * Sets the pan gesture properties stored in the scene object directly,
100    * @param[in]  pan  The pan gesture to override the properties with.
101    * @return true if Core::Update required
102    * @note If we are already processing a normal pan, then this call is ignored.
103    */
104   bool SetPanGestureProperties(const Dali::PanGesture& pan);
105
106   /**
107    * Called to provide pan-gesture profiling information.
108    */
109   void EnableProfiling();
110
111   /**
112    * Called to set the prediction mode for pan gestures
113    *
114    * @param[in] mode The prediction mode
115    *
116    * Valid modes:
117    * 0 - No prediction
118    * 1 - Prediction using average acceleration
119    */
120   void SetPredictionMode(int mode);
121
122   /**
123    * @brief Sets the prediction amount of the pan gesture
124    *
125    * @param[in] amount The prediction amount in milliseconds
126    */
127   void SetPredictionAmount(unsigned int amount);
128
129   /**
130    * @brief Sets the upper bound of the prediction amount for clamping
131    *
132    * @param[in] amount The prediction amount in milliseconds
133    */
134   void SetMaximumPredictionAmount(unsigned int amount);
135
136   /**
137    * @brief Sets the lower bound of the prediction amount for clamping
138    *
139    * @param[in] amount The prediction amount in milliseconds
140    */
141   void SetMinimumPredictionAmount(unsigned int amount);
142
143   /**
144    * @brief Sets the amount of prediction interpolation to adjust when the pan velocity is changed
145    *
146    * @param[in] amount The prediction amount in milliseconds
147    */
148   void SetPredictionAmountAdjustment(unsigned int amount);
149
150   /**
151    * Called to set the prediction mode for pan gestures
152    *
153    * @param[in] mode The prediction mode
154    *
155    * Valid modes:
156    * 0 - No smoothing
157    * 1 - average between last 2 values
158    */
159   void SetSmoothingMode(int mode);
160
161   /**
162    * @brief Sets the smoothing amount of the pan gesture
163    *
164    * @param[in] amount The smotthing amount from 0.0f (none) to 1.0f (full)
165    */
166   void SetSmoothingAmount(float amount);
167
168   /*
169    * @brief Sets whether to use actual times of the real gesture and frames or not.
170    *
171    * @param[in] value True = use actual times, False = use perfect values
172    */
173   void SetUseActualTimes(bool value);
174
175   /**
176    * @brief Sets the interpolation time range (ms) of past points to use (with weights) when interpolating.
177    *
178    * @param[in] value Time range in ms
179    */
180   void SetInterpolationTimeRange(int value);
181
182   /**
183    * @brief Sets whether to use scalar only prediction, which when enabled, ignores acceleration.
184    *
185    * @param[in] value True = use scalar prediction only
186    */
187   void SetScalarOnlyPredictionEnabled(bool value);
188
189   /**
190    * @brief Sets whether to use two point prediction. This combines two interpolated points to get more steady acceleration and velocity values.
191    *
192    * @param[in] value True = use two point prediction
193    */
194   void SetTwoPointPredictionEnabled(bool value);
195
196   /**
197    * @brief Sets the time in the past to interpolate the second point when using two point interpolation.
198    *
199    * @param[in] value Time in past in ms
200    */
201   void SetTwoPointInterpolatePastTime(int value);
202
203   /**
204    * @brief Sets the two point velocity bias. This is the ratio of first and second points to use for velocity.
205    *
206    * @param[in] value 0.0f = 100% first point. 1.0f = 100% of second point.
207    */
208   void SetTwoPointVelocityBias(float value);
209
210   /**
211    * @brief Sets the two point acceleration bias. This is the ratio of first and second points to use for acceleration.
212    *
213    * @param[in] value 0.0f = 100% first point. 1.0f = 100% of second point.
214    */
215   void SetTwoPointAccelerationBias(float value);
216
217   /**
218    * @brief Sets the range of time (ms) of points in the history to perform multitap smoothing with (if enabled).
219    *
220    * @param[in] value Time in past in ms
221    */
222   void SetMultitapSmoothingRange(int value);
223
224   /**
225    * This method sets the minimum distance to start a pan
226    * @param[in] value The distance in pixels
227    */
228   void SetMinimumDistance(int32_t value);
229
230   /**
231    * Sets the minimum touch events required before a pan can be started
232    * @param[in] value The number of touch events
233    */
234   void SetMinimumPanEvents(int32_t value);
235
236 public: // for PanGestureDetector
237   /**
238    * @return the pan gesture scene object
239    */
240   const SceneGraph::PanGesture& GetSceneObject() const;
241
242 private:
243   // Undefined
244   PanGestureProcessor(const PanGestureProcessor&);
245   PanGestureProcessor& operator=(const PanGestureProcessor& rhs);
246
247   /**
248    * Iterates through our GestureDetectors and determines if we need to ask the adaptor to update
249    * its detection policy.  If it does, it sends the appropriate gesture update request to adaptor.
250    */
251   void UpdateDetection();
252
253   /**
254    * Creates a PanGesture and asks the specified detector to emit its detected signal.
255    * @param[in]  actor             The actor that has been panned.
256    * @param[in]  gestureDetectors  The gesture detector container that should emit the signal.
257    * @param[in]  panEvent          The panEvent received from the adaptor.
258    * @param[in]  localCurrent      Current position relative to the actor attached to the detector.
259    * @param[in]  state             The state of the gesture.
260    * @param[in]  renderTask        The renderTask to use.
261    */
262   void EmitPanSignal(Actor*                          actor,
263                      const GestureDetectorContainer& gestureDetectors,
264                      const PanGestureEvent&          panEvent,
265                      Vector2                         localCurrent,
266                      GestureState                    state,
267                      RenderTaskPtr                   renderTask);
268
269   // GestureProcessor overrides
270
271   /**
272    * @copydoc GestureProcessor::OnGesturedActorStageDisconnection()
273    */
274   void OnGesturedActorStageDisconnection() override;
275
276   /**
277    * @copydoc GestureProcessor::CheckGestureDetector()
278    */
279   bool CheckGestureDetector(GestureDetector* detector, Actor* actor) override;
280
281   /**
282    * @copydoc GestureProcessor::EmitGestureSignal()
283    */
284   void EmitGestureSignal(Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates) override;
285
286 private:
287   PanGestureDetectorContainer mPanGestureDetectors;
288   GestureDetectorContainer    mCurrentPanEmitters;
289   RenderTaskPtr               mCurrentRenderTask;
290   Vector2                     mPossiblePanPosition;
291
292   uint32_t mMinTouchesRequired;
293   uint32_t mMaxTouchesRequired;
294   uint32_t mMaxMotionEventAge;
295
296   Vector2 mLastVelocity;       ///< The last recorded velocity in local actor coordinates.
297   Vector2 mLastScreenVelocity; ///< The last recorded velocity in screen coordinates.
298
299   const PanGestureEvent*  mCurrentPanEvent; ///< Pointer to current PanEvent, used when calling ProcessAndEmit()
300   SceneGraph::PanGesture* mSceneObject;     ///< Not owned, but we write to it directly
301 };
302
303 } // namespace Internal
304
305 } // namespace Dali
306
307 #endif // DALI_INTERNAL_EVENT_PAN_GESTURE_EVENT_PROCESSOR_H