Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include the follow-viewport-transform into CanvasLayer transform calculations #59682

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/classes/CanvasItem.xml
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@
<method name="get_canvas_transform" qualifiers="const">
<return type="Transform2D" />
<description>
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.
</description>
</method>
<method name="get_global_mouse_position" qualifiers="const">
Expand All @@ -422,7 +422,7 @@
<method name="get_global_transform_with_canvas" qualifiers="const">
<return type="Transform2D" />
<description>
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.
</description>
</method>
<method name="get_local_mouse_position" qualifiers="const">
Expand Down Expand Up @@ -453,7 +453,7 @@
<method name="get_viewport_transform" qualifiers="const">
<return type="Transform2D" />
<description>
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.
</description>
</method>
<method name="get_world_2d" qualifiers="const">
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/CanvasLayer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
Returns the RID of the canvas used by this layer.
</description>
</method>
<method name="get_final_transform" qualifiers="const">
<return type="Transform2D" />
<description>
Returns the transform from the [CanvasLayer]s coordinate system to the [Viewport]s coordinate system.
</description>
</method>
<method name="hide">
<return type="void" />
<description>
Expand Down
6 changes: 3 additions & 3 deletions scene/main/canvas_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void CanvasItem::_redraw_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 {
Expand Down Expand Up @@ -1041,7 +1041,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<CanvasItem>(get_parent())) {
return Object::cast_to<CanvasItem>(get_parent())->get_canvas_transform();
} else {
Expand All @@ -1053,7 +1053,7 @@ Transform2D CanvasItem::get_viewport_transform() const {
ERR_FAIL_COND_V(!is_inside_tree(), Transform2D());

if (canvas_layer) {
return get_viewport()->get_final_transform() * canvas_layer->get_transform();
return get_viewport()->get_final_transform() * canvas_layer->get_final_transform();
} else {
return get_viewport()->get_final_transform() * get_viewport()->get_canvas_transform();
}
Expand Down
13 changes: 13 additions & 0 deletions scene/main/canvas_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,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;
}
Comment on lines +93 to +96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should follow scale be applied if vp is invalid? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a philosophical question. I would be happy to adjust the PR, acording to the preference of the maintainers.

return follow * transform;
}
return transform;
}

void CanvasLayer::_update_xform() {
transform.set_rotation_and_scale(rot, scale);
transform.set_origin(ofs);
Expand Down Expand Up @@ -303,6 +315,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);
Expand Down
1 change: 1 addition & 0 deletions scene/main/canvas_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class CanvasLayer : public Node {

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;
Expand Down
2 changes: 1 addition & 1 deletion scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ void Viewport::_process_picking() {
ObjectID canvas_layer_id;
if (E) {
// A descendant CanvasLayer.
canvas_layer_transform = E->get_transform();
canvas_layer_transform = E->get_final_transform();
canvas_layer_id = E->get_instance_id();
} else {
// This Viewport's builtin canvas.
Expand Down