How to center-align the TextureRect used with set_drag_preview()?

Godot Version



Hi, I use the following code to display a dragged item preview:

    func _get_drag_data(at_position: Vector2) -> Variant:
		var drag_preview :=
		drag_preview.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
		drag_preview.texture = icon_texture_rect.texture
		drag_preview.custom_minimum_size = Vector2(80, 80)
		drag_preview.modulate = Color(1, 1, 1, 0.75)
		# return this item slot as the thing to be dragged
		return self

It works, but the dragged rectangle is being dragged by its top-left corner, while I’d like the mouse pointer to drag its middle:


Does anybody please have a suggestion on how to implement this? I have not found a suitable method in TextureRect and Control docs


This doesn’t work because whatever position you set for the drag_preview is overwritten by the engine’s implementation of set_drag_preview. You can make it work using two nodes, though:

func _get_drag_data(at_position: Vector2) -> Variant:
	var drag_preview :=

	var texture_rect :=
	texture_rect.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
	texture_rect.texture = icon_texture_rect.texture
	texture_rect.custom_minimum_size = Vector2(80, 80)
	texture_rect.position = -0.5 * texture_rect.custom_minimum_size
	texture_rect.modulate = Color(1, 1, 1, 0.75)



	return self
1 Like