Getting mouse coordinates in tool mode

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

Hi,

I’m trying to get mouse coordinates in tool mode for an editor script.

My problems are:

  1. Input seems to have removed Input.get_mouse_pos()
  2. _input callback is not called in tool node.

Is there any other solution?

Cheers.

I don’t know for in-scene tool scripts, but you can get the input from an editor plugin instead: EditorPlugin — Godot Engine (latest) documentation in English

Zylann | 2016-08-18 23:45

:bust_in_silhouette: Reply From: GlaDOSik
  1. You’re right, but you can get mouse position from InputEvent (more precisely InputEventMouseButton) in _input or _input_event callback.

  2. It is working for me. If you want to receive _input callback, you have to first call set_process_input(true). The alternative is to use _input_event. In that case, you don’t have to call set_process_input(true), but it’s working only for Control nodes.

:bust_in_silhouette: Reply From: twinpixel

I solved the problem by adding a addon/plugin dock. “Output” is a label in the dock, that displays the mouse position.

If the current scene has a Position2D node, it will be placed at the mouse position and the displayed vector will be taken from there.

func _enter_tree():
  set_process_input(true)

func _input(event):
	var mp = get_tree().get_edited_scene_root().get_global_mouse_pos()
	get_node("Output").set_text(str(mp))

	#use Position2D if available
	get_tree().get_edited_scene_root().get_node("Position2D").set_pos(mp + Vector2(0, -40))
	var cursorpos = get_tree().get_edited_scene_root().get_node("Position2D").get_pos()
		cursorpos = cursorpos.snapped(Vector2(8, 8)) #snap to grid
		get_node("Output").set_text(str( cursorpos ))

It somehow works but depends strongly on the zoom level in the editor. So far, I’ve not found a way around that.