2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 #include <dali/internal/event/events/pinch-gesture-processor.h>
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>
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.
51 PinchGestureDetectorContainer& gestureDetectors,
52 const Integration::PinchGestureEvent& pinchEvent,
55 PinchGesture pinch(pinchEvent.state);
56 pinch.time = pinchEvent.time;
58 pinch.scale = pinchEvent.scale;
59 pinch.speed = pinchEvent.speed;
60 pinch.screenCenterPoint = pinchEvent.centerPoint;
62 pinch.localCenterPoint = localCenter;
64 for ( PinchGestureDetectorContainer::iterator iter = gestureDetectors.begin(), endIter = gestureDetectors.end(); iter != endIter; ++iter )
66 (*iter)->EmitPinchGestureSignal(actor, pinch);
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.
74 struct IsNotAttachedFunctor
78 * @param[in] actor The actor to check whether it is attached.
80 IsNotAttachedFunctor(Actor* actor)
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.
90 bool operator()(const PinchGestureDetector* detector) const
92 return !detector->IsAttached(*actorToCheck);
95 Actor* actorToCheck; ///< The actor to check whether it is attached or not.
98 } // unnamed namespace
100 struct PinchGestureProcessor::PinchEventFunctor : public GestureProcessor::Functor
104 * @param[in] pinchEvent The current gesture event.
105 * @param[in] processor Reference to the processor.
107 PinchEventFunctor( const Integration::PinchGestureEvent& pinchEvent, PinchGestureProcessor& processor )
108 : pinchEvent( pinchEvent ),
109 processor( processor )
114 * Check if the detector meets the current gesture event parameters.
116 virtual bool operator() ( GestureDetector*, Actor* )
122 * Gestured actor and gesture detectors that meet the gesture's parameters found, emit and save required information.
124 virtual void operator() ( Dali::Actor actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
126 PinchGestureDetectorContainer derivedContainer;
127 DownCastContainer<PinchGestureDetector>( gestureDetectors, derivedContainer );
129 EmitPinchSignal( actor, derivedContainer, pinchEvent, actorCoordinates );
131 if ( actor.OnStage() )
133 processor.mCurrentPinchEmitters = derivedContainer;
134 processor.SetActor( actor );
138 const Integration::PinchGestureEvent& pinchEvent;
139 PinchGestureProcessor& processor;
142 PinchGestureProcessor::PinchGestureProcessor( Stage& stage, Integration::GestureManager& gestureManager )
143 : GestureProcessor( Gesture::Pinch ),
145 mGestureManager(gestureManager),
147 mCurrentPinchEmitters()
151 PinchGestureProcessor::~PinchGestureProcessor()
155 void PinchGestureProcessor::Process( const Integration::PinchGestureEvent& pinchEvent )
157 switch ( pinchEvent.state )
159 case Gesture::Started:
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.
164 mCurrentPinchEmitters.clear();
167 HitTestAlgorithm::Results hitTestResults;
168 if( HitTest( mStage, pinchEvent.centerPoint, hitTestResults ) )
170 // Record the current render-task for Screen->Actor coordinate conversions
171 mCurrentRenderTask = hitTestResults.renderTask;
173 PinchEventFunctor functor( pinchEvent, *this ); // Sets mCurrentGesturedActor
174 GestureDetectorContainer gestureDetectors;
175 UpCastContainer<PinchGestureDetector>( mGestureDetectors, gestureDetectors );
176 ProcessAndEmit( hitTestResults, gestureDetectors, functor );
181 case Gesture::Continuing:
182 case Gesture::Finished:
183 case Gesture::Cancelled:
185 // Only send subsequent pinch gesture signals if we processed the pinch gesture when it started.
186 // Check if actor is still touchable.
188 Actor* currentGesturedActor = GetCurrentGesturedActor();
189 if ( currentGesturedActor )
191 if ( currentGesturedActor->IsHittable() && !mCurrentPinchEmitters.empty() && mCurrentRenderTask )
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() );
197 if ( !mCurrentPinchEmitters.empty() )
200 RenderTask& renderTaskImpl( GetImplementation(mCurrentRenderTask) );
201 currentGesturedActor->ScreenToLocal( renderTaskImpl, actorCoords.x, actorCoords.y, pinchEvent.centerPoint.x, pinchEvent.centerPoint.y );
203 EmitPinchSignal( Dali::Actor(currentGesturedActor), mCurrentPinchEmitters, pinchEvent, actorCoords );
207 // If we have no current emitters then clear pinched actor as well.
211 // Clear current emitters if pinch gesture has ended or been cancelled.
212 if ( pinchEvent.state == Gesture::Finished || pinchEvent.state == Gesture::Cancelled )
214 mCurrentPinchEmitters.clear();
220 mCurrentPinchEmitters.clear();
228 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Clear\n" );
231 case Gesture::Possible:
232 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Possible\n" );
237 void PinchGestureProcessor::AddGestureDetector( PinchGestureDetector* gestureDetector )
239 bool registerWithAdaptor(mGestureDetectors.empty());
241 mGestureDetectors.push_back(gestureDetector);
243 if (registerWithAdaptor)
245 Integration::GestureRequest request(Gesture::Pinch);
246 mGestureManager.Register(request);
250 void PinchGestureProcessor::RemoveGestureDetector( PinchGestureDetector* gestureDetector )
252 if ( !mCurrentPinchEmitters.empty() )
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() );
258 // If we no longer have any emitters, then we should clear mCurrentGesturedActor as well
259 if ( mCurrentPinchEmitters.empty() )
265 // Find the detector...
266 PinchGestureDetectorContainer::iterator endIter = std::remove( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector );
267 DALI_ASSERT_DEBUG( endIter != mGestureDetectors.end() );
270 mGestureDetectors.erase(endIter, mGestureDetectors.end());
272 if (mGestureDetectors.empty())
274 Integration::GestureRequest request(Gesture::Pinch);
275 mGestureManager.Unregister(request);
279 void PinchGestureProcessor::GestureDetectorUpdated( PinchGestureDetector* gestureDetector )
281 // Nothing to do as PinchGestureDetector does not have any specific parameters.
284 void PinchGestureProcessor::OnGesturedActorStageDisconnection()
286 mCurrentPinchEmitters.clear();
289 } // namespace Internal