Center Container on Screen

Hi!

I have a Container which is kind of a dialog which I display by adding it to the guiNode. I want to have it centered on the screen, so I calculate the position together with the size of the screen and the size of the container and set it with “setLocalTranslation”.

So far its working, but when using a Layout with a preferred size its not really correct. Because the size of the container will only get updated when revalidate of the GuiControl gets called (from normal update loop), so I would have to wait one render cycle until I get the correct size using getSize.

My question is, what is the best pattern to do this. From the API perspective I would have expected something like revalidate or validate, … but the only thing I’ve found is the revalidate of the Control which is protected (and kind of tricky to access, because you manually need to get it using getControl of the spatial). Or is there a better way to do this?

Currently I have it with just using the preferred size when calculating the position, but this is not really correct because size can be different depending on the layout and the sizes of the components… I also tried using the center() method, but this caused some NPE crash because (I think) the worldBounds have been null - but maybe this would also work when waiting one frame, did not check it. I know it would be possible and easy to wait this one frame, but I think this is kind of messy.

Anyway, I think this is a usual enough use case to have the best way to do this in the forums - maybe from the mastermind himself;)

Hope this is not already somewhere in the forums - tried to search for it, but got no real answer (maybe there is some example in one of the many lemur threads…).

No… for root level GUI elements the preferred size will always be what the size will be. I mean, the components may (incorrectly) draw themselves outside of that but the layout doesn’t know about it and getSize() will then report the same as preferred size. Basically, when a GUI element is not the part of some other layout (like for a root container) then the preferred size is calculated and forced as the size.

If you look at the OptionPanelState in Lemur-proto (a good class to know how to use anyway, by the way) you can see how it centers its own dialog. (It also supports modal dialogs and there’s a chance you can reuse some stuff… though I plan to add a modal state to be more general as I personally had to cut-paste the modal support at least once.)

Note: that if you really require dynamic centering (or positioning) then you can also add a GuiControlListener to the GuiControl and do things on its reshape event:
http://jmonkeyengine-contributions.github.io/Lemur/javadoc/Lemur/com/simsilica/lemur/core/GuiControlListener.html

…thanks for reminding me that I needed to update the online javadocs. :slight_smile:

A lot of useful information!

About the preferred size - I understand. I thought there might be a possibility where the “root” size gets expanded or reduces beyond the preferred size. But if this cannot happen then using the preferred size is alright.

The OptionPanelState has some interesting stuff in there - most of these things I already have in place, but the setupBlocker thingy is interesting to me. Was already starting to work on something similar but this solution is more elegant :slight_smile:

GuiControlListener is also good to know - not for any of my current requirements but can see a lot of stuff that could require this observer.

About the documentation: Generally the javadoc is more then I would have expected, but I prefer to read the code anyway. The most useful resource so far for me was the wiki and the 2 examples. I personally would prefer to have a few more examples then to have a better javadoc :smiley:

Yeah, I meant that the online javadocs were old. I hadn’t rerun the command in several releases.

Edit: and about examples, the SimArboreal editor is kind of one giant example.
http://simsilica.github.io/SimArboreal-Editor/

Ah, yes. I know about this editor and followed it back then when you made it. But I didn’t think of looking into it for lemur usage example. Maybe you should put this link also on the documentation area for lemur. I think this might also be interesting for other people that want to give lemur a try!