Author Topic: centering a table  (Read 4964 times)

duncanx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
centering a table
« on: June 22, 2013, 08:48:38 AM »
Everything was going great until I needed to use a table. Using an anchor doesn't work because the pivot on the table isn't in the middle. I was trying to layout a series of evenly spaced items that can ultimately scroll...such as in an inventory. I need to support several different screen sizes which is why I was trying to use some combination of anchor and stretches. Anyone have some suggestions for this or a sample (seems like a common case)? Or alternatively any suggestions on making an inventory of objects that should occupy the majority of the screen?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: centering a table
« Reply #1 on: June 22, 2013, 01:54:32 PM »
Use NGUIMath.CalculateRelativeWidgetBounds to calculate the dimensions of the table, then just reposition it by half the width and/or height.

duncanx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: centering a table
« Reply #2 on: June 24, 2013, 05:57:41 AM »
This works perfectly, except if I add padding to the table. I thought maybe it was some kind of accumulation of padding, but after some toil I figured out if I just subtract the padding size once from the total offset, then it works again.


duncanx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: centering a table
« Reply #3 on: June 24, 2013, 07:30:51 AM »
I also noticed that sometimes it will still position itself slightly off, but it only happens sometimes. I wondered if maybe the position stuff was taking up some time and not being finished by the time the script was checking the bounds. So I hacked in a few frame delay (count to 10) from checking the bounds and now it works well, but that hack bothers me. Is there a nice way to know the table has finished beautifying itself?

Inside the table I have a bunch of ui elements, which stretch themselves based on the view rect size. The table's padding also gets defined by a similar measurement. I imagine that the table finishes moving stuff around, but then the padding gets updated...and somehow somewhere in the middle the bounds get checked (until I delayed it). In code, everything is done in the logical order...change padding, check bounds, move table.

duncanx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: centering a table
« Reply #4 on: June 26, 2013, 11:14:54 PM »
Any idea about this?

If I set padding, then call Reposition, and finally check bounds - the answer is wrong. I need to wait a while before checking bounds then the answer is correct. Right now I'm using a hack to just count a few frames before the check. Is there a better way?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: centering a table
« Reply #5 on: June 27, 2013, 12:17:13 AM »
If you have elements that stretch themselves, then their stretch script won't kick in until the following frame. You have to delay your operation by one or two frames in order for it to be correct -- or force update the stretch yourself.

duncanx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: centering a table
« Reply #6 on: June 27, 2013, 02:18:20 AM »
Yep - I have stretches based on the device screen size. I guess I'll count to 10 then do the calls.