From c6e0ed298cd81763256075c6bd4416fa27083f90 Mon Sep 17 00:00:00 2001 From: Markus Sauermann <6299227+Sauermann@users.noreply.github.com> Date: Mon, 19 Dec 2022 19:18:54 +0100 Subject: [PATCH] Include the follow-viewport-transform into CanvasLayer transform calculations The follow-viewport-transform was missing from several calculations 3.x version of #59682 (cherry picked from commit 608cbd8296b7e85b0921250813e18a9b8f34a941) --- doc/classes/CanvasItem.xml | 6 +++--- doc/classes/CanvasLayer.xml | 6 ++++++ scene/2d/canvas_item.cpp | 8 ++++---- scene/main/canvas_layer.cpp | 13 +++++++++++++ scene/main/canvas_layer.h | 1 + scene/main/viewport.cpp | 2 +- 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/doc/classes/CanvasItem.xml b/doc/classes/CanvasItem.xml index c2dce0b0735..a145c9f09cb 100644 --- a/doc/classes/CanvasItem.xml +++ b/doc/classes/CanvasItem.xml @@ -289,7 +289,7 @@ - Returns the transform matrix of this item's canvas. + Returns the transform from the coordinate system of the canvas, this item is in, to the [Viewport]s coordinate system. @@ -307,7 +307,7 @@ - Returns the global transform matrix of this item in relation to the canvas. + Returns the transform from the local coordinate system of this [CanvasItem] to the [Viewport]s coordinate system. @@ -331,7 +331,7 @@ - Returns this item's transform in relation to the viewport. + Returns the transform from the coordinate system of the canvas, this item is in, to the [Viewport]s embedders coordinate system. diff --git a/doc/classes/CanvasLayer.xml b/doc/classes/CanvasLayer.xml index 28d878f2cde..742815ba708 100644 --- a/doc/classes/CanvasLayer.xml +++ b/doc/classes/CanvasLayer.xml @@ -18,6 +18,12 @@ Returns the RID of the canvas used by this layer. + + + + Returns the transform from the [CanvasLayer]s coordinate system to the [Viewport]s coordinate system. + + diff --git a/scene/2d/canvas_item.cpp b/scene/2d/canvas_item.cpp index 084fd038050..4a09014c17e 100644 --- a/scene/2d/canvas_item.cpp +++ b/scene/2d/canvas_item.cpp @@ -466,7 +466,7 @@ void CanvasItem::_update_callback() { Transform2D CanvasItem::get_global_transform_with_canvas() const { if (canvas_layer) { - return canvas_layer->get_transform() * get_global_transform(); + return canvas_layer->get_final_transform() * get_global_transform(); } else if (is_inside_tree()) { return get_viewport()->get_canvas_transform() * get_global_transform(); } else { @@ -1213,7 +1213,7 @@ Transform2D CanvasItem::get_canvas_transform() const { ERR_FAIL_COND_V(!is_inside_tree(), Transform2D()); if (canvas_layer) { - return canvas_layer->get_transform(); + return canvas_layer->get_final_transform(); } else if (Object::cast_to(get_parent())) { return Object::cast_to(get_parent())->get_canvas_transform(); } else { @@ -1226,9 +1226,9 @@ Transform2D CanvasItem::get_viewport_transform() const { if (canvas_layer) { if (get_viewport()) { - return get_viewport()->get_final_transform() * canvas_layer->get_transform(); + return get_viewport()->get_final_transform() * canvas_layer->get_final_transform(); } else { - return canvas_layer->get_transform(); + return canvas_layer->get_final_transform(); } } else { diff --git a/scene/main/canvas_layer.cpp b/scene/main/canvas_layer.cpp index f5c9100c084..c5cc48f06dd 100644 --- a/scene/main/canvas_layer.cpp +++ b/scene/main/canvas_layer.cpp @@ -82,6 +82,18 @@ Transform2D CanvasLayer::get_transform() const { return transform; } +Transform2D CanvasLayer::get_final_transform() const { + if (is_following_viewport()) { + Transform2D follow; + follow.scale(Vector2(get_follow_viewport_scale(), get_follow_viewport_scale())); + if (vp) { + follow = vp->get_canvas_transform() * follow; + } + return follow * transform; + } + return transform; +} + void CanvasLayer::_update_xform() { transform.set_rotation_and_scale(rot, scale); transform.set_origin(ofs); @@ -300,6 +312,7 @@ void CanvasLayer::_bind_methods() { ClassDB::bind_method(D_METHOD("set_transform", "transform"), &CanvasLayer::set_transform); ClassDB::bind_method(D_METHOD("get_transform"), &CanvasLayer::get_transform); + ClassDB::bind_method(D_METHOD("get_final_transform"), &CanvasLayer::get_final_transform); ClassDB::bind_method(D_METHOD("set_offset", "offset"), &CanvasLayer::set_offset); ClassDB::bind_method(D_METHOD("get_offset"), &CanvasLayer::get_offset); diff --git a/scene/main/canvas_layer.h b/scene/main/canvas_layer.h index 2366a31acf0..90bdcd7a78b 100644 --- a/scene/main/canvas_layer.h +++ b/scene/main/canvas_layer.h @@ -77,6 +77,7 @@ public: void set_transform(const Transform2D &p_xform); Transform2D get_transform() const; + Transform2D get_final_transform() const; void set_offset(const Vector2 &p_offset); Vector2 get_offset() const; diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 8be9eb05ff3..ffa5cdc42a1 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -547,7 +547,7 @@ void Viewport::_process_picking(bool p_ignore_paused) { ObjectID canvas_layer_id; if (E->get()) { // A descendant CanvasLayer - canvas_transform = E->get()->get_transform(); + canvas_transform = E->get()->get_final_transform(); canvas_layer_id = E->get()->get_instance_id(); } else { // This Viewport's builtin canvas