Tree
Control to show a tree of items.
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 objects to create the structure. They have a single root but multiple roots can be simulated if a dummy hidden root is added.
To iterate over all the TreeItem objects in a object, use and TreeItem.get_children after getting the root through . You can use Object.free on a to remove it from the Tree
.
Incremental search: Like ItemList and , 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
Methods
void | ( ) |
( Object parent=null, idx=-1 ) | |
( ) | |
void | |
get_button_id_at_position ( position ) const | |
( Vector2 position ) const | |
get_column_title ( column ) const | |
( int column ) const | |
get_custom_popup_rect ( ) const | |
get_drop_section_at_position ( position ) const | |
( ) const | |
( ) const | |
( Object item, column=-1 ) const | |
( Vector2 position ) const | |
get_next_selected ( from ) | |
( ) const | |
( ) | |
( ) const | |
( ) const | |
( ) const | |
void | scroll_to_item ( item ) |
void | set_column_expand ( column, bool expand ) |
void | ( int column, min_width ) |
void | set_column_title ( column, String title ) |
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
Signals
Emitted when a button on the tree was pressed (see ).
- cell_selected ( )
Emitted when a cell is selected.
- column_title_pressed ( int column )
Emitted when a column’s title is pressed.
- custom_popup_edited ( arrow_clicked )
Emitted when a cell with the TreeItem.CELL_MODE_CUSTOM is clicked to be edited.
- empty_rmb ( position )
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.
- item_collapsed ( item )
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 ( position )
Emitted when an item is selected with the right mouse button.
- item_selected ( )
Emitted when an item is selected.
Emitted instead of if select_mode
is .
- 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 .
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 , these drop sections halves the height and stays on top / bottom accordingly.
- allow_reselect
Default |
|
Setter | set_allow_reselect(value) |
Getter | get_allow_reselect() |
If true
, the currently selected cell may be selected again.
- bool allow_rmb_select
Default |
|
Setter | set_allow_rmb_select(value) |
Getter | get_allow_rmb_select() |
If true
, a right mouse button click can select items.
- column_titles_visible
Default |
|
Setter | set_column_titles_visible(value) |
Getter | are_column_titles_visible() |
If true
, column titles are visible.
Default |
|
Setter | set_columns(value) |
Getter | get_columns() |
The number of columns.
- int drop_mode_flags
Default |
|
Setter | set_drop_mode_flags(value) |
Getter | get_drop_mode_flags() |
The drop mode as an OR combination of flags. See constants. Once dropping is done, reverts to DROP_MODE_DISABLED. Setting this during is recommended.
This controls the drop sections, i.e. the decision and drawing of possible drop locations based on the mouse position.
- bool hide_folding
If true
, the folding arrow is hidden.
- hide_root
Default |
|
Setter | set_hide_root(value) |
Getter | is_root_hidden() |
If true
, the tree’s root is hidden.
- SelectMode select_mode
Default |
|
Setter | set_select_mode(value) |
Getter | get_select_mode() |
Allows single or multiple selection. See the constants.
Method Descriptions
- void clear ( )
Clears the tree. This removes all items.
- create_item ( Object parent=null, idx=-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 idx
th child of parent, or it will be the last child if there are not enough siblings.
- 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 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 ( Vector2 position ) const
Returns the button id at position
, or -1 if no button is there.
- get_column_at_position ( Vector2 position ) const
Returns the column index at position
, or -1 if no item is there.
- get_column_title ( int column ) const
Returns the column’s title.
- get_column_width ( int column ) const
Returns the column’s width in pixels.
- 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 ( 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 for a description of each drop section.
To get the item which the returned drop section is relative to, use get_item_at_position.
- get_edited ( ) const
Returns the currently edited item. Can be used with item_edited to get the item that was modified.
func _ready():
$Tree.connect("item_edited", self, "on_Tree_item_edited")
func on_Tree_item_edited():
- get_edited_column ( ) const
Returns the column for the currently edited item.
Returns the rectangle area for the specified . If column
is specified, only get the position and size of that column, otherwise get the rectangle containing all columns.
- TreeItem get_item_at_position ( position ) const
Returns the tree item at the specified position (relative to the tree origin position).
- TreeItem get_next_selected ( from )
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 ( ) const
Returns the last pressed button’s index.
- TreeItem get_root ( )
Returns the tree’s root item, or null
if the tree is empty.
- get_scroll ( ) const
Returns the current scrolling position.
- TreeItem get_selected ( ) const
Returns the currently focused item, or null
if no item is focused.
In and SELECT_SINGLE modes, the focused item is same as the selected item. In 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 ( ) 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 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 .
- void scroll_to_item ( Object item )
Causes the Tree
to jump to the specified .
- void set_column_expand ( int column, 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 .
- void set_column_min_width ( int column, 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 ( column, String title )
Sets the title of a column.
Theme Property Descriptions
- Color custom_button_font_highlight
Default |
|
Text for a TreeItem.CELL_MODE_CUSTOM mode cell when it’s hovered.
Default |
|
used to draw possible drop locations. See DropModeFlags constants for further description of drop locations.
- font_color
Default |
|
Default text Color of the item.
- font_color_selected
Default |
|
Text Color used when the item is selected.
- guide_color
Default |
|
Color of the guideline.
- relationship_line_color
Color of the relationship lines.
- title_button_color
Default |
|
Default text Color of the title button.
- button_margin
Default |
|
The horizontal space between each button in a cell.
- int draw_guides
Default |
|
Draws the guidelines if not zero, this acts as a boolean. The guideline is a horizontal line drawn at the bottom of each item.
- draw_relationship_lines
Default |
|
Draws the relationship lines if not zero, this acts as a boolean. Relationship lines are drawn at the start of child items to show hierarchy.
- int hseparation
Default |
|
The horizontal space between item cells. This is also used as the margin at the start of an item when folding is disabled.
- item_margin
Default |
|
The horizontal margin at the start of an item. This is used when folding is enabled for the item.
- int scroll_border
Default |
|
The maximum distance between the mouse cursor and the control’s border to trigger border scrolling when dragging.
- scroll_speed
The speed of border scrolling.
- int vseparation
Default |
|
The vertical padding inside each item, i.e. the distance between the item’s content and top/bottom border.
- font
Font of the item’s text.
- title_button_font
Font of the title button’s text.
- arrow
The arrow icon used when a foldable item is not collapsed.
- Texture arrow_collapsed
The arrow icon used when a foldable item is collapsed.
- checked
The check icon to display when the TreeItem.CELL_MODE_CHECK mode cell is checked.
- select_arrow
The arrow icon to display for the TreeItem.CELL_MODE_RANGE mode cell.
- unchecked
The check icon to display when the TreeItem.CELL_MODE_CHECK mode cell is unchecked.
- updown
The updown arrow icon to display for the TreeItem.CELL_MODE_RANGE mode cell.
- bg
Default StyleBox for the Tree
, i.e. used when the control is not being focused.
- bg_focus
StyleBox used when the Tree
is being focused.
- button_pressed
StyleBox used when a button in the tree is pressed.
- cursor
StyleBox used for the cursor, when the Tree
is being focused.
- cursor_unfocused
StyleBox used for the cursor, when the Tree
is not being focused.
- custom_button
Default StyleBox for a mode cell.
- StyleBox custom_button_hover
for a TreeItem.CELL_MODE_CUSTOM mode cell when it’s hovered.
- custom_button_pressed
StyleBox for a mode cell when it’s pressed.
- StyleBox selected
for the selected items, used when the is not being focused.
- StyleBox selected_focus
for the selected items, used when the Tree
is being focused.
- StyleBox title_button_hover
used when the title button is being hovered.
- StyleBox title_button_normal
- title_button_pressed
StyleBox used when the title button is being pressed.