Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
Center control programatically

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);
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can change the size of your control from code by setting its rect_size and rect_position.

And you can do that in a method connected to the "resized" signal of the root of the scene tree (the root of the scene is a Viewport, and - as you would expect - it emits the "resized" every time its size changes).

As per the original window size, you can read it from project settings, by calling ProjectSettings.get_setting passing the property path as parameter. The properties for the window size are "display/window/size/width" and "display/window/size/height" (you can figure out these by looking in the Project Settings window or on the documentation for ProjectSettings).

And yes, you can set margins (with the method set_margin) and anchors (with the method set_anchor) from code, and also use the presets (with the methods set_margins_preset, set_anchors_preset, and set_anchors_and_margins_preset - this last method is a convenient way to set both at the same time). Setting the preset Control.PRESET_CENTER should do.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda