Author Topic: Custom Drag Drop Item Behaviour  (Read 5376 times)

Genhain

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 38
    • View Profile
Custom Drag Drop Item Behaviour
« on: July 10, 2014, 05:29:17 AM »
I have extended a few of the Interaction classes but one of them is perplexing me quite a bit. The interaction is...click and hold on the object, it scales up and move to the root panel i.e is on top of everything else and then on drag it scales down again and you can drop it in containers...

My issue currently is it only works if i don't make it move to the UIRoot panel, but then the view it is currently in there can be undesired overlapping and such. If i try to bring it to the front when they click and hold it then starts acting a bit odd. one of the first issues i encountered is that if i click and bring it to the front and let go it gets stuck in the UIRoot as to successfully bring it to the front i have to disable the collider...I essentially copied the base.UIDragDropItem's OnDragDropStart() for this...but it does not work unless i comment out this line mTouchID = UICamera.currentTouchID; now it seems to work, until i do the click and release without dragging then attempting to select it again causes it to shoot to the bottom right and any attempt to drag it makes it jitter but return back to that point until i let go.

any suggestions please?

Here is the Code that starts the zooming behaviour
  1. IEnumerator isHolding( bool isPressed )
  2. {
  3.         float waitDuration = 0.2f;
  4.  
  5.         while (waitDuration > 0)
  6.         {
  7.                 waitDuration -= Time.deltaTime;
  8.                 tweenTarget.gameObject.transform.localScale = new Vector3(1,1,1);
  9.  
  10.                 yield return null;
  11.         }
  12.  
  13.         WCScrollView scrollView = (WCScrollView)_wcDDISelection.scrollView;
  14.  
  15.         if( scrollView.isDragging )
  16.         {
  17.                 StopCoroutine("isHolding");
  18.                 yield break;
  19.         }
  20.  
  21.         TweenScale.Begin( tweenTarget.gameObject, duration, thingy(isPressed) ).method = UITweener.Method.EaseInOut;
  22.         _wcDDISelection.restriction = UIDragDropItem.Restriction.None;
  23.         _wcDDISelection.ManuallyStartDrag(isPressed);
  24. }
  25.  

and here is the code that extends the drag drop class

  1. public class WCDragDropItemSelection : UIDragDropItem
  2. {
  3.         int count;
  4.         bool hasShrunkFlag;
  5.         private Vector3 _significantMovementDelta;
  6.         public UIScrollView scrollView;
  7.         UIScrollView mScroll;
  8.         bool mAutoFind = false;
  9.  
  10.         private bool _isScrollViewScrolling;
  11.         public bool isScrollViewScrolling { get{return _isScrollViewScrolling;} }
  12.  
  13.         public Transform Parent { get {return mParent;} set {mParent = value;} }
  14.  
  15.         public UIPanel rootPanel;
  16.  
  17.         void Awake()
  18.         {
  19.                 _isScrollViewScrolling = true;
  20.                 _significantMovementDelta = new Vector3();
  21.                 count = 0;
  22.                 hasShrunkFlag = false;
  23.         }
  24.  
  25.         protected override void Start ()
  26.         {
  27.                 base.Start ();
  28.  
  29.                 _significantMovementDelta = mTrans.localPosition;
  30.         }
  31.  
  32.         void OnEnable()
  33.         {
  34.                 StartCoroutine(FindScrollView());
  35.  
  36.                 if(mScroll)
  37.                 {
  38.                         mScroll.onDragFinished += scrollingEnd;
  39.                 }
  40.         }
  41.  
  42.         IEnumerator FindScrollView ()
  43.         {
  44.                 yield return new WaitForSeconds(0.5f);
  45.  
  46.                 // If the scroll view is on a parent, don't try to remember it (as we want it to be dynamic in case of re-parenting)
  47.                 UIScrollView sv = NGUITools.FindInParents<UIScrollView>(transform);
  48.                
  49.                 if (scrollView == null)
  50.                 {
  51.                         scrollView = sv;
  52.                         mAutoFind = true;
  53.                 }
  54.                 else if (scrollView == sv)
  55.                 {
  56.                         mAutoFind = true;
  57.                 }
  58.                 mScroll = scrollView;
  59.  
  60.                 yield return null;
  61.         }
  62.                        
  63.         public void ManuallyStartDrag (bool isStarting)
  64.         {
  65.                 if(!isStarting)
  66.                 {
  67.                         return;
  68.                 }
  69.  
  70.                 // Automatically disable the scroll view
  71.                 if (mDragScrollView != null) mDragScrollView.enabled = false;
  72.  
  73. //              // Disable the collider so that it doesn't intercept events
  74.                 if (mCollider != null) mCollider.enabled = false;
  75.  
  76.                 mParent = mTrans.parent;
  77.                 mRoot = NGUITools.FindInParents<UIRoot>(mParent);
  78.                 mGrid = NGUITools.FindInParents<UIGrid>(mParent);
  79.                 mTable = NGUITools.FindInParents<UITable>(mParent);
  80.                
  81.                 // Re-parent the item
  82.                 if (UIDragDropRoot.root != null)
  83.                         mTrans.parent = UIDragDropRoot.root;
  84.                
  85.                 Vector3 pos = mTrans.localPosition;
  86.                 pos.z = 0f;
  87.                 mTrans.localPosition = pos;
  88.                
  89.                 // Notify the widgets that the parent has changed
  90.                 NGUITools.MarkParentAsChanged(gameObject);
  91.                
  92.                 if (mTable != null) mTable.repositionNow = true;
  93.                 if (mGrid != null) mGrid.repositionNow = true;
  94.         }
  95.  
  96.         public void TempSetGrid( UIGrid grid )
  97.         {
  98.                 mGrid = grid;
  99.         }
  100.  
  101.         protected override void OnDragDropStart ()
  102.         {
  103.                 base.OnDragDropStart ();
  104.  
  105.                 //here
  106.                 if(mGrid)
  107.                 {
  108.                         WCGrid grid = (WCGrid)mGrid;
  109.                         WCCard card = gameObject.GetComponent<WCCard>();
  110.                         grid.CardPulledFromGrid(card);
  111.                 }
  112.         }
  113.  
  114.         protected override void OnDragDropMove (Vector3 delta)
  115.         {
  116.                 count++;
  117.                 if(count > 1)
  118.                 {
  119.                         mTrans.localPosition += delta;
  120.  
  121.                         Vector3 vec = _significantMovementDelta - mTrans.localPosition;
  122.  
  123.                         if(vec.magnitude > 10 && !hasShrunkFlag)
  124.                         {
  125.                                 hasShrunkFlag = true;
  126.                                 _significantMovementDelta = mTrans.localPosition;
  127.                                 WCSelectionScale selectionScale = gameObject.GetComponent<WCSelectionScale>();
  128.                                 selectionScale.scale(false);
  129.                         }
  130.                 }
  131.         }
  132.        
  133.         protected override void OnDragDropRelease (GameObject surface)
  134.         {
  135.                 base.OnDragDropRelease (surface);
  136.  
  137.                 count = 0;
  138.                 hasShrunkFlag = false;
  139.  
  140.                 UIDragDropContainer container = UICamera.hoveredObject ? NGUITools.FindInParents<UIDragDropContainer>(surface) : null;
  141.                
  142.                 if (container != null)
  143.                 {
  144.                         WCGrid grid = NGUITools.FindInParents<WCGrid>(transform);
  145.  
  146.                         if(mGrid)
  147.                         {
  148.                                 WCCard card = gameObject.GetComponent<WCCard>();
  149.  
  150.                                 grid.CardDroppedIntoGrid(card);
  151.                         }
  152.                 }
  153.         }
  154.  
  155.         void scrollingEnd()
  156.         {
  157.                 _isScrollViewScrolling = false;
  158.         }
  159.  
  160.         public void setRoot(UIRoot root)
  161.         {
  162.                 this.mRoot = root;
  163.         }
  164. }
  165.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom Drag Drop Item Behaviour
« Reply #1 on: July 10, 2014, 09:06:38 PM »
Drag & drop scripts were written to already be reparented -- the object is reparented to UIDragDropRoot. You shouldn't reparent to the UI root because that has a panel on it that's going to be behind every other panel you add on top of it. The idea is to have UIDragDropRoot on a separate panel -- a panel that's on top of everything else. That's how whatever you're dragging ends up on top as well.

Genhain

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 38
    • View Profile
Re: Custom Drag Drop Item Behaviour
« Reply #2 on: July 10, 2014, 10:20:17 PM »
Yes and it works quite well, but only when you start dragging, i am trying to cause the same calls that get called when you start dragging a bit earlier...i.e when you click and hold for a few seconds, it should pop to a panel higher in the hierarchy and reparent again if dragged or let go...this is where it gets a bit tricky, i have not tried your suggestion of having a different panel other than UIRoot to have drag drop root but i don't think that will solve the issue.

EDIT:
Just to hopefully clarify what i want, when i press and hold on a draggable item, i then scale it up and then i wish to pop it a panel higher up the hierarchy so its on top of the grid it was last on, which in essence is what UIDragDropItem.OnDragDropStart() does, and then if i drag it or release it, it scales down and reparents to the container i pulled it from. From writing this out it seems i will not be able to rely on UIDragDropItem to reparent in the case of just holding and releasing without any dragging...will try and update any insight is appreciated
« Last Edit: July 10, 2014, 11:35:06 PM by Genhain »

Genhain

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 38
    • View Profile
Re: Custom Drag Drop Item Behaviour
« Reply #3 on: July 11, 2014, 02:00:35 AM »
Okay I have gotten to the point where it if i try reparenting it to a panel above it before the UIDragDrop script it causes things to go a bit weird. Right now if i click and release with or without dragging it gets reparented to the container fine...but then a split second later it gets parented to the ui panel above it, and it looks like it's being ordered like in a grid as well as every consecutive card does this and orders them like the grid i dropped them in. The main cause for this behaviour seems to be if i do this...

  1. // Disable the collider so that it doesn't intercept events
  2. collider.enabled = false;
  3.  
  4. // Re-parent the item
  5. if (UIDragDropRoot.root != null)
  6.         transform.parent = UIDragDropRoot.root;
  7.  
  8. Vector3 pos = transform.localPosition;
  9. pos.z = 0f;
  10. transform.localPosition = pos;
  11.  
  12. // Notify the widgets that the parent has changed
  13. NGUITools.MarkParentAsChanged(gameObject);
  14.  

before UIDragDropItem does...

I dont want to hack at NGUI too much to get this working so the only option i can see that i am sure would work, is to duplicate the item i am selecting  and bringing that to the front and scaling it without a collider and it will shrink and disappear when the user starts dragging.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom Drag Drop Item Behaviour
« Reply #4 on: July 11, 2014, 08:03:16 PM »
UIDragDropItem script is meant to be derived from. Override the OnDragDropStart function and change its behaviour to what you need it to be -- including what it gets reparented to, if at all. You shouldn't have reparenting logic be in two places.

Genhain

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 38
    • View Profile
Re: Custom Drag Drop Item Behaviour
« Reply #5 on: July 13, 2014, 01:30:14 AM »
Which i have done but the point i need to stress is that i need it to call OnDragDropStart not when they start dragging, but when they click and hold, more specifically I need it to pop it to a panel above the container it was in previously which OnDragDropStart does intrinsically as well as some other stuff and then it would proceed as normal once they actually begin dragging. I did get it working with the exception that if i click and hold and let go without dragging it would reparent wrong.

I could not figure out how to get it to work without going all over the shop so i went with creating a copy for display purposes on click and hold, and when they actually drag it gets deleted and you drag the actual item in question around and it seems to do the trick.

arun02139

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Custom Drag Drop Item Behaviour
« Reply #6 on: August 25, 2014, 04:50:34 AM »
Dear Aren,

Thanks for your vigilant support in these forums!

I've used a custom UIDragDropItem to move little widgets between NGUI scroll views, but now I'm trying to drag a widget from a scroll view and onto a 3D object (drawn by a different camera than NGUI) and my 'surface' is always null..

Is there an intended/clean way to accomplish this? 

Thanks :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom Drag Drop Item Behaviour
« Reply #7 on: August 26, 2014, 02:04:37 AM »
Did you forget to attach UICamera to your main camera? Check the Drag & drop example, it does exactly this.