Using buttons in Tree panels

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By yokopoko

I could add buttons to TreeItem items with the add_button() method, but then how am I supposed to use those buttons?

Tree has a get_pressed_button() method but it returns the same number every time, if I press different buttons in different rows and columns.

Pic

public void UpdateQueueTree()
{
	queue.Clear();
	queue.Columns = 4;
	var r = queue.CreateItem();
	queue.SetColumnTitlesVisible(true);
	queue.SetColumnTitle(0, "Queue");
	queue.SetColumnMinWidth(0, 5);
	queue.SetColumnTitle(1, "N.");
	foreach (var item in c.Production.queue.items)
	{
		var p = queue.CreateItem(r);
		p.SetText(0, buildingName(item.id));
		p.SetExpandRight(0, true);
		p.SetText(1, item.quantity.ToString());
		p.SetTextAlign(1, (TreeItem.TextAlign)1);
		p.AddButton(2, Res.treeButtonPlus);
		p.AddButton(3, Res.treeButtonMinus);
	}
}

void QueueButtons(InputEvent @event)
{
	if (@event is InputEventMouseButton)
	{
		InputEventMouseButton ev = (InputEventMouseButton)@event;
		if (ev.ButtonIndex == (int)ButtonList.Left && ev.Pressed)
		{
			GD.Print("pressed: " + queue.GetPressedButton());
		}
	}
}
:bust_in_silhouette: Reply From: Sersch

The button id on its own doesn’t help much, you’ll have to combine it with the item to find out which button has been pressed.

The button_pressed() signal of a Tree uses three arguments: item, column and id.

You assign each of your four elements (title, quantity, plus, minus) to its own column. This causes the two buttons to both have the id 0, because they are both the first item in their column.

I don’t know what’s happening inside of your GetPressedButton() function, so I don’t know where your values come from. But it should work if you take the button_pressed()'s item and column values into account to know which button of which item has been pressed.