Author Topic: UIWrapContent with one end closed  (Read 3193 times)

Agent_007

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 5
  • Posts: 27
    • View Profile
UIWrapContent with one end closed
« on: April 14, 2014, 01:23:28 AM »
How hard would it be to modify UIWrapContent to make it closed from other end? (with closed I mean you cannot scroll around that end)

It would be very useful for top list since you can't go to negative positions (player #1 would be the first item)

Currently I am thinking about limiting the UIpanel values from WrapContent(), but I am not sure if it is the correct way to do this.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIWrapContent with one end closed
« Reply #1 on: April 14, 2014, 03:13:48 AM »
You can certainly modify the wrap content script but I'd recommend you create a copy of it so that your changes don't get wiped when you update. The function you will want to change is WrapContent() -- It does the repositioning inside.

nkls

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: UIWrapContent with one end closed
« Reply #2 on: April 14, 2014, 03:47:02 AM »
Could someone please give me a hint on what to change inside the WrapContent() to get this behavior?  :)

Agent_007

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 5
  • Posts: 27
    • View Profile
Re: UIWrapContent with one end closed
« Reply #3 on: April 14, 2014, 04:11:06 AM »
  1. float startY;
  2.  
to line 39

  1. startY = mPanel.transform.localPosition.y;
to line 112

  1. if (mPanel.transform.localPosition.y < startY)
  2. {
  3.         Vector3 overRide = mPanel.transform.localPosition;
  4.         overRide.y = startY;
  5.         mPanel.transform.localPosition = overRide;
  6.         mPanel.clipOffset = new Vector2(mPanel.clipOffset.x, -startY);
  7. }
to line 138

that is for vertical scroll list.

nkls

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: UIWrapContent with one end closed
« Reply #4 on: April 14, 2014, 04:30:05 AM »
sweet, thank you!

Agent_007

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 5
  • Posts: 27
    • View Profile
Re: UIWrapContent with one end closed
« Reply #5 on: April 14, 2014, 07:22:40 AM »
Better subclassed version with real indexing. It isn't perfect, but it should work. Code still needs another ending for the bottom limit, since there is a limited amount of scores.
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TopPlayersWrap : UIWrapContent {
  5.  
  6.         int itemCount;
  7.         int itemsTotalSize;
  8.         int startPositionForFirstItem;
  9.         float startY;
  10.         bool areUpdateItemPreCalcsDone = false;
  11.         bool areOnMovePreCalcsDone = false;
  12.  
  13.         private void DoUpdateItemPreCalcs()
  14.         {
  15.                 this.itemCount = transform.childCount;
  16.                 this.itemsTotalSize = this.itemCount * Mathf.RoundToInt(itemSize);
  17.         }
  18.  
  19.         private void DoOnMovePreCalcs(UIPanel panel)
  20.         {
  21.                 this.startY = panel.transform.localPosition.y;
  22.         }
  23.  
  24.         public void InitTopPlayers()
  25.         {
  26.                 // Set positions
  27.                 this.RepositionChildrenToStartFromTop(NGUITools.FindInParents<UIPanel>(gameObject));
  28.                 // Modify items
  29.                 for (int i = 0; i < transform.childCount; i++)
  30.                 {
  31.                         this.UpdateTopPlayerItem(transform.GetChild(i), i);
  32.                 }
  33.         }
  34.  
  35.         private void RepositionChildrenToStartFromTop(UIPanel panel)
  36.         {
  37.                 this.startPositionForFirstItem = (Mathf.RoundToInt(panel.GetViewSize().y) / 2) - Mathf.RoundToInt(itemSize)/2 - 10;
  38.                 for (int i = 0; i < transform.childCount; i++)
  39.                 {
  40.                         transform.GetChild(i).localPosition = new Vector3(0, startPositionForFirstItem - i * Mathf.RoundToInt(itemSize), 0);
  41.                 }
  42.         }
  43.  
  44.         protected override void UpdateItem (Transform item, int index)
  45.         {
  46.                 if (!this.areUpdateItemPreCalcsDone)
  47.                 {
  48.                         this.areUpdateItemPreCalcsDone = true;
  49.                         this.DoUpdateItemPreCalcs();
  50.                 }
  51.  
  52.                 int realIndex = (Mathf.RoundToInt(item.localPosition.y-startPositionForFirstItem) / -itemsTotalSize * itemCount) + index;
  53.                 this.UpdateTopPlayerItem(item, realIndex);
  54.         }
  55.  
  56.         private void UpdateTopPlayerItem(Transform item, int realIndex)
  57.         {
  58.                 // INSERT YOUR ITEM UPDATE CODE HERE!!!
  59.                 //item.Find("PlayerName").GetComponent<UILabel>().text = "Player " + realIndex;
  60.         }
  61.  
  62.         protected override void OnMove (UIPanel panel)
  63.         {
  64.                 if (!this.areOnMovePreCalcsDone)
  65.                 {
  66.                         this.areOnMovePreCalcsDone = true;
  67.                         this.DoOnMovePreCalcs(panel);
  68.                 }
  69.  
  70.                 if (panel.transform.localPosition.y < startY)
  71.                 {
  72.                         Vector3 overRide = panel.transform.localPosition;
  73.                         overRide.y = startY;
  74.                         panel.transform.localPosition = overRide;
  75.                         panel.clipOffset = new Vector2(panel.clipOffset.x, -startY);
  76.                 }
  77.  
  78.                 base.OnMove(panel);
  79.         }
  80. }

And init it with
  1. something.transform.Find("UIGrid").GetComponent<TopPlayersWrap>().InitTopPlayers();