TreeItem mouse hover

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

I’ve been trying to find a way to implement a mouse enter/leave event for a list of Tree Items within a Tree parent (select mode set to row), with no luck so far. From what I can see the only enter/leave signal resides in the parent control (using its rect).

Does anyone know how I can resolve this issue? Or at the very least implement a custom event?

Thanks

:bust_in_silhouette: Reply From: exuin

If you look at the docs for TreeItem, you can see that it only inherits from Object, so it doesn’t have any mouse signals. You’ll either have to try using the Tree functions or making your own tree with other controls.

Ah i see. I was hoping that wasn’t the case. Thanks for the info

Motenrain | 2021-02-26 16:56

:bust_in_silhouette: Reply From: Motenrain

Ok so I figured out how to achieve this effect (Mono C#).

Attach a script to your tree

Add a TreeItem var.

TreeItem hover

Declare a trigger for both mouse_entered and mouse_exited:

    Connect("mouse_entered",this,"Mouse_Entered");
    Connect("mouse_exited",this,"Mouse_Exited");

If other unrelated code exists in the _process function add a bool and swap its state in these triggers. In my case the process only has one purpose so I enabled/disable the function.

private void Mouse_Entered(){
    SetProcess(true);
}

private void Mouse_Exited(){
    SetProcess(false);
}

Then add this to process: (use a bool check if not needed).

 public override void _Process(float delta)
{
    TreeItem temp = GetItemAtPosition(GetLocalMousePosition());
    if (temp != hover){
        for (int i = 0; i < Columns; i++)
        {
            if (hover != null) hover.ClearCustomColor(i);
            if (temp != null) temp.SetCustomColor(i, Colors.Red); 
        }
        hover = temp;
    }        
}