godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.13k stars 21.19k forks source link

Editor viewport global_canvas_transform auto reset when mouse hover on viewport #57898

Open yikescloud opened 2 years ago

yikescloud commented 2 years ago

Godot version

3.4.2 stable[45eaa2daf] steam version

System information

windows 10

Issue description

When use set_global_canvas_transform() on editor viewport, the editor viewport transform immediately EditorTransform tscn - EditorCanvasTransform - Godot Engine-2022-02-10 16_20_52 But when any input(keyboard pressed, or mouse hovered on canvas editor), it will be reset as before. And the transform only change the item on canvas but not the hints( grids, axis..etc)

Steps to reproduce

Add a tool script then call get_tree().edited_scene_root.get_viewport().set_global_canvas_transform()

Minimal reproduction project

EditorCanvasTransform.zip

yikescloud commented 2 years ago

Finally I figure it out that you cannot change canvas item editor position by set the viewport's global_canvas_transform. I think the document should be point it out maybe.

Canvas Item Editor hierarchy

For anyone who wants change editor center by code:

func set_editor_centor(var _center: Vector2):#set editor 2d center
    var v = get_tree().edited_scene_root.get_viewport()
    var t = v.global_canvas_transform
    var s = t.get_scale()
    var _center_offset = (v.size/s.x)/2#canvas editor center offset
    _center = _center - _center_offset

    var p = v.get_parent().get_parent()
    for i in p.get_children():
        print(i.get_class())
        if i.get_class() == "CanvasItemEditorViewport":
            for j in i.get_children():
                if j.get_class() == "HScrollBar":
                    j.set_value(_center.x)
                elif j.get_class() == "VScrollBar":
                    j.set_value(_center.y)
Calinou commented 2 years ago

See also https://github.com/godotengine/godot-proposals/issues/1302.

KoBeWi commented 10 months ago

Reopening. #68696 was merged and changing global_canvas_transform is supposedly the recommended way to access canvas transform (and modify it). However this issue still persists.

Not sure how it should be solved.

KoBeWi commented 10 months ago

In the meantime, here's a more robust solution for setting both zoom and position:

var editor = EditorInterface.get_base_control().find_child("@CanvasItemEditorViewport*", true, false)

var zoomer = editor.find_child("@EditorZoomWidget*", true, false)
zoomer.set_zoom(zoom)
zoomer.zoom_changed.emit(zoom)

var scroll = editor.find_child("@HScrollBar*", true, false)
scroll.value = pos.x
scroll = editor.find_child("@VScrollBar*", true, false)
scroll.value = pos.y