Add methods to set variables for Pinch and Rotation gesture processing
[platform/core/uifw/dali-core.git] / dali / internal / event / events / pinch-gesture / pinch-gesture-processor.cpp
1 /*
2  * Copyright (c) 2019 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/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/internal/event/events/pinch-gesture/pinch-gesture-event.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/internal/event/common/scene-impl.h>
31 #include <dali/internal/event/render-tasks/render-task-impl.h>
32 #include <dali/internal/event/events/pinch-gesture/pinch-gesture-recognizer.h>
33 #include <dali/internal/event/events/gesture-requests.h>
34
35 namespace Dali
36 {
37
38 namespace Internal
39 {
40
41 namespace
42 {
43 const uint32_t MINIMUM_TOUCH_EVENTS_REQUIRED = 4u;
44 const uint32_t MINIMUM_TOUCH_EVENTS_REQUIRED_AFTER_START = 4u;
45
46 /**
47  * Creates a PinchGesture and asks the specified detector to emit its detected signal.
48  * @param[in]  actor             The actor that has been pinched.
49  * @param[in]  gestureDetectors  The gesture detector container that should emit the signal.
50  * @param[in]  pinchEvent        The pinchEvent received from the adaptor.
51  * @param[in]  localCenter       Relative to the actor attached to the detector.
52  */
53 void EmitPinchSignal(
54     Actor* actor,
55     const GestureDetectorContainer& gestureDetectors,
56     const PinchGestureEvent& pinchEvent,
57     Vector2 localCenter)
58 {
59   PinchGesture pinch(pinchEvent.state);
60   pinch.time = pinchEvent.time;
61
62   pinch.scale = pinchEvent.scale;
63   pinch.speed = pinchEvent.speed;
64   pinch.screenCenterPoint = pinchEvent.centerPoint;
65
66   pinch.localCenterPoint = localCenter;
67
68   Dali::Actor actorHandle( actor );
69   const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
70   for ( GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter )
71   {
72     static_cast< PinchGestureDetector* >( *iter )->EmitPinchGestureSignal( actorHandle, pinch );
73   }
74 }
75
76 /**
77  * Functor which checks whether the specified actor is attached to the gesture detector.
78  * It returns true if it is no longer attached.  This can be used in remove_if functions.
79  */
80 struct IsNotAttachedFunctor
81 {
82   /**
83    * Constructor
84    * @param[in]  actor  The actor to check whether it is attached.
85    */
86   IsNotAttachedFunctor(Actor* actor)
87   : actorToCheck(actor)
88   {
89   }
90
91   /**
92    * Returns true if not attached, false if it is still attached.
93    * @param[in]  detector  The detector to check.
94    * @return true, if not attached, false otherwise.
95    */
96   bool operator()(const GestureDetector* detector) const
97   {
98     return !detector->IsAttached(*actorToCheck);
99   }
100
101   Actor* actorToCheck; ///< The actor to check whether it is attached or not.
102 };
103
104 } // unnamed namespace
105
106 PinchGestureProcessor::PinchGestureProcessor()
107 : GestureProcessor( Gesture::Pinch ),
108   mPinchGestureDetectors(),
109   mCurrentPinchEmitters(),
110   mCurrentPinchEvent(NULL),
111   mMinimumPinchDistance(-1.0f),
112   mMinimumTouchEvents( MINIMUM_TOUCH_EVENTS_REQUIRED ),
113   mMinimumTouchEventsAfterStart( MINIMUM_TOUCH_EVENTS_REQUIRED_AFTER_START )
114 {
115 }
116
117 PinchGestureProcessor::~PinchGestureProcessor()
118 {
119 }
120
121 void PinchGestureProcessor::SetMinimumPinchDistance( float value )
122 {
123   mMinimumPinchDistance = value;
124
125   if( mGestureRecognizer )
126   {
127     PinchGestureRecognizer* pinchRecognizer = dynamic_cast<PinchGestureRecognizer*>(mGestureRecognizer.Get());
128     if( pinchRecognizer )
129     {
130       pinchRecognizer->SetMinimumPinchDistance(value);
131     }
132   }
133 }
134
135 void PinchGestureProcessor::SetMinimumTouchEvents( uint32_t value )
136 {
137   if( value > 1u && mMinimumTouchEvents != value )
138   {
139     mMinimumTouchEvents = value;
140
141     if( mGestureRecognizer )
142     {
143       PinchGestureRecognizer* pinchRecognizer = dynamic_cast<PinchGestureRecognizer*>( mGestureRecognizer.Get() );
144       if( pinchRecognizer )
145       {
146         pinchRecognizer->SetMinimumTouchEvents( value );
147       }
148     }
149   }
150 }
151
152 void PinchGestureProcessor::SetMinimumTouchEventsAfterStart( uint32_t value )
153 {
154   if( value > 1u && mMinimumTouchEventsAfterStart != value )
155   {
156     mMinimumTouchEventsAfterStart = value;
157
158     if( mGestureRecognizer )
159     {
160       PinchGestureRecognizer* pinchRecognizer = dynamic_cast<PinchGestureRecognizer*>( mGestureRecognizer.Get() );
161       if( pinchRecognizer )
162       {
163         pinchRecognizer->SetMinimumTouchEventsAfterStart( value );
164       }
165     }
166   }
167 }
168
169 void PinchGestureProcessor::Process( Scene& scene, const PinchGestureEvent& pinchEvent )
170 {
171   switch ( pinchEvent.state )
172   {
173     case Gesture::Started:
174     {
175       // The pinch gesture should only be sent to the gesture detector which first received it so that
176       // it can be told when the gesture ends as well.
177
178       mCurrentPinchEmitters.clear();
179       ResetActor();
180
181       HitTestAlgorithm::Results hitTestResults;
182       if( HitTest( scene, pinchEvent.centerPoint, hitTestResults ) )
183       {
184         // Record the current render-task for Screen->Actor coordinate conversions
185         mCurrentRenderTask = hitTestResults.renderTask;
186
187         // Set mCurrentPinchEvent to use inside overridden methods called from ProcessAndEmit()
188         mCurrentPinchEvent = &pinchEvent;
189         ProcessAndEmit( hitTestResults );
190         mCurrentPinchEvent = NULL;
191       }
192       break;
193     }
194
195     case Gesture::Continuing:
196     case Gesture::Finished:
197     case Gesture::Cancelled:
198     {
199       // Only send subsequent pinch gesture signals if we processed the pinch gesture when it started.
200       // Check if actor is still touchable.
201
202       Actor* currentGesturedActor = GetCurrentGesturedActor();
203       if ( currentGesturedActor )
204       {
205         if ( currentGesturedActor->IsHittable() && !mCurrentPinchEmitters.empty() && mCurrentRenderTask )
206         {
207           // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
208           GestureDetectorContainer::iterator endIter = std::remove_if( mCurrentPinchEmitters.begin(), mCurrentPinchEmitters.end(), IsNotAttachedFunctor(currentGesturedActor) );
209           mCurrentPinchEmitters.erase( endIter, mCurrentPinchEmitters.end() );
210
211           if ( !mCurrentPinchEmitters.empty() )
212           {
213             Vector2 actorCoords;
214             RenderTask& renderTaskImpl( *mCurrentRenderTask.Get() );
215             currentGesturedActor->ScreenToLocal( renderTaskImpl, actorCoords.x, actorCoords.y, pinchEvent.centerPoint.x, pinchEvent.centerPoint.y );
216
217             EmitPinchSignal( currentGesturedActor, mCurrentPinchEmitters, pinchEvent, actorCoords );
218           }
219           else
220           {
221             // If we have no current emitters then clear pinched actor as well.
222             ResetActor();
223           }
224
225           // Clear current emitters if pinch gesture has ended or been cancelled.
226           if ( pinchEvent.state == Gesture::Finished || pinchEvent.state == Gesture::Cancelled )
227           {
228             mCurrentPinchEmitters.clear();
229             ResetActor();
230           }
231         }
232         else
233         {
234           mCurrentPinchEmitters.clear();
235           ResetActor();
236         }
237       }
238       break;
239     }
240
241     case Gesture::Clear:
242     {
243       DALI_ABORT( "Incorrect state received from Integration layer: Clear\n" );
244       break;
245     }
246     case Gesture::Possible:
247     {
248       DALI_ABORT( "Incorrect state received from Integration layer: Possible\n" );
249       break;
250     }
251   }
252 }
253
254 void PinchGestureProcessor::AddGestureDetector( PinchGestureDetector* gestureDetector, Scene& scene )
255 {
256   bool createRecognizer(mPinchGestureDetectors.empty());
257
258   mPinchGestureDetectors.push_back(gestureDetector);
259
260   if (createRecognizer)
261   {
262     Size size = scene.GetSize();
263     mGestureRecognizer = new PinchGestureRecognizer( *this, Vector2(size.width, size.height), scene.GetDpi(), mMinimumPinchDistance, mMinimumTouchEventsAfterStart, mMinimumTouchEventsAfterStart );
264   }
265 }
266
267 void PinchGestureProcessor::RemoveGestureDetector( PinchGestureDetector* gestureDetector )
268 {
269   if ( !mCurrentPinchEmitters.empty() )
270   {
271     // Check if the removed detector was one that is currently being pinched and remove it from emitters.
272     GestureDetectorContainer::iterator endIter = std::remove( mCurrentPinchEmitters.begin(), mCurrentPinchEmitters.end(), gestureDetector );
273     mCurrentPinchEmitters.erase( endIter, mCurrentPinchEmitters.end() );
274
275     // If we no longer have any emitters, then we should clear mCurrentGesturedActor as well
276     if ( mCurrentPinchEmitters.empty() )
277     {
278       ResetActor();
279     }
280   }
281
282   // Find the detector...
283   PinchGestureDetectorContainer::iterator endIter = std::remove( mPinchGestureDetectors.begin(), mPinchGestureDetectors.end(), gestureDetector );
284   DALI_ASSERT_DEBUG( endIter != mPinchGestureDetectors.end() );
285
286   // ...and remove it
287   mPinchGestureDetectors.erase(endIter, mPinchGestureDetectors.end());
288
289   if (mPinchGestureDetectors.empty())
290   {
291     mGestureRecognizer.Detach();
292   }
293 }
294
295 void PinchGestureProcessor::GestureDetectorUpdated( PinchGestureDetector* gestureDetector )
296 {
297   // Nothing to do as PinchGestureDetector does not have any specific parameters.
298 }
299
300 void PinchGestureProcessor::OnGesturedActorStageDisconnection()
301 {
302   mCurrentPinchEmitters.clear();
303 }
304
305 bool PinchGestureProcessor::CheckGestureDetector( GestureDetector* detector, Actor* actor )
306 {
307   // No special case required for pinch.
308   return true;
309 }
310
311 void PinchGestureProcessor::EmitGestureSignal( Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
312 {
313   DALI_ASSERT_DEBUG( mCurrentPinchEvent );
314
315   EmitPinchSignal( actor, gestureDetectors, *mCurrentPinchEvent, actorCoordinates );
316
317   if ( actor->OnStage() )
318   {
319     mCurrentPinchEmitters = gestureDetectors;
320     SetActor( actor );
321   }
322 }
323
324 } // namespace Internal
325
326 } // namespace Dali