Author Topic: Example 11, Drag&Drop in same scrollview.  (Read 1737 times)

wallabie

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 200
    • View Profile
Example 11, Drag&Drop in same scrollview.
« on: March 03, 2014, 08:54:26 AM »
Great example.

I would like to be able to reorder the items in the "Same" scrollview using drag and drop.  Can this be done?  If yes, how to achieve this effect.

Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Example 11, Drag&Drop in same scrollview.
« Reply #1 on: March 03, 2014, 09:50:26 PM »
You would need to figure out where in the list you dropped the item, then sort things based on Y position instead of using the grid. Thinking about it... I could just add it as an option to the grid. One moment...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Example 11, Drag&Drop in same scrollview.
« Reply #2 on: March 03, 2014, 09:57:35 PM »
Replace the UIGrid script with this one, and change the Grid settings on the drag & drop's scroll views to use "Vertical" sorting.
  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7. using System.Collections.Generic;
  8.  
  9. /// <summary>
  10. /// All children added to the game object with this script will be repositioned to be on a grid of specified dimensions.
  11. /// If you want the cells to automatically set their scale based on the dimensions of their content, take a look at UITable.
  12. /// </summary>
  13.  
  14. [AddComponentMenu("NGUI/Interaction/Grid")]
  15. public class UIGrid : UIWidgetContainer
  16. {
  17.         public delegate void OnReposition ();
  18.  
  19.         public enum Arrangement
  20.         {
  21.                 Horizontal,
  22.                 Vertical,
  23.         }
  24.  
  25.         public enum Sorting
  26.         {
  27.                 None,
  28.                 Alphabetic,
  29.                 Horizontal,
  30.                 Vertical,
  31.                 Custom,
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Type of arrangement -- vertical or horizontal.
  36.         /// </summary>
  37.  
  38.         public Arrangement arrangement = Arrangement.Horizontal;
  39.  
  40.         /// <summary>
  41.         /// How to sort the grid's elements.
  42.         /// </summary>
  43.  
  44.         public Sorting sorting = Sorting.None;
  45.  
  46.         /// <summary>
  47.         /// Maximum children per line.
  48.         /// If the arrangement is horizontal, this denotes the number of columns.
  49.         /// If the arrangement is vertical, this stands for the number of rows.
  50.         /// </summary>
  51.  
  52.         public int maxPerLine = 0;
  53.  
  54.         /// <summary>
  55.         /// The width of each of the cells.
  56.         /// </summary>
  57.  
  58.         public float cellWidth = 200f;
  59.  
  60.         /// <summary>
  61.         /// The height of each of the cells.
  62.         /// </summary>
  63.  
  64.         public float cellHeight = 200f;
  65.  
  66.         /// <summary>
  67.         /// Whether the grid will smoothly animate its children into the correct place.
  68.         /// </summary>
  69.  
  70.         public bool animateSmoothly = false;
  71.  
  72.         /// <summary>
  73.         /// Whether the children will be sorted alphabetically prior to repositioning.
  74.         /// </summary>
  75.  
  76.         [HideInInspector]
  77.         [System.Obsolete("Use the 'sorting' property instead")]
  78.         public bool sorted = false;
  79.  
  80.         /// <summary>
  81.         /// Whether to ignore the disabled children or to treat them as being present.
  82.         /// </summary>
  83.  
  84.         public bool hideInactive = true;
  85.  
  86.         /// <summary>
  87.         /// Whether the parent container will be notified of the grid's changes.
  88.         /// </summary>
  89.  
  90.         public bool keepWithinPanel = false;
  91.  
  92.         /// <summary>
  93.         /// Callback triggered when the grid repositions its contents.
  94.         /// </summary>
  95.  
  96.         public OnReposition onReposition;
  97.  
  98.         /// <summary>
  99.         /// Reposition the children on the next Update().
  100.         /// </summary>
  101.  
  102.         public bool repositionNow { set { if (value) { mReposition = true; enabled = true; } } }
  103.  
  104.         protected bool mReposition = false;
  105.         protected UIPanel mPanel;
  106.         protected bool mInitDone = false;
  107.  
  108.         protected virtual void Init ()
  109.         {
  110.                 mInitDone = true;
  111.                 mPanel = NGUITools.FindInParents<UIPanel>(gameObject);
  112.         }
  113.  
  114.         protected virtual void Start ()
  115.         {
  116.                 if (!mInitDone) Init();
  117.                 bool smooth = animateSmoothly;
  118.                 animateSmoothly = false;
  119.                 Reposition();
  120.                 animateSmoothly = smooth;
  121.                 enabled = false;
  122.         }
  123.  
  124.         protected virtual void Update ()
  125.         {
  126.                 if (mReposition) Reposition();
  127.                 enabled = false;
  128.         }
  129.  
  130.         static protected int SortByName (Transform a, Transform b) { return string.Compare(a.name, b.name); }
  131.         static protected int SortHorizontal (Transform a, Transform b) { return a.localPosition.x.CompareTo(b.localPosition.x); }
  132.         static protected int SortVertical (Transform a, Transform b) { return b.localPosition.y.CompareTo(a.localPosition.y); }
  133.  
  134.         /// <summary>
  135.         /// Want your own custom sorting logic? Override this function.
  136.         /// </summary>
  137.  
  138.         protected virtual void Sort (List<Transform> list) { list.Sort(SortByName); }
  139.  
  140.         /// <summary>
  141.         /// Recalculate the position of all elements within the grid, sorting them alphabetically if necessary.
  142.         /// </summary>
  143.  
  144.         [ContextMenu("Execute")]
  145.         public virtual void Reposition ()
  146.         {
  147.                 if (Application.isPlaying && !mInitDone && NGUITools.GetActive(this))
  148.                 {
  149.                         mReposition = true;
  150.                         return;
  151.                 }
  152.  
  153.                 if (!mInitDone) Init();
  154.  
  155.                 mReposition = false;
  156.                 Transform myTrans = transform;
  157.  
  158.                 int x = 0;
  159.                 int y = 0;
  160.  
  161.                 if (sorting != Sorting.None)
  162.                 {
  163.                         List<Transform> list = new List<Transform>();
  164.  
  165.                         for (int i = 0; i < myTrans.childCount; ++i)
  166.                         {
  167.                                 Transform t = myTrans.GetChild(i);
  168.                                 if (t && (!hideInactive || NGUITools.GetActive(t.gameObject))) list.Add(t);
  169.                         }
  170.  
  171.                         if (sorting == Sorting.Alphabetic) list.Sort(SortByName);
  172.                         else if (sorting == Sorting.Horizontal) list.Sort(SortHorizontal);
  173.                         else if (sorting == Sorting.Vertical) list.Sort(SortVertical);
  174.                         else Sort(list);
  175.  
  176.                         for (int i = 0, imax = list.Count; i < imax; ++i)
  177.                         {
  178.                                 Transform t = list[i];
  179.  
  180.                                 if (!NGUITools.GetActive(t.gameObject) && hideInactive) continue;
  181.  
  182.                                 float depth = t.localPosition.z;
  183.                                 Vector3 pos = (arrangement == Arrangement.Horizontal) ?
  184.                                         new Vector3(cellWidth * x, -cellHeight * y, depth) :
  185.                                         new Vector3(cellWidth * y, -cellHeight * x, depth);
  186.  
  187.                                 if (animateSmoothly && Application.isPlaying)
  188.                                 {
  189.                                         SpringPosition.Begin(t.gameObject, pos, 15f).updateScrollView = true;
  190.                                 }
  191.                                 else t.localPosition = pos;
  192.  
  193.                                 if (++x >= maxPerLine && maxPerLine > 0)
  194.                                 {
  195.                                         x = 0;
  196.                                         ++y;
  197.                                 }
  198.                         }
  199.                 }
  200.                 else
  201.                 {
  202.                         for (int i = 0; i < myTrans.childCount; ++i)
  203.                         {
  204.                                 Transform t = myTrans.GetChild(i);
  205.  
  206.                                 if (!NGUITools.GetActive(t.gameObject) && hideInactive) continue;
  207.  
  208.                                 float depth = t.localPosition.z;
  209.                                 Vector3 pos = (arrangement == Arrangement.Horizontal) ?
  210.                                         new Vector3(cellWidth * x, -cellHeight * y, depth) :
  211.                                         new Vector3(cellWidth * y, -cellHeight * x, depth);
  212.  
  213.                                 if (animateSmoothly && Application.isPlaying)
  214.                                 {
  215.                                         SpringPosition.Begin(t.gameObject, pos, 15f).updateScrollView = true;
  216.                                 }
  217.                                 else t.localPosition = pos;
  218.  
  219.                                 if (++x >= maxPerLine && maxPerLine > 0)
  220.                                 {
  221.                                         x = 0;
  222.                                         ++y;
  223.                                 }
  224.                         }
  225.                 }
  226.  
  227.                 if (keepWithinPanel && mPanel != null)
  228.                         mPanel.ConstrainTargetToBounds(myTrans, true);
  229.  
  230.                 if (onReposition != null)
  231.                         onReposition();
  232.         }
  233. }

wallabie

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 200
    • View Profile
Re: Example 11, Drag&Drop in same scrollview.
« Reply #3 on: March 09, 2014, 09:39:41 AM »
Awesome work.  Thanks for the addition.  This makes the drag and drop functionality really intuitive. 

In many scrollviews that supports drag and drop, as the user drags an object over the scrollview, there is usually a "red line" that appears in between the items to show where the object would be dropped.  This helps the user to know where the drop will occur. 

What would be a good way to do this.

Cheers.