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

Add make_local_canvas_position_global, the inverse to make_canvas_position_local #36931

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion doc/classes/CanvasItem.xml
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,17 @@
<argument index="0" name="screen_point" type="Vector2">
</argument>
<description>
Assigns [code]screen_point[/code] as this node's new local transform.
Transforms [code]screen_point[/code] to this node's local transform.
KoBeWi marked this conversation as resolved.
Show resolved Hide resolved
</description>
</method>
<method name="make_local_canvas_position" qualifiers="const">
<return type="Vector2">
</return>
<argument index="0" name="local_point" type="Vector2">
</argument>
<description>
Transforms [code]local_point[/code] to a screen point.
Basically the reverse of [code] make_canvas_position_local [/code]
Comment on lines +545 to +546
Copy link
Member

@KoBeWi KoBeWi Oct 23, 2024

Choose a reason for hiding this comment

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

Suggested change
Transforms [code]local_point[/code] to a screen point.
Basically the reverse of [code] make_canvas_position_local [/code]
Transforms [code]local_point[/code] to a point in viewport's coordinate system.
See also [method get_global_transform_with_canvas].

</description>
</method>
<method name="make_input_local" qualifiers="const">
Expand Down
14 changes: 12 additions & 2 deletions scene/2d/canvas_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,9 +1054,18 @@ Vector2 CanvasItem::make_canvas_position_local(const Vector2 &screen_point) cons

ERR_FAIL_COND_V(!is_inside_tree(), screen_point);

Transform2D local_matrix = (get_canvas_transform() * get_global_transform()).affine_inverse();
Transform2D global_to_local_matrix = (get_canvas_transform() * get_global_transform()).affine_inverse();

return local_matrix.xform(screen_point);
return global_to_local_matrix.xform(screen_point);
}

Vector2 CanvasItem::make_local_canvas_position_global(const Vector2 &local_point) const {

ERR_FAIL_COND_V(!is_inside_tree(), local_point);

Transform2D local_to_global_matrix = (get_canvas_transform() * get_global_transform());

return local_to_global_matrix.xform(local_point);
}

Ref<InputEvent> CanvasItem::make_input_local(const Ref<InputEvent> &p_event) const {
Expand Down Expand Up @@ -1192,6 +1201,7 @@ void CanvasItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("force_update_transform"), &CanvasItem::force_update_transform);

ClassDB::bind_method(D_METHOD("make_canvas_position_local", "screen_point"), &CanvasItem::make_canvas_position_local);
ClassDB::bind_method(D_METHOD("make_local_canvas_position_global", "local_postion"), &CanvasItem::make_local_canvas_position_global);
ClassDB::bind_method(D_METHOD("make_input_local", "event"), &CanvasItem::make_input_local);

ClassDB::bind_method(D_METHOD("set_texture_filter", "mode"), &CanvasItem::set_texture_filter);
Expand Down
1 change: 1 addition & 0 deletions scene/2d/canvas_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ class CanvasItem : public Node {

Ref<InputEvent> make_input_local(const Ref<InputEvent> &p_event) const;
Vector2 make_canvas_position_local(const Vector2 &screen_point) const;
Vector2 make_local_canvas_position_global(const Vector2 &local_point) const;

Vector2 get_global_mouse_position() const;
Vector2 get_local_mouse_position() const;
Expand Down