Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - xerohuang

Pages: [1]
1
NGUI 3 Support / Bug 3.7.1 UIDragDropItem when cloneOnDrag
« on: August 25, 2014, 04:25:35 AM »
UIDragDropItem

"mDragging = true;" in StartDragging() but lost changing when cloneOnDrag.

My fix:

  1. protected virtual void StartDragging ()
  2.         {
  3.                 if (!mDragging)
  4.                 {
  5.                         mDragging = true;
  6.  
  7.                         if (cloneOnDrag)
  8.                         {
  9.                                 //...
  10.                                 //...
  11.                                 //...
  12.                                 item.Start();
  13.  
  14.                                 //fix:
  15.                                 item.mDragging = true;
  16.                                 mDragging = false;
  17.  
  18.                                 item.OnDragDropStart();
  19.                         }
  20.                         else OnDragDropStart();
  21.                 }
  22.         }

2
NGUI 3 Support / Re: Pivot position in UITable
« on: July 31, 2014, 04:58:24 AM »
Add Vertical Pivot  :)


  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 arranged into a table
  11. /// with rows and columns automatically adjusting their size to fit their content
  12. /// (think "table" tag in HTML).
  13. /// </summary>
  14.  
  15. [AddComponentMenu("NGUI/Interaction/Table (Pivot)")]
  16. public class UITablePivot : UIWidgetContainer
  17. {
  18.         public delegate void OnReposition();
  19.        
  20.         public enum Direction
  21.         {
  22.                 Down,
  23.                 Up,
  24.         }
  25.        
  26.         public enum Pivot
  27.         {
  28.                 Left,
  29.                 Center,
  30.                 Right
  31.         }
  32.  
  33.         public enum PivotVertical
  34.         {
  35.                 Up,
  36.                 Middle,
  37.                 Down
  38.         }
  39.        
  40.         public enum Sorting
  41.         {
  42.                 None,
  43.                 Alphabetic,
  44.                 Horizontal,
  45.                 Vertical,
  46.                 Custom,
  47.         }
  48.        
  49.         /// <summary>
  50.         /// How many columns there will be before a new line is started. 0 means unlimited.
  51.         /// </summary>
  52.        
  53.         public int columns = 0;
  54.        
  55.         /// <summary>
  56.         /// Which way the new lines will be added.
  57.         /// </summary>
  58.        
  59.         public Direction direction = Direction.Down;
  60.        
  61.         /// <summary>
  62.         /// How to sort the grid's elements.
  63.         /// </summary>
  64.        
  65.         public Sorting sorting = Sorting.None;
  66.        
  67.        
  68.         /// <summary>
  69.         /// Table pivot
  70.         /// </summary>
  71.         ///
  72.         public Pivot pivot = Pivot.Left;
  73.  
  74.         /// <summary>
  75.         /// Table vertical pivot
  76.         /// </summary>
  77.         public PivotVertical pivotVertical = PivotVertical.Middle;
  78.  
  79.         /// <summary>
  80.         /// Whether inactive children will be discarded from the table's calculations.
  81.         /// </summary>
  82.        
  83.         public bool hideInactive = true;
  84.        
  85.         /// <summary>
  86.         /// Whether the parent container will be notified of the table's changes.
  87.         /// </summary>
  88.        
  89.         public bool keepWithinPanel = false;
  90.        
  91.         /// <summary>
  92.         /// Padding around each entry, in pixels.
  93.         /// </summary>
  94.        
  95.         public Vector2 padding = Vector2.zero;
  96.        
  97.         /// <summary>
  98.         /// Delegate function that will be called when the table repositions its content.
  99.         /// </summary>
  100.        
  101.         public OnReposition onReposition;
  102.        
  103.         protected UIPanel mPanel;
  104.         protected bool mInitDone = false;
  105.         protected bool mReposition = false;
  106.         protected List<Transform> mChildren = new List<Transform>();
  107.        
  108.         // Use the 'sorting' property instead
  109.         [HideInInspector]
  110.         [SerializeField]
  111.         bool
  112.                 sorted = false;
  113.        
  114.         /// <summary>
  115.         /// Reposition the children on the next Update().
  116.         /// </summary>
  117.        
  118.         public bool repositionNow { set { if (value) { mReposition = true; enabled = true; } } }
  119.        
  120.         /// <summary>
  121.         /// Returns the list of table's children, sorted alphabetically if necessary.
  122.         /// </summary>
  123.        
  124.         public List<Transform> children
  125.         {
  126.                 get
  127.                 {
  128.                         if (mChildren.Count == 0)
  129.                         {
  130.                                 Transform myTrans = transform;
  131.                                 mChildren.Clear();
  132.                                
  133.                                 for (int i = 0; i < myTrans.childCount; ++i)
  134.                                 {
  135.                                         Transform child = myTrans.GetChild(i);
  136.                                         if (child && child.gameObject && (!hideInactive || NGUITools.GetActive(child.gameObject)))
  137.                                                 mChildren.Add(child);
  138.                                 }
  139.                                
  140.                                 if (sorting != Sorting.None || sorted)
  141.                                 {
  142.                                         if (sorting == Sorting.Alphabetic) mChildren.Sort(UIGrid.SortByName);
  143.                                         else if (sorting == Sorting.Horizontal) mChildren.Sort(UIGrid.SortHorizontal);
  144.                                         else if (sorting == Sorting.Vertical) mChildren.Sort(UIGrid.SortVertical);
  145.                                         else Sort(mChildren);
  146.                                 }
  147.                         }
  148.                         return mChildren;
  149.                 }
  150.         }
  151.        
  152.         /// <summary>
  153.         /// Want your own custom sorting logic? Override this function.
  154.         /// </summary>
  155.        
  156.         protected virtual void Sort (List<Transform> list) { list.Sort(UIGrid.SortByName); }
  157.        
  158.         /// <summary>
  159.         /// Positions the grid items, taking their own size into consideration.
  160.         /// </summary>
  161.        
  162.         protected void RepositionVariableSize (List<Transform> children)
  163.         {
  164.                 float xOffset = 0;
  165.                 float yOffset = 0;
  166.                
  167.                 int cols = columns > 0 ? children.Count / columns + 1 : 1;
  168.                 int rows = columns > 0 ? columns : children.Count;
  169.                
  170.                 Bounds[,] bounds = new Bounds[cols, rows];
  171.                 Bounds[] boundsRows = new Bounds[rows];
  172.                 Bounds[] boundsCols = new Bounds[cols];
  173.                
  174.                 int x = 0;
  175.                 int y = 0;
  176.                
  177.                 for (int i = 0, imax = children.Count; i < imax; ++i)
  178.                 {
  179.                         Transform t = children[i];
  180.                         Bounds b = NGUIMath.CalculateRelativeWidgetBounds(t, !hideInactive);
  181.                        
  182.                         Vector3 scale = t.localScale;
  183.                         b.min = Vector3.Scale(b.min, scale);
  184.                         b.max = Vector3.Scale(b.max, scale);
  185.                         bounds[y, x] = b;
  186.                        
  187.                         boundsRows[x].Encapsulate(b);
  188.                         boundsCols[y].Encapsulate(b);
  189.                        
  190.                         if (++x >= columns && columns > 0)
  191.                         {
  192.                                 x = 0;
  193.                                 ++y;
  194.                         }
  195.                 }
  196.                
  197.                 x = 0;
  198.                 y = 0;
  199.                
  200.                 for (int i = 0, imax = children.Count; i < imax; ++i)
  201.                 {
  202.                         Transform t = children[i];
  203.                         Bounds b = bounds[y, x];
  204.                         Bounds br = boundsRows[x];
  205.                         Bounds bc = boundsCols[y];
  206.                        
  207.                         Vector3 pos = t.localPosition;
  208.                         pos.x = xOffset + b.extents.x - b.center.x;
  209.                         pos.x += b.min.x - br.min.x + padding.x;
  210.                        
  211.                         if (direction == Direction.Down)
  212.                         {
  213.                                 pos.y = -yOffset - b.extents.y - b.center.y;
  214.                                 pos.y += (b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f - padding.y;
  215.                         }
  216.                         else
  217.                         {
  218.                                 pos.y = yOffset + (b.extents.y - b.center.y);
  219.                                 pos.y -= (b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f - padding.y;
  220.                         }
  221.                        
  222.                         xOffset += br.max.x - br.min.x + padding.x * 2f;
  223.                        
  224.                         t.localPosition = pos;
  225.                        
  226.                         if (++x >= columns && columns > 0)
  227.                         {
  228.                                 x = 0;
  229.                                 ++y;
  230.                                
  231.                                 xOffset = 0f;
  232.                                 yOffset += bc.size.y + padding.y * 2f;
  233.                         }
  234.                 }
  235.                
  236.                 RepositionPivot(children);
  237.         }
  238.        
  239.         /// <summary>
  240.         /// Recalculate the position of all the elements of the table, to change the pivot
  241.         /// </summary>
  242.        
  243.        
  244.         public void RepositionPivot(List<Transform> children) {
  245.                 float Width = 0;
  246.                 if (pivot != Pivot.Left)
  247.                         Width = NGUIMath.CalculateRelativeWidgetBounds(transform).size.x;
  248.  
  249.                 float Height = 0;
  250.                 if (pivotVertical != PivotVertical.Middle)
  251.                         Height = NGUIMath.CalculateRelativeWidgetBounds(transform).size.y;
  252.                                
  253.                 for (int i = 0, imax = children.Count; i < imax; ++i) {
  254.                         Transform t = children [i];
  255.                         Vector3 pos = t.localPosition;
  256.                         if (pivot == Pivot.Center)
  257.                                 pos.x -= Width / 2 + padding.x;
  258.                         else if (pivot == Pivot.Right)
  259.                                 pos.x -= Width + padding.x;
  260.                         else if (pivot == Pivot.Left)
  261.                                 pos.x -= padding.x;
  262.  
  263.                         float childHeight = NGUIMath.CalculateRelativeWidgetBounds(t).size.y;
  264.                         if (pivotVertical == PivotVertical.Up)
  265.                                 pos.y += Height / 2 - childHeight / 2;
  266.                         else if (pivotVertical == PivotVertical.Down)
  267.                                 pos.y -= Height / 2 - childHeight / 2;
  268.                
  269.  
  270.                         t.localPosition = pos;
  271.                 }
  272.         }
  273.        
  274.         /// <summary>
  275.         /// Recalculate the position of all elements within the table, sorting them alphabetically if necessary.
  276.         /// </summary>
  277.        
  278.         [ContextMenu("Execute")]
  279.         public virtual void Reposition ()
  280.         {
  281.                 if (Application.isPlaying && !mInitDone && NGUITools.GetActive(this))
  282.                 {
  283.                         mReposition = true;
  284.                         return;
  285.                 }
  286.                
  287.                 if (!mInitDone) Init();
  288.                
  289.                 mReposition = false;
  290.                 Transform myTrans = transform;
  291.                 mChildren.Clear();
  292.                 List<Transform> ch = children;
  293.                 if (ch.Count > 0) RepositionVariableSize(ch);
  294.                
  295.                 if (keepWithinPanel && mPanel != null)
  296.                 {
  297.                         mPanel.ConstrainTargetToBounds(myTrans, true);
  298.                         UIScrollView sv = mPanel.GetComponent<UIScrollView>();
  299.                         if (sv != null) sv.UpdateScrollbars(true);
  300.                 }
  301.                
  302.                 if (onReposition != null)
  303.                         onReposition();
  304.         }
  305.        
  306.         /// <summary>
  307.         /// Position the grid's contents when the script starts.
  308.         /// </summary>
  309.        
  310.         protected virtual void Start ()
  311.         {
  312.                 Init();
  313.                 Reposition();
  314.                 enabled = false;
  315.         }
  316.        
  317.         /// <summary>
  318.         /// Find the necessary components.
  319.         /// </summary>
  320.        
  321.         protected virtual void Init ()
  322.         {
  323.                 mInitDone = true;
  324.                 mPanel = NGUITools.FindInParents<UIPanel>(gameObject);
  325.         }
  326.        
  327.         /// <summary>
  328.         /// Is it time to reposition? Do so now.
  329.         /// </summary>
  330.        
  331.         protected virtual void LateUpdate ()
  332.         {
  333.                 if (mReposition) Reposition();
  334.                 enabled = false;
  335.         }
  336. }
  337.  

Pages: [1]