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 LongPressGestureDetectorContainer& 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 for ( LongPressGestureDetectorContainer::iterator iter = gestureDetectors.begin(), endIter = gestureDetectors.end(); iter != endIter; ++iter )
65 (*iter)->EmitLongPressGestureSignal(actor, longPress);
70 * Functor which checks whether the specified actor is attached to the gesture detector.
71 * It returns true if it is no longer attached. This can be used in remove_if functions.
73 struct IsNotAttachedFunctor
77 * @param[in] actor The actor to check whether it is attached.
79 IsNotAttachedFunctor( Actor* actor )
80 : actorToCheck( actor )
85 * Returns true if not attached, false if it is still attached.
86 * @param[in] detector The detector to check.
87 * @return true, if not attached, false otherwise.
89 bool operator()( const LongPressGestureDetector* detector ) const
91 return !detector->IsAttached( *actorToCheck );
94 Actor* actorToCheck; ///< The actor to check whether it is attached or not.
97 } // unnamed namespace
99 struct LongPressGestureProcessor::LongPressEventFunctor : public GestureProcessor::Functor
103 * @param[in] event The current gesture event.
104 * @param[in] processor Reference to the processor.
106 LongPressEventFunctor( const Integration::LongPressGestureEvent& event, LongPressGestureProcessor& processor )
107 : longPressEvent( event ),
108 processor( processor )
113 * Check if the detector meets the current gesture event parameters.
115 virtual bool operator() ( GestureDetector* detector, Actor* )
117 LongPressGestureDetector* longPressDetector ( static_cast< LongPressGestureDetector* >( detector ) );
119 return ( longPressDetector->GetMinimumTouchesRequired() <= longPressEvent.numberOfTouches ) &&
120 ( longPressDetector->GetMaximumTouchesRequired() >= longPressEvent.numberOfTouches );
124 * Gestured actor and gesture detectors that meet the gesture's parameters found, emit and save required information.
126 virtual void operator() ( Dali::Actor actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
128 LongPressGestureDetectorContainer derivedContainer;
129 DownCastContainer<LongPressGestureDetector>( gestureDetectors, derivedContainer );
132 processor.mCurrentEmitters.clear();
133 processor.ResetActor();
135 EmitLongPressSignal( actor, derivedContainer, longPressEvent, actorCoordinates );
137 if ( actor.OnStage() )
139 processor.mCurrentEmitters = derivedContainer;
140 processor.SetActor( actor );
144 const Integration::LongPressGestureEvent& longPressEvent;
145 LongPressGestureProcessor& processor;
148 LongPressGestureProcessor::LongPressGestureProcessor(
150 Integration::GestureManager& gestureManager)
152 mGestureManager( gestureManager ),
155 mCurrentRenderTask(),
156 mMinTouchesRequired( 1 ),
157 mMaxTouchesRequired( 1 )
161 LongPressGestureProcessor::~LongPressGestureProcessor()
165 void LongPressGestureProcessor::Process( const Integration::LongPressGestureEvent& longPressEvent )
167 switch ( longPressEvent.state )
169 case Gesture::Possible:
171 mCurrentEmitters.clear();
174 HitTestAlgorithm::Results hitTestResults;
175 if( HitTest( mStage, longPressEvent.point, hitTestResults ) )
177 SetActor( hitTestResults.actor );
182 case Gesture::Started:
184 Actor* currentGesturedActor = GetCurrentGesturedActor();
185 if ( currentGesturedActor )
187 HitTestAlgorithm::Results hitTestResults;
188 HitTestAlgorithm::HitTest( mStage, longPressEvent.point, hitTestResults );
190 if ( hitTestResults.actor && ( currentGesturedActor == &GetImplementation( hitTestResults.actor ) ) )
192 // Record the current render-task for Screen->Actor coordinate conversions
193 mCurrentRenderTask = hitTestResults.renderTask;
195 LongPressEventFunctor functor( longPressEvent, *this );
196 GestureDetectorContainer gestureDetectors;
197 UpCastContainer<LongPressGestureDetector>( mGestureDetectors, gestureDetectors );
198 ProcessAndEmit( hitTestResults, gestureDetectors, functor );
202 mCurrentEmitters.clear();
209 case Gesture::Finished:
211 // The gesture should only be sent to the gesture detector which first received it so that it
212 // can be told when the gesture ends as well.
214 // Only send subsequent long press gesture signals if we processed the gesture when it started.
215 // Check if actor is still touchable.
217 Actor* currentGesturedActor = GetCurrentGesturedActor();
218 if ( currentGesturedActor )
220 if ( currentGesturedActor->IsHittable() && !mCurrentEmitters.empty() && mCurrentRenderTask )
222 // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
223 LongPressGestureDetectorContainer::iterator endIter = std::remove_if( mCurrentEmitters.begin(), mCurrentEmitters.end(), IsNotAttachedFunctor(currentGesturedActor) );
224 mCurrentEmitters.erase( endIter, mCurrentEmitters.end() );
226 if ( !mCurrentEmitters.empty() )
229 RenderTask& renderTaskImpl( GetImplementation( mCurrentRenderTask ) );
230 currentGesturedActor->ScreenToLocal( renderTaskImpl, actorCoords.x, actorCoords.y, longPressEvent.point.x, longPressEvent.point.y );
232 EmitLongPressSignal( Dali::Actor( currentGesturedActor ), mCurrentEmitters, longPressEvent, actorCoords );
236 // Clear current emitters and emitted actor
237 mCurrentEmitters.clear();
243 case Gesture::Cancelled:
245 mCurrentEmitters.clear();
250 case Gesture::Continuing:
251 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Continuing\n" );
255 DALI_ASSERT_ALWAYS( false && "Incorrect state received from Integration layer: Clear\n" );
260 void LongPressGestureProcessor::AddGestureDetector( LongPressGestureDetector* gestureDetector )
262 bool firstRegistration(mGestureDetectors.empty());
264 mGestureDetectors.push_back(gestureDetector);
266 if (firstRegistration)
268 mMinTouchesRequired = gestureDetector->GetMinimumTouchesRequired();
269 mMaxTouchesRequired = gestureDetector->GetMaximumTouchesRequired();
271 Integration::LongPressGestureRequest request;
272 request.minTouches = mMinTouchesRequired;
273 request.maxTouches = mMaxTouchesRequired;
274 mGestureManager.Register( request );
282 void LongPressGestureProcessor::RemoveGestureDetector( LongPressGestureDetector* gestureDetector )
285 LongPressGestureDetectorContainer::iterator endIter = std::remove( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector );
286 DALI_ASSERT_DEBUG( endIter != mGestureDetectors.end() );
289 mGestureDetectors.erase( endIter, mGestureDetectors.end() );
291 if ( mGestureDetectors.empty() )
293 Integration::GestureRequest request( Gesture::LongPress );
294 mGestureManager.Unregister(request);
302 void LongPressGestureProcessor::GestureDetectorUpdated( LongPressGestureDetector* gestureDetector )
304 DALI_ASSERT_DEBUG( find( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector ) != mGestureDetectors.end() );
309 void LongPressGestureProcessor::UpdateDetection()
311 DALI_ASSERT_DEBUG(!mGestureDetectors.empty());
313 unsigned int minimumRequired = UINT_MAX;
314 unsigned int maximumRequired = 0;
316 for ( LongPressGestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter != endIter; ++iter )
318 LongPressGestureDetector* current(*iter);
320 unsigned int minimum = current->GetMinimumTouchesRequired();
321 if (minimum < minimumRequired)
323 minimumRequired = minimum;
326 unsigned int maximum = current->GetMaximumTouchesRequired();
327 if ( maximum > maximumRequired )
329 maximumRequired = maximum;
333 if ( (minimumRequired != mMinTouchesRequired) || (maximumRequired != mMaxTouchesRequired) )
335 mMinTouchesRequired = minimumRequired;
336 mMaxTouchesRequired = maximumRequired;
338 Integration::LongPressGestureRequest request;
339 request.minTouches = mMinTouchesRequired;
340 request.maxTouches = mMaxTouchesRequired;
341 mGestureManager.Update(request);
345 void LongPressGestureProcessor::OnGesturedActorStageDisconnection()
347 mCurrentEmitters.clear();
350 } // namespace Internal