Merge "Add input source type and source data about Gesture. This is similar to MouseB...
[platform/core/uifw/dali-core.git] / dali / internal / event / events / tap-gesture / tap-gesture-recognizer.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/events/tap-gesture/tap-gesture-recognizer.h>
20
21 // EXTERNAL INCLUDES
22 #include <cmath>
23
24 #include <dali/public-api/math/vector2.h>
25
26 #include <dali/integration-api/events/touch-event-integ.h>
27
28 // INTERNAL INCLUDES
29 #include <dali/internal/event/common/scene-impl.h>
30 #include <dali/internal/event/events/gesture-requests.h>
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 namespace
37 {
38 // TODO: Set these according to DPI
39 constexpr float MAXIMUM_MOTION_ALLOWED = 20.0f;
40 } // unnamed namespace
41
42 TapGestureRecognizer::TapGestureRecognizer(Observer& observer, Vector2 screenSize, const TapGestureRequest& request, uint32_t maximumAllowedTime)
43 : GestureRecognizer(screenSize, GestureType::TAP),
44   mObserver(observer),
45   mState(CLEAR),
46   mMinimumTapsRequired(request.minTaps),
47   mMaximumTapsRequired(request.maxTaps),
48   mTapsRegistered(0),
49   mTouchPosition(),
50   mTouchTime(0u),
51   mLastTapTime(0u),
52   mLastTouchTime(0u),
53   mMaximumAllowedTime(maximumAllowedTime)
54 {
55 }
56
57 TapGestureRecognizer::~TapGestureRecognizer() = default;
58
59 void TapGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
60 {
61   GestureRecognizerPtr ptr(this); // To keep us from being destroyed during the life-time of this method
62
63   if(event.GetPointCount() == 1)
64   {
65     const Integration::Point& point      = event.points[0];
66     PointState::Type          pointState = point.GetState();
67
68     switch(mState)
69     {
70       case CLEAR:
71       {
72         if(pointState == PointState::DOWN)
73         {
74           SetupForTouchDown(event, point);
75           mLastTouchTime = event.time;
76         }
77         break;
78       }
79
80       case TOUCHED:
81       {
82         uint32_t deltaBetweenTouchDownTouchUp = event.time - mTouchTime;
83
84         if(pointState == PointState::UP)
85         {
86           if(deltaBetweenTouchDownTouchUp < mMaximumAllowedTime)
87           {
88             mLastTapTime = event.time;
89             EmitSingleTap(event.time, point);
90             mState = REGISTERED;
91           }
92           else
93           {
94             mState = CLEAR;
95           }
96         }
97         else if(pointState == PointState::INTERRUPTED)
98         {
99           mState = CLEAR;
100         }
101         break;
102       }
103
104       case REGISTERED:
105       {
106         if(pointState == PointState::UP)
107         {
108           uint32_t deltaBetweenLastTouchDownTouchUp = event.time - mLastTouchTime;
109           // Clear if the time between touch down and touch up is long.
110           if(deltaBetweenLastTouchDownTouchUp > mMaximumAllowedTime)
111           {
112             mState = CLEAR;
113           }
114           else
115           {
116             mLastTapTime = event.time;
117             EmitGesture(GestureState::STARTED, event.time);
118           }
119         }
120         else if(pointState == PointState::DOWN)
121         {
122           const Vector2& screen(point.GetScreenPosition());
123           Vector2        distanceDelta(std::abs(mTouchPosition.x - screen.x),
124                                 std::abs(mTouchPosition.y - screen.y));
125
126           uint32_t deltaBetweenInitTouchDownCurrentTouchDown = event.time - mTouchTime;
127           uint32_t timeDelta                                 = event.time - mLastTapTime;
128           mLastTouchTime                                     = event.time;
129           ++mTapsRegistered;
130
131           if(distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
132              distanceDelta.y > MAXIMUM_MOTION_ALLOWED ||
133              timeDelta > mMaximumAllowedTime ||                                 // If the time between tabs is long, it starts over from SetupForTouchDown.
134              deltaBetweenInitTouchDownCurrentTouchDown > mMaximumAllowedTime || // If it times out compared to the first touchdown time, it starts over from SetupForTouchDown.
135              mTapsRegistered > mMaximumTapsRequired)                            // If it is greater than MaximumTapsRequired, it starts over from SetupForTouchDown.
136           {
137             SetupForTouchDown(event, point);
138           }
139           else
140           {
141             EmitPossibleState(event);
142           }
143         }
144         break;
145       }
146
147       case FAILED:
148       default:
149       {
150         mState = CLEAR;
151         break;
152       }
153     }
154   }
155   else
156   {
157     mState = FAILED;
158
159     // We have entered a multi-touch event so emit registered gestures if required.
160     EmitGesture(GestureState::STARTED, event.time);
161   }
162 }
163
164 void TapGestureRecognizer::SetupForTouchDown(const Integration::TouchEvent& event, const Integration::Point& point)
165 {
166   mTouchPosition  = point.GetScreenPosition();
167   mTouchTime      = event.time;
168   mLastTapTime    = 0u;
169   mTapsRegistered = 0;
170   mState          = TOUCHED;
171
172   EmitPossibleState(event);
173 }
174
175 void TapGestureRecognizer::EmitPossibleState(const Integration::TouchEvent& event)
176 {
177   TapGestureEvent tapEvent(GestureState::POSSIBLE);
178   tapEvent.point = mTouchPosition;
179   tapEvent.time  = event.time;
180
181   ProcessEvent(tapEvent);
182 }
183
184 void TapGestureRecognizer::Update(const GestureRequest& request)
185 {
186   const TapGestureRequest& tap = static_cast<const TapGestureRequest&>(request);
187
188   mMinimumTapsRequired = tap.minTaps;
189   mMaximumTapsRequired = tap.maxTaps;
190 }
191
192 void TapGestureRecognizer::SetMaximumAllowedTime(uint32_t time)
193 {
194   mMaximumAllowedTime = time;
195 }
196
197 void TapGestureRecognizer::EmitGesture(GestureState state, uint32_t time)
198 {
199   if((state == GestureState::CANCELLED) ||
200      (mTapsRegistered >= mMinimumTapsRequired && mTapsRegistered <= mMaximumTapsRequired))
201
202   {
203     TapGestureEvent event(state);
204     EmitTap(time, event);
205   }
206 }
207
208 void TapGestureRecognizer::EmitSingleTap(uint32_t time, const Integration::Point& point)
209 {
210   TapGestureEvent event(GestureState::STARTED);
211   const Vector2&  screen(point.GetScreenPosition());
212   Vector2         distanceDelta(std::abs(mTouchPosition.x - screen.x),
213                         std::abs(mTouchPosition.y - screen.y));
214
215   if(distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
216      distanceDelta.y > MAXIMUM_MOTION_ALLOWED)
217   {
218     event.state = GestureState::CANCELLED;
219   }
220   mTapsRegistered = 1u;
221   EmitTap(time, event);
222 }
223
224 void TapGestureRecognizer::EmitTap(uint32_t time, TapGestureEvent& event)
225 {
226   event.numberOfTaps = mTapsRegistered;
227   event.point        = mTouchPosition;
228   event.time         = time;
229
230   ProcessEvent(event);
231 }
232
233 void TapGestureRecognizer::ProcessEvent(TapGestureEvent& event)
234 {
235   event.sourceType = mSourceType;
236   event.sourceData = mSourceData;
237   if(mScene)
238   {
239     // Create another handle so the recognizer cannot be destroyed during process function
240     GestureRecognizerPtr recognizerHandle = this;
241     mObserver.Process(*mScene, event);
242   }
243 }
244
245 } // namespace Internal
246
247 } // namespace Dali