Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: mixd on February 14, 2013, 05:15:54 PM

Title: Draggable Panel "Drag Start Jump" Fix / Suggested Improvement
Post by: mixd on February 14, 2013, 05:15:54 PM
Currently while dragging a panel, the drag does not start until the drag threshold is reached. This is fine, however results in a "jump" when the drag starts.

If you observe standard iOS behavior, there is no jump. Anyway here is the code to fix it so dragging panels have no jumpiness. Note that the drag threshold is still preserved as it should be.

In UIDraggablePanel.cs add the following lines:
  1. Vector2 _startOffset;
  2. bool _startOffsetSet = false;

Then in the Drag function, after UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta; Add the following:
  1. if(!_startOffsetSet)
  2. {
  3.         _startOffset = UICamera.currentTouch.totalDelta;
  4.         _startOffsetSet = true;
  5. }

Then also in the Drag function, update this line and move it below the code you just added (just added - _startOffset to it)
  1. Ray ray = UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos - _startOffset);

Then in the OnPress function in the "else" block, add the following:
  1. _startOffsetSet = false;


Overall looks like this:
  1. public void Press (bool pressed)
  2.         {
  3.                 if (enabled && NGUITools.GetActive(gameObject))
  4.                 {
  5.                         if (!pressed && mDragID == UICamera.currentTouchID) mDragID = -10;
  6.  
  7.                         mCalculatedBounds = false;
  8.                         mShouldMove = shouldMove;
  9.                         if (!mShouldMove) return;
  10.                         mPressed = pressed;
  11.  
  12.                         if (pressed)
  13.                         {
  14.                                 // Remove all momentum on press
  15.                                 mMomentum = Vector3.zero;
  16.                                 mScroll = 0f;
  17.  
  18.                                 // Disable the spring movement
  19.                                 DisableSpring();
  20.  
  21.                                 // Remember the hit position
  22.                                 mLastPos = UICamera.lastHit.point;
  23.  
  24.                                 // Create the plane to drag along
  25.                                 mPlane = new Plane(mTrans.rotation * Vector3.back, mLastPos);
  26.                         }
  27.                         else
  28.                         {
  29.                                 _startOffsetSet = false;
  30.                                 if (restrictWithinPanel && mPanel.clipping != UIDrawCall.Clipping.None && dragEffect == DragEffect.MomentumAndSpring)
  31.                                 {
  32.                                         RestrictWithinBounds(false);
  33.                                 }
  34.                                 if (onDragFinished != null) onDragFinished();
  35.                         }
  36.                 }
  37.         }
  38.  
  39.         /// <summary>
  40.         /// Drag the object along the plane.
  41.         /// </summary>
  42.  
  43.        
  44.         Vector2 _startOffset;
  45.         bool _startOffsetSet = false;
  46.        
  47.         public void Drag (Vector2 delta)
  48.         {
  49.                 if (enabled && NGUITools.GetActive(gameObject) && mShouldMove)
  50.                 {
  51.                         if (mDragID == -10) mDragID = UICamera.currentTouchID;
  52.                         UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;
  53.                        
  54.                         if(!_startOffsetSet)
  55.                         {
  56.                                 _startOffset = UICamera.currentTouch.totalDelta;
  57.                                 _startOffsetSet = true;
  58.                         }
  59.  
  60.                         Ray ray = UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos - _startOffset);
  61.                         float dist = 0f;
  62.                        
Title: Re: Draggable Panel "Smooth Drag" Improvement Suggestion
Post by: andrew on February 14, 2013, 11:44:43 PM
This looks/feels great on the iPad.  The jumpiness of the draggable panel was bothersome and noticeable before.

However, clicking and dragging on the last item in my horizontal scrolling panel, all of the items disappear.  Not sure if this is some kind of depth issue with my draggable panel or what.  It wasn't happening before this change, and I changed nothing else.  The good news is it's repeatable so I'll debug it and post back my findings.

Thanks!
Title: Re: Draggable Panel "Smooth Drag" Improvement Suggestion
Post by: mixd on February 15, 2013, 01:39:33 AM
Great thanks for looking into the bug, let me know if it is a problem with the code I posted.  So far I have not run into any issues on my app.

Also, here is more code (I realize the old UIDraggableCamera script needed an update as well)

UIDraggableCamera.cs, in Press function, add anywhere:
  1. if(!isPressed)
  2.         _dragStarted = false;

and at the start of the Drag function, add:
  1. if(!_dragStarted) {
  2.         _dragStarted = true;
  3.         return;
  4. }
Title: Re: Draggable Panel "Drag Start Jump" Fix / Suggested Improvement
Post by: badawe on February 15, 2013, 08:17:23 AM
Amazing work!

Hope we'll see this in next release :)
Title: Re: Draggable Panel "Smooth Drag" Improvement Suggestion
Post by: andrew on February 15, 2013, 01:12:42 PM
Not sure if this is some kind of depth issue with my draggable panel or what.

It was a depth issue on my end  :D  Looks and works great.  Thanks!
Title: Re: Draggable Panel "Drag Start Jump" Fix / Suggested Improvement
Post by: ArenMook on February 17, 2013, 11:32:51 PM
I snuck this into 2.3.3f
Title: Re: Draggable Panel "Drag Start Jump" Fix / Suggested Improvement
Post by: mixd on February 18, 2013, 01:38:42 AM
Nice! Thanks Aren -- always happy to contribute