Up to date
This page is up to date for Godot 4.0
.
If you still find outdated information, please open an issue.
Tree¶
Inherits: Control < CanvasItem < Node < Object
Control to show a tree of items.
Description¶
This shows a tree of items that can be selected, expanded and collapsed. The tree can have multiple columns with custom controls like text editing, 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 if a dummy hidden root is added.
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")
public override void _Ready()
{
var tree = new Tree();
TreeItem root = tree.CreateItem();
tree.HideRoot = true;
TreeItem child1 = tree.CreateItem(root);
TreeItem child2 = tree.CreateItem(root);
TreeItem subchild1 = tree.CreateItem(child1);
subchild1.SetText(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.
Properties¶
|
||
|
||
clip_contents |
|
|
|
||
|
||
|
||
|
||
focus_mode |
|
|
|
||
|
||
|
||
|
||
|
Methods¶
Theme Properties¶
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
Signals¶
button_clicked ( TreeItem item, int column, int id, int mouse_button_index )
Emitted when a button on the tree was pressed (see TreeItem.add_button).
cell_selected ( )
Emitted when a cell is selected.
check_propagated_to_item ( TreeItem item, int column )
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.
column_title_clicked ( int column, int mouse_button_index )
Emitted when a column's title is clicked with either @GlobalScope.MOUSE_BUTTON_LEFT or @GlobalScope.MOUSE_BUTTON_RIGHT.
custom_item_clicked ( int mouse_button_index )
Emitted when an item with TreeItem.CELL_MODE_CUSTOM is clicked with a mouse button.
custom_popup_edited ( bool arrow_clicked )
Emitted when a cell with the TreeItem.CELL_MODE_CUSTOM is clicked to be edited.
empty_clicked ( Vector2 position, int mouse_button_index )
Emitted when a mouse button is clicked in the empty space of the tree.
item_activated ( )
Emitted when an item is double-clicked, or selected with a ui_accept
input event (e.g. using Enter or Space on the keyboard).
item_collapsed ( TreeItem item )
Emitted when an item is collapsed by a click on the folding arrow.
item_edited ( )
Emitted when an item is edited.
item_icon_double_clicked ( )
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.
item_mouse_selected ( Vector2 position, int mouse_button_index )
Emitted when an item is selected with a mouse button.
item_selected ( )
Emitted when an item is selected.
multi_selected ( TreeItem item, int column, bool selected )
Emitted instead of item_selected
if select_mode
is SELECT_MULTI.
nothing_selected ( )
Emitted when a left mouse button click does not select any item.
Enumerations¶
enum SelectMode:
SelectMode SELECT_SINGLE = 0
Allows 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.
SelectMode SELECT_ROW = 1
Allows 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.
SelectMode SELECT_MULTI = 2
Allows 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.
enum DropModeFlags:
DropModeFlags DROP_MODE_DISABLED = 0
Disables all drop sections, but still allows to detect the "on item" drop section by get_drop_section_at_position.
Note: This is the default flag, it has no effect when combined with other flags.
DropModeFlags DROP_MODE_ON_ITEM = 1
Enables the "on item" drop section. This drop section covers the entire item.
When combined with DROP_MODE_INBETWEEN, this drop section halves the height and stays centered vertically.
DropModeFlags DROP_MODE_INBETWEEN = 2
Enables "above item" and "below item" drop sections. The "above item" drop section covers the top half of the item, and the "below item" drop section covers the bottom half.
When combined with DROP_MODE_ON_ITEM, these drop sections halves the height and stays on top / bottom accordingly.
Property Descriptions¶
bool allow_reselect = false
If true
, the currently selected cell may be selected again.
bool allow_rmb_select = false
If true
, a right mouse button click can select items.
bool column_titles_visible = false
If true
, column titles are visible.
int columns = 1
The number of columns.
int drop_mode_flags = 0
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.
bool enable_recursive_folding = true
If true
, recursive folding is enabled for this Tree. Holding down Shift while clicking the fold arrow collapses or uncollapses the TreeItem and all its descendants.
bool hide_folding = false
If true
, the folding arrow is hidden.
bool hide_root = false
If true
, the tree's root is hidden.
bool scroll_horizontal_enabled = true
If true
, enables horizontal scrolling.
bool scroll_vertical_enabled = true
If true
, enables vertical scrolling.
SelectMode select_mode = 0
void set_select_mode ( SelectMode value )
SelectMode get_select_mode ( )
Allows single or multiple selection. See the SelectMode constants.
Method Descriptions¶
void clear ( )
Clears the tree. This removes all items.
TreeItem create_item ( TreeItem parent=null, int index=-1 )
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.
void deselect_all ( )
Deselects all tree items (rows and columns). In SELECT_MULTI mode also removes selection cursor.
bool edit_selected ( )
Edits the selected tree item as if it was clicked. The item must be set editable with TreeItem.set_editable. Returns true
if the item could be edited. Fails if no item is selected.
void ensure_cursor_is_visible ( )
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.
int get_button_id_at_position ( Vector2 position ) const
Returns the button ID at position
, or -1 if no button is there.
int get_column_at_position ( Vector2 position ) const
Returns the column index at position
, or -1 if no item is there.
int get_column_expand_ratio ( int column ) const
Returns the expand ratio assigned to the column.
String get_column_title ( int column ) const
Returns the column's title.
TextDirection get_column_title_direction ( int column ) const
Returns column title base writing direction.
String get_column_title_language ( int column ) const
Returns column title language code.
int get_column_width ( int column ) const
Returns the column's width in pixels.
Rect2 get_custom_popup_rect ( ) const
Returns the rectangle for custom popups. Helper to create custom cell controls that display a popup. See TreeItem.set_cell_mode.
int get_drop_section_at_position ( Vector2 position ) const
Returns the drop section at position
, or -100 if no item is there.
Values -1, 0, or 1 will be returned for the "above item", "on item", and "below item" drop sections, respectively. See DropModeFlags for a description of each drop section.
To get the item which the returned drop section is relative to, use get_item_at_position.
TreeItem get_edited ( ) const
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).
public override void _Ready()
{
GetNode<Tree>("Tree").ItemEdited += OnTreeItemEdited;
}
public void OnTreeItemEdited()
{
GD.Print(GetNode<Tree>("Tree").GetEdited()); // This item just got edited (e.g. checked).
}
int get_edited_column ( ) const
Returns the column for the currently edited item.
Rect2 get_item_area_rect ( TreeItem item, int column=-1, int button_index=-1 ) const
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.
TreeItem get_item_at_position ( Vector2 position ) const
Returns the tree item at the specified position (relative to the tree origin position).
TreeItem get_next_selected (