2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/events/pinch-gesture-processor.h>
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>
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.
52 PinchGestureDetectorContainer& gestureDetectors,
53 const Integration::PinchGestureEvent& pinchEvent,
56 PinchGesture pinch(pinchEvent.state);
57 pinch.time = pinchEvent.time;
59 pinch.scale = pinchEvent.scale;
60 pinch.speed = pinchEvent.speed;
61 pinch.screenCenterPoint = pinchEvent.centerPoint;
63 pinch.localCenterPoint = localCenter;
65 for ( PinchGestureDetectorContainer::iterator iter = gestureDetectors.begin(), endIter = gestureDetectors.end(); iter != endIter; ++iter )
67 (*iter)->EmitPinchGestureSignal(actor, pinch);
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.
75 struct IsNotAttachedFunctor
79 * @param[in] actor The actor to check whether it is attached.
81 IsNotAttachedFunctor(Actor* actor)
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.
91 bool operator()(const PinchGestureDetector* detector) const
93 return !detector->IsAttached(*actorToCheck);
96 Actor* actorToCheck; ///< The actor to check whether it is attached or not.
99 } // unnamed namespace
101 struct PinchGestureProcessor::PinchEventFunctor : public GestureProcessor::Functor
105 * @param[in] pinchEvent The current gesture event.
106 * @param[in] processor Reference to the processor.
108 PinchEventFunctor( const Integration::PinchGestureEvent& pinchEvent, PinchGestureProcessor& processor )
109 : pinchEvent( pinchEvent ),
110 processor( processor )
115 * Check if the detector meets the current gesture event parameters.
117 virtual bool operator() ( GestureDetector*, Actor* )
123 * Gestured actor and gesture detectors that meet the gesture's parameters found, emit and save required information.
125 virtual void operator() ( Dali::Actor actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
127 PinchGestureDetectorContainer derivedContainer;
128 DownCastContainer<PinchGestureDetector>( gestureDetectors, derivedContainer );
130 EmitPinchSignal( actor, derivedContainer, pinchEvent, actorCoordinates );
132 if ( actor.OnStage() )
134 processor.mCurrentPinchEmitters = derivedContainer;
135 processor.SetActor( actor );
139 const Integration::PinchGestureEvent& pinchEvent;
140 PinchGestureProcessor& processor;
143 PinchGestureProcessor::PinchGestureProcessor( Stage& stage, Integration::GestureManager& gestureManager )
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