2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include "long-press-gesture-processor.h"
24 #include <dali/public-api/actors/actor.h>
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/events/long-press-gesture.h>
27 #include <dali/integration-api/events/long-press-gesture-event.h>
28 #include <dali/integration-api/gesture-manager.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/internal/event/actors/actor-impl.h>
31 #include <dali/internal/event/common/stage-impl.h>
32 #include <dali/internal/event/render-tasks/render-task-impl.h>
44 * Creates a LongPressGesture and asks the specified detector to emit its detected signal.
45 * @param[in] actor The actor on which the long press gesture has occurred.
46 * @param[in] gestureDetectors A reference to gesture detectors that should emit the signal.
47 * @param[in] longPressEvent The longPressEvent received from the adaptor.
48 * @param[in] localPoint Relative to the actor attached to the detector.
50 void EmitLongPressSignal(
52 LongPressGestureDetectorContainer& gestureDetectors,
53 const Integration::LongPressGestureEvent& longPressEvent,
56 LongPressGesture longPress(longPressEvent.state);
57 longPress.time = longPressEvent.time;
58 longPress.numberOfTouches = longPressEvent.numberOfTouches;
59 longPress.screenPoint = longPressEvent.point;
60 longPress.localPoint = localPoint;
62 for ( LongPressGestureDetectorContainer::iterator iter = gestureDetectors.begin(), endIter = gestureDetectors.end(); iter != endIter; ++iter )
64 (*iter)->EmitLongPressGestureSignal(actor, longPress);
69 * Functor which checks whether the specified actor is attached to the gesture detector.
70 * It returns true if it is no longer attached. This can be used in remove_if functions.
72 struct IsNotAttachedFunctor
76 * @param[in] actor The actor to check whether it is attached.
78 IsNotAttachedFunctor( Actor* actor )
79 : actorToCheck( actor )
84 * Returns true if not attached, false if it is still attached.
85 * @param[in] detector The detector to check.
86 * @return true, if not attached, false otherwise.
88 bool operator()( const LongPressGestureDetector* detector ) const
90 return !detector->IsAttached( *actorToCheck );
93 Actor* actorToCheck; ///< The actor to check whether it is attached or not.
96 } // unnamed namespace
98 struct LongPressGestureProcessor::LongPressEventFunctor : public GestureProcessor::Functor
102 * @param[in] event The current gesture event.
103 * @param[in] processor Reference to the processor.
105 LongPressEventFunctor( const Integration::LongPressGestureEvent& event, LongPressGestureProcessor& processor )
106 : longPressEvent( event ),
107 processor( processor )
112 * Check if the detector meets the current gesture event parameters.
114 virtual bool operator() ( GestureDetector* detector, Actor* )
116 LongPressGestureDetector* longPressDetector ( static_cast< LongPressGestureDetector* >( detector ) );
118 return ( longPressDetector->GetMinimumTouchesRequired() <= longPressEvent.numberOfTouches ) &&
119 ( longPressDetector->GetMaximumTouchesRequired() >= longPressEvent.numberOfTouches );
123 * Gestured actor and gesture detectors that meet the gesture's parameters found, emit and save required information.
125 virtual void operator() ( Dali::Actor actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
127 LongPressGestureDetectorContainer derivedContainer;
128 DownCastContainer<LongPressGestureDetector>( gestureDetectors, derivedContainer );
131 processor.mCurrentEmitters.clear();
132 processor.ResetActor();
134 EmitLongPressSignal( actor, derivedContainer, longPressEvent, actorCoordinates );
136 if ( actor.OnStage() )
138 processor.mCurrentEmitters = derivedContainer;
139 processor.SetActor( actor );
143 const Integration::LongPressGestureEvent& longPressEvent;
144 LongPressGestureProcessor& processor;
147 LongPressGestureProcessor::LongPressGestureProcessor(
149 Integration::GestureManager& gestureManager)
151 mGestureManager( gestureManager ),
154 mCurrentRenderTask(),
155 mMinTouchesRequired( 1 ),
156 mMaxTouchesRequired( 1 )
160 LongPressGestureProcessor::~LongPressGestureProcessor()
164 void LongPressGestureProcessor::Process( const Integration::LongPressGestureEvent& longPressEvent )
166 switch ( longPressEvent.state )
168 case Gesture::Possible:
170 mCurrentEmitters.clear();
173 HitTestAlgorithm::Results hitTestResults;
174 if( HitTest( mStage, longPressEvent.point, hitTestResults ) )
176 SetActor( hitTestResults.actor );
181 case Gesture::Started:
183 Actor* currentGesturedActor = GetCurrentGesturedActor();
184 if ( currentGesturedActor )
186 HitTestAlgorithm::Results hitTestResults;
187 HitTestAlgorithm::HitTest( mStage, longPressEvent.point, hitTestResults );
189 if ( hitTestResults.actor && ( currentGesturedActor == &GetImplementation( hitTestResults.actor ) ) )
191 // Record the current render-task for Screen->Actor coordinate conversions
192 mCurrentRenderTask = hitTestResults.renderTask;
194 LongPressEventFunctor functor( longPressEvent, *this );
195 GestureDetectorContainer gestureDetectors;
196 UpCastContainer<LongPressGestureDetector>( mGestureDetectors, gestureDetectors );
197 ProcessAndEmit( hitTestResults, gestureDetectors, functor );
201 mCurrentEmitters.clear();
208 case Gesture::Finished:
210 // The gesture should only be sent to the gesture detector which first received it so that it
211 // can be told when the gesture ends as well.
213 // Only send subsequent long press gesture signals if we processed the gesture when it started.
214 // Check if actor is still touchable.
216 Actor* currentGesturedActor = GetCurrentGesturedActor();
217 if ( currentGesturedActor )
219 if ( currentGesturedActor->IsHittable() && !mCurrentEmitters.empty() && mCurrentRenderTask )
221 // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
222 LongPressGestureDetectorContainer::iterator endIter = std::remove_if( mCurrentEmitters.begin(), mCurrentEmitters.end(), IsNotAttachedFunctor(currentGesturedActor) );
223 mCurrentEmitters.erase( endIter, mCurrentEmitters.end() );
225 if ( !mCurrentEmitters.empty() )
228 RenderTask& renderTaskImpl( GetImplementation( mCurrentRenderTask ) );
229 currentGesturedActor->ScreenToLocal( renderTaskImpl, actorCoords.x, actorCoords.y, longPressEvent.point.x, longPressEvent.point.y );
231 EmitLongPressSignal( Dali::Actor( currentGesturedActor ), mCurrentEmitters, longPressEvent, actorCoords );
235 // Clear current emitters and emitted actor
236 mCurrentEmitters.clear();
242 case Gesture::Cancelled:
244 mCurrentEmitters.clear();
249 case Gesture::Continuing:
250 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Continuing\n" );
254 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Clear\n" );
259 void LongPressGestureProcessor::AddGestureDetector( LongPressGestureDetector* gestureDetector )
261 bool firstRegistration(mGestureDetectors.empty());
263 mGestureDetectors.push_back(gestureDetector);
265 if (firstRegistration)
267 mMinTouchesRequired = gestureDetector->GetMinimumTouchesRequired();
268 mMaxTouchesRequired = gestureDetector->GetMaximumTouchesRequired();
270 Integration::LongPressGestureRequest request;
271 request.minTouches = mMinTouchesRequired;
272 request.maxTouches = mMaxTouchesRequired;
273 mGestureManager.Register( request );
281 void LongPressGestureProcessor::RemoveGestureDetector( LongPressGestureDetector* gestureDetector )
284 LongPressGestureDetectorContainer::iterator endIter = std::remove( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector );
285 DALI_ASSERT_DEBUG( endIter != mGestureDetectors.end() );
288 mGestureDetectors.erase( endIter, mGestureDetectors.end() );
290 if ( mGestureDetectors.empty() )
292 Integration::GestureRequest request( Gesture::LongPress );
293 mGestureManager.Unregister(request);
301 void LongPressGestureProcessor::GestureDetectorUpdated( LongPressGestureDetector* gestureDetector )
303 DALI_ASSERT_DEBUG( find( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector ) != mGestureDetectors.end() );
308 void LongPressGestureProcessor::UpdateDetection()
310 DALI_ASSERT_DEBUG(!mGestureDetectors.empty());
312 unsigned int minimumRequired = UINT_MAX;
313 unsigned int maximumRequired = 0;
315 for ( LongPressGestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter != endIter; ++iter )
317 LongPressGestureDetector* current(*iter);
319 unsigned int minimum = current->GetMinimumTouchesRequired();
320 if (minimum < minimumRequired)
322 minimumRequired = minimum;
325 unsigned int maximum = current->GetMaximumTouchesRequired();
326 if ( maximum > maximumRequired )
328 maximumRequired = maximum;
332 if ( (minimumRequired != mMinTouchesRequired) || (maximumRequired != mMaxTouchesRequired) )
334 mMinTouchesRequired = minimumRequired;
335 mMaxTouchesRequired = maximumRequired;
337 Integration::LongPressGestureRequest request;
338 request.minTouches = mMinTouchesRequired;
339 request.maxTouches = mMaxTouchesRequired;
340 mGestureManager.Update(request);
344 void LongPressGestureProcessor::OnGesturedActorStageDisconnection()
346 mCurrentEmitters.clear();
349 } // namespace Internal