(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 / pinch-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 <dali/internal/event/events/pinch-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/events/pinch-gesture.h>
26 #include <dali/public-api/math/vector2.h>
27 #include <dali/integration-api/events/pinch-gesture-event.h>
28 #include <dali/integration-api/gesture-manager.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/internal/event/common/stage-impl.h>
31 #include <dali/internal/event/render-tasks/render-task-impl.h>
32
33 namespace Dali
34 {
35
36 namespace Internal
37 {
38
39 namespace
40 {
41
42 /**
43  * Creates a PinchGesture and asks the specified detector to emit its detected signal.
44  * @param[in]  actor             The actor that has been pinched.
45  * @param[in]  gestureDetectors  The gesture detector container that should emit the signal.
46  * @param[in]  pinchEvent        The pinchEvent received from the adaptor.
47  * @param[in]  localCenter       Relative to the actor attached to the detector.
48  */
49 void EmitPinchSignal(
50     Dali::Actor actor,
51     PinchGestureDetectorContainer& gestureDetectors,
52     const Integration::PinchGestureEvent& pinchEvent,
53     Vector2 localCenter)
54 {
55   PinchGesture pinch(pinchEvent.state);
56   pinch.time = pinchEvent.time;
57
58   pinch.scale = pinchEvent.scale;
59   pinch.speed = pinchEvent.speed;
60   pinch.screenCenterPoint = pinchEvent.centerPoint;
61
62   pinch.localCenterPoint = localCenter;
63
64   for ( PinchGestureDetectorContainer::iterator iter = gestureDetectors.begin(), endIter = gestureDetectors.end(); iter != endIter; ++iter )
65   {
66     (*iter)->EmitPinchGestureSignal(actor, pinch);
67   }
68 }
69
70 /**
71  * Functor which checks whether the specified actor is attached to the gesture detector.
72  * It returns true if it is no longer attached.  This can be used in remove_if functions.
73  */
74 struct IsNotAttachedFunctor
75 {
76   /**
77    * Constructor
78    * @param[in]  actor  The actor to check whether it is attached.
79    */
80   IsNotAttachedFunctor(Actor* actor)
81   : actorToCheck(actor)
82   {
83   }
84
85   /**
86    * Returns true if not attached, false if it is still attached.
87    * @param[in]  detector  The detector to check.
88    * @return true, if not attached, false otherwise.
89    */
90   bool operator()(const PinchGestureDetector* detector) const
91   {
92     return !detector->IsAttached(*actorToCheck);
93   }
94
95   Actor* actorToCheck; ///< The actor to check whether it is attached or not.
96 };
97
98 } // unnamed namespace
99
100 struct PinchGestureProcessor::PinchEventFunctor : public GestureProcessor::Functor
101 {
102   /**
103    * Constructor
104    * @param[in]  pinchEvent  The current gesture event.
105    * @param[in]  processor   Reference to the processor.
106    */
107   PinchEventFunctor( const Integration::PinchGestureEvent& pinchEvent, PinchGestureProcessor& processor )
108   : pinchEvent( pinchEvent ),
109     processor( processor )
110   {
111   }
112
113   /**
114    * Check if the detector meets the current gesture event parameters.
115    */
116   virtual bool operator() ( GestureDetector*, Actor* )
117   {
118     return true;
119   }
120
121   /**
122    * Gestured actor and gesture detectors that meet the gesture's parameters found, emit and save required information.
123    */
124   virtual void operator() ( Dali::Actor actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
125   {
126     PinchGestureDetectorContainer derivedContainer;
127     DownCastContainer<PinchGestureDetector>( gestureDetectors, derivedContainer );
128
129     EmitPinchSignal( actor, derivedContainer, pinchEvent, actorCoordinates );
130
131     if ( actor.OnStage() )
132     {
133       processor.mCurrentPinchEmitters = derivedContainer;
134       processor.SetActor( actor );
135     }
136   }
137
138   const Integration::PinchGestureEvent& pinchEvent;
139   PinchGestureProcessor& processor;
140 };
141
142 PinchGestureProcessor::PinchGestureProcessor( Stage& stage, Integration::GestureManager& gestureManager )
143 : GestureProcessor( Gesture::Pinch ),
144   mStage(stage),
145   mGestureManager(gestureManager),
146   mGestureDetectors(),
147   mCurrentPinchEmitters()
148 {
149 }
150
151 PinchGestureProcessor::~PinchGestureProcessor()
152 {
153 }
154
155 void PinchGestureProcessor::Process( const Integration::PinchGestureEvent& pinchEvent )
156 {
157   switch ( pinchEvent.state )
158   {
159     case Gesture::Started:
160     {
161       // The pinch gesture should only be sent to the gesture detector which first received it so that
162       // it can be told when the gesture ends as well.
163
164       mCurrentPinchEmitters.clear();
165       ResetActor();
166
167       HitTestAlgorithm::Results hitTestResults;
168       if( HitTest( mStage, pinchEvent.centerPoint, hitTestResults ) )
169       {
170         // Record the current render-task for Screen->Actor coordinate conversions
171         mCurrentRenderTask = hitTestResults.renderTask;
172
173         PinchEventFunctor functor( pinchEvent, *this ); // Sets mCurrentGesturedActor
174         GestureDetectorContainer gestureDetectors;
175         UpCastContainer<PinchGestureDetector>( mGestureDetectors, gestureDetectors );
176         ProcessAndEmit( hitTestResults, gestureDetectors, functor );
177       }
178       break;
179     }
180
181     case Gesture::Continuing:
182     case Gesture::Finished:
183     case Gesture::Cancelled:
184     {
185       // Only send subsequent pinch gesture signals if we processed the pinch gesture when it started.
186       // Check if actor is still touchable.
187
188       Actor* currentGesturedActor = GetCurrentGesturedActor();
189       if ( currentGesturedActor )
190       {
191         if ( currentGesturedActor->IsHittable() && !mCurrentPinchEmitters.empty() && mCurrentRenderTask )
192         {
193           // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
194           PinchGestureDetectorContainer::iterator endIter = std::remove_if( mCurrentPinchEmitters.begin(), mCurrentPinchEmitters.end(), IsNotAttachedFunctor(currentGesturedActor) );
195           mCurrentPinchEmitters.erase( endIter, mCurrentPinchEmitters.end() );
196
197           if ( !mCurrentPinchEmitters.empty() )
198           {
199             Vector2 actorCoords;
200             RenderTask& renderTaskImpl( GetImplementation(mCurrentRenderTask) );
201             currentGesturedActor->ScreenToLocal( renderTaskImpl, actorCoords.x, actorCoords.y, pinchEvent.centerPoint.x, pinchEvent.centerPoint.y );
202
203             EmitPinchSignal( Dali::Actor(currentGesturedActor), mCurrentPinchEmitters, pinchEvent, actorCoords );
204           }
205           else
206           {
207             // If we have no current emitters then clear pinched actor as well.
208             ResetActor();
209           }
210
211           // Clear current emitters if pinch gesture has ended or been cancelled.
212           if ( pinchEvent.state == Gesture::Finished || pinchEvent.state == Gesture::Cancelled )
213           {
214             mCurrentPinchEmitters.clear();
215             ResetActor();
216           }
217         }
218         else
219         {
220           mCurrentPinchEmitters.clear();
221           ResetActor();
222         }
223       }
224       break;
225     }
226
227     case Gesture::Clear:
228       DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Clear\n" );
229       break;
230
231     case Gesture::Possible:
232       DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Possible\n" );
233       break;
234   }
235 }
236
237 void PinchGestureProcessor::AddGestureDetector( PinchGestureDetector* gestureDetector )
238 {
239   bool registerWithAdaptor(mGestureDetectors.empty());
240
241   mGestureDetectors.push_back(gestureDetector);
242
243   if (registerWithAdaptor)
244   {
245     Integration::GestureRequest request(Gesture::Pinch);
246     mGestureManager.Register(request);
247   }
248 }
249
250 void PinchGestureProcessor::RemoveGestureDetector( PinchGestureDetector* gestureDetector )
251 {
252   if ( !mCurrentPinchEmitters.empty() )
253   {
254     // Check if the removed detector was one that is currently being pinched and remove it from emitters.
255     PinchGestureDetectorContainer::iterator endIter = std::remove( mCurrentPinchEmitters.begin(), mCurrentPinchEmitters.end(), gestureDetector );
256     mCurrentPinchEmitters.erase( endIter, mCurrentPinchEmitters.end() );
257
258     // If we no longer have any emitters, then we should clear mCurrentGesturedActor as well
259     if ( mCurrentPinchEmitters.empty() )
260     {
261       ResetActor();
262     }
263   }
264
265   // Find the detector...
266   PinchGestureDetectorContainer::iterator endIter = std::remove( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector );
267   DALI_ASSERT_DEBUG( endIter != mGestureDetectors.end() );
268
269   // ...and remove it
270   mGestureDetectors.erase(endIter, mGestureDetectors.end());
271
272   if (mGestureDetectors.empty())
273   {
274     Integration::GestureRequest request(Gesture::Pinch);
275     mGestureManager.Unregister(request);
276   }
277 }
278
279 void PinchGestureProcessor::GestureDetectorUpdated( PinchGestureDetector* gestureDetector )
280 {
281   // Nothing to do as PinchGestureDetector does not have any specific parameters.
282 }
283
284 void PinchGestureProcessor::OnGesturedActorStageDisconnection()
285 {
286   mCurrentPinchEmitters.clear();
287 }
288
289 } // namespace Internal
290
291 } // namespace Dali