Author Topic: Is it possible to make widget fit it's content?  (Read 7339 times)

artfabrique

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 45
    • View Profile
Is it possible to make widget fit it's content?
« on: January 31, 2014, 08:11:24 PM »
Good day!
I'm creating in-game skype-like messenger and it has contacts list on the right.
The thing is that i plan to change font size depending on physical device display size. (32px on iPhone and 18px on iPad)
So my contact list item height will be varied.
How can i make single list item wrapper(container) widget fit item content? Font size changes, and i need to update wrapper height as well...
Later i want to wrap this wrapper with another widget, twice bigger height and the inner wrapper(with all elements) should be centered vertically.
Check out scheme in attachment.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Is it possible to make widget fit it's content?
« Reply #1 on: February 01, 2014, 12:55:43 PM »
You want to scale the outer rectangles based on the dimensions of the inner ones, and you have two inner ones? That's pretty complicated. You need to use advanced anchors, where you anchor things to different objects. I would advise you to treat your labels as your main item, with the contacts wrappers anchored to the labels, rather than having labels be children of your contact wrapper.

artfabrique

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 45
    • View Profile
Re: Is it possible to make widget fit it's content?
« Reply #2 on: February 01, 2014, 05:43:24 PM »
Ah.. i see so this doesn't work because it can be 100 different UILabels inside a container and i don't know wich will become too large. Anyway this would be a good feature to have a checkbox on UIWidget: "Fit inner content" and a dropdown list: "Fit width only", "Fit height only", "Both".

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Is it possible to make widget fit it's content?
« Reply #3 on: February 01, 2014, 06:55:08 PM »
If you have 2 labels: Label Top, Label Bottom... and you have a sprite for the background, you can use advanced anchors to anchor the top to Label Top's top, and bottom to Label Bottom's bottom. You can also set Label Top to "Resize Height", same as Label Bottom, and have Label Bottom's top be anchored to the bottom of Label Top.

artfabrique

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 45
    • View Profile
Re: Is it possible to make widget fit it's content?
« Reply #4 on: February 01, 2014, 07:42:31 PM »
Sure! I've tried this approach but after i ran into other problem  :)
What if my content looks like this and i'll have let's say 9 types of content layout?
How do i know which widget is the most right,left,top and bottom?
This is layout for quest messages and other special message templates and it will be modified/added over time(months) because it's basically part of the game core content. And i'd love to have possibility to update only this parts without updating my window manager.

artfabrique

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 45
    • View Profile
Re: Is it possible to make widget fit it's content?
« Reply #5 on: February 02, 2014, 07:43:43 PM »
Any thoughts on this case?
I'll try to write some loop where i'll recursively ask each of children their left-top-right-bottom coordinates (still don't know which space to use) and then find max&min to use as left-top-right-bottom.
I've found this method:
  1. NGUIMath.CalculateRelativeWidgetBounds
But i'm not sure how it works and which units it use and in which coordinates space?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Is it possible to make widget fit it's content?
« Reply #6 on: February 04, 2014, 02:01:28 AM »
There is no silver bullet for something highly custom like that.

Your best bet is to simply position the widgets yourself based on your own logic in a custom script written just for that purpose.

artfabrique

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 45
    • View Profile
Re: Is it possible to make widget fit it's content?
« Reply #7 on: February 04, 2014, 04:24:53 AM »
There is no silver bullet for something highly custom like that.

Your best bet is to simply position the widgets yourself based on your own logic in a custom script written just for that purpose.
Sure! Any way i've bought latest NGUI and pretty happy with it.
But what about NGUIMath.Calculate.. which returns Bounds.size?
Now i'm thinking of this scene structure:
- Container1 (widget) (this is main window)
--- Container2 Game Object (Anchor to Container1 TopLeft corner)
------ Title (UILabel) (Anchor to Container2 left to center it vertically)
------ Description (UILabel) (Anchor to Title)
------ Icon (Sprite) (Anchor to Title)
------ Other content elems..
--- Background (Sprite) (Anchor to Container1 left top right bottom)

I'm trying to get Container2 dimensions by:
  1. Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(transform, transform.FindChild("Container2").transform);
Then i just resize the Container1 by:
  1. GetComponent<UIWidget>().width = bounds.size.x + myCustomPadding;
  2. GetComponent<UIWidget>().height = bounds.size.y + myCustomPadding;
  3.  

Am i on a right route or there is some gotchas?
For example old UIAnchor will not update automatically to center content. How can i trigger this recalculation from outside? I have RunOnlyOnce checkbox checked.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Is it possible to make widget fit it's content?
« Reply #8 on: February 04, 2014, 07:34:58 AM »
I don't advise using UIAnchor anymore. Regardless, you can force it to update by simply enabling it (it disables itself after running once if 'run only once' option is on).