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 const GestureDetectorContainer& 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 Dali::Actor actorHandle( actor );
66 const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
67 for ( GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter )
69 static_cast< PinchGestureDetector* >( *iter )->EmitPinchGestureSignal( actorHandle, pinch );
74 * Functor which checks whether the specified actor is attached to the gesture detector.
75 * It returns true if it is no longer attached. This can be used in remove_if functions.
77 struct IsNotAttachedFunctor
81 * @param[in] actor The actor to check whether it is attached.
83 IsNotAttachedFunctor(Actor* actor)
89 * Returns true if not attached, false if it is still attached.
90 * @param[in] detector The detector to check.
91 * @return true, if not attached, false otherwise.
93 bool operator()(const GestureDetector* detector) const
95 return !detector->IsAttached(*actorToCheck);
98 Actor* actorToCheck; ///< The actor to check whether it is attached or not.
101 } // unnamed namespace
103 PinchGestureProcessor::PinchGestureProcessor( Stage& stage, Integration::GestureManager& gestureManager )
104 : GestureProcessor( Gesture::Pinch ),
106 mGestureManager(gestureManager),
108 mCurrentPinchEmitters(),
109 mCurrentPinchEvent(NULL)
113 PinchGestureProcessor::~PinchGestureProcessor()
117 void PinchGestureProcessor::Process( const Integration::PinchGestureEvent& pinchEvent )
119 switch ( pinchEvent.state )
121 case Gesture::Started:
123 // The pinch gesture should only be sent to the gesture detector which first received it so that
124 // it can be told when the gesture ends as well.
126 mCurrentPinchEmitters.clear();
129 HitTestAlgorithm::Results hitTestResults;
130 if( HitTest( mStage, pinchEvent.centerPoint, hitTestResults ) )
132 // Record the current render-task for Screen->Actor coordinate conversions
133 mCurrentRenderTask = hitTestResults.renderTask;
135 // Set mCurrentPinchEvent to use inside overridden methods called from ProcessAndEmit()
136 mCurrentPinchEvent = &pinchEvent;
137 ProcessAndEmit( hitTestResults );
138 mCurrentPinchEvent = NULL;
143 case Gesture::Continuing:
144 case Gesture::Finished:
145 case Gesture::Cancelled:
147 // Only send subsequent pinch gesture signals if we processed the pinch gesture when it started.
148 // Check if actor is still touchable.
150 Actor* currentGesturedActor = GetCurrentGesturedActor();
151 if ( currentGesturedActor )
153 if ( currentGesturedActor->IsHittable() && !mCurrentPinchEmitters.empty() && mCurrentRenderTask )
155 // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
156 GestureDetectorContainer::iterator endIter = std::remove_if( mCurrentPinchEmitters.begin(), mCurrentPinchEmitters.end(), IsNotAttachedFunctor(currentGesturedActor) );
157 mCurrentPinchEmitters.erase( endIter, mCurrentPinchEmitters.end() );
159 if ( !mCurrentPinchEmitters.empty() )
162 RenderTask& renderTaskImpl( GetImplementation(mCurrentRenderTask) );
163 currentGesturedActor->ScreenToLocal( renderTaskImpl, actorCoords.x, actorCoords.y, pinchEvent.centerPoint.x, pinchEvent.centerPoint.y );
165 EmitPinchSignal( currentGesturedActor, mCurrentPinchEmitters, pinchEvent, actorCoords );
169 // If we have no current emitters then clear pinched actor as well.
173 // Clear current emitters if pinch gesture has ended or been cancelled.
174 if ( pinchEvent.state == Gesture::Finished || pinchEvent.state == Gesture::Cancelled )
176 mCurrentPinchEmitters.clear();
182 mCurrentPinchEmitters.clear();
190 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Clear\n" );
193 case Gesture::Possible:
194 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Possible\n" );
199 void PinchGestureProcessor::AddGestureDetector( PinchGestureDetector* gestureDetector )
201 bool registerWithAdaptor(mGestureDetectors.empty());
203 mGestureDetectors.push_back(gestureDetector);
205 if (registerWithAdaptor)
207 Integration::GestureRequest request(Gesture::Pinch);
208 mGestureManager.Register(request);
212 void PinchGestureProcessor::RemoveGestureDetector( PinchGestureDetector* gestureDetector )
214 if ( !mCurrentPinchEmitters.empty() )
216 // Check if the removed detector was one that is currently being pinched and remove it from emitters.
217 GestureDetectorContainer::iterator endIter = std::remove( mCurrentPinchEmitters.begin(), mCurrentPinchEmitters.end(), gestureDetector );
218 mCurrentPinchEmitters.erase( endIter, mCurrentPinchEmitters.end() );
220 // If we no longer have any emitters, then we should clear mCurrentGesturedActor as well
221 if ( mCurrentPinchEmitters.empty() )
227 // Find the detector...
228 PinchGestureDetectorContainer::iterator endIter = std::remove( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector );
229 DALI_ASSERT_DEBUG( endIter != mGestureDetectors.end() );
232 mGestureDetectors.erase(endIter, mGestureDetectors.end());
234 if (mGestureDetectors.empty())
236 Integration::GestureRequest request(Gesture::Pinch);
237 mGestureManager.Unregister(request);
241 void PinchGestureProcessor::GestureDetectorUpdated( PinchGestureDetector* gestureDetector )
243 // Nothing to do as PinchGestureDetector does not have any specific parameters.
246 void PinchGestureProcessor::OnGesturedActorStageDisconnection()
248 mCurrentPinchEmitters.clear();
251 bool PinchGestureProcessor::CheckGestureDetector( GestureDetector* detector, Actor* actor )
253 // No special case required for pinch.
257 void PinchGestureProcessor::EmitGestureSignal( Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
259 DALI_ASSERT_DEBUG( mCurrentPinchEvent );
261 EmitPinchSignal( actor, gestureDetectors, *mCurrentPinchEvent, actorCoordinates );
263 if ( actor->OnStage() )
265 mCurrentPinchEmitters = gestureDetectors;
270 } // namespace Internal