Dragging TreeItems within Tree control node

Hi !

I know I’m necroposting right now, but since I was looking for this answer and found your post, I think other people might find it too.

Thanks for your answer, I removed the project specific stuff and changed it to be able to also drag non-root elements. Moving items as children of another item wasn’t obvious. I’m using a trick where I create and then remove a child, it works pretty well since it let you use “move_after” instead of destroying and re-creating the items (which looses selection status).

I hope this will help someone as your post helped me!

extends Tree

func _get_drag_data(at_position : Vector2) -> Variant:
	var items = []
	var next_selected : TreeItem = get_next_selected(get_root())
	var preview_vertical_container = VBoxContainer.new()
	
	while next_selected: # get_selected only get the first_selected, must get all selected recursively
		items.append(next_selected)
		var preview_item_label = Label.new()
		preview_item_label.text = next_selected.get_text(0)
		preview_vertical_container.add_child(preview_item_label)
		
		next_selected = get_next_selected(next_selected)
	
	set_drag_preview(preview_vertical_container)
	
	return items

func _can_drop_data(at_position : Vector2, data : Variant) -> bool:
	drop_mode_flags = Tree.DROP_MODE_INBETWEEN + Tree.DROP_MODE_ON_ITEM # Enables the "on item", "above item" and "below item" drop sections
	var drop_section : int = get_drop_section_at_position(at_position)
	
	if drop_section == -100:
		return false
	
	var item : TreeItem = get_item_at_position(at_position)
	
	# Can't drop on itself
	if item in data:
		return false
	
	return true

func _drop_data(at_position : Vector2, data : Variant) -> void:
	
	var drop_section : int = get_drop_section_at_position(at_position)
	var dropped_on_item : TreeItem = get_item_at_position(at_position)
	
	if drop_section == -100:
		return
	
	while not data.is_empty() and not data[0] is TreeItem:
		data.pop_front()
	
	if not data is Array or data.is_empty():
		return
	
	# Place first selected item
	
	var previous_selected_item : TreeItem = data[0]
	
	match drop_section:
		-1: # the position is above the item
			previous_selected_item.move_before(dropped_on_item)
		0: # the position is on the item
			var fake_item : TreeItem = dropped_on_item.create_child()
			previous_selected_item.move_after(fake_item)
			fake_item.free()
		1: # the position is below the item, when the item has no children
			previous_selected_item.move_after(dropped_on_item)
		2: # the position is below the item, when the item has children
			previous_selected_item.move_before(dropped_on_item.get_child(0))
	
	# Place other selected items after the first one
	
	for i in range(1, data.size()):
		if not data[i] is TreeItem:
			continue
		
		var selected_item : TreeItem = data[i]
		
		# Check if an ancestor is selected already
		# if ancestor already moved, no need to move its children
		var ancestor = selected_item.get_parent()
		var ancestor_selected = false
		
		while ancestor != get_root():
			if ancestor in data:
				ancestor_selected = true
				break
			ancestor = ancestor.get_parent()
		
		# Move to the last moved item
		if not ancestor_selected:
			selected_item.move_after(previous_selected_item)
			
			previous_selected_item = selected_item

If you find any issue anyone, let me know.

Have a nice day everyone!