Hi, everyone.

I am trying to find a way to add a DrawString to the top left of the editor 2D viewport, so that no matter where I view, it still stays at the top left, just like the "center view" and "zoom in" and "zoom out" buttons. Unfortunately, so far, I've only been able to get the DrawString to a fixed position in the 2D world, so that when I look in a different area of the map, I can't see it anymore.

  • xyz replied to this.

    DeanJGrey In a tool script get the 2d viewport origin form EditorInterface. The negative value of this position will always be at the top left corner of the viewport.

    @tool
    extends Node2D
    func _process(delta):
    	$someNode.global_position = -EditorInterface.get_editor_viewport_2d().global_canvas_transform.origin
    6 days later

    Thank you for your answer. Your answer gets me almost there, but not quite. When moving the viewport, the element tracks with it a little bit, but not 100%.

    Here is a video to demonstrate:

    Do you have any idea how to make the element track 100% with the screen movement?

    • xyz replied to this.

      DeanJGrey You need to count in viewport zoom as well. If you want to keep the apparent scale of the object independent of zoom the most elegant way is to just assign it viewport's inverse matrix:

      	var t = EditorInterface.get_editor_viewport_2d().global_canvas_transform
      	t.origin -= Vector2(64, 64) # add some offset from origin
      	$someNode.transform = t.affine_inverse()

      You're a legend. That did it!

      Here is the code in C#, adapted from xyz's code:
      var screenPositionOffset = new Vector2 (128, 128); // xy position you want the element to be on screen
      var globalCanvasTransform = EditorInterface.Singleton.GetEditorViewport2D ().GlobalCanvasTransform;
      globalCanvasTransform.Origin -= screenPositionOffset;
      var screenPosition = globalCanvasTransform.AffineInverse ().Origin;