How to use CELL_MODE_CUSTOM in TreeItem?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Andika Satya Wisnu
:warning: Old Version Published before Godot 3 was released.

Hi guys, i want to use CELL_MODE_CUSTOM in TreeItem but i dont know how to start customing. This is the docs about TreeItem http://docs.godotengine.org/en/stable/classes/class_treeitem.html

I’ve figure out how to use

CELL_MODE_STRING = 0
CELL_MODE_CHECK = 1
CELL_MODE_RANGE = 2
CELL_MODE_RANGE_EXPRESSION = 3
CELL_MODE_ICON = 4

but for CELL_MODE_CUSTOM i just dont know, i need this mode.

:bust_in_silhouette: Reply From: mollusca

You mainly use them to display popups like for example a drop down menu or a position entry popup. You connect the custom_popup_edited signal, then in the signal handling function you can use get_edited() to check which TreeItem was clicked and get_custom_popup_rect() to position the popup that you want to display.

Note that the cell itself needs to be set as editable item.set_editable(0, true) before the tree could emit custom_popup_edited signal.

Xrayez | 2019-07-29 20:17

:bust_in_silhouette: Reply From: elamaunt

To use the custom mode you have to switch the column of the TreeItem to CELL_MODE_CUSTOM mode and provide a custom render callback by calling set_custom_draw for every TreeItem

var tree = Tree.new()
var item = tree.create_item()
item.set_cell_mode(0, CELL_MODE_CUSTOM)
item.set_custom_draw(0, callbackHolder, "customDraw")

# this method must be in the callbackHolder 
func customDraw(item, rect):
    tree.draw_rect(rect, Color.new(1,1,1))  # draws your color as cell's background

In this method you may use drawing methods from this tutorial.

It is important to call the draw_something method on the Tree object, not on the callback holder itself. Any Godot’s object may be used as a callbackHolder.