Centering/Component Alignment
Problem: My CenterContainer isn't centering my component! Am I going crazy?
Answer: Yes, you probably are. However, CenterContainer doesn't center just any Node. It will center TextureRect, and perhaps any type of Control node, but not all nodes. If you set the CenterContainer to AnchorLeft and AnchorTop at 0, and AnchorRight and AnchorBottom at 1, and add a node that's a TextureRect to it, and set the TextureRect's Texture to an image of the appropriate size for what needs to be centered, it should center all right.
Problem: Okay, I'm using a TextureRect, but my VBox/HBox only works if I put the TextureRect at the beginning. If I put it at the end, it appears off the edge of the screen. Why
Answer: The TextureRect must have a texture applied; that is where the VBox/HBox grabs its dimensions from. If you only added children, then even if those children have dimensions, the VBox/HBox will not be able to figure out the size of your TextureRect, and it will not appear properly unless it's beginning aligned.
If you don't have one uniform texture for the background, but instead are composing it of several children, a solution is to create an ImageTexture programmatically, set that to be 100% transparent, and use that as the texture for your TextureRect. See the example below:
Code:
ImageTexture thisTexture = new ImageTexture();
Image image = new Image();
image.Create(540, 320, false, Image.Format.Rgba8);
image.Fill(Color.Color8(0, 0, 0, 0));
thisTexture.CreateFromImage(image);
this.Texture = thisTexture;
----------
This is probably a corollary of
@Puppeteer 's "If you want it to move or scale with the interface window, use a Control node (or its many descendants)". And I believe it's probably related to why he made LowerRightInfoBox a TextureRect. I recently went through this with the Domestic Advisor (which should roll over to all advisors), and I must admit I still don't entirely follow
why CenterContainer doesn't center a plain-old Node2D which has descendants with dimensions of their own (including TextureRect descendants).
@Puppeteer - If you can explain or know a blog post that can explain why this is, I'd love to add it to the documentation. I probably spent an hour trying to get the Advisor to be centered in a CenterContainer the "proper" way so the CenterContainer would cover the whole window and it would be nicely centered without hard-coding the margins, but 45 minutes of that was trying to figure out why seemingly sensible values didn't work, and trying every possible combination I could find in the Node view of the CenterContainer to try to get it to center, and 13 minutes of it was figuring out how MainMenu did it and why that seemingly didn't translate to the advisor area. It feels like one of the bigger "gotchas" I've run across with Godot.