Adding controls upwards, or move it after drawing?

I want to draw a little menu above an object when it is selected, such that the bottom of the menu is at the top of the object. I have no trouble finding the top of the object, but I need to offset from there depending on the number of options visible on the menu.

My first thought was to just go through the options, figure out the visibility, sum each CalcSize(), then start over and begin adding controls from the top.

However, I’d like to avoid repeating the visibility tests, so I thought I could put the visible ones in a temporary list.

Anyway, that seems like a lot of trouble (going through the options twice, temp list, etc). Performance probably won’t be affected with the number of options I am likely to see, but I’m wondering if there is an easier or cleaner way.

Is there a way to either: move the controls after adding them to the Area (e.g. by adjusting the Area’s Rect), or have GUILayout add the controls “upwards”? (e.g. if I went through the options backwards and stacked them on top of each other). I thought maybe I could translate the GUI.matrix, but I think that will only affect the controls I draw after modifying it, so I’m left with the same problem (the amount to move it is not known until after going through them).

I guess I’ll get cracking on the original way (I’d probably be done by now :slight_smile: ) but I figured I’d ask for future reference.

Thanks!

Some notes as a follow-up:

  • CalcSize() does not handle text wrapping; use CalcMinMaxWidth and CalcHeight
  • you need to take into account the spacing as well
  • the spacing is the greater of padding or margin, depending on whether you’re measuring between containers or controls
  • e.g. for a button inside an area, the space above the button is Max(button’s margin top, areas padding top)
  • e.g. for between buttons placed vertically, the space is Max(button1’s margin bottom, button2’s margin top)
  • you probably need to add other things (e.g. overflow or offset too) but I zeroed those in the style (I hope we don’t need them later…)[/list:u:e68a57d872]
    [/list:u:e68a57d872]