Author Topic: Swapping elements between UIGrids  (Read 6469 times)

poolts

  • Newbie
  • *
  • Thank You
  • -Given: 6
  • -Receive: 0
  • Posts: 33
    • View Profile
Swapping elements between UIGrids
« on: June 02, 2014, 08:39:28 AM »
I have 4 grids with 4 elements inside each.

I want to be able to drag an element from one grid and swap it with an element (the element the dragged element is dropped on) in another grid.

I've created a script that extends UIDragDropItem called DragDropView and I'm basically stating that once the element is dragged and dropped on another element of the same tag, swap them by adding and removing them from their respective lists and insert them at the same index in the grid. I don't want the grid to reposition the elements, I want them to swap positions 1 for 1.

Here's the code I'm using:

  1.         protected override void OnDragDropRelease (GameObject surface)
  2.         {
  3.                 if(surface.CompareTag("dragDropView"))
  4.         {
  5.                         base.OnDragDropRelease (surface);      
  6.        
  7.              // Get the grid of the item dropped on
  8.                         UIGrid surfaceGrid = surface.GetComponent<DragDropView>().Grid;
  9.  
  10.                         int draggedItemIndex = m_grid.GetIndex(transform);
  11.                         int surfaceItemIndex = surfaceGrid.GetIndex(surface.transform);
  12.  
  13.                         m_grid.AddChild(surface.transform, draggedItemIndex);
  14.                         surfaceGrid.AddChild(transform, surfaceItemIndex);
  15.  
  16.                         m_grid.RemoveChild(transform);
  17.                         surfaceGrid.RemoveChild(surface.transform);
  18.                 }
  19.         }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Swapping elements between UIGrids
« Reply #1 on: June 02, 2014, 11:22:59 PM »
If you're just straight-up swapping elements, then just reparent them. Don't even touch the grid.

P.S. NGUITools.MarkParentAsChanged should still be called however.

poolts

  • Newbie
  • *
  • Thank You
  • -Given: 6
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: Swapping elements between UIGrids
« Reply #2 on: June 04, 2014, 09:53:06 AM »
I've put together this piece of code, but can't get the elements to switch. I want them to swap grids and retain each others places.

In the code I'm trying to:

1) Get the surface's parent (the grid I want to swap to),
2) Swap the surface's parent (the object I've dropped on) to the transform's parent (the object I'm dragging)
3) Marking the parent's as changed (following Aren's advice)

  1.         protected override void OnDragDropRelease (GameObject surface)
  2.         {
  3.                 if(surface != null && surface.CompareTag("dragDropView"))
  4.                 {
  5.                         Transform surfaceOriginalTrans = surface.transform.parent;
  6.  
  7.             surface.transform.parent = transform.parent;
  8.  
  9.                         transform.parent = surfaceOriginalTrans;
  10.  
  11.                         NGUITools.MarkParentAsChanged(surface.transform.parent.gameObject);
  12.  
  13.                         NGUITools.MarkParentAsChanged(transform.parent.gameObject);
  14.                 }
  15.  
  16.                 base.OnDragDropRelease (surface);      
  17.         }

I've added an awfully drawn diagram, just to illustrate what I'm trying to do. Thanks :)
« Last Edit: June 04, 2014, 10:10:20 AM by poolts »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Swapping elements between UIGrids
« Reply #3 on: June 05, 2014, 12:20:55 AM »
You are changing the parents and informing the widgets, but you never swap their position -- so they will never actually move. In the same place you're saving the transform parent, swap the positions.

poolts

  • Newbie
  • *
  • Thank You
  • -Given: 6
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: Swapping elements between UIGrids
« Reply #4 on: June 16, 2014, 08:23:31 AM »
Hi Aren & all,

Having trouble with this purely because it's a 3D UI. Does the drag and drop work with perspective? I've got my settings set to 3D world (not sure whether it should be 3D UI or world).

I've attached an image of how my objects are laid out in 3D space. I'm trying to swap their position once one of them is dragged on top of the other.

The the other way I thought of doing it is to set up an invisible 2D UI, which consists of buttons, then having the object in 3D space follow the cursor, and once the 2D invisible element is placed upon another 2D element, swapping the 3D objects position in world space.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Swapping elements between UIGrids
« Reply #5 on: June 16, 2014, 09:01:14 AM »
Without seeing your code I can't suggest much. Drag & drop is done via events which work across colliders, and whether they are in 2D or 3D doesn't matter. Swapping objects is as trivial as swapping their positions, as I pointed out. You will also likely need to swap the rotation and scale, however.

poolts

  • Newbie
  • *
  • Thank You
  • -Given: 6
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: Swapping elements between UIGrids
« Reply #6 on: June 16, 2014, 09:48:42 AM »
  1. public class PlayerDropDrag : UIDragDropItem {
  2.  
  3.         [SerializeField]
  4.         Collider m_collider;
  5.  
  6.         Vector3 m_originalPos;
  7.  
  8.         protected override void OnDragDropStart ()
  9.         {
  10.                 base.OnDragDropStart ();
  11.  
  12.                 m_collider.enabled = false;
  13.  
  14.                 m_originalPos = transform.position;
  15.         }
  16.  
  17.         protected override void OnDragDropRelease (GameObject surface)
  18.         {
  19.                 base.OnDragDropRelease (surface);      
  20.  
  21.                 if(surface != null && surface.CompareTag("ObjectView"))
  22.                 {
  23.                         transform.position = surface.transform.position;
  24.  
  25.                         surface.transform.position = m_originalPos;
  26.  
  27.                         m_collider.enabled = true;
  28.                 }
  29.         }
  30. }

I'm also there now, I can get the objects to swap positions, however when I go to click on one of the objects that has been swapped it quickly reverts back to it's previous position. I've included the code.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Swapping elements between UIGrids
« Reply #7 on: June 17, 2014, 10:37:41 AM »
Why do you call base.OnDragDropRelease if you're doing your custom logic? There is quite a bit of code in UIDragDropItem.OnDragDropRelease that re-parents the item and changes its position. This is not what you want.