e266cbf60f60c2d7a895ffb8e270957cf9c6f786
[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 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/pinch-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/events/pinch-gesture.h>
27 #include <dali/public-api/math/vector2.h>
28 #include <dali/integration-api/events/pinch-gesture-event.h>
29 #include <dali/integration-api/gesture-manager.h>
30 #include <dali/integration-api/debug.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 PinchGesture and asks the specified detector to emit its detected signal.
45  * @param[in]  actor             The actor that has been pinched.
46  * @param[in]  gestureDetectors  The gesture detector container that should emit the signal.
47  * @param[in]  pinchEvent        The pinchEvent received from the adaptor.
48  * @param[in]  localCenter       Relative to the actor attached to the detector.
49  */
50 void EmitPinchSignal(
51     Dali::Actor actor,
52     PinchGestureDetectorContainer& gestureDetectors,
53     const Integration::PinchGestureEvent& pinchEvent,
54     Vector2 localCenter)
55 {
56   PinchGesture pinch(pinchEvent.state);
57   pinch.time = pinchEvent.time;
58
59   pinch.scale = pinchEvent.scale;
60   pinch.speed = pinchEvent.speed;
61   pinch.screenCenterPoint = pinchEvent.centerPoint;
62
63   pinch.localCenterPoint = localCenter;
64
65   for ( PinchGestureDetectorContainer::iterator iter = gestureDetectors.begin(), endIter = gestureDetectors.end(); iter != endIter; ++iter )
66   {
67     (*iter)->EmitPinchGestureSignal(actor, pinch);
68   }
69 }
70
71 /**
72  * Functor which checks whether the specified actor is attached to the gesture detector.
73  * It returns true if it is no longer attached.  This can be used in remove_if functions.
74  */
75 struct IsNotAttachedFunctor
76 {
77   /**
78    * Constructor
79    * @param[in]  actor  The actor to check whether it is attached.
80    */
81   IsNotAttachedFunctor(Actor* actor)
82   : actorToCheck(actor)
83   {
84   }
85
86   /**
87    * Returns true if not attached, false if it is still attached.
88    * @param[in]  detector  The detector to check.
89    * @return true, if not attached, false otherwise.
90    */
91   bool operator()(const PinchGestureDetector* detector) const
92   {
93     return !detector->IsAttached(*actorToCheck);
94   }
95
96   Actor* actorToCheck; ///< The actor to check whether it is attached or not.
97 };
98
99 } // unnamed namespace
100
101 struct PinchGestureProcessor::PinchEventFunctor : public GestureProcessor::Functor
102 {
103   /**
104    * Constructor
105    * @param[in]  pinchEvent  The current gesture event.
106    * @param[in]  processor   Reference to the processor.
107    */
108   PinchEventFunctor( const Integration::PinchGestureEvent& pinchEvent, PinchGestureProcessor& processor )
109   : pinchEvent( pinchEvent ),
110     processor( processor )
111   {
112   }
113
114   /**
115    * Check if the detector meets the current gesture event parameters.
116    */
117   virtual bool operator() ( GestureDetector*, Actor* )
118   {
119     return true;
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     PinchGestureDetectorContainer derivedContainer;
128     DownCastContainer<PinchGestureDetector>( gestureDetectors, derivedContainer );
129
130     EmitPinchSignal( actor, derivedContainer, pinchEvent, actorCoordinates );
131
132     if ( actor.OnStage() )
133     {
134       processor.mCurrentPinchEmitters = derivedContainer;
135       processor.SetActor( actor );
136     }
137   }
138
139   const Integration::PinchGestureEvent& pinchEvent;
140   PinchGestureProcessor& processor;
141 };
142
143 PinchGestureProcessor::PinchGestureProcessor( Stage& stage, Integration::GestureManager& gestureManager )
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