Define trace macro with message generate function
[platform/core/uifw/dali-core.git] / dali / internal / event / events / tap-gesture / tap-gesture-processor.cpp
1 /*
2  * Copyright (c) 2023 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-processor.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/trace.h>
27 #include <dali/internal/event/actors/actor-impl.h>
28 #include <dali/internal/event/common/scene-impl.h>
29 #include <dali/internal/event/events/gesture-requests.h>
30 #include <dali/internal/event/events/tap-gesture/tap-gesture-event.h>
31 #include <dali/internal/event/events/tap-gesture/tap-gesture-impl.h>
32 #include <dali/internal/event/events/tap-gesture/tap-gesture-recognizer.h>
33 #include <dali/internal/event/render-tasks/render-task-impl.h>
34 #include <dali/public-api/actors/actor.h>
35 #include <dali/public-api/common/dali-common.h>
36 #include <dali/public-api/events/tap-gesture.h>
37 #include <dali/public-api/math/vector2.h>
38
39 namespace Dali
40 {
41 namespace Internal
42 {
43 namespace
44 {
45 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_PERFORMANCE_MARKER, false);
46 constexpr uint32_t DEFAULT_MAXIMUM_ALLOWED_TIME = 330u;
47 constexpr uint32_t DEFAULT_RECOGNIZER_TIME      = 330u;
48
49 /**
50  * Creates a TapGesture and asks the specified detector to emit its detected signal.
51  * @param[in]  actor             The actor on which a tap has occurred.
52  * @param[in]  gestureDetectors  A reference to gesture detectors that should emit the signal.
53  * @param[in]  tapEvent          The tapEvent received from the adaptor.
54  * @param[in]  localPoint        Relative to the actor attached to the detector.
55  */
56 void EmitTapSignal(
57   Actor*                          actor,
58   const GestureDetectorContainer& gestureDetectors,
59   const TapGestureEvent&          tapEvent,
60   Vector2                         localPoint)
61 {
62   Internal::TapGesturePtr tap(new Internal::TapGesture(tapEvent.state));
63   tap->SetTime(tapEvent.time);
64   tap->SetNumberOfTaps(tapEvent.numberOfTaps);
65   tap->SetNumberOfTouches(tapEvent.numberOfTouches);
66   tap->SetScreenPoint(tapEvent.point);
67   tap->SetLocalPoint(localPoint);
68   tap->SetSourceType(tapEvent.sourceType);
69   tap->SetSourceData(tapEvent.sourceData);
70
71   DALI_TRACE_BEGIN_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_EMIT_TAP_GESTURE_SIGNAL", [&](std::ostringstream& oss) {
72     oss << "[" << gestureDetectors.size() << "]";
73   });
74
75   Dali::Actor                                    actorHandle(actor);
76   const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
77   for(GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter)
78   {
79     static_cast<TapGestureDetector*>(*iter)->EmitTapGestureSignal(actorHandle, Dali::TapGesture(tap.Get()));
80   }
81
82   DALI_TRACE_END_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_EMIT_TAP_GESTURE_SIGNAL", [&](std::ostringstream& oss) {
83     oss << "[" << gestureDetectors.size() << "]";
84   });
85 }
86
87 } // unnamed namespace
88
89 TapGestureProcessor::TapGestureProcessor()
90 : GestureProcessor(GestureType::TAP),
91   mTapGestureDetectors(),
92   mMinTouchesRequired(1),
93   mMaxTouchesRequired(1),
94   mCurrentTapEvent(nullptr),
95   mPossibleProcessed(false),
96   mMaximumAllowedTime(DEFAULT_MAXIMUM_ALLOWED_TIME),
97   mRecognizerTime(DEFAULT_RECOGNIZER_TIME)
98 {
99 }
100
101 TapGestureProcessor::~TapGestureProcessor() = default;
102
103 void TapGestureProcessor::Process(Scene& scene, const TapGestureEvent& tapEvent)
104 {
105   DALI_TRACE_SCOPE(gTraceFilter, "DALI_PROCESS_TAP_GESTURE");
106   switch(tapEvent.state)
107   {
108     case GestureState::POSSIBLE:
109     {
110       // Do a hit test and if an actor has been hit then save to see if tap event is still valid on a tap( same actor being hit )
111       HitTestAlgorithm::Results hitTestResults;
112       if(HitTest(scene, tapEvent.point, hitTestResults))
113       {
114         SetActor(&GetImplementation(hitTestResults.actor));
115         mCurrentTapActor.SetActor(GetCurrentGesturedActor());
116
117         // Indicate that we've processed a touch down. Bool should be sufficient as a change in actor will result in a cancellation
118         mPossibleProcessed = true;
119       }
120       else
121       {
122         ResetActor();
123       }
124       break;
125     }
126
127     case GestureState::STARTED:
128     {
129       // Ensure that we're processing a hit on the current actor and that we've already processed a touch down
130       HitTestAlgorithm::Results hitTestResults;
131       if(GetCurrentGesturedActor() && HitTest(scene, tapEvent.point, hitTestResults) && mPossibleProcessed)
132       {
133         // Check that this actor is still the one that was used for the last touch down ?
134         if(mCurrentTapActor.GetActor() == &GetImplementation(hitTestResults.actor))
135         {
136           mCurrentTapEvent = &tapEvent;
137           ProcessAndEmit(hitTestResults);
138         }
139         mCurrentTapEvent   = nullptr;
140         mPossibleProcessed = false;
141       }
142       break;
143     }
144
145     case GestureState::CANCELLED:
146     {
147       mPossibleProcessed = false;
148       ResetActor();
149       break;
150     }
151
152     case GestureState::CONTINUING:
153     {
154       DALI_ABORT("Incorrect state received from Integration layer: CONTINUING\n");
155       break;
156     }
157     case GestureState::FINISHED:
158     {
159       DALI_ABORT("Incorrect state received from Integration layer: FINISHED\n");
160       break;
161     }
162     case GestureState::CLEAR:
163     {
164       DALI_ABORT("Incorrect state received from Integration layer: CLEAR\n");
165       break;
166     }
167   }
168 }
169
170 void TapGestureProcessor::AddGestureDetector(TapGestureDetector* gestureDetector, Scene& scene)
171 {
172   bool firstRegistration(mTapGestureDetectors.empty());
173
174   mTapGestureDetectors.push_back(gestureDetector);
175
176   const unsigned int touchesRequired = gestureDetector->GetTouchesRequired();
177
178   if(firstRegistration)
179   {
180     // If this is the first tap gesture detector that has been added, then our minimum and maximum
181     // requirements are the same as each other.
182
183     mMinTouchesRequired = mMaxTouchesRequired = touchesRequired;
184
185     TapGestureRequest request;
186     request.minTouches = mMinTouchesRequired;
187     request.maxTouches = mMaxTouchesRequired;
188
189     Size size          = scene.GetSize();
190     mGestureRecognizer = new TapGestureRecognizer(*this, Vector2(size.width, size.height), static_cast<const TapGestureRequest&>(request), mMaximumAllowedTime, mRecognizerTime);
191   }
192   else
193   {
194     // If we have already registered for tap gesture detection before then we need to check our
195     // minimum and maximums and see if our gesture detection requirements have changed, if they
196     // have, then we should ask the adaptor to update its detection policy.
197
198     // This is quicker than calling UpdateDetection as there is no need to iterate through the container
199
200     unsigned int minTouches = mMinTouchesRequired < touchesRequired ? mMinTouchesRequired : touchesRequired;
201     unsigned int maxTouches = mMaxTouchesRequired > touchesRequired ? mMaxTouchesRequired : touchesRequired;
202
203     if((minTouches != mMinTouchesRequired) || (maxTouches != mMaxTouchesRequired))
204     {
205       TapGestureRequest request;
206       request.minTouches = mMinTouchesRequired = minTouches;
207       request.maxTouches = mMaxTouchesRequired = maxTouches;
208
209       mGestureRecognizer->Update(request);
210     }
211   }
212 }
213
214 void TapGestureProcessor::RemoveGestureDetector(TapGestureDetector* gestureDetector)
215 {
216   // Find detector ...
217   TapGestureDetectorContainer::iterator endIter = std::remove(mTapGestureDetectors.begin(), mTapGestureDetectors.end(), gestureDetector);
218   DALI_ASSERT_DEBUG(endIter != mTapGestureDetectors.end());
219
220   // ... and remove it
221   mTapGestureDetectors.erase(endIter, mTapGestureDetectors.end());
222
223   if(mTapGestureDetectors.empty())
224   {
225     mGestureRecognizer = nullptr;
226
227     ResetActor();
228   }
229   else
230   {
231     UpdateDetection();
232   }
233 }
234
235 void TapGestureProcessor::GestureDetectorUpdated(TapGestureDetector* gestureDetector)
236 {
237   // Nothing to do.
238 }
239
240 void TapGestureProcessor::SetMaximumAllowedTime(uint32_t time)
241 {
242   if(time == 0u)
243   {
244     DALI_LOG_WARNING("MaximumAllowedTime must be greater than zero.");
245     return;
246   }
247   if(mMaximumAllowedTime != time)
248   {
249     mMaximumAllowedTime = time;
250
251     if(mGestureRecognizer)
252     {
253       TapGestureRecognizer* tapRecognizer = dynamic_cast<TapGestureRecognizer*>(mGestureRecognizer.Get());
254       if(tapRecognizer)
255       {
256         tapRecognizer->SetMaximumAllowedTime(time);
257       }
258     }
259   }
260 }
261
262 uint32_t TapGestureProcessor::GetMaximumAllowedTime() const
263 {
264   return mMaximumAllowedTime;
265 }
266
267 void TapGestureProcessor::SetRecognizerTime(uint32_t time)
268 {
269   if(time == 0u)
270   {
271     DALI_LOG_WARNING("RecognizerTime must be greater than zero.");
272     return;
273   }
274   if(mRecognizerTime != time)
275   {
276     mRecognizerTime = time;
277
278     if(mGestureRecognizer)
279     {
280       TapGestureRecognizer* tapRecognizer = dynamic_cast<TapGestureRecognizer*>(mGestureRecognizer.Get());
281       if(tapRecognizer)
282       {
283         tapRecognizer->SetRecognizerTime(time);
284       }
285     }
286   }
287 }
288
289 void TapGestureProcessor::UpdateDetection()
290 {
291   DALI_ASSERT_DEBUG(!mTapGestureDetectors.empty());
292
293   unsigned int minTouches = UINT_MAX;
294   unsigned int maxTouches = 0;
295
296   for(TapGestureDetectorContainer::iterator iter = mTapGestureDetectors.begin(), endIter = mTapGestureDetectors.end(); iter != endIter; ++iter)
297   {
298     TapGestureDetector* detector(*iter);
299
300     if(detector)
301     {
302       const unsigned int touchesRequired = detector->GetTouchesRequired();
303
304       minTouches = touchesRequired < minTouches ? touchesRequired : minTouches;
305       maxTouches = touchesRequired > maxTouches ? touchesRequired : maxTouches;
306     }
307   }
308
309   if((minTouches != mMinTouchesRequired) || (maxTouches != mMaxTouchesRequired))
310   {
311     TapGestureRequest request;
312     request.minTouches = mMinTouchesRequired = minTouches;
313     request.maxTouches = mMaxTouchesRequired = maxTouches;
314
315     mGestureRecognizer->Update(request);
316   }
317 }
318
319 bool TapGestureProcessor::CheckGestureDetector(GestureDetector* detector, Actor* actor)
320 {
321   DALI_ASSERT_DEBUG(mCurrentTapEvent);
322
323   TapGestureDetector* tapDetector(static_cast<TapGestureDetector*>(detector));
324
325   return (tapDetector->GetMinimumTapsRequired() <= mCurrentTapEvent->numberOfTaps) && (tapDetector->GetTouchesRequired() == mCurrentTapEvent->numberOfTouches);
326 }
327
328 void TapGestureProcessor::EmitGestureSignal(Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates)
329 {
330   DALI_ASSERT_DEBUG(mCurrentTapEvent);
331
332   EmitTapSignal(actor, gestureDetectors, *mCurrentTapEvent, actorCoordinates);
333 }
334
335 } // namespace Internal
336
337 } // namespace Dali