2 * Copyright (c) 2020 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/internal/event/events/long-press-gesture/long-press-gesture-impl.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 Internal::LongPressGesturePtr longPress( new Internal::LongPressGesture(longPressEvent.state ) );
61 longPress->SetTime( longPressEvent.time );
62 longPress->SetNumberOfTouches( longPressEvent.numberOfTouches );
63 longPress->SetScreenPoint( longPressEvent.point );
64 longPress->SetLocalPoint( 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, Dali::LongPressGesture( longPress.Get() ) );
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( GestureType::LONG_PRESS ),
106 mLongPressGestureDetectors(),
108 mCurrentRenderTask(),
109 mMinTouchesRequired( 1 ),
110 mMaxTouchesRequired( 1 ),
111 mCurrentLongPressEvent( nullptr ),
112 mMinimumHoldingTime( DEFAULT_MINIMUM_HOLDING_TIME )
116 LongPressGestureProcessor::~LongPressGestureProcessor() = default;
118 void LongPressGestureProcessor::Process( Scene& scene, const LongPressGestureEvent& longPressEvent )
120 switch ( longPressEvent.state )
122 case GestureState::POSSIBLE:
124 mCurrentEmitters.clear();
127 HitTestAlgorithm::Results hitTestResults;
128 if( HitTest( scene, longPressEvent.point, hitTestResults ) )
130 SetActor( &GetImplementation( hitTestResults.actor ) );
135 case GestureState::STARTED:
137 Actor* currentGesturedActor = GetCurrentGesturedActor();
138 if ( currentGesturedActor )
140 HitTestAlgorithm::Results hitTestResults;
141 HitTest( scene, longPressEvent.point, hitTestResults );
143 if ( hitTestResults.actor && ( currentGesturedActor == &GetImplementation( hitTestResults.actor ) ) )
145 // Record the current render-task for Screen->Actor coordinate conversions
146 mCurrentRenderTask = hitTestResults.renderTask;
148 // Set mCurrentLongPressEvent to use inside overridden methods called from ProcessAndEmit()
149 mCurrentLongPressEvent = &longPressEvent;
150 ProcessAndEmit( hitTestResults );
151 mCurrentLongPressEvent = nullptr;
155 mCurrentEmitters.clear();
162 case GestureState::FINISHED:
164 // The gesture should only be sent to the gesture detector which first received it so that it
165 // can be told when the gesture ends as well.
167 // Only send subsequent long press gesture signals if we processed the gesture when it started.
168 // Check if actor is still touchable.
170 Actor* currentGesturedActor = GetCurrentGesturedActor();
171 if ( currentGesturedActor )
173 if ( currentGesturedActor->IsHittable() && !mCurrentEmitters.empty() && mCurrentRenderTask )
175 // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
176 GestureDetectorContainer::iterator endIter = std::remove_if( mCurrentEmitters.begin(), mCurrentEmitters.end(), IsNotAttachedFunctor(currentGesturedActor) );
177 mCurrentEmitters.erase( endIter, mCurrentEmitters.end() );
179 if ( !mCurrentEmitters.empty() )
182 RenderTask& renderTaskImpl = *mCurrentRenderTask.Get();
183 currentGesturedActor->ScreenToLocal( renderTaskImpl, actorCoords.x, actorCoords.y, longPressEvent.point.x, longPressEvent.point.y );
185 EmitLongPressSignal( currentGesturedActor, mCurrentEmitters, longPressEvent, actorCoords );
189 // Clear current emitters and emitted actor
190 mCurrentEmitters.clear();
196 case GestureState::CANCELLED:
198 mCurrentEmitters.clear();
203 case GestureState::CONTINUING:
205 DALI_ABORT( "Incorrect state received from Integration layer: CONTINUING\n" );
209 case GestureState::CLEAR:
211 DALI_ABORT( "Incorrect state received from Integration layer: CLEAR\n" );
217 void LongPressGestureProcessor::AddGestureDetector( LongPressGestureDetector* gestureDetector, Scene& scene )
219 bool firstRegistration(mLongPressGestureDetectors.empty());
221 mLongPressGestureDetectors.push_back(gestureDetector);
223 if (firstRegistration)
225 mMinTouchesRequired = gestureDetector->GetMinimumTouchesRequired();
226 mMaxTouchesRequired = gestureDetector->GetMaximumTouchesRequired();
228 LongPressGestureRequest request;
229 request.minTouches = mMinTouchesRequired;
230 request.maxTouches = mMaxTouchesRequired;
232 Size size = scene.GetSize();
234 mGestureRecognizer = new LongPressGestureRecognizer(*this, Vector2(size.width, size.height), static_cast<const LongPressGestureRequest&>(request), mMinimumHoldingTime );
242 void LongPressGestureProcessor::RemoveGestureDetector( LongPressGestureDetector* gestureDetector )
245 LongPressGestureDetectorContainer::iterator endIter = std::remove( mLongPressGestureDetectors.begin(), mLongPressGestureDetectors.end(), gestureDetector );
246 DALI_ASSERT_DEBUG( endIter != mLongPressGestureDetectors.end() );
249 mLongPressGestureDetectors.erase( endIter, mLongPressGestureDetectors.end() );
251 if ( mLongPressGestureDetectors.empty() )
253 mGestureRecognizer = nullptr;
261 void LongPressGestureProcessor::GestureDetectorUpdated( LongPressGestureDetector* gestureDetector )
263 DALI_ASSERT_DEBUG( find( mLongPressGestureDetectors.begin(), mLongPressGestureDetectors.end(), gestureDetector ) != mLongPressGestureDetectors.end() );
268 void LongPressGestureProcessor::SetMinimumHoldingTime( uint32_t time )
270 if( time > 0u && mMinimumHoldingTime != time )
272 mMinimumHoldingTime = time;
274 if( mGestureRecognizer )
276 LongPressGestureRecognizer* longPressRecognizer = dynamic_cast<LongPressGestureRecognizer*>( mGestureRecognizer.Get() );
277 if( longPressRecognizer )
279 longPressRecognizer->SetMinimumHoldingTime( time );
285 uint32_t LongPressGestureProcessor::GetMinimumHoldingTime() const
287 return mMinimumHoldingTime;
290 void LongPressGestureProcessor::UpdateDetection()
292 DALI_ASSERT_DEBUG(!mLongPressGestureDetectors.empty());
294 unsigned int minimumRequired = UINT_MAX;
295 unsigned int maximumRequired = 0;
297 for ( LongPressGestureDetectorContainer::iterator iter = mLongPressGestureDetectors.begin(), endIter = mLongPressGestureDetectors.end(); iter != endIter; ++iter )
299 LongPressGestureDetector* current(*iter);
303 unsigned int minimum = current->GetMinimumTouchesRequired();
304 if (minimum < minimumRequired)
306 minimumRequired = minimum;
309 unsigned int maximum = current->GetMaximumTouchesRequired();
310 if ( maximum > maximumRequired )
312 maximumRequired = maximum;
317 if ( (minimumRequired != mMinTouchesRequired) || (maximumRequired != mMaxTouchesRequired) )
319 mMinTouchesRequired = minimumRequired;
320 mMaxTouchesRequired = maximumRequired;
322 LongPressGestureRequest request;
323 request.minTouches = mMinTouchesRequired;
324 request.maxTouches = mMaxTouchesRequired;
325 mGestureRecognizer->Update(request);
329 void LongPressGestureProcessor::OnGesturedActorStageDisconnection()
331 mCurrentEmitters.clear();
334 bool LongPressGestureProcessor::CheckGestureDetector( GestureDetector* detector, Actor* actor )
336 DALI_ASSERT_DEBUG( mCurrentLongPressEvent );
338 LongPressGestureDetector* longPressDetector ( static_cast< LongPressGestureDetector* >( detector ) );
340 return ( longPressDetector->GetMinimumTouchesRequired() <= mCurrentLongPressEvent->numberOfTouches ) &&
341 ( longPressDetector->GetMaximumTouchesRequired() >= mCurrentLongPressEvent->numberOfTouches );
344 void LongPressGestureProcessor::EmitGestureSignal( Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
346 DALI_ASSERT_DEBUG( mCurrentLongPressEvent );
348 mCurrentEmitters.clear();
351 EmitLongPressSignal( actor, gestureDetectors, *mCurrentLongPressEvent, actorCoordinates );
353 if ( actor->OnScene() )
355 mCurrentEmitters = gestureDetectors;
360 } // namespace Internal