From 0cf99cf95d098392d7d1943aac37b12bd831a1d9 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Thu, 24 Aug 2023 18:22:20 +0200 Subject: Add a Viewport method to get automatically computed 2D stretch transform `Viewport.get_stretch_transform()` returns the automatically computed 2D stretch transform. Combined with `Transform2D.get_scale()`, this is useful when using the `canvas_items` stretch mode in a project. There are many situations where knowing this factor is useful: - Divide Camera2D zoom to keep the size of the 2D game world identical regardless of the 2D scale factor (so that UI elements can still be scaled). - Make certain controls always drawn at 1:1 scale (e.g. for the crosshair in a FPS). This is done by dividing the Control node's scale by the scale factor. --- scene/main/viewport.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'scene/main/viewport.cpp') diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 54f66e8d4e..917da7d19e 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -1254,6 +1254,10 @@ Ref Viewport::get_world_2d() const { return world_2d; } +Transform2D Viewport::get_stretch_transform() const { + return stretch_transform; +} + Transform2D Viewport::get_final_transform() const { ERR_READ_THREAD_GUARD_V(Transform2D()); return stretch_transform * global_canvas_transform; @@ -4553,6 +4557,7 @@ void Viewport::_bind_methods() { ClassDB::bind_method(D_METHOD("set_global_canvas_transform", "xform"), &Viewport::set_global_canvas_transform); ClassDB::bind_method(D_METHOD("get_global_canvas_transform"), &Viewport::get_global_canvas_transform); + ClassDB::bind_method(D_METHOD("get_stretch_transform"), &Viewport::get_stretch_transform); ClassDB::bind_method(D_METHOD("get_final_transform"), &Viewport::get_final_transform); ClassDB::bind_method(D_METHOD("get_screen_transform"), &Viewport::get_screen_transform); -- cgit v1.2.3