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