How to convert a coordinator from on canvas layer to another?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By alexzheng
:warning: Old Version Published before Godot 3 was released.

scene tree:
/root/game/camera2d
/root/game/button
/root/canvas_layer/hud

camera is in the center.

project settings:
stretch mode 2d
stretch aspect: keep_height

when touch the button, I want to get its coordinator in canvas_layer and add a control in canvas_layer in the same position as button.

I wonder wether this is the right way to do:

var pos = button.get_viewport_transform().xform(button.get_global_pos())
mycontrol.set_global_pos(pos)

this seems woks as expected in my test in some kinds of screen size.
I’m new to Godot, Could some expert confirm this?

alexzheng | 2017-04-14 04:24

:bust_in_silhouette: Reply From: volzhs

I think this will work as you expect.

var button_global_pos = button.get_global_pos()
var local_pos = node_in_another_layer.make_canvas_pos_local(button_global_pos)

Yes,this seems much more intuitive and works as expected.Thanks!

alexzheng | 2017-04-14 08:32

But it seems control must be added to a container first,since canvas layer has no such API

alexzheng | 2017-04-14 08:34

you’re right. I thought CanvasLayer inherited by CanvasItem.
I should edit answer a little bit.

volzhs | 2017-04-14 11:35

Oh, I’m sorry to say this API does not work as expected, when the test_width is not the same as width in project settings.

This may be caused by the camera in the center and the viewport has transformed.

the method I show in the question can handle these different test size.

alexzheng | 2017-04-14 14:57