[dali_2.3.27] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / events / tap-gesture / tap-gesture-recognizer.h
1 #ifndef DALI_INTERNAL_EVENT_EVENTS_TAP_GESTURE_RECOGNIZER_H
2 #define DALI_INTERNAL_EVENT_EVENTS_TAP_GESTURE_RECOGNIZER_H
3
4 /*
5  * Copyright (c) 2023 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 <dali/integration-api/events/point.h>
23 #include <dali/public-api/common/vector-wrapper.h>
24 #include <cstdint>
25
26 // INTERNAL INCLUDES
27 #include <dali/internal/event/events/gesture-recognizer.h>
28 #include <dali/internal/event/events/tap-gesture/tap-gesture-event.h>
29
30 namespace Dali
31 {
32 namespace Integration
33 {
34 struct TouchEvent;
35 }
36
37 namespace Internal
38 {
39 struct TapGestureRequest;
40
41 /**
42  * When given a set of touch events, this detector attempts to determine if a tap gesture has taken place.
43  */
44 class TapGestureRecognizer : public GestureRecognizer
45 {
46 public:
47   using Observer = RecognizerObserver<TapGestureEvent>;
48
49   /**
50    * Constructor
51    * @param[in] coreEventInterface Used to send events to Core.
52    * @param[in]  screenSize  The size of the screen.
53    * @param[in]  request     The tap gesture request.
54    * @param[in]  maximumAllowedTime    The maximum allowed time required in milliseconds.
55    * @param[in]  recognizerTime    This recognizer time required in milliseconds.
56    * @param[in]  maximumMotionAllowedDistance    This recognizer distance required.
57    */
58   TapGestureRecognizer(Observer& observer, Vector2 screenSize, const TapGestureRequest& request, uint32_t maximumAllowedTime, uint32_t recognizerTime, float maximumMotionAllowedDistance);
59
60   /**
61    * Virtual destructor.
62    */
63   ~TapGestureRecognizer() override;
64
65 public:
66   /**
67    * @copydoc Dali::Internal::GestureDetector::SendEvent(const Integration::TouchEvent&)
68    */
69   void SendEvent(const Integration::TouchEvent& event) override;
70
71   /**
72    * @copydoc Dali::Internal::GestureDetector::Update(const Integration::GestureRequest&)
73    */
74   void Update(const GestureRequest& request) override;
75
76   /**
77    * @brief This method sets the maximum allowed time required to be recognized as a multi tap gesture (millisecond)
78    *
79    * @param[in] time The time value in milliseconds
80    */
81   void SetMaximumAllowedTime(uint32_t time);
82
83   /**
84    * @brief This method sets the recognizer time required to be recognized as a tap gesture (millisecond)
85    *
86    * @param[in] time The time value in milliseconds
87    */
88   void SetRecognizerTime(uint32_t time);
89
90
91   /**
92    * @brief This method sets the recognizer distance required to be recognized as a tap gesture
93    *
94    * This distance is from touch down to touch up to recognize the tap gesture.
95    *
96    * @param[in] distance The distance
97    */
98   void SetMaximumMotionAllowedDistance(float distance);
99
100
101 private:
102   /**
103    * Checks if registered taps are within required bounds and emits tap gesture if they are.
104    *
105    * @param[in] state current state of incomplete gesture
106    * @param[in] time time of this latest touch event
107    */
108   void EmitGesture(GestureState state, uint32_t time);
109
110   /**
111    * Initialises tap gesture detector for next tap sequence
112    *
113    * @param[in] event registered touch event
114    * @param[in] point position touch event occurred
115    */
116   void SetupForTouchDown(const Integration::TouchEvent& event, const Integration::Point& point);
117
118   /**
119    * Emit a touch down event for hit testing
120    *
121    * @param[in] event registered touch event
122    */
123   void EmitPossibleState(const Integration::TouchEvent& event);
124
125   /**
126    * Force a touch event sequence to be treated as a single tap
127    *
128    * @param[in] time time of this latest touch event
129    * @param[in] point position touch event occurred
130     */
131   void EmitSingleTap(uint32_t time, const Integration::Point& point);
132
133   /**
134    * Emit a tap event
135    *
136    * @param[in] time time of this latest touch event
137    * @param[in] event registered touch event
138    */
139   void EmitTap(uint32_t time, TapGestureEvent& event);
140
141   /**
142    * Send the event for processing
143    *
144    * @param[in] tap event for processing
145    */
146   void ProcessEvent(TapGestureEvent& event);
147
148 private:
149   // Reference to the gesture processor for this recognizer
150   Observer& mObserver;
151
152   /**
153    * Internal state machine.
154    */
155   enum State
156   {
157     CLEAR,      ///< No gesture detected.
158     TOUCHED,    ///< User is touching the screen.
159     REGISTERED, ///< At least one tap has been registered.
160     FAILED,     ///< Gesture has failed.
161   };
162
163   State mState; ///< Current state of the detector.
164
165   Vector2  mTouchPosition;                ///< The initial touch down position.
166   uint32_t mTapsRegistered;               ///< In current detection, the number of taps registered.
167   uint32_t mTouchTime;                    ///< The touch down time.
168   uint32_t mLastTapTime;                  ///< Time last tap gesture was registered
169   uint32_t mDeltaBetweenTouchDownTouchUp; ///< Time from touchdown to touchup
170   uint32_t mMaximumAllowedTime;           ///< The maximum allowed time required to be recognized as a multi tap gesture (millisecond)
171   uint32_t mRecognizerTime;               ///< The recognizer time required to be recognized as a tap gesture (millisecond)
172   float    mMaximumMotionAllowedDistance; ///< The recognizer distance required to be recognized as a tap gesture
173 };
174
175 } // namespace Internal
176
177 } // namespace Dali
178
179 #endif // DALI_INTERNAL_EVENT_EVENTS_TAP_GESTURE_RECOGNIZER_H