2 * Copyright (c) 2019 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/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/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>
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.
53 const GestureDetectorContainer& gestureDetectors,
54 const PinchGestureEvent& pinchEvent,
57 PinchGesture pinch(pinchEvent.state);
58 pinch.time = pinchEvent.time;
60 pinch.scale = pinchEvent.scale;
61 pinch.speed = pinchEvent.speed;
62 pinch.screenCenterPoint = pinchEvent.centerPoint;
64 pinch.localCenterPoint = localCenter;
66 Dali::Actor actorHandle( actor );
67 const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
68 for ( GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter )
70 static_cast< PinchGestureDetector* >( *iter )->EmitPinchGestureSignal( actorHandle, pinch );
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.
78 struct IsNotAttachedFunctor
82 * @param[in] actor The actor to check whether it is attached.
84 IsNotAttachedFunctor(Actor* actor)
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.
94 bool operator()(const GestureDetector* detector) const
96 return !detector->IsAttached(*actorToCheck);
99 Actor* actorToCheck; ///< The actor to check whether it is attached or not.
102 } // unnamed namespace
104 PinchGestureProcessor::PinchGestureProcessor()
105 : GestureProcessor( Gesture::Pinch ),
106 mPinchGestureDetectors(),
107 mCurrentPinchEmitters(),
108 mCurrentPinchEvent(NULL),
109 mMinimumPinchDistance(-1.0f)
113 PinchGestureProcessor::~PinchGestureProcessor()
117 void PinchGestureProcessor::SetMinimumPinchDistance( float value )
119 mMinimumPinchDistance = value;
121 if( mGestureRecognizer )
123 PinchGestureRecognizer* pinchRecognizer = dynamic_cast<PinchGestureRecognizer*>(mGestureRecognizer.Get());
124 if( pinchRecognizer )
126 pinchRecognizer->SetMinimumPinchDistance(value);
131 void PinchGestureProcessor::Process( Scene& scene, const PinchGestureEvent& pinchEvent )
133 switch ( pinchEvent.state )
135 case Gesture::Started:
137 // The pinch gesture should only be sent to the gesture detector which first received it so that
138 // it can be told when the gesture ends as well.
140 mCurrentPinchEmitters.clear();
143 HitTestAlgorithm::Results hitTestResults;
144 if( HitTest( scene, pinchEvent.centerPoint, hitTestResults ) )
146 // Record the current render-task for Screen->Actor coordinate conversions
147 mCurrentRenderTask = hitTestResults.renderTask;
149 // Set mCurrentPinchEvent to use inside overridden methods called from ProcessAndEmit()
150 mCurrentPinchEvent = &pinchEvent;
151 ProcessAndEmit( hitTestResults );
152 mCurrentPinchEvent = NULL;
157 case Gesture::Continuing:
158 case Gesture::Finished:
159 case Gesture::Cancelled:
161 // Only send subsequent pinch gesture signals if we processed the pinch gesture when it started.
162 // Check if actor is still touchable.
164 Actor* currentGesturedActor = GetCurrentGesturedActor();
165 if ( currentGesturedActor )
167 if ( currentGesturedActor->IsHittable() && !mCurrentPinchEmitters.empty() && mCurrentRenderTask )
169 // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
170 GestureDetectorContainer::iterator endIter = std::remove_if( mCurrentPinchEmitters.begin(), mCurrentPinchEmitters.end(), IsNotAttachedFunctor(currentGesturedActor) );
171 mCurrentPinchEmitters.erase( endIter, mCurrentPinchEmitters.end() );
173 if ( !mCurrentPinchEmitters.empty() )
176 RenderTask& renderTaskImpl( *mCurrentRenderTask.Get() );
177 currentGesturedActor->ScreenToLocal( renderTaskImpl, actorCoords.x, actorCoords.y, pinchEvent.centerPoint.x, pinchEvent.centerPoint.y );
179 EmitPinchSignal( currentGesturedActor, mCurrentPinchEmitters, pinchEvent, actorCoords );
183 // If we have no current emitters then clear pinched actor as well.
187 // Clear current emitters if pinch gesture has ended or been cancelled.
188 if ( pinchEvent.state == Gesture::Finished || pinchEvent.state == Gesture::Cancelled )
190 mCurrentPinchEmitters.clear();
196 mCurrentPinchEmitters.clear();
205 DALI_ABORT( "Incorrect state received from Integration layer: Clear\n" );
208 case Gesture::Possible:
210 DALI_ABORT( "Incorrect state received from Integration layer: Possible\n" );
216 void PinchGestureProcessor::AddGestureDetector( PinchGestureDetector* gestureDetector, Scene& scene )
218 bool createRecognizer(mPinchGestureDetectors.empty());
220 mPinchGestureDetectors.push_back(gestureDetector);
222 if (createRecognizer)
224 Size size = scene.GetSize();
225 mGestureRecognizer = new PinchGestureRecognizer( *this, Vector2(size.width, size.height), scene.GetDpi(), mMinimumPinchDistance );
229 void PinchGestureProcessor::RemoveGestureDetector( PinchGestureDetector* gestureDetector )
231 if ( !mCurrentPinchEmitters.empty() )
233 // Check if the removed detector was one that is currently being pinched and remove it from emitters.
234 GestureDetectorContainer::iterator endIter = std::remove( mCurrentPinchEmitters.begin(), mCurrentPinchEmitters.end(), gestureDetector );
235 mCurrentPinchEmitters.erase( endIter, mCurrentPinchEmitters.end() );
237 // If we no longer have any emitters, then we should clear mCurrentGesturedActor as well
238 if ( mCurrentPinchEmitters.empty() )
244 // Find the detector...
245 PinchGestureDetectorContainer::iterator endIter = std::remove( mPinchGestureDetectors.begin(), mPinchGestureDetectors.end(), gestureDetector );
246 DALI_ASSERT_DEBUG( endIter != mPinchGestureDetectors.end() );
249 mPinchGestureDetectors.erase(endIter, mPinchGestureDetectors.end());
251 if (mPinchGestureDetectors.empty())
253 mGestureRecognizer.Detach();
257 void PinchGestureProcessor::GestureDetectorUpdated( PinchGestureDetector* gestureDetector )
259 // Nothing to do as PinchGestureDetector does not have any specific parameters.
262 void PinchGestureProcessor::OnGesturedActorStageDisconnection()
264 mCurrentPinchEmitters.clear();
267 bool PinchGestureProcessor::CheckGestureDetector( GestureDetector* detector, Actor* actor )
269 // No special case required for pinch.
273 void PinchGestureProcessor::EmitGestureSignal( Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
275 DALI_ASSERT_DEBUG( mCurrentPinchEvent );
277 EmitPinchSignal( actor, gestureDetectors, *mCurrentPinchEvent, actorCoordinates );
279 if ( actor->OnStage() )
281 mCurrentPinchEmitters = gestureDetectors;
286 } // namespace Internal