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