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( Stage& stage, Integration::GestureManager& gestureManager)
148 : GestureProcessor( Gesture::LongPress ),
150 mGestureManager( gestureManager ),
153 mCurrentRenderTask(),
154 mMinTouchesRequired( 1 ),
155 mMaxTouchesRequired( 1 )
159 LongPressGestureProcessor::~LongPressGestureProcessor()
163 void LongPressGestureProcessor::Process( const Integration::LongPressGestureEvent& longPressEvent )
165 switch ( longPressEvent.state )
167 case Gesture::Possible:
169 mCurrentEmitters.clear();
172 HitTestAlgorithm::Results hitTestResults;
173 if( HitTest( mStage, longPressEvent.point, hitTestResults ) )
175 SetActor( hitTestResults.actor );
180 case Gesture::Started:
182 Actor* currentGesturedActor = GetCurrentGesturedActor();
183 if ( currentGesturedActor )
185 HitTestAlgorithm::Results hitTestResults;
186 HitTest( mStage, longPressEvent.point, hitTestResults );
188 if ( hitTestResults.actor && ( currentGesturedActor == &GetImplementation( hitTestResults.actor ) ) )
190 // Record the current render-task for Screen->Actor coordinate conversions
191 mCurrentRenderTask = hitTestResults.renderTask;
193 LongPressEventFunctor functor( longPressEvent, *this );
194 GestureDetectorContainer gestureDetectors;
195 UpCastContainer<LongPressGestureDetector>( mGestureDetectors, gestureDetectors );
196 ProcessAndEmit( hitTestResults, gestureDetectors, functor );
200 mCurrentEmitters.clear();
207 case Gesture::Finished:
209 // The gesture should only be sent to the gesture detector which first received it so that it
210 // can be told when the gesture ends as well.
212 // Only send subsequent long press gesture signals if we processed the gesture when it started.
213 // Check if actor is still touchable.
215 Actor* currentGesturedActor = GetCurrentGesturedActor();
216 if ( currentGesturedActor )
218 if ( currentGesturedActor->IsHittable() && !mCurrentEmitters.empty() && mCurrentRenderTask )
220 // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
221 LongPressGestureDetectorContainer::iterator endIter = std::remove_if( mCurrentEmitters.begin(), mCurrentEmitters.end(), IsNotAttachedFunctor(currentGesturedActor) );
222 mCurrentEmitters.erase( endIter, mCurrentEmitters.end() );
224 if ( !mCurrentEmitters.empty() )
227 RenderTask& renderTaskImpl( GetImplementation( mCurrentRenderTask ) );
228 currentGesturedActor->ScreenToLocal( renderTaskImpl, actorCoords.x, actorCoords.y, longPressEvent.point.x, longPressEvent.point.y );
230 EmitLongPressSignal( Dali::Actor( currentGesturedActor ), mCurrentEmitters, longPressEvent, actorCoords );
234 // Clear current emitters and emitted actor
235 mCurrentEmitters.clear();
241 case Gesture::Cancelled:
243 mCurrentEmitters.clear();
248 case Gesture::Continuing:
249 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Continuing\n" );
253 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Clear\n" );
258 void LongPressGestureProcessor::AddGestureDetector( LongPressGestureDetector* gestureDetector )
260 bool firstRegistration(mGestureDetectors.empty());
262 mGestureDetectors.push_back(gestureDetector);
264 if (firstRegistration)
266 mMinTouchesRequired = gestureDetector->GetMinimumTouchesRequired();
267 mMaxTouchesRequired = gestureDetector->GetMaximumTouchesRequired();
269 Integration::LongPressGestureRequest request;
270 request.minTouches = mMinTouchesRequired;
271 request.maxTouches = mMaxTouchesRequired;
272 mGestureManager.Register( request );
280 void LongPressGestureProcessor::RemoveGestureDetector( LongPressGestureDetector* gestureDetector )
283 LongPressGestureDetectorContainer::iterator endIter = std::remove( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector );
284 DALI_ASSERT_DEBUG( endIter != mGestureDetectors.end() );
287 mGestureDetectors.erase( endIter, mGestureDetectors.end() );
289 if ( mGestureDetectors.empty() )
291 Integration::GestureRequest request( Gesture::LongPress );
292 mGestureManager.Unregister(request);
300 void LongPressGestureProcessor::GestureDetectorUpdated( LongPressGestureDetector* gestureDetector )
302 DALI_ASSERT_DEBUG( find( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector ) != mGestureDetectors.end() );
307 void LongPressGestureProcessor::UpdateDetection()
309 DALI_ASSERT_DEBUG(!mGestureDetectors.empty());
311 unsigned int minimumRequired = UINT_MAX;
312 unsigned int maximumRequired = 0;
314 for ( LongPressGestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter != endIter; ++iter )
316 LongPressGestureDetector* current(*iter);
318 unsigned int minimum = current->GetMinimumTouchesRequired();
319 if (minimum < minimumRequired)
321 minimumRequired = minimum;
324 unsigned int maximum = current->GetMaximumTouchesRequired();
325 if ( maximum > maximumRequired )
327 maximumRequired = maximum;
331 if ( (minimumRequired != mMinTouchesRequired) || (maximumRequired != mMaxTouchesRequired) )
333 mMinTouchesRequired = minimumRequired;
334 mMaxTouchesRequired = maximumRequired;
336 Integration::LongPressGestureRequest request;
337 request.minTouches = mMinTouchesRequired;
338 request.maxTouches = mMaxTouchesRequired;
339 mGestureManager.Update(request);
343 void LongPressGestureProcessor::OnGesturedActorStageDisconnection()
345 mCurrentEmitters.clear();
348 } // namespace Internal