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 "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/integration-api/events/long-press-gesture-event.h>
29 #include <dali/integration-api/gesture-manager.h>
30 #include <dali/integration-api/debug.h>
31 #include <dali/internal/event/actors/actor-impl.h>
32 #include <dali/internal/event/common/stage-impl.h>
33 #include <dali/internal/event/render-tasks/render-task-impl.h>
45 * Creates a LongPressGesture and asks the specified detector to emit its detected signal.
46 * @param[in] actor The actor on which the long press gesture has occurred.
47 * @param[in] gestureDetectors A reference to gesture detectors that should emit the signal.
48 * @param[in] longPressEvent The longPressEvent received from the adaptor.
49 * @param[in] localPoint Relative to the actor attached to the detector.
51 void EmitLongPressSignal(
53 const GestureDetectorContainer& gestureDetectors,
54 const Integration::LongPressGestureEvent& longPressEvent,
57 LongPressGesture longPress(longPressEvent.state);
58 longPress.time = longPressEvent.time;
59 longPress.numberOfTouches = longPressEvent.numberOfTouches;
60 longPress.screenPoint = longPressEvent.point;
61 longPress.localPoint = localPoint;
63 Dali::Actor actorHandle( actor );
64 const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
65 for ( GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter )
67 static_cast< LongPressGestureDetector* >( *iter )->EmitLongPressGestureSignal( actorHandle, longPress );
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 )
82 : actorToCheck( 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 GestureDetector* detector ) const
93 return !detector->IsAttached( *actorToCheck );
96 Actor* actorToCheck; ///< The actor to check whether it is attached or not.
99 } // unnamed namespace
101 LongPressGestureProcessor::LongPressGestureProcessor( Stage& stage, Integration::GestureManager& gestureManager)
102 : GestureProcessor( Gesture::LongPress ),
104 mGestureManager( gestureManager ),
107 mCurrentRenderTask(),
108 mMinTouchesRequired( 1 ),
109 mMaxTouchesRequired( 1 ),
110 mCurrentLongPressEvent( NULL )
114 LongPressGestureProcessor::~LongPressGestureProcessor()
118 void LongPressGestureProcessor::Process( const Integration::LongPressGestureEvent& longPressEvent )
120 switch ( longPressEvent.state )
122 case Gesture::Possible:
124 mCurrentEmitters.clear();
127 HitTestAlgorithm::Results hitTestResults;
128 if( HitTest( mStage, longPressEvent.point, hitTestResults ) )
130 SetActor( &GetImplementation( hitTestResults.actor ) );
135 case Gesture::Started:
137 Actor* currentGesturedActor = GetCurrentGesturedActor();
138 if ( currentGesturedActor )
140 HitTestAlgorithm::Results hitTestResults;
141 HitTest( mStage, 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 = NULL;
155 mCurrentEmitters.clear();
162 case Gesture::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( GetImplementation( mCurrentRenderTask ) );
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 Gesture::Cancelled:
198 mCurrentEmitters.clear();
203 case Gesture::Continuing:
204 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Continuing\n" );
208 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Clear\n" );
213 void LongPressGestureProcessor::AddGestureDetector( LongPressGestureDetector* gestureDetector )
215 bool firstRegistration(mGestureDetectors.empty());
217 mGestureDetectors.push_back(gestureDetector);
219 if (firstRegistration)
221 mMinTouchesRequired = gestureDetector->GetMinimumTouchesRequired();
222 mMaxTouchesRequired = gestureDetector->GetMaximumTouchesRequired();
224 Integration::LongPressGestureRequest request;
225 request.minTouches = mMinTouchesRequired;
226 request.maxTouches = mMaxTouchesRequired;
227 mGestureManager.Register( request );
235 void LongPressGestureProcessor::RemoveGestureDetector( LongPressGestureDetector* gestureDetector )
238 LongPressGestureDetectorContainer::iterator endIter = std::remove( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector );
239 DALI_ASSERT_DEBUG( endIter != mGestureDetectors.end() );
242 mGestureDetectors.erase( endIter, mGestureDetectors.end() );
244 if ( mGestureDetectors.empty() )
246 Integration::GestureRequest request( Gesture::LongPress );
247 mGestureManager.Unregister(request);
255 void LongPressGestureProcessor::GestureDetectorUpdated( LongPressGestureDetector* gestureDetector )
257 DALI_ASSERT_DEBUG( find( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector ) != mGestureDetectors.end() );
262 void LongPressGestureProcessor::UpdateDetection()
264 DALI_ASSERT_DEBUG(!mGestureDetectors.empty());
266 unsigned int minimumRequired = UINT_MAX;
267 unsigned int maximumRequired = 0;
269 for ( LongPressGestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter != endIter; ++iter )
271 LongPressGestureDetector* current(*iter);
273 unsigned int minimum = current->GetMinimumTouchesRequired();
274 if (minimum < minimumRequired)
276 minimumRequired = minimum;
279 unsigned int maximum = current->GetMaximumTouchesRequired();
280 if ( maximum > maximumRequired )
282 maximumRequired = maximum;
286 if ( (minimumRequired != mMinTouchesRequired) || (maximumRequired != mMaxTouchesRequired) )
288 mMinTouchesRequired = minimumRequired;
289 mMaxTouchesRequired = maximumRequired;
291 Integration::LongPressGestureRequest request;
292 request.minTouches = mMinTouchesRequired;
293 request.maxTouches = mMaxTouchesRequired;
294 mGestureManager.Update(request);
298 void LongPressGestureProcessor::OnGesturedActorStageDisconnection()
300 mCurrentEmitters.clear();
303 bool LongPressGestureProcessor::CheckGestureDetector( GestureDetector* detector, Actor* actor )
305 DALI_ASSERT_DEBUG( mCurrentLongPressEvent );
307 LongPressGestureDetector* longPressDetector ( static_cast< LongPressGestureDetector* >( detector ) );
309 return ( longPressDetector->GetMinimumTouchesRequired() <= mCurrentLongPressEvent->numberOfTouches ) &&
310 ( longPressDetector->GetMaximumTouchesRequired() >= mCurrentLongPressEvent->numberOfTouches );
313 void LongPressGestureProcessor::EmitGestureSignal( Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
315 DALI_ASSERT_DEBUG( mCurrentLongPressEvent );
317 mCurrentEmitters.clear();
320 EmitLongPressSignal( actor, gestureDetectors, *mCurrentLongPressEvent, actorCoordinates );
322 if ( actor->OnStage() )
324 mCurrentEmitters = gestureDetectors;
329 } // namespace Internal