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:
205 DALI_ABORT( "Incorrect state received from Integration layer: Continuing\n" );
210 DALI_ABORT( "Incorrect state received from Integration layer: Clear\n" );
216 void LongPressGestureProcessor::AddGestureDetector( LongPressGestureDetector* gestureDetector )
218 bool firstRegistration(mGestureDetectors.empty());
220 mGestureDetectors.push_back(gestureDetector);
222 if (firstRegistration)
224 mMinTouchesRequired = gestureDetector->GetMinimumTouchesRequired();
225 mMaxTouchesRequired = gestureDetector->GetMaximumTouchesRequired();
227 Integration::LongPressGestureRequest request;
228 request.minTouches = mMinTouchesRequired;
229 request.maxTouches = mMaxTouchesRequired;
230 mGestureManager.Register( request );
238 void LongPressGestureProcessor::RemoveGestureDetector( LongPressGestureDetector* gestureDetector )
241 LongPressGestureDetectorContainer::iterator endIter = std::remove( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector );
242 DALI_ASSERT_DEBUG( endIter != mGestureDetectors.end() );
245 mGestureDetectors.erase( endIter, mGestureDetectors.end() );
247 if ( mGestureDetectors.empty() )
249 Integration::GestureRequest request( Gesture::LongPress );
250 mGestureManager.Unregister(request);
258 void LongPressGestureProcessor::GestureDetectorUpdated( LongPressGestureDetector* gestureDetector )
260 DALI_ASSERT_DEBUG( find( mGestureDetectors.begin(), mGestureDetectors.end(), gestureDetector ) != mGestureDetectors.end() );
265 void LongPressGestureProcessor::UpdateDetection()
267 DALI_ASSERT_DEBUG(!mGestureDetectors.empty());
269 unsigned int minimumRequired = UINT_MAX;
270 unsigned int maximumRequired = 0;
272 for ( LongPressGestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter != endIter; ++iter )
274 LongPressGestureDetector* current(*iter);
278 unsigned int minimum = current->GetMinimumTouchesRequired();
279 if (minimum < minimumRequired)
281 minimumRequired = minimum;
284 unsigned int maximum = current->GetMaximumTouchesRequired();
285 if ( maximum > maximumRequired )
287 maximumRequired = maximum;
292 if ( (minimumRequired != mMinTouchesRequired) || (maximumRequired != mMaxTouchesRequired) )
294 mMinTouchesRequired = minimumRequired;
295 mMaxTouchesRequired = maximumRequired;
297 Integration::LongPressGestureRequest request;
298 request.minTouches = mMinTouchesRequired;
299 request.maxTouches = mMaxTouchesRequired;
300 mGestureManager.Update(request);
304 void LongPressGestureProcessor::OnGesturedActorStageDisconnection()
306 mCurrentEmitters.clear();
309 bool LongPressGestureProcessor::CheckGestureDetector( GestureDetector* detector, Actor* actor )
311 DALI_ASSERT_DEBUG( mCurrentLongPressEvent );
313 LongPressGestureDetector* longPressDetector ( static_cast< LongPressGestureDetector* >( detector ) );
315 return ( longPressDetector->GetMinimumTouchesRequired() <= mCurrentLongPressEvent->numberOfTouches ) &&
316 ( longPressDetector->GetMaximumTouchesRequired() >= mCurrentLongPressEvent->numberOfTouches );
319 void LongPressGestureProcessor::EmitGestureSignal( Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates )
321 DALI_ASSERT_DEBUG( mCurrentLongPressEvent );
323 mCurrentEmitters.clear();
326 EmitLongPressSignal( actor, gestureDetectors, *mCurrentLongPressEvent, actorCoordinates );
328 if ( actor->OnStage() )
330 mCurrentEmitters = gestureDetectors;
335 } // namespace Internal