Godot Version
v4.2.1 Win64
Question
Hello everybody!
I’m trying to create something like a blueprint system for a game where you create vehicles, and to make it easier to group parts together I need to use a Tree control node, which works perfectly for this scenario, however, although I have the ability to rename the tree items I can’t reorder them which is very strange, considering the Tree control node has a DROP_MODE_FLAGS option, but no way to access what item index the mouse is hovering over and there seems to be nothing online for dragging things around except for entire control nodes, not TreeItems.
If anyone knows a way to drag and drop TreeItems to reorganise them within the Tree control node, that would be great!
Thank you for your time!
You will need to implement the drag and drop functionality yourself. I have implemented it in a project before, the code goes like this:
func _get_drag_data(_at_position: Vector2) -> Variant:
var items := []
var next: TreeItem = get_next_selected(null)
var v := VBoxContainer.new()
while next:
if get_root() == next.get_parent():
items.append(next)
var l := Label.new()
l.text = next.get_text(0)
v.add_child(l)
next = get_next_selected(next)
set_drag_preview(v)
return items
func _can_drop_data(at_position: Vector2, data: Variant) -> bool:
drop_mode_flags = Tree.DROP_MODE_INBETWEEN
var drop_section := get_drop_section_at_position(at_position)
if drop_section == -100:
return false
var item := get_item_at_position(at_position)
if item in data:
return false
return true
func _drop_data(at_position: Vector2, data: Variant) -> void:
var drop_section := get_drop_section_at_position(at_position)
var other_item := get_item_at_position(at_position)
var sprite_groups := []
for item in data:
sprite_groups.append(item.get_meta("sprite_group"))
if drop_section == -1:
group_moved.emit(sprite_groups, other_item.get_index())
else:
group_moved.emit(sprite_groups, other_item.get_index() + 1)
for i in data.size():
var item := data[i] as TreeItem
if drop_section == -1:
item.move_before(other_item)
elif drop_section == 1:
if i == 0:
item.move_after(other_item)
else:
item.move_after(data[i - 1])
There’s some extra stuff in there that’s project specific, like the signals and the metadata, you can just ignore that.
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!