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