Author Topic: Pivot position in UITable  (Read 11203 times)

deckard

  • Guest
Pivot position in UITable
« on: December 29, 2012, 05:51:56 AM »
Hello,

A change in the uitable to choose the position of the pivot





  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2012 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. [ExecuteInEditMode]
  16. [AddComponentMenu("NGUI/Interaction/Table")]
  17. public class UITable : MonoBehaviour
  18. {
  19.         public delegate void OnReposition ();
  20.  
  21.         public enum Direction
  22.         {
  23.                 Down,
  24.                 Up,
  25.         }
  26.         // Pivot Position v1.0
  27.         public enum PivotPosition
  28.         {
  29.                 Left,
  30.                 Center,
  31.                 Right
  32.         }
  33.         // / Pivot Position v1.0
  34.         public int columns = 0;
  35.         public Direction direction = Direction.Down;
  36.         // Pivot Position v1.0
  37.         public PivotPosition pivotPosition = PivotPosition.Left;
  38.         // / Pivot Position v1.0
  39.         public Vector2 padding = Vector2.zero;
  40.         public bool sorted = false;
  41.         public bool hideInactive = true;
  42.         public bool repositionNow = false;
  43.         public bool keepWithinPanel = false;
  44.         public OnReposition onReposition;
  45.  
  46.         UIPanel mPanel;
  47.         UIDraggablePanel mDrag;
  48.         bool mStarted = false;
  49.         List<Transform> mChildren = new List<Transform>();
  50.  
  51.         /// <summary>
  52.         /// Function that sorts items by name.
  53.         /// </summary>
  54.  
  55.         static public int SortByName (Transform a, Transform b) { return string.Compare(a.name, b.name); }
  56.  
  57.         /// <summary>
  58.         /// Returns the list of table's children, sorted alphabetically if necessary.
  59.         /// </summary>
  60.  
  61.         public List<Transform> children
  62.         {
  63.                 get
  64.                 {
  65.                         if (mChildren.Count == 0)
  66.                         {
  67.                                 Transform myTrans = transform;
  68.                                 mChildren.Clear();
  69.  
  70.                                 for (int i = 0; i < myTrans.childCount; ++i)
  71.                                 {
  72.                                         Transform child = myTrans.GetChild(i);
  73.  
  74.                                         if (child && child.gameObject && (!hideInactive || NGUITools.GetActive(child.gameObject))) mChildren.Add(child);
  75.                                 }
  76.                                 if (sorted) mChildren.Sort(SortByName);
  77.                         }
  78.                         return mChildren;
  79.                 }
  80.         }
  81.  
  82.         /// <summary>
  83.         /// Positions the grid items, taking their own size into consideration.
  84.         /// </summary>
  85.  
  86.         void RepositionVariableSize (List<Transform> children)
  87.         {
  88.                
  89.                 float xOffset = 0;
  90.                 float yOffset = 0;
  91.  
  92.                 int cols = columns > 0 ? children.Count / columns + 1 : 1;
  93.                 int rows = columns > 0 ? columns : children.Count;
  94.  
  95.                 Bounds[,] bounds = new Bounds[cols, rows];
  96.                 Bounds[] boundsRows = new Bounds[rows];
  97.                 Bounds[] boundsCols = new Bounds[cols];
  98.                
  99.                 // Pivot Position v1.0
  100.                 Dictionary<int, float> columnsWidth = new Dictionary<int, float>();
  101.                 // / Pivot Position v1.0
  102.                
  103.                 int x = 0;
  104.                 int y = 0;
  105.                 // Pivot Position v1.0
  106.                 float currentColumnWidth = 0F;
  107.                 // / Pivot Position v1.0
  108.                 for (int i = 0, imax = children.Count; i < imax; ++i)
  109.                 {
  110.                         Transform t = children[i];
  111.                         Bounds b = NGUIMath.CalculateRelativeWidgetBounds(t);
  112.                         Vector3 scale = t.localScale;
  113.                         b.min = Vector3.Scale(b.min, scale);
  114.                         b.max = Vector3.Scale(b.max, scale);
  115.                         bounds[y, x] = b;
  116.                        
  117.                         // Pivot Position v1.0
  118.                         currentColumnWidth += b.max.x + padding.x;
  119.                         // / Pivot Position v1.0
  120.                        
  121.                         boundsRows[x].Encapsulate(b);
  122.                         boundsCols[y].Encapsulate(b);
  123.                        
  124.                         if (++x >= columns && columns > 0)
  125.                         {
  126.                                 // Pivot Position v1.0
  127.                                 columnsWidth.Add(y, currentColumnWidth);
  128.                                 currentColumnWidth=0F;
  129.                                 // / Pivot Position v1.0
  130.                                 x = 0;
  131.                                 ++y;
  132.                         }
  133.                 }
  134.                 // Pivot Position v1.0
  135.                 columnsWidth.Add(y, currentColumnWidth);
  136.                 // / Pivot Position v1.0
  137.                 x = 0;
  138.                 y = 0;
  139.  
  140.                 for (int i = 0, imax = children.Count; i < imax; ++i)
  141.                 {
  142.                         Transform t = children[i];
  143.                         Bounds b = bounds[y, x];
  144.                         Bounds br = boundsRows[x];
  145.                         Bounds bc = boundsCols[y];
  146.  
  147.                         Vector3 pos = t.localPosition;
  148.                         pos.x = xOffset + b.extents.x - b.center.x;
  149.                         pos.x += b.min.x - br.min.x + padding.x;
  150.                         // Pivot Position v1.0
  151.                         if(pivotPosition == PivotPosition.Center)pos.x -= columnsWidth[y];
  152.                         else if(pivotPosition == PivotPosition.Right)pos.x -= columnsWidth[y]*2;
  153.                         // / Pivot Position v1.0
  154.                         if (direction == Direction.Down)
  155.                         {
  156.                                 pos.y = -yOffset - b.extents.y - b.center.y;
  157.                                 pos.y += (b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f - padding.y;
  158.                         }
  159.                         else
  160.                         {
  161.                                 pos.y = yOffset + b.extents.y - b.center.y;
  162.                                 pos.y += (b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f - padding.y;
  163.                         }
  164.                        
  165.                         xOffset += br.max.x - br.min.x + padding.x * 2f;
  166.                        
  167.                         t.localPosition = pos;
  168.                
  169.                         if (++x >= columns && columns > 0)
  170.                         {
  171.                                 x = 0;
  172.                                 ++y;
  173.                                
  174.                                 xOffset = 0f;
  175.                                 yOffset += bc.size.y + padding.y * 2f;
  176.                         }
  177.                 }
  178.         }
  179.  
  180.         /// <summary>
  181.         /// Recalculate the position of all elements within the table, sorting them alphabetically if necessary.
  182.         /// </summary>
  183.  
  184.         public void Reposition ()
  185.         {
  186.                 if (mStarted)
  187.                 {
  188.                         Transform myTrans = transform;
  189.                         mChildren.Clear();
  190.                         List<Transform> ch = children;
  191.                         if (ch.Count > 0) RepositionVariableSize(ch);
  192.  
  193.                         if (mDrag != null)
  194.                         {
  195.                                 mDrag.UpdateScrollbars(true);
  196.                                 mDrag.RestrictWithinBounds(true);
  197.                         }
  198.                         else if (mPanel != null)
  199.                         {
  200.                                 mPanel.ConstrainTargetToBounds(myTrans, true);
  201.                         }
  202.                         if (onReposition != null) onReposition();
  203.                 }
  204.                 else repositionNow = true;
  205.         }
  206.  
  207.         /// <summary>
  208.         /// Position the grid's contents when the script starts.
  209.         /// </summary>
  210.  
  211.         void Start ()
  212.         {
  213.                 mStarted = true;
  214.  
  215.                 if (keepWithinPanel)
  216.                 {
  217.                         mPanel = NGUITools.FindInParents<UIPanel>(gameObject);
  218.                         mDrag = NGUITools.FindInParents<UIDraggablePanel>(gameObject);
  219.                 }
  220.                 Reposition();
  221.         }
  222.  
  223.         /// <summary>
  224.         /// Is it time to reposition? Do so now.
  225.         /// </summary>
  226.  
  227.         void LateUpdate ()
  228.         {
  229.                 if (repositionNow)
  230.                 {
  231.                         repositionNow = false;
  232.                         Reposition();
  233.                 }
  234.         }
  235. }
« Last Edit: December 29, 2012, 06:31:31 AM by deckard »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Pivot position in UITable
« Reply #1 on: December 29, 2012, 06:44:15 AM »
« Last Edit: December 29, 2012, 06:46:42 AM by ArenMook »

lime-green.at

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 60
    • View Profile
Re: Pivot position in UITable
« Reply #2 on: December 29, 2012, 09:12:35 AM »
Im not getting it running:



As you can see the pivot of the UITable gameobject stays centered when i pick right.

lime-green.at

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 60
    • View Profile
Re: Pivot position in UITable
« Reply #3 on: December 29, 2012, 10:18:54 AM »
I should tell you exactly what i need:

I have some tables with contents from a database. These tables need to be right aligned, so that the right end of the last element will remain the same even with the table content changes, just like the normal pivot at any widgets, for illustration (it should like this) :




deckard

  • Guest
Re: Pivot position in UITable
« Reply #4 on: January 07, 2013, 08:55:47 AM »
My last version was buggy ... It was Christmas!
Here is a fully functional version:

  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2012 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. [ExecuteInEditMode]
  16. [AddComponentMenu("NGUI/Interaction/TablePositioned")]
  17. public class UITablePositioned : MonoBehaviour
  18. {
  19.         public delegate void OnReposition ();
  20.  
  21.         public enum Direction
  22.         {
  23.                 Down,
  24.                 Up,
  25.         }
  26.         public enum PivotPosition
  27.         {
  28.                 Left,
  29.                 Center,
  30.                 Right
  31.         }
  32.         public int columns = 0;
  33.         public Direction direction = Direction.Down;
  34.         public PivotPosition pivotPosition = PivotPosition.Left;
  35.         public Vector2 padding = Vector2.zero;
  36.         public bool sorted = false;
  37.         public bool hideInactive = true;
  38.         public bool repositionNow = false;
  39.         public bool keepWithinPanel = false;
  40.         public OnReposition onReposition;
  41.  
  42.         UIPanel mPanel;
  43.         UIDraggablePanel mDrag;
  44.         bool mStarted = false;
  45.         List<Transform> mChildren = new List<Transform>();
  46.  
  47.         /// <summary>
  48.         /// Function that sorts items by name.
  49.         /// </summary>
  50.  
  51.         static public int SortByName (Transform a, Transform b) { return string.Compare(a.name, b.name); }
  52.  
  53.         /// <summary>
  54.         /// Returns the list of table's children, sorted alphabetically if necessary.
  55.         /// </summary>
  56.  
  57.         public List<Transform> children
  58.         {
  59.                 get
  60.                 {
  61.                         if (mChildren.Count == 0)
  62.                         {
  63.                                 Transform myTrans = transform;
  64.                                 mChildren.Clear();
  65.  
  66.                                 for (int i = 0; i < myTrans.childCount; ++i)
  67.                                 {
  68.                                         Transform child = myTrans.GetChild(i);
  69.  
  70.                                         if (child && child.gameObject && (!hideInactive || NGUITools.GetActive(child.gameObject))) mChildren.Add(child);
  71.                                 }
  72.                                 if (sorted) mChildren.Sort(SortByName);
  73.                         }
  74.                         return mChildren;
  75.                 }
  76.         }
  77.  
  78.         /// <summary>
  79.         /// Positions the grid items, taking their own size into consideration.
  80.         /// </summary>
  81.  
  82.         void RepositionVariableSize (List<Transform> children)
  83.         {
  84.                 float xOffset = 0;
  85.                 float yOffset = 0;
  86.  
  87.                 int cols = columns > 0 ? children.Count / columns + 1 : 1;
  88.                 int rows = columns > 0 ? columns : children.Count;
  89.  
  90.                 Bounds[,] bounds = new Bounds[cols, rows];
  91.                 Bounds[] boundsRows = new Bounds[rows];
  92.                 Bounds[] boundsCols = new Bounds[cols];
  93.  
  94.                 int x = 0;
  95.                 int y = 0;
  96.  
  97.                 for (int i = 0, imax = children.Count; i < imax; ++i)
  98.                 {
  99.                         Transform t = children[i];
  100.                         Bounds b = NGUIMath.CalculateRelativeWidgetBounds(t);
  101.                         Vector3 scale = t.localScale;
  102.                         b.min = Vector3.Scale(b.min, scale);
  103.                         b.max = Vector3.Scale(b.max, scale);
  104.                         bounds[y, x] = b;
  105.  
  106.                         boundsRows[x].Encapsulate(b);
  107.                         boundsCols[y].Encapsulate(b);
  108.  
  109.                         if (++x >= columns && columns > 0)
  110.                         {
  111.                                 x = 0;
  112.                                 ++y;
  113.                         }
  114.                 }
  115.  
  116.                 x = 0;
  117.                 y = 0;
  118.  
  119.                 for (int i = 0, imax = children.Count; i < imax; ++i)
  120.                 {
  121.                         Transform t = children[i];
  122.                         Bounds b = bounds[y, x];
  123.                         Bounds br = boundsRows[x];
  124.                         Bounds bc = boundsCols[y];
  125.  
  126.                         Vector3 pos = t.localPosition;
  127.                         pos.x = xOffset + b.extents.x - b.center.x;
  128.                         pos.x += b.min.x - br.min.x + padding.x;
  129.  
  130.                         if (direction == Direction.Down)
  131.                         {
  132.                                 pos.y = -yOffset - b.extents.y - b.center.y;
  133.                                 pos.y += (b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f - padding.y;
  134.                         }
  135.                         else
  136.                         {
  137.                                 pos.y = yOffset + b.extents.y - b.center.y;
  138.                                 pos.y += (b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f - padding.y;
  139.                         }
  140.  
  141.                         xOffset += br.max.x - br.min.x + padding.x * 2f;
  142.  
  143.                         t.localPosition = pos;
  144.  
  145.                         if (++x >= columns && columns > 0)
  146.                         {
  147.                                 x = 0;
  148.                                 ++y;
  149.  
  150.                                 xOffset = 0f;
  151.                                 yOffset += bc.size.y + padding.y * 2f;
  152.                         }
  153.                 }
  154.                
  155.                 RepositionPivot(children);
  156.         }
  157.        
  158.         /// <summary>
  159.         /// Recalculate the position of all the elements of the table, to change the pivot
  160.         /// </summary>
  161.  
  162.         public void RepositionPivot (List<Transform> children){
  163.                
  164.                 float Width = 0;
  165.                 if(pivotPosition != PivotPosition.Left)Width = NGUIMath.CalculateRelativeWidgetBounds(transform).size.x;
  166.                
  167.                 for (int i = 0, imax = children.Count; i < imax; ++i)
  168.                 {
  169.                         Transform t = children[i];
  170.                         Vector3 pos = t.localPosition;
  171.                         if(pivotPosition == PivotPosition.Center)pos.x -= Width/2+padding.x;
  172.                         else if(pivotPosition == PivotPosition.Right)pos.x -= Width+padding.x;
  173.                         else if(pivotPosition == PivotPosition.Left)pos.x -= padding.x;
  174.                                
  175.                         t.localPosition = pos;
  176.                 }
  177.                
  178.         }
  179.  
  180.         /// <summary>
  181.         /// Recalculate the position of all elements within the table, sorting them alphabetically if necessary.
  182.         /// </summary>
  183.  
  184.         public void Reposition ()
  185.         {
  186.                 if (mStarted)
  187.                 {
  188.                         Transform myTrans = transform;
  189.                         mChildren.Clear();
  190.                         List<Transform> ch = children;
  191.                         if (ch.Count > 0) RepositionVariableSize(ch);
  192.  
  193.                         if (mDrag != null)
  194.                         {
  195.                                 mDrag.UpdateScrollbars(true);
  196.                                 mDrag.RestrictWithinBounds(true);
  197.                         }
  198.                         else if (mPanel != null)
  199.                         {
  200.                                 mPanel.ConstrainTargetToBounds(myTrans, true);
  201.                         }
  202.                         if (onReposition != null) onReposition();
  203.                 }
  204.                 else repositionNow = true;
  205.         }
  206.  
  207.         /// <summary>
  208.         /// Position the grid's contents when the script starts.
  209.         /// </summary>
  210.  
  211.         void Start ()
  212.         {
  213.                 mStarted = true;
  214.  
  215.                 if (keepWithinPanel)
  216.                 {
  217.                         mPanel = NGUITools.FindInParents<UIPanel>(gameObject);
  218.                         mDrag = NGUITools.FindInParents<UIDraggablePanel>(gameObject);
  219.                 }
  220.                 Reposition();
  221.         }
  222.  
  223.         /// <summary>
  224.         /// Is it time to reposition? Do so now.
  225.         /// </summary>
  226.  
  227.         void LateUpdate ()
  228.         {
  229.                 if (repositionNow)
  230.                 {
  231.                         repositionNow = false;
  232.                         Reposition();
  233.                 }
  234.         }
  235. }
« Last Edit: January 08, 2013, 04:57:32 PM by deckard »

deckard

  • Guest
Re: Pivot position in UITable
« Reply #5 on: January 08, 2013, 04:58:01 PM »
New version

eedok

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Pivot position in UITable
« Reply #6 on: March 07, 2013, 04:43:07 PM »
until this gets integrated with the main release I'm going to leave some keywords here as currently searching this on google yields less than desirable results, and this one is a good one:
NGUI right justify UITable
right align
right alignment
center justify
center alignment

hexaust

  • Newbie
  • *
  • Thank You
  • -Given: 14
  • -Receive: 1
  • Posts: 35
    • View Profile
Re: Pivot position in UITable
« Reply #7 on: April 01, 2014, 05:32:13 AM »
Another update

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

xerohuang

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 2
    • View Profile
Re: Pivot position in UITable
« Reply #8 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.  

PoN

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 111
    • View Profile
Re: Pivot position in UITable
« Reply #9 on: September 16, 2014, 11:51:57 PM »
What about is bottom alignment  of table items ? seem still not working
Worked on Doc&DogAge Of Fury 3D. Actually working on WarMach.

damelin

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Pivot position in UITable
« Reply #10 on: August 18, 2015, 09:14:55 PM »
Thanks, it's an useful class.

I found a small glitch about it (especially when adding/removing/adding children)

When NGUIMath.CalculateRelativeWidgetBounds is used, it should always pass "!hideInactive" as the second parameters