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