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.

  1. func _ready():
  2. var tree = Tree.new()
  3. var root = tree.create_item()
  4. tree.set_hide_root(true)
  5. var child1 = tree.create_item(root)
  6. var child2 = tree.create_item(root)
  7. var subchild1 = tree.create_item(child1)
  8. subchild1.set_text(0, "Subchild1")

To iterate over all the TreeItem objects in a Tree object, use TreeItem.get_next and TreeItem.get_children after getting the root through get_root. You can use Object.free on a TreeItem to remove it from the Tree.

Properties

boolallow_reselectfalse
boolallow_rmb_selectfalse
intcolumns1
intdrop_mode_flags0
FocusModefocus_mode2 (parent override)
boolhide_foldingfalse
boolhide_rootfalse
boolrect_clip_contenttrue (parent override)
SelectModeselect_mode0

Methods

boolare_column_titles_visible ( ) const
voidclear ( )
TreeItemcreate_item ( Object parent=null, int idx=-1 )
voidensure_cursor_is_visible ( )
intget_column_at_position ( Vector2 position ) const
Stringget_column_title ( int column ) const
intget_column_width ( int column ) const
Rect2get_custom_popup_rect ( ) const
intget_drop_section_at_position ( Vector2 position ) const
TreeItemget_edited ( ) const
intget_edited_column ( ) const
Rect2get_item_area_rect ( Object item, int column=-1 ) const
TreeItemget_item_at_position ( Vector2 position ) const
TreeItemget_next_selected ( Object from )
intget_pressed_button ( ) const
TreeItemget_root ( )
Vector2get_scroll ( ) const
TreeItemget_selected ( ) const
intget_selected_column ( ) const
voidset_column_expand ( int column, bool expand )
voidset_column_min_width ( int column, int min_width )
voidset_column_title ( int column, String title )
voidset_column_titles_visible ( bool visible )

Theme Properties

Texturearrow 
Texturearrow_collapsed 
StyleBoxbg 
StyleBoxbg_focus 
intbutton_margin4
StyleBoxbutton_pressed 
Texturechecked 
StyleBoxcursor 
StyleBoxcursor_unfocused 
StyleBoxcustom_button 
Colorcustom_button_font_highlightColor( 0.94, 0.94, 0.94, 1 )
StyleBoxcustom_button_hover 
StyleBoxcustom_button_pressed 
intdraw_guides1
intdraw_relationship_lines0
Colordrop_position_colorColor( 1, 0.3, 0.2, 1 )
Fontfont 
Colorfont_colorColor( 0.69, 0.69, 0.69, 1 )
Colorfont_color_selectedColor( 1, 1, 1, 1 )
Colorguide_colorColor( 0, 0, 0, 0.1 )
inthseparation4
intitem_margin12
Colorrelationship_line_colorColor( 0.27, 0.27, 0.27, 1 )
intscroll_border4
intscroll_speed12
Textureselect_arrow 
StyleBoxselected 
StyleBoxselected_focus 
Colortitle_button_colorColor( 0.88, 0.88, 0.88, 1 )
Fonttitle_button_font 
StyleBoxtitle_button_hover 
StyleBoxtitle_button_normal 
StyleBoxtitle_button_pressed 
Textureunchecked 
Textureupdown 
intvseparation4

Signals

Emitted when a button on the tree was pressed (see TreeItem.add_button).


  • cell_selected ( )

Emitted when a cell is selected.


  • column_title_pressed ( int column )

Emitted when a column’s title is pressed.


  • custom_popup_edited ( bool arrow_clicked )

Emitted when a cell with the TreeItem.CELL_MODE_CUSTOM is clicked to be edited.


Emitted when the right mouse button is pressed in the empty space of the tree.


  • empty_tree_rmb_selected ( Vector2 position )

Emitted when the right mouse button is pressed if right mouse button selection is active and the tree is empty.


  • item_activated ( )

Emitted when an item’s label is double-clicked.


Emitted when an item is collapsed by a click on the folding arrow.


  • item_custom_button_pressed ( )

Emitted when a custom button is pressed (i.e. in a TreeItem.CELL_MODE_CUSTOM mode cell).


  • item_double_clicked ( )

Emitted when an item’s icon is double-clicked.


  • item_edited ( )

Emitted when an item is edited.


  • item_rmb_edited ( )

Emitted when an item is edited using the right mouse button.


  • item_rmb_selected ( Vector2 position )

Emitted when an item is selected with the right mouse button.


  • item_selected ( )

Emitted when an item is 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:

  • 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.

  • 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.

  • 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:

  • 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.

  • 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.

  • 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
Defaultfalse
Setterset_allow_reselect(value)
Getterget_allow_reselect()

If true, the currently selected cell may be selected again.


  • bool allow_rmb_select
Defaultfalse
Setterset_allow_rmb_select(value)
Getterget_allow_rmb_select()

If true, a right mouse button click can select items.


Default1
Setterset_columns(value)
Getterget_columns()

The number of columns.


  • int drop_mode_flags
Default0
Setterset_drop_mode_flags(value)
Getterget_drop_mode_flags()

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.


Defaultfalse
Setterset_hide_folding(value)
Getteris_folding_hidden()

If true, the folding arrow is hidden.


Defaultfalse
Setterset_hide_root(value)
Getteris_root_hidden()

If true, the tree’s root is hidden.


Default0
Setterset_select_mode(value)
Getterget_select_mode()

Allows single or multiple selection. See the SelectMode constants.

Method Descriptions

  • bool are_column_titles_visible ( ) const

Returns true if the column titles are being shown.


  • void clear ( )

Clears the tree. This removes all items.


Creates an item in the tree and adds it as a child of parent.

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 idxth child of parent, or it will be the last child if there are not enough siblings.


  • 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_column_at_position ( Vector2 position ) const

Returns the column index at position, or -1 if no item is there.


  • String get_column_title ( int column ) const

Returns the column’s title.


  • 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.


Returns the currently edited item. Can be used with item_edited to get the item that was modified.

  1. func _ready():
  2. $Tree.item_edited.connect(on_Tree_item_edited)
  3. func on_Tree_item_edited():
  4. print($Tree.get_edited()) # This item just got edited (e.g. checked).

  • int get_edited_column ( ) const

Returns the column for the currently edited item.


Returns the rectangle area for the specified item. If column is specified, only get the position and size of that column, otherwise get the rectangle containing all columns.


Returns the tree item at the specified position (relative to the tree origin position).


Returns the next selected item after the given one, or null if the end is reached.

If from is null, this returns the first selected item.


  • int get_pressed_button ( ) const

Returns the last pressed button’s index.


Returns the tree’s root item, or null if the tree is empty.


Returns the current scrolling position.


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.


  • int get_selected_column ( ) const

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.


  • void set_column_expand ( int column, bool expand )

If true, the column will have the “Expand” flag of Control. Columns that have the “Expand” flag will use their “min_width” in a similar fashion to Control.size_flags_stretch_ratio.


  • void set_column_min_width ( int column, int min_width )

Sets the minimum width of a column. Columns that have the “Expand” flag will use their “min_width” in a similar fashion to Control.size_flags_stretch_ratio.


  • void set_column_title ( int column, String title )

Sets the title of a column.


  • void set_column_titles_visible ( bool visible )

If true, column titles are visible.