[Tree / TreeItem] How to place a TreeItem between two TreeItems ?

The_MoyeThe_Moye Posts: 3Member

Hi everyone,

I'm desperately trying to deal with the Tree and TreeItem elements and I can't find a method to add a TreeItem between two existing TreeItems.
There are only two methods to move a TreeItem :

  • move_to_bottom()
  • move_to_top()

Am I missing something ?


Tags :

Best Answer

Answers

  • klaasklaas Posts: 21Member
    Accepted Answer

    Hi,
    i dont think your missing something .. its just very limited.

    https://github.com/godotengine/godot/issues/30116

    You could implement your own move commands by extending the tree.

  • The_MoyeThe_Moye Posts: 3Member

    Hi klaas, thank you for this answer.

    Yeah, that's what I figured. So I implemented two methods:

    func move_before(item_to_move : TreeItem, target_item : TreeItem) -> void:
    
        var nb_next_item : int = count_next(target_item)
    
        if !target_item.get_prev():
            item_to_move.move_to_top()
        else:
            item_to_move.move_to_bottom()
            item_to_move = target_item
    
            for item in range(0, nb_next_item):
                var next = item_to_move.get_next()
                item_to_move.move_to_bottom()
                item_to_move = next
    
    
    func move_after(item_to_move : TreeItem, target_item : TreeItem) -> void:
    
        var nb_prev_item : int = count_prev(target_item)
    
        if !target_item.get_next():
            item_to_move.move_to_bottom()
        else:
            item_to_move.move_to_top()
            item_to_move = target_item
    
            for item in range(0, nb_prev_item):
                var prev = item_to_move.get_prev()
                item_to_move.move_to_top()
                item_to_move = prev
    

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file