A control used to show a set of internal TreeItems in a hierarchical structure.
A control used to show a set of internal TreeItems in a hierarchical structure. The tree items can be selected, expanded and collapsed. The tree can have multiple columns with custom controls like LineEdits, buttons and popups. It can be useful for structured displays and interactions.
Trees are built via code, using TreeItem objects to create the structure. They have a single root, but multiple roots can be simulated with hide_root:
func _ready():
var tree = Tree.new()
var root = tree.create_item()
tree.hide_root = true
var child1 = tree.create_item(root)
var child2 = tree.create_item(root)
var subchild1 = tree.create_item(child1)
subchild1.set_text(0, "Subchild1")To iterate over all the TreeItem objects in a Tree object, use TreeItem.get_next() and TreeItem.get_first_child() after getting the root through get_root(). You can use Object.free() on a TreeItem to remove it from the Tree.
Incremental search: Like ItemList and PopupMenu, Tree supports searching within the list while the control is focused. Press a key that matches the first letter of an item's name to select the first item starting with the given letter. After that point, there are two ways to perform incremental search: 1) Press the same key again before the timeout duration to select the next item starting with the same letter. 2) Press letter keys that match the rest of the word before the timeout duration to match to select the item in question directly. Both of these actions will be reset to the beginning of the list if the timeout duration has passed since the last keystroke was registered. You can adjust the timeout duration by changing ProjectSettings.gui/timers/incremental_search_max_interval_msec.
If true, allows navigating the Tree with letter keys through incremental search.
If true, tree items with no tooltip assigned display their text as their tooltip. See also TreeItem.get_tooltip_text() and TreeItem.get_button_tooltip_text().
The number of columns.
Prints an error and does not allow setting the columns during mouse selection.
The drop mode as an OR combination of flags. See DropModeFlags constants. Once dropping is done, reverts to DROP_MODE_DISABLED. Setting this during Control._can_drop_data() is recommended.
This controls the drop sections, i.e. the decision and drawing of possible drop locations based on the mouse position.
If true, tree items will unfold when hovered over during a drag-and-drop. The delay for when this happens is dictated by dragging_unfold_wait_msec.
The way which scroll hints (indicators that show that the content can still be scrolled in a certain direction) will be shown.
Allows single or multiple selection. See the SelectMode constants.
If true, the scroll hint texture will be tiled instead of stretched. See scroll_hint_mode.
Clears the tree. This removes all items.
Prints an error and does not allow clearing the tree if called during mouse selection.
Creates an item in the tree and adds it as a child of parent, which can be either a valid TreeItem or null.
If parent is null, the root item will be the parent, or the new item will be the root itself if the tree is empty.
The new item will be the index-th child of parent, or it will be the last child if there are not enough siblings.
Prints an error and returns null if called during mouse selection, or if the parent does not belong to this tree.
Deselects all tree items (rows and columns). In SELECT_MULTI mode also removes selection cursor.
Edits the selected tree item as if it was clicked.
Either the item must be set editable with TreeItem.set_editable() or force_edit must be true.
Returns true if the item could be edited. Fails if no item is selected.
Makes the currently focused cell visible.
This will scroll the tree if necessary. In SELECT_ROW mode, this will not do horizontal scrolling, as all the cells in the selected row is focused logically.
Note: Despite the name of this method, the focus cursor itself is only visible in SELECT_MULTI mode.
Returns the internal canvas item designated for custom drawing. See TreeItem.set_custom_draw_callback().
Note: This canvas item clears automatically on each Tree draw call.
Returns the rectangle for custom popups. Helper to create custom cell controls that display a popup. See TreeItem.set_cell_mode().
Returns the drop section at position, as permitted by enabled DropModeFlags.
- -1 if the position is above the item. Typically used to insert as the item's previous sibling.
- 0 if the position is on the item. Typically used to insert as the item's last child.
- 1 if the position is below the item, when the item has no children. Typically used to insert as the item's next sibling. If the item does have children, this section is still reachable by hovering to the left of the item's collapse arrow, and below.
- 2 if the position is below the item, when the item has children. Typically used to insert as the item's first child.
- -100 if the position is not over any item, or no DropModeFlags are set.
See DropModeFlags for a description of each drop region. To get the item which the returned drop section refers to, use get_item_at_position().
Returns the currently edited item. Can be used with item_edited to get the item that was modified.
func _ready():
$Tree.item_edited.connect(on_Tree_item_edited)
func on_Tree_item_edited():
print($Tree.get_edited()) # This item just got edited (e.g. checked).Returns the rectangle area for the specified TreeItem. If column is specified, only get the position and size of that column, otherwise get the rectangle containing all columns. If a button index is specified, the rectangle of that button will be returned.
Returns the next selected TreeItem after the given one, or null if the end is reached.
If from is null, this returns the first selected item.
Returns the currently focused item, or null if no item is focused.
In SELECT_ROW and SELECT_SINGLE modes, the focused item is same as the selected item. In SELECT_MULTI mode, the focused item is the item under the focus cursor, not necessarily selected.
To get the currently selected item(s), use get_next_selected().
Returns the currently focused column, or -1 if no column is focused.
In SELECT_SINGLE mode, the focused column is the selected column. In SELECT_ROW mode, the focused column is always 0 if any item is selected. In SELECT_MULTI mode, the focused column is the column under the focus cursor, and there are not necessarily any column selected.
To tell whether a column of an item is selected, use TreeItem.is_selected().
Returns true if the column has enabled clipping (see set_column_clip_content()).
Returns true if the column has enabled expanding (see set_column_expand()).
Overrides the calculated minimum width of a column. It can be set to 0 to restore the default behavior. Columns that have the "Expand" flag will use their "min_width" in a similar fashion to Control.size_flags_stretch_ratio.
If true, the column will have the "Expand" flag of Control. Columns that have the "Expand" flag will use their expand ratio in a similar fashion to Control.size_flags_stretch_ratio (see set_column_expand_ratio()).
Sets the relative expand ratio for a column. See set_column_expand().
Sets the column title alignment. Note that HORIZONTAL_ALIGNMENT_FILL is not supported for column titles.
Sets the language code of the given column's title to language. This is used for line-breaking and text shaping algorithms. If language is empty, the current locale is used.
Emitted when a button on the tree was pressed (see TreeItem.add_button()).
Emitted when TreeItem.propagate_check() is called. Connect to this signal to process the items that are affected when TreeItem.propagate_check() is invoked. The order that the items affected will be processed is as follows: the item that invoked the method, children of that item, and finally parents of that item.
Emitted when a column's title is clicked with either MOUSE_BUTTON_LEFT or MOUSE_BUTTON_RIGHT.
Emitted when an item with TreeItem.CELL_MODE_CUSTOM is clicked with a mouse button.
Emitted when a cell with the TreeItem.CELL_MODE_CUSTOM is clicked to be edited.
Emitted when an item is double-clicked, or selected with a ui_accept input event (e.g. using Enter or Space on the keyboard).
Emitted when an item is expanded or collapsed by clicking on the folding arrow or through code.
Note: Despite its name, this signal is also emitted when an item is expanded.
Emitted when an item's icon is double-clicked. For a signal that emits when any part of the item is double-clicked, see item_activated.
Emitted instead of item_selected if select_mode is set to SELECT_MULTI.
SELECT_SINGLE = 0Allows selection of a single cell at a time. From the perspective of items, only a single item is allowed to be selected. And there is only one column selected in the selected item.
The focus cursor is always hidden in this mode, but it is positioned at the current selection, making the currently selected item the currently focused item.
SELECT_ROW = 1Allows selection of a single row at a time. From the perspective of items, only a single items is allowed to be selected. And all the columns are selected in the selected item.
The focus cursor is always hidden in this mode, but it is positioned at the first column of the current selection, making the currently selected item the currently focused item.
SELECT_MULTI = 2Allows selection of multiple cells at the same time. From the perspective of items, multiple items are allowed to be selected. And there can be multiple columns selected in each selected item.
The focus cursor is visible in this mode, the item or column under the cursor is not necessarily selected.
DROP_MODE_DISABLED = 0Disables all drop sections.
Note: This is the default flag, it has no effect when combined with other flags.
DROP_MODE_ON_ITEM = 1Enables the "on item" drop section. This drop section covers the entire item.
When combined with DROP_MODE_INBETWEEN, this drop section halves in height and stays centered vertically.
DROP_MODE_INBETWEEN = 2Enables "above item" and "below item" drop sections. The "above item" drop section covers the top half of the item, while the "below item" drop section covers the bottom half, and extends downward to the left of any children.
When combined with DROP_MODE_ON_ITEM, these drop sections halve in height and stay at the top and bottom respectively.