81cead43fd017139d746408399fd09a8982d1293
[platform/core/uifw/dali-core.git] / dali / internal / event / events / long-press-gesture / long-press-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/long-press-gesture/long-press-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/long-press-gesture/long-press-gesture-event.h>
31 #include <dali/internal/event/events/long-press-gesture/long-press-gesture-impl.h>
32 #include <dali/internal/event/events/long-press-gesture/long-press-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
37 namespace Dali
38 {
39 namespace Internal
40 {
41 namespace
42 {
43 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_PERFORMANCE_MARKER, false);
44 const unsigned long DEFAULT_MINIMUM_HOLDING_TIME = 500u;
45
46 /**
47  * Creates a LongPressGesture and asks the specified detector to emit its detected signal.
48  * @param[in]  actor             The actor on which the long press gesture has occurred.
49  * @param[in]  gestureDetectors  A reference to gesture detectors that should emit the signal.
50  * @param[in]  longPressEvent    The longPressEvent received from the adaptor.
51  * @param[in]  localPoint        Relative to the actor attached to the detector.
52  */
53 void EmitLongPressSignal(
54   Actor*                          actor,
55   const GestureDetectorContainer& gestureDetectors,
56   const LongPressGestureEvent&    longPressEvent,
57   Vector2                         localPoint)
58 {
59   Internal::LongPressGesturePtr longPress(new Internal::LongPressGesture(longPressEvent.state));
60   longPress->SetTime(longPressEvent.time);
61   longPress->SetNumberOfTouches(longPressEvent.numberOfTouches);
62   longPress->SetScreenPoint(longPressEvent.point);
63   longPress->SetLocalPoint(localPoint);
64   longPress->SetSourceType(longPressEvent.sourceType);
65   longPress->SetSourceData(longPressEvent.sourceData);
66
67   DALI_TRACE_BEGIN_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_EMIT_LONG_PRESS_GESTURE_SIGNAL", [&](std::ostringstream& oss) {
68     oss << "[" << gestureDetectors.size() << "]";
69   });
70
71   Dali::Actor                                    actorHandle(actor);
72   const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
73   for(GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter)
74   {
75     static_cast<LongPressGestureDetector*>(*iter)->EmitLongPressGestureSignal(actorHandle, Dali::LongPressGesture(longPress.Get()));
76   }
77
78   DALI_TRACE_END_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_EMIT_LONG_PRESS_GESTURE_SIGNAL", [&](std::ostringstream& oss) {
79     oss << "[" << gestureDetectors.size() << "]";
80   });
81 }
82
83 /**
84  * Functor which checks whether the specified actor is attached to the gesture detector.
85  * It returns true if it is no longer attached.  This can be used in remove_if functions.
86  */
87 struct IsNotAttachedFunctor
88 {
89   /**
90    * Constructor
91    * @param[in]  actor  The actor to check whether it is attached.
92    */
93   IsNotAttachedFunctor(Actor* actor)
94   : actorToCheck(actor)
95   {
96   }
97
98   /**
99    * Returns true if not attached, false if it is still attached.
100    * @param[in]  detector  The detector to check.
101    * @return true, if not attached, false otherwise.
102    */
103   bool operator()(const GestureDetector* detector) const
104   {
105     return !detector->IsAttached(*actorToCheck);
106   }
107
108   Actor* actorToCheck; ///< The actor to check whether it is attached or not.
109 };
110
111 } // unnamed namespace
112
113 LongPressGestureProcessor::LongPressGestureProcessor()
114 : GestureProcessor(GestureType::LONG_PRESS),
115   mLongPressGestureDetectors(),
116   mCurrentEmitters(),
117   mCurrentRenderTask(),
118   mMinTouchesRequired(1),
119   mMaxTouchesRequired(1),
120   mCurrentLongPressEvent(nullptr),
121   mMinimumHoldingTime(DEFAULT_MINIMUM_HOLDING_TIME)
122 {
123 }
124
125 LongPressGestureProcessor::~LongPressGestureProcessor() = default;
126
127 void LongPressGestureProcessor::Process(Scene& scene, const LongPressGestureEvent& longPressEvent)
128 {
129   DALI_TRACE_SCOPE(gTraceFilter, "DALI_PROCESS_LONG_PRESS_GESTURE");
130   switch(longPressEvent.state)
131   {
132     case GestureState::POSSIBLE:
133     {
134       mCurrentEmitters.clear();
135       ResetActor();
136
137       HitTestAlgorithm::Results hitTestResults;
138       if(GetFeededActor())
139       {
140         SetActor(GetFeededActor());
141       }
142       else if(HitTest(scene, longPressEvent.point, hitTestResults))
143       {
144         SetActor(&GetImplementation(hitTestResults.actor));
145       }
146       break;
147     }
148
149     case GestureState::STARTED:
150     {
151       Actor* currentGesturedActor = GetCurrentGesturedActor();
152       if(currentGesturedActor)
153       {
154         HitTestAlgorithm::Results hitTestResults;
155         if(GetFeededActor())
156         {
157           hitTestResults.actor = Dali::Actor(GetFeededActor());
158           hitTestResults.renderTask = GetFeededRenderTask();
159
160           Vector2     actorCoords;
161           currentGesturedActor->ScreenToLocal(*hitTestResults.renderTask.Get(), actorCoords.x, actorCoords.y, longPressEvent.point.x, longPressEvent.point.y);
162           hitTestResults.actorCoordinates = actorCoords;
163         }
164         else
165         {
166           HitTest(scene, longPressEvent.point, hitTestResults);
167         }
168
169         if(hitTestResults.actor && (currentGesturedActor == &GetImplementation(hitTestResults.actor)))
170         {
171           // Record the current render-task for Screen->Actor coordinate conversions
172           mCurrentRenderTask = hitTestResults.renderTask;
173
174           // Set mCurrentLongPressEvent to use inside overridden methods called from ProcessAndEmit()
175           mCurrentLongPressEvent = &longPressEvent;
176           if(GetFeededActor())
177           {
178             ProcessAndEmitActor(hitTestResults, GetFeededGestureDetector());
179           }
180           else
181           {
182             ProcessAndEmit(hitTestResults);
183           }
184           mCurrentLongPressEvent = nullptr;
185         }
186         else
187         {
188           mCurrentEmitters.clear();
189           ResetActor();
190         }
191       }
192       break;
193     }
194
195     case GestureState::FINISHED:
196     {
197       // The gesture should only be sent to the gesture detector which first received it so that it
198       // can be told when the gesture ends as well.
199
200       // Only send subsequent long press gesture signals if we processed the gesture when it started.
201       // Check if actor is still touchable.
202       Actor* currentGesturedActor = GetCurrentGesturedActor();
203       if(currentGesturedActor)
204       {
205         if(currentGesturedActor->IsHittable() && !mCurrentEmitters.empty() && mCurrentRenderTask)
206         {
207           // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
208           GestureDetectorContainer::iterator endIter = std::remove_if(mCurrentEmitters.begin(), mCurrentEmitters.end(), IsNotAttachedFunctor(currentGesturedActor));
209           mCurrentEmitters.erase(endIter, mCurrentEmitters.end());
210           if(GetFeededGestureDetector())
211           {
212             mCurrentEmitters.push_back(GetFeededGestureDetector());
213           }
214           if(!mCurrentEmitters.empty())
215           {
216             Vector2     actorCoords;
217             RenderTask& renderTaskImpl = *mCurrentRenderTask.Get();
218             currentGesturedActor->ScreenToLocal(renderTaskImpl, actorCoords.x, actorCoords.y, longPressEvent.point.x, longPressEvent.point.y);
219
220             EmitLongPressSignal(currentGesturedActor, mCurrentEmitters, longPressEvent, actorCoords);
221           }
222         }
223
224         // Clear current emitters and emitted actor
225         mCurrentEmitters.clear();
226         ResetActor();
227       }
228       break;
229     }
230
231     case GestureState::CANCELLED:
232     {
233       mCurrentEmitters.clear();
234       ResetActor();
235       break;
236     }
237
238     case GestureState::CONTINUING:
239     {
240       DALI_ABORT("Incorrect state received from Integration layer: CONTINUING\n");
241       break;
242     }
243
244     case GestureState::CLEAR:
245     {
246       DALI_ABORT("Incorrect state received from Integration layer: CLEAR\n");
247       break;
248     }
249   }
250 }
251
252 void LongPressGestureProcessor::AddGestureDetector(LongPressGestureDetector* gestureDetector, Scene& scene)
253 {
254   bool firstRegistration(mLongPressGestureDetectors.empty());
255
256   mLongPressGestureDetectors.push_back(gestureDetector);
257
258   if(firstRegistration)
259   {
260     mMinTouchesRequired = gestureDetector->GetMinimumTouchesRequired();
261     mMaxTouchesRequired = gestureDetector->GetMaximumTouchesRequired();
262
263     LongPressGestureRequest request;
264     request.minTouches = mMinTouchesRequired;
265     request.maxTouches = mMaxTouchesRequired;
266
267     Size size = scene.GetSize();
268
269     mGestureRecognizer = new LongPressGestureRecognizer(*this, Vector2(size.width, size.height), static_cast<const LongPressGestureRequest&>(request), mMinimumHoldingTime);
270   }
271   else
272   {
273     UpdateDetection();
274   }
275 }
276
277 void LongPressGestureProcessor::RemoveGestureDetector(LongPressGestureDetector* gestureDetector)
278 {
279   // Find detector ...
280   LongPressGestureDetectorContainer::iterator endIter = std::remove(mLongPressGestureDetectors.begin(), mLongPressGestureDetectors.end(), gestureDetector);
281   DALI_ASSERT_DEBUG(endIter != mLongPressGestureDetectors.end());
282
283   // ... and remove it
284   mLongPressGestureDetectors.erase(endIter, mLongPressGestureDetectors.end());
285
286   if(mLongPressGestureDetectors.empty())
287   {
288     mGestureRecognizer = nullptr;
289   }
290   else
291   {
292     UpdateDetection();
293   }
294 }
295
296 void LongPressGestureProcessor::GestureDetectorUpdated(LongPressGestureDetector* gestureDetector)
297 {
298   DALI_ASSERT_DEBUG(find(mLongPressGestureDetectors.begin(), mLongPressGestureDetectors.end(), gestureDetector) != mLongPressGestureDetectors.end());
299
300   UpdateDetection();
301 }
302
303 void LongPressGestureProcessor::SetMinimumHoldingTime(uint32_t time)
304 {
305   if(time > 0u && mMinimumHoldingTime != time)
306   {
307     mMinimumHoldingTime = time;
308
309     if(mGestureRecognizer)
310     {
311       LongPressGestureRecognizer* longPressRecognizer = dynamic_cast<LongPressGestureRecognizer*>(mGestureRecognizer.Get());
312       if(longPressRecognizer)
313       {
314         longPressRecognizer->SetMinimumHoldingTime(time);
315       }
316     }
317   }
318 }
319
320 uint32_t LongPressGestureProcessor::GetMinimumHoldingTime() const
321 {
322   return mMinimumHoldingTime;
323 }
324
325 void LongPressGestureProcessor::UpdateDetection()
326 {
327   DALI_ASSERT_DEBUG(!mLongPressGestureDetectors.empty());
328
329   unsigned int minimumRequired = UINT_MAX;
330   unsigned int maximumRequired = 0;
331
332   for(LongPressGestureDetectorContainer::iterator iter = mLongPressGestureDetectors.begin(), endIter = mLongPressGestureDetectors.end(); iter != endIter; ++iter)
333   {
334     LongPressGestureDetector* current(*iter);
335
336     if(current)
337     {
338       unsigned int minimum = current->GetMinimumTouchesRequired();
339       if(minimum < minimumRequired)
340       {
341         minimumRequired = minimum;
342       }
343
344       unsigned int maximum = current->GetMaximumTouchesRequired();
345       if(maximum > maximumRequired)
346       {
347         maximumRequired = maximum;
348       }
349     }
350   }
351
352   if((minimumRequired != mMinTouchesRequired) || (maximumRequired != mMaxTouchesRequired))
353   {
354     mMinTouchesRequired = minimumRequired;
355     mMaxTouchesRequired = maximumRequired;
356
357     LongPressGestureRequest request;
358     request.minTouches = mMinTouchesRequired;
359     request.maxTouches = mMaxTouchesRequired;
360     mGestureRecognizer->Update(request);
361   }
362 }
363
364 void LongPressGestureProcessor::OnGesturedActorStageDisconnection()
365 {
366   mCurrentEmitters.clear();
367 }
368
369 bool LongPressGestureProcessor::CheckGestureDetector(GestureDetector* detector, Actor* actor)
370 {
371   DALI_ASSERT_DEBUG(mCurrentLongPressEvent);
372
373   LongPressGestureDetector* longPressDetector(static_cast<LongPressGestureDetector*>(detector));
374
375   return (longPressDetector->GetMinimumTouchesRequired() <= mCurrentLongPressEvent->numberOfTouches) &&
376          (longPressDetector->GetMaximumTouchesRequired() >= mCurrentLongPressEvent->numberOfTouches);
377 }
378
379 void LongPressGestureProcessor::EmitGestureSignal(Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates)
380 {
381   DALI_ASSERT_DEBUG(mCurrentLongPressEvent);
382
383   mCurrentEmitters.clear();
384   ResetActor();
385
386   EmitLongPressSignal(actor, gestureDetectors, *mCurrentLongPressEvent, actorCoordinates);
387
388   if(actor->OnScene())
389   {
390     mCurrentEmitters = gestureDetectors;
391     SetActor(actor);
392   }
393 }
394
395 } // namespace Internal
396
397 } // namespace Dali