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/long-press-gesture/long-press-gesture-processor.h>
25 #include <dali/public-api/actors/actor.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/events/long-press-gesture.h>
28 #include <dali/internal/event/events/long-press-gesture/long-press-gesture-event.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/internal/event/actors/actor-impl.h>
31 #include <dali/internal/event/common/scene-impl.h>
32 #include <dali/internal/event/render-tasks/render-task-impl.h>
33 #include <dali/internal/event/events/long-press-gesture/long-press-gesture-recognizer.h>
34 #include <dali/internal/event/events/gesture-requests.h>
45 const unsigned long DEFAULT_MINIMUM_HOLDING_TIME = 500u;
48 * Creates a LongPressGesture and asks the specified detector to emit its detected signal.
49 * @param[in] actor The actor on which the long press gesture has occurred.
50 * @param[in] gestureDetectors A reference to gesture detectors that should emit the signal.
51 * @param[in] longPressEvent The longPressEvent received from the adaptor.
52 * @param[in] localPoint Relative to the actor attached to the detector.
54 void EmitLongPressSignal(
56 const GestureDetectorContainer& gestureDetectors,
57 const LongPressGestureEvent& longPressEvent,
60 LongPressGesture longPress(longPressEvent.state);
61 longPress.time = longPressEvent.time;
62 longPress.numberOfTouches = longPressEvent.numberOfTouches;
63 longPress.screenPoint = longPressEvent.point;
64 longPress.localPoint = localPoint;
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< LongPressGestureDetector* >( *iter )->EmitLongPressGestureSignal( actorHandle, longPress );
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 )
85 : actorToCheck( 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 LongPressGestureProcessor::LongPressGestureProcessor()
105 : GestureProcessor( Gesture::LongPress ),
106 mLongPressGestureDetectors(),
108 mCurrentRenderTask(),
109 mMinTouchesRequired( 1 ),
110 mMaxTouchesRequired( 1 ),
111 mCurrentLongPressEvent( NULL ),
112 mMinimumHoldingTime( DEFAULT_MINIMUM_HOLDING_TIME )
116 LongPressGestureProcessor::~LongPressGestureProcessor()
120 void LongPressGestureProcessor::Process( Scene& scene, const LongPressGestureEvent& longPressEvent )
122 switch ( longPressEvent.state )
124 case Gesture::Possible:
126 mCurrentEmitters.clear();
129 HitTestAlgorithm::Results hitTestResults;
130 if( HitTest( scene, longPressEvent.point, hitTestResults ) )
132 SetActor( &GetImplementation( hitTestResults.actor ) );
137 case Gesture::Started:
139 Actor* currentGesturedActor = GetCurrentGesturedActor();
140 if ( currentGesturedActor )
142 HitTestAlgorithm::Results hitTestResults;
143 HitTest( scene, longPressEvent.point, hitTestResults );
145 if ( hitTestResults.actor && ( currentGesturedActor == &GetImplementation( hitTestResults.actor ) ) )
147 // Record the current render-task for Screen->Actor coordinate conversions
148 mCurrentRenderTask = hitTestResults.renderTask;
150 // Set mCurrentLongPressEvent to use inside overridden methods called from ProcessAndEmit()
151 mCurrentLongPressEvent = &longPressEvent;
152 ProcessAndEmit( hitTestResults );
153 mCurrentLongPressEvent = NULL;
157 mCurrentEmitters.clear();
164 case Gesture::Finished:
166 // The gesture should only be sent to the gesture detector which first received it so that it
167 // can be told when the gesture ends as well.
169 // Only send subsequent long press gesture signals if we processed the gesture when it started.
170 // Check if actor is still touchable.
172 Actor* currentGesturedActor = GetCurrentGesturedActor();
173 if ( currentGesturedActor )
175 if ( currentGesturedActor->IsHittable() && !mCurrentEmitters.empty() && mCurrentRenderTask )
177 // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
178 GestureDetectorContainer::iterator endIter = std::remove_if( mCurrentEmitters.begin(), mCurrentEmitters.end(), IsNotAttachedFunctor(currentGesturedActor) );
179 mCurrentEmitters.erase( endIter, mCurrentEmitters.end() );
181 if ( !mCurrentEmitters.empty() )
184 RenderTask& renderTaskImpl = *mCurrentRenderTask.Get();
185 currentGesturedActor->ScreenToLocal( renderTaskImpl, actorCoords.x, actorCoords.y, longPressEvent.point.x, longPressEvent.point.y );
187 EmitLongPressSignal( currentGesturedActor, mCurrentEmitters, longPressEvent, actorCoords );
191 // Clear current emitters and emitted actor
192 mCurrentEmitters.clear();
198 case Gesture::Cancelled:
200 mCurrentEmitters.clear();
205 case Gesture::Continuing:
207 DALI_ABORT( "Incorrect state received from Integration layer: Continuing\n" );
213 DALI_ABORT( "Incorrect state received from Integration layer: Clear\n" );
219 void LongPressGestureProcessor::AddGestureDetector( LongPressGestureDetector* gestureDetector, Scene& scene )
221 bool firstRegistration(mLongPressGestureDetectors.empty());
223 mLongPressGestureDetectors.push_back(gestureDetector);
225 if (firstRegistration)
227 mMinTouchesRequired = gestureDetector->GetMinimumTouchesRequired();
228 mMaxTouchesRequired = gestureDetector->GetMaximumTouchesRequired();
230 LongPressGestureRequest request;
231 request.minTouches = mMinTouchesRequired;
232 request.maxTouches = mMaxTouchesRequired;
234 Size size = scene.GetSize();
236 mGestureRecognizer = new LongPressGestureRecognizer(*this, Vector2(size.width, size.height), static_cast<const LongPressGestureRequest&>(request), mMinimumHoldingTime );
244 void LongPressGestureProcessor::RemoveGestureDetector( LongPressGestureDetector* gestureDetector )
247 LongPressGestureDetectorContainer::iterator endIter = std::remove( mLongPressGestureDetectors.begin(), mLongPressGestureDetectors.end(), gestureDetector );
248 DALI_ASSERT_DEBUG( endIter != mLongPressGestureDetectors.end() );
251 mLongPressGestureDetectors.erase( endIter, mLongPressGestureDetectors.end() );
253 if ( mLongPressGestureDetectors.empty() )
255 mGestureRecognizer.Detach();
263 void LongPressGestureProcessor::GestureDetectorUpdated( LongPressGestureDetector* gestureDetector )
265 DALI_ASSERT_DEBUG( find( mLongPressGestureDetectors.begin(), mLongPressGestureDetectors.end(), gestureDetector ) != mLongPressGestureDetectors.end() );
270 void LongPressGestureProcessor::SetMinimumHoldingTime( uint32_t time )
272 if( time > 0u && mMinimumHoldingTime != time )
274 mMinimumHoldingTime = time;
276 if( mGestureRecognizer )
278 LongPressGestureRecognizer* longPressRecognizer = dynamic_cast<LongPressGestureRecognizer*>( mGestureRecognizer.Get() );
279 if( longPressRecognizer )
281 longPressRecognizer->SetMinimumHoldingTime( time );
287 uint32_t LongPressGestureProcessor::GetMinimumHoldingTime() const
289 return mMinimumHoldingTime;
292 void LongPressGestureProcessor::UpdateDetection()
294 DALI_ASSERT_DEBUG(!mLongPressGestureDetectors.empty());
296 unsigned int minimumRequired = UINT_MAX;
297 unsigned int maximumRequired = 0;
299 for ( LongPressGestureDetectorContainer::iterator iter = mLongPressGestureDetectors.begin(), endIter = mLongPressGestureDetectors.end(); iter != endIter; ++iter )
301 LongPressGestureDetector* current(*iter);
305 unsigned int minimum = current->GetMinimumTouchesRequired();
306 if (minimum < minimumRequired)
308 minimumRequired = minimum;
311 unsigned int maximum = current->GetMaximumTouchesRequired();
312 if ( maximum > maximumRequired )
314 maximumRequired = maximum;
319 if ( (minimumRequired != mMinTouchesRequired) || (maximumRequired != mMaxTouchesRequired) )
321 mMinTouchesRequired = minimumRequired;
322 mMaxTouchesRequired = maximumRequired;
324 LongPressGestureRequest request;
325 request.minTouches = mMinTouchesRequired;
326 request.maxTouches = mMaxTouchesRequired;
327 mGestureRecognizer->Update(request);
331 void LongPressGestureProcessor::OnGesturedActorStageDisconnection()
333 mCurrentEmitters.clear();
336 bool LongPressGestureProcessor::CheckGestureDetector( GestureDetector* detector, Actor* actor )
338 DALI_ASSERT_DEBUG( mCurrentLongPressEvent );
340 LongPressGestureDetector* longPressDetector ( static_cast< LongPressGestureDetector* >( detector ) );
342 return ( longPressDetector->GetMinimumTouchesRequired() <= mCurrentLongPressEvent->numberOfTouches ) &&
343 ( longPressDetector->GetMaximumTouchesRequired() >= mCurrentLongPressEvent->numberOfTouches );
346 void LongPressGestureProcessor::EmitGestureSignal( Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
348 DALI_ASSERT_DEBUG( mCurrentLongPressEvent );
350 mCurrentEmitters.clear();
353 EmitLongPressSignal( actor, gestureDetectors, *mCurrentLongPressEvent, actorCoordinates );
355 if ( actor->OnStage() )
357 mCurrentEmitters = gestureDetectors;
362 } // namespace Internal