Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Viewport oraz Transformy Canvy (canvas tranform)

Wprowadzenie

This is an overview of the 2D transforms going on for nodes from the moment they draw their content locally to the time they are drawn onto the screen. This overview discusses very low level details of the engine.

Celem tego samouczka jest przedstawienie sposobu przekazywania zdarzeń wejściowych do Input z pozycją we właściwym układzie współrzędnych.

Bardziej obszerny opis wszystkich układów współrzędnych i transformacji 2d jest dostępny w 2D coordinate systems and 2D transforms.

Transformacje Kanwy (Canvas transform)

Jak wspomniano w poprzednim samouczku, Warstwy Kanwy (Canvas layers), każdy węzeł CanvasItem (pamiętaj, że węzły oparte na Node2D i Control używają CanvasItem jako wspólnego korzenia) będzie znajdował się w Canvas Layer. Każda warstwa kanwy ma transformację (translację, obrót, skalę itp.), która może być dostępna jako Transform2D.

Węzły są domyślnie rysowane w warstwie 0, we wbudowanym obszarze canvas. Aby umieścić węzły w innej warstwie, można użyć węzła CanvasLayer.

Globalna transformacja kanwy

Viewporty mają również globalną transformację Canvas (również Transform2D). Jest to transformacja nadrzędna i wpływa na wszystkie indywidualne transformacje warstwy Canvas. Ogólnie jest ona używana głównie w edytorze CanvasItem Godota.

Transofrmacja - rozciąganie (Stretch transform)

Viewporty posiadają Stretch Transform, który jest używany podczas zmiany rozmiaru lub rozciągania ekranu. Ta transformacja jest używana wewnętrznie (jak opisano w Wiele rozdzielczości), ale może być również ustawiona ręcznie na każdym widoku.

Input events are multiplied by this transform but lack the ones above. To convert InputEvent coordinates to local CanvasItem coordinates, the CanvasItem.make_input_local() function was added for convenience.

Transformacje Okna

The root viewport is a Window. In order to scale and position the Window's content as described in Wiele rozdzielczości, each Window contains a window transform. It is for example responsible for the black bars at the Window's sides so that the Viewport is displayed with a fixed aspect ratio.

Transform order

To convert a CanvasItem local coordinate to an actual screen coordinate, the following chain of transforms must be applied:

../../_images/viewport_transforms3.webp

Transform functions

The above graphic shows some available transform functions. All transforms are directed from right to left, this means multiplying a transform with a coordinate results in a coordinate system further to the left, multiplying the affine inverse of a transform results in a coordinate system further to the right:

# Called from a CanvasItem.
canvas_pos = get_global_transform() * local_pos
local_pos = get_global_transform().affine_inverse() * canvas_pos

Finally, then, to convert a CanvasItem local coordinates to screen coordinates, just multiply in the following order:

var screen_coord = get_viewport().get_screen_transform() * get_global_transform_with_canvas() * local_pos

Keep in mind, however, that it is generally not desired to work with screen coordinates. The recommended approach is to simply work in Canvas coordinates (CanvasItem.get_global_transform()), to allow automatic screen resolution resizing to work properly.

Feeding custom input events

It is often desired to feed custom input events to the game. With the above knowledge, to correctly do this in the focused window, it must be done the following way:

var local_pos = Vector2(10, 20) # Local to Control/Node2D.
var ie = InputEventMouseButton.new()
ie.button_index = MOUSE_BUTTON_LEFT
ie.position = get_viewport().get_screen_transform() * get_global_transform_with_canvas() * local_pos
Input.parse_input_event(ie)