Is there a convenient way to center a node, or more specifically, a TextureRect, into the screen? Note in my case my display window is set to be stretched in 2D "keep" aspect.
Obviously the position could be calculated with the formula:
x = WindowWidth / 2 + TextureWidth / 2
y = WindowHeight / 2 + TextureHeight / 2
But I've not found how to get the original window size of the Godot project. I'm obtaining the screen width through this.get_tree().root.size.x, and it varies according to how the window is resized. For example, my project width is 1024 and I'd like to not express this width in my code. Is that possible?
Godot Engine also seems to have the notion of anchor and centering when you use its visual editor, so I suspect there is a way of centering the node without manual calculus.
Update
The answer by @Theraot shows method of positioning node to center based on a preset, but it positions the node elsewhere, not on the center:
(Note "state" means "scene")
import State from '../State.jsx';
import RecoyxLogo from 'res://img/company-logo/recoyx.png';
export default class PresentsState extends State {
constructor() {
super();
}
async _ready() {
await godot.yield(this.get_tree(), 'idle_frame');
let recoyxLogo = new godot.TextureRect;
recoyxLogo.texture = RecoyxLogo;
recoyxLogo.expand = true;
recoyxLogo.rect_size = new godot.Vector2(1200 / 2, 600 / 2);
// recoyxLogo.rect_position = new godot.Vector2(this.get_tree().root.size.x / 2 + (1200 / 2 / 2), this.get_tree().root.size.y / 2 + (600 / 2) / 2);
recoyxLogo.set_anchors_and_margins_preset(godot.Control.PRESET_CENTER);
this.add_child(recoyxLogo);
}
_process(delta) {
}
}
Here's State.jsx:
export default class State extends godot.Node2D {
constructor() {
super();
// this.scale = new godot.Vector2(0.47, 0.47);
}
}