Skip to content

Tree

Inherits: Control < CanvasItem < Entity < Object

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:

public override void OnStart()
{
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.

Type Name Default
bool allow_reselect false
bool allow_rmb_select false
bool allow_search true
bool auto_tooltip true
bool clip_contents true (overrides Control)
bool column_titles_visible false
int columns 1
int drop_mode_flags 0
bool enable_drag_unfolding true
bool enable_recursive_folding true
Control.FocusMode focus_mode 2 (overrides Control)
bool hide_folding false
bool hide_root false
ScrollHintMode scroll_hint_mode 0
bool scroll_horizontal_enabled true
bool scroll_vertical_enabled true
SelectMode select_mode 0
bool tile_scroll_hint false
Returns Method
void clear()
TreeItem create_item(parent: TreeItem = null, index: int = -1)
void deselect_all()
bool edit_selected(force_edit: bool = false)
void ensure_cursor_is_visible()
int get_button_id_at_position(position: Vector2) const
int get_column_at_position(position: Vector2) const
int get_column_expand_ratio(column: int) const
String get_column_title(column: int) const
HorizontalAlignment get_column_title_alignment(column: int) const
Control.TextDirection get_column_title_direction(column: int) const
String get_column_title_language(column: int) const
String get_column_title_tooltip_text(column: int) const
int get_column_width(column: int) const
RID get_custom_drawing_canvas_item() const
Rect2 get_custom_popup_rect() const
int get_drop_section_at_position(position: Vector2) const
TreeItem get_edited() const
int get_edited_column() const
Rect2 get_item_area_rect(item: TreeItem, column: int = -1, button_index: int = -1) const
TreeItem get_item_at_position(position: Vector2) const
TreeItem get_next_selected(from: TreeItem)
int get_pressed_button() const
TreeItem get_root() const
Vector2 get_scroll() const
TreeItem get_selected() const
int get_selected_column() const
bool is_column_clipping_content(column: int) const
bool is_column_expanding(column: int) const
void scroll_to_item(item: TreeItem, center_on_item: bool = false)
void set_column_clip_content(column: int, enable: bool)
void set_column_custom_minimum_width(column: int, min_width: int)
void set_column_expand(column: int, expand: bool)
void set_column_expand_ratio(column: int, ratio: int)
void set_column_title(column: int, title: String)
void set_column_title_alignment(column: int, title_alignment: HorizontalAlignment)
void set_column_title_direction(column: int, direction: Control.TextDirection)
void set_column_title_language(column: int, language: String)
void set_column_title_tooltip_text(column: int, tooltip_text: String)
void set_selected(item: TreeItem, column: int)

button_clicked

button_clicked(item: TreeItem, column: int, id: int, mouse_button_index: int)

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

cell_selected

cell_selected()

Emitted when a cell is selected.

check_propagated_to_item

check_propagated_to_item(item: TreeItem, column: int)

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

column_title_clicked(column: int, mouse_button_index: int)

Emitted when a column’s title is clicked with either MOUSE_BUTTON_LEFT or MOUSE_BUTTON_RIGHT.

custom_item_clicked

custom_item_clicked(mouse_button_index: int)

Emitted when an item with TreeItem.CELL_MODE_CUSTOM is clicked with a mouse button.

custom_popup_edited

custom_popup_edited(arrow_clicked: bool)

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

empty_clicked

empty_clicked(click_position: Vector2, mouse_button_index: int)

Emitted when a mouse button is clicked in the empty space of the tree.

item_activated

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

item_collapsed(item: TreeItem)

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.

item_edited

item_edited()

Emitted when an item is edited.

item_icon_double_clicked

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

item_mouse_selected(mouse_position: Vector2, mouse_button_index: int)

Emitted when an item is selected with a mouse button.

item_selected

item_selected()

Emitted when an item is selected.

multi_selected

multi_selected(item: TreeItem, column: int, selected: bool)

Emitted instead of item_selected if select_mode is set to SELECT_MULTI.

nothing_selected

nothing_selected()

Emitted when a left mouse button click does not select any item.

SelectMode

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.

DropModeFlags

enum DropModeFlags

DROP_MODE_DISABLED = 0

Disables all drop sections.

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 in 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, 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.

ScrollHintMode

enum ScrollHintMode

SCROLL_HINT_MODE_DISABLED = 0

Scroll hints will never be shown.

SCROLL_HINT_MODE_BOTH = 1

Scroll hints will be shown at the top and bottom.

SCROLL_HINT_MODE_TOP = 2

Only the top scroll hint will be shown.

SCROLL_HINT_MODE_BOTTOM = 3

Only the bottom scroll hint will be shown.

children_hl_line_color

Color children_hl_line_color (color) = Color(0.27, 0.27, 0.27, 1)

custom_button_font_highlight

Color custom_button_font_highlight (color) = Color(0.95, 0.95, 0.95, 1)

drop_on_item_color

Color drop_on_item_color (color) = Color(1, 1, 1, 1)

drop_position_color

Color drop_position_color (color) = Color(1, 1, 1, 1)

font_color

Color font_color (color) = Color(0.7, 0.7, 0.7, 1)

font_disabled_color

Color font_disabled_color (color) = Color(0.875, 0.875, 0.875, 0.5)

font_hovered_color

Color font_hovered_color (color) = Color(0.95, 0.95, 0.95, 1)

font_hovered_dimmed_color

Color font_hovered_dimmed_color (color) = Color(0.875, 0.875, 0.875, 1)

font_hovered_selected_color

Color font_hovered_selected_color (color) = Color(1, 1, 1, 1)

font_outline_color

Color font_outline_color (color) = Color(0, 0, 0, 1)

font_selected_color

Color font_selected_color (color) = Color(1, 1, 1, 1)

guide_color

Color guide_color (color) = Color(0.7, 0.7, 0.7, 0.25)

parent_hl_line_color

Color parent_hl_line_color (color) = Color(0.27, 0.27, 0.27, 1)

relationship_line_color

Color relationship_line_color (color) = Color(0.27, 0.27, 0.27, 1)

scroll_hint_color

Color scroll_hint_color (color) = Color(0, 0, 0, 1)

title_button_color

Color title_button_color (color) = Color(0.875, 0.875, 0.875, 1)

button_margin

int button_margin (constant) = 4

check_h_separation

int check_h_separation (constant) = 4

children_hl_line_width

int children_hl_line_width (constant) = 1

dragging_unfold_wait_msec

int dragging_unfold_wait_msec (constant) = 500

draw_guides

int draw_guides (constant) = 1

draw_relationship_lines

int draw_relationship_lines (constant) = 0

h_separation

int h_separation (constant) = 4

icon_h_separation

int icon_h_separation (constant) = 4

icon_max_width

int icon_max_width (constant) = 0

inner_item_margin_bottom

int inner_item_margin_bottom (constant) = 0

inner_item_margin_left

int inner_item_margin_left (constant) = 0

inner_item_margin_right

int inner_item_margin_right (constant) = 0

inner_item_margin_top

int inner_item_margin_top (constant) = 0

item_margin

int item_margin (constant) = 16

outline_size

int outline_size (constant) = 0

parent_hl_line_margin

int parent_hl_line_margin (constant) = 0

parent_hl_line_width

int parent_hl_line_width (constant) = 1

relationship_line_width

int relationship_line_width (constant) = 1

scroll_border

int scroll_border (constant) = 4

scroll_max_sticky_items

int scroll_max_sticky_items (constant) = 0

scroll_speed

int scroll_speed (constant) = 12

scrollbar_h_separation

int scrollbar_h_separation (constant) = 4

scrollbar_margin_bottom

int scrollbar_margin_bottom (constant) = -1

scrollbar_margin_left

int scrollbar_margin_left (constant) = -1

scrollbar_margin_right

int scrollbar_margin_right (constant) = -1

scrollbar_margin_top

int scrollbar_margin_top (constant) = -1

scrollbar_v_separation

int scrollbar_v_separation (constant) = 4

v_separation

int v_separation (constant) = 4

font

Font font (font)

title_button_font

Font title_button_font (font)

font_size

int font_size (font_size)

title_button_font_size

int title_button_font_size (font_size)

arrow

Texture2D arrow (icon)

arrow_collapsed

Texture2D arrow_collapsed (icon)

arrow_collapsed_mirrored

Texture2D arrow_collapsed_mirrored (icon)

checked

Texture2D checked (icon)

checked_disabled

Texture2D checked_disabled (icon)

indeterminate

Texture2D indeterminate (icon)

indeterminate_disabled

Texture2D indeterminate_disabled (icon)

scroll_hint

Texture2D scroll_hint (icon)

select_arrow

Texture2D select_arrow (icon)

unchecked

Texture2D unchecked (icon)

unchecked_disabled

Texture2D unchecked_disabled (icon)

updown

Texture2D updown (icon)

focus_sound

AudioStream focus_sound (sound)

item_hovered_sound

AudioStream item_hovered_sound (sound)

item_selected_sound

AudioStream item_selected_sound (sound)

button_hover

StyleBox button_hover (style)

button_pressed

StyleBox button_pressed (style)

cursor

StyleBox cursor (style)

cursor_unfocused

StyleBox cursor_unfocused (style)

custom_button

StyleBox custom_button (style)

custom_button_hover

StyleBox custom_button_hover (style)

custom_button_pressed

StyleBox custom_button_pressed (style)

focus

StyleBox focus (style)

hovered

StyleBox hovered (style)

hovered_dimmed

StyleBox hovered_dimmed (style)

hovered_selected

StyleBox hovered_selected (style)

hovered_selected_focus

StyleBox hovered_selected_focus (style)

panel

StyleBox panel (style)

selected

StyleBox selected (style)

selected_focus

StyleBox selected_focus (style)

title_button_hover

StyleBox title_button_hover (style)

title_button_normal

StyleBox title_button_normal (style)

title_button_pressed

StyleBox title_button_pressed (style)

allow_reselect

bool allow_reselect = false

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

allow_rmb_select

bool allow_rmb_select = false

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

bool allow_search = true

If true, allows navigating the Tree with letter keys through incremental search.

auto_tooltip

bool auto_tooltip = true

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().

column_titles_visible

bool column_titles_visible = false

If true, column titles are visible.

columns

int columns = 1

The number of columns.

Prints an error and does not allow setting the columns during mouse selection.

drop_mode_flags

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.

enable_drag_unfolding

bool enable_drag_unfolding = true

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.

enable_recursive_folding

bool enable_recursive_folding = true

If true, recursive folding is enabled for this Tree. Holding down Shift while clicking the fold arrow or using ui_right/ui_left shortcuts collapses or uncollapses the TreeItem and all its descendants.

hide_folding

bool hide_folding = false

If true, the folding arrow is hidden.

hide_root

bool hide_root = false

If true, the tree’s root is hidden.

scroll_hint_mode

ScrollHintMode scroll_hint_mode = 0

The way which scroll hints (indicators that show that the content can still be scrolled in a certain direction) will be shown.

scroll_horizontal_enabled

bool scroll_horizontal_enabled = true

If true, enables horizontal scrolling.

scroll_vertical_enabled

bool scroll_vertical_enabled = true

If true, enables vertical scrolling.

select_mode

SelectMode select_mode = 0

Allows single or multiple selection. See the SelectMode constants.

tile_scroll_hint

bool tile_scroll_hint = false

If true, the scroll hint texture will be tiled instead of stretched. See scroll_hint_mode.

clear

void clear()

Clears the tree. This removes all items.

Prints an error and does not allow clearing the tree if called during mouse selection.

create_item

TreeItem create_item(parent: TreeItem = null, index: int = -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.

Prints an error and returns null if called during mouse selection, or if the parent does not belong to this tree.

deselect_all

void deselect_all()

Deselects all tree items (rows and columns). In SELECT_MULTI mode also removes selection cursor.

edit_selected

bool edit_selected(force_edit: bool = false)

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.

ensure_cursor_is_visible

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.

get_button_id_at_position

int get_button_id_at_position(position: Vector2) const

Returns the button ID at position, or -1 if no button is there.

get_column_at_position

int get_column_at_position(position: Vector2) const

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

get_column_expand_ratio

int get_column_expand_ratio(column: int) const

Returns the expand ratio assigned to the column.

get_column_title

String get_column_title(column: int) const

Returns the column’s title.

get_column_title_alignment

HorizontalAlignment get_column_title_alignment(column: int) const

Returns the column title alignment.

get_column_title_direction

Control.TextDirection get_column_title_direction(column: int) const

Returns column title base writing direction.

get_column_title_language

String get_column_title_language(column: int) const

Returns column title language code.

get_column_title_tooltip_text

String get_column_title_tooltip_text(column: int) const

Returns the column title’s tooltip text.

get_column_width

int get_column_width(column: int) const

Returns the column’s width in pixels.

get_custom_drawing_canvas_item

RID get_custom_drawing_canvas_item() const

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.

get_custom_popup_rect

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().

get_drop_section_at_position

int get_drop_section_at_position(position: Vector2) const

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().

get_edited

TreeItem get_edited() const

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

public override void OnStart()
{
GetEntity<Tree>("Tree").ItemEdited += OnTreeItemEdited;
}
public void OnTreeItemEdited()
{
Orb.Print(GetEntity<Tree>("Tree").GetEdited()); // This item just got edited (e.g. checked).
}

get_edited_column

int get_edited_column() const

Returns the column for the currently edited item.

get_item_area_rect

Rect2 get_item_area_rect(item: TreeItem, column: int = -1, button_index: int = -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.

get_item_at_position

TreeItem get_item_at_position(position: Vector2) const

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

get_next_selected

TreeItem get_next_selected(from: TreeItem)

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.

get_pressed_button

int get_pressed_button() const

Returns the last pressed button’s index.

get_root

TreeItem get_root() const

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

get_scroll

Vector2 get_scroll() const

Returns the current scrolling position.

get_selected

TreeItem get_selected() const

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().

get_selected_column

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().

is_column_clipping_content

bool is_column_clipping_content(column: int) const

Returns true if the column has enabled clipping (see set_column_clip_content()).

is_column_expanding

bool is_column_expanding(column: int) const

Returns true if the column has enabled expanding (see set_column_expand()).

scroll_to_item

void scroll_to_item(item: TreeItem, center_on_item: bool = false)

Causes the Tree to jump to the specified TreeItem.

set_column_clip_content

void set_column_clip_content(column: int, enable: bool)

Allows to enable clipping for column’s content, making the content size ignored.

set_column_custom_minimum_width

void set_column_custom_minimum_width(column: int, min_width: int)

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.

set_column_expand

void set_column_expand(column: int, expand: bool)

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()).

set_column_expand_ratio

void set_column_expand_ratio(column: int, ratio: int)

Sets the relative expand ratio for a column. See set_column_expand().

set_column_title

void set_column_title(column: int, title: String)

Sets the title of a column.

set_column_title_alignment

void set_column_title_alignment(column: int, title_alignment: HorizontalAlignment)

Sets the column title alignment. Note that @GlobalScope.HORIZONTAL_ALIGNMENT_FILL is not supported for column titles.

set_column_title_direction

void set_column_title_direction(column: int, direction: Control.TextDirection)

Sets column title base writing direction.

set_column_title_language

void set_column_title_language(column: int, language: String)

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.

set_column_title_tooltip_text

void set_column_title_tooltip_text(column: int, tooltip_text: String)

Sets the column title’s tooltip text.

set_selected

void set_selected(item: TreeItem, column: int)

Selects the specified TreeItem and column.