Commit 6e6f5ae6c07f04ebed2fd77ffc7926302c5486f7

Authored by Adeel Kazmi
1 parent 4e0bfdbd

(Gestures) Only attach pan after long-press

Change-Id: I18fc6fbea5258039ae49095576cb2f2059110e97
examples/gestures/gesture-example.cpp
... ... @@ -166,9 +166,8 @@ private:
166 166 mLongPressDetector.Attach( touchControl );
167 167 mLongPressDetector.DetectedSignal().Connect( this, &GestureExample::OnLongPress );
168 168  
169   - // Create a pan gesture detector, attach the actor & connect
  169 + // Create a pan gesture detector & connect, don't attach the actor as we'll attach it when we detect a long-press
170 170 mPanDetector = PanGestureDetector::New();
171   - mPanDetector.Attach( touchControl );
172 171 mPanDetector.DetectedSignal().Connect( this, &GestureExample::OnPan );
173 172  
174 173 // Create a tap gesture detector, attach the actor & connect
... ... @@ -267,36 +266,19 @@ private:
267 266 */
268 267 void OnLongPress( Actor actor, const LongPressGesture& longPress )
269 268 {
270   - switch( longPress.state )
  269 + if( longPress.state == Gesture::Started )
271 270 {
272   - case Gesture::Started:
273   - {
274   - // When we first receive a long press, change state to pan mode.
  271 + // When we first receive a long press, attach the actor to the pan detector.
  272 + mPanDetector.Attach( actor );
275 273  
276   - // Do a small animation to indicate to the user that we are in pan mode.
277   - Animation anim = Animation::New( PAN_MODE_CHANGE_ANIMATION_DURATION );
278   - anim.AnimateTo( Property( actor, Actor::Property::SCALE ), actor.GetCurrentScale() * PAN_MODE_START_ANIMATION_SCALE, AlphaFunction::BOUNCE );
279   - anim.Play();
280   - mPanMode = true;
  274 + // Do a small animation to indicate to the user that we are in pan mode.
  275 + Animation anim = Animation::New( PAN_MODE_CHANGE_ANIMATION_DURATION );
  276 + anim.AnimateTo( Property( actor, Actor::Property::SCALE ), actor.GetCurrentScale() * PAN_MODE_START_ANIMATION_SCALE, AlphaFunction::BOUNCE );
  277 + anim.Play();
281 278  
282   - // Start the shake animation so the user knows when they are in pan mode.
283   - mShakeAnimation.SetLooping( true );
284   - mShakeAnimation.Play();
285   - break;
286   - }
287   -
288   - case Gesture::Finished:
289   - case Gesture::Cancelled:
290   - {
291   - // We get this state when all touches are released after a long press. We end pan mode...
292   - mPanMode = false;
293   - break;
294   - }
295   -
296   - default:
297   - {
298   - break;
299   - }
  279 + // Start the shake animation so the user knows when they are in pan mode.
  280 + mShakeAnimation.SetLooping( true );
  281 + mShakeAnimation.Play();
300 282 }
301 283 }
302 284  
... ... @@ -308,56 +290,55 @@ private:
308 290 */
309 291 void OnPan( Actor actor, const PanGesture& pan )
310 292 {
311   - if( mPanMode || mPanStarted )
312   - {
313   - // When we are in Pan mode, just move the actor by the displacement.
  293 + // Just move the actor by the displacement.
314 294  
315   - // As the displacement is in local actor coords, we will have to multiply the displacement by the
316   - // actor's scale so that it moves the correct amount in the parent's coordinate system.
317   - Vector3 scaledDisplacement( pan.displacement );
318   - scaledDisplacement *= actor.GetCurrentScale();
  295 + // As the displacement is in local actor coords, we will have to multiply the displacement by the
  296 + // actor's scale so that it moves the correct amount in the parent's coordinate system.
  297 + Vector3 scaledDisplacement( pan.displacement );
  298 + scaledDisplacement *= actor.GetCurrentScale();
319 299  
320   - Vector3 currentPosition;
321   - actor.GetProperty( Actor::Property::POSITION ).Get( currentPosition );
  300 + Vector3 currentPosition;
  301 + actor.GetProperty( Actor::Property::POSITION ).Get( currentPosition );
322 302  
323   - Vector3 newPosition = currentPosition + scaledDisplacement;
324   - actor.SetPosition( newPosition );
  303 + Vector3 newPosition = currentPosition + scaledDisplacement;
  304 + actor.SetPosition( newPosition );
325 305  
326   - switch( pan.state )
  306 + switch( pan.state )
  307 + {
  308 + case Gesture::Started:
327 309 {
328   - case Gesture::Started:
329   - {
330   - mPanStarted = true;
331   - break;
332   - }
333   -
334   - case Gesture::Finished:
335   - case Gesture::Cancelled:
336   - {
337   - // If we cancel or finish the pan, do an animation to indicate this and stop the shake animation.
  310 + mPanStarted = true;
  311 + break;
  312 + }
338 313  
339   - Animation anim = Animation::New( PAN_MODE_CHANGE_ANIMATION_DURATION );
340   - anim.AnimateTo( Property( actor, Actor::Property::COLOR ), Vector4::ONE );
341   - anim.AnimateTo( Property( actor, Actor::Property::SCALE ), actor.GetCurrentScale() * PAN_MODE_END_ANIMATION_SCALE, AlphaFunction::BOUNCE );
342   -
343   - // Move actor back to center if we're out of bounds
344   - Vector2 halfStageSize = Stage::GetCurrent().GetSize() * 0.5f;
345   - if( ( std::abs( newPosition.x ) > halfStageSize.width ) ||
346   - ( std::abs( newPosition.y ) > halfStageSize.height ) )
347   - {
348   - anim.AnimateTo( Property( actor, Actor::Property::POSITION ), Vector3::ZERO, AlphaFunction::EASE_IN );
349   - }
350   - anim.Play();
  314 + case Gesture::Finished:
  315 + case Gesture::Cancelled:
  316 + {
  317 + // If we cancel or finish the pan, do an animation to indicate this and stop the shake animation.
351 318  
352   - mShakeAnimation.SetLooping( false );
353   - mPanStarted = false;
354   - break;
355   - }
  319 + Animation anim = Animation::New( PAN_MODE_CHANGE_ANIMATION_DURATION );
  320 + anim.AnimateTo( Property( actor, Actor::Property::COLOR ), Vector4::ONE );
  321 + anim.AnimateTo( Property( actor, Actor::Property::SCALE ), actor.GetCurrentScale() * PAN_MODE_END_ANIMATION_SCALE, AlphaFunction::BOUNCE );
356 322  
357   - default:
  323 + // Move actor back to center if we're out of bounds
  324 + Vector2 halfStageSize = Stage::GetCurrent().GetSize() * 0.5f;
  325 + if( ( std::abs( newPosition.x ) > halfStageSize.width ) ||
  326 + ( std::abs( newPosition.y ) > halfStageSize.height ) )
358 327 {
359   - break;
  328 + anim.AnimateTo( Property( actor, Actor::Property::POSITION ), Vector3::ZERO, AlphaFunction::EASE_IN );
360 329 }
  330 + anim.Play();
  331 +
  332 + // Set end of pan configuration and disconnect the actor from the pan detector
  333 + mShakeAnimation.SetLooping( false );
  334 + mPanStarted = false;
  335 + mPanDetector.Detach( actor );
  336 + break;
  337 + }
  338 +
  339 + default:
  340 + {
  341 + break;
361 342 }
362 343 }
363 344 }
... ... @@ -493,7 +474,6 @@ private:
493 474 Vector3 mStartingScale; ///< Set to the scale of the control when pinch starts.
494 475 Quaternion mStartingOrientation; ///< Set to the orientation of the control when the rotation starts.
495 476 Animation mShakeAnimation; ///< "Shake" animation to show when we are in panning mode.
496   - bool mPanMode = false; ///< Set to true when we have long-pressed to put us into panning mode.
497 477 bool mPanStarted = false; ///< Set to true to state that panning has started.
498 478 };
499 479  
... ...