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;
    }        
}

Yeah i based mine off this guys nice C# implementation. Not its pretty flexible if you extend Tree and use _input() (gdscript btw).

For example I used the below to custom highlight some tree items with a plain red while excluding some.

class_name FileTabTree
extends Tree

var prev_item_highlight: TreeItem
var hover_process: bool = false
var hover_exempts: Array[TreeItem]

func _ready() → void:
mouse_entered.connect(on_mouse_entered)
mouse_exited.connect(on_mouse_exit)

func _input(event: InputEvent) → void:
if event is InputEventMouseMotion:
if hover_process:
_custom_highlight()

func on_mouse_entered() → void:
hover_process = true

func on_mouse_exit() → void:
hover_process = false
_unhover_previous()

func _custom_highlight() → void:
var item: TreeItem = get_item_at_position(get_local_mouse_position())
if item != null and item not in hover_exempts:
_unhover_previous()
item.set_custom_bg_color(0,Color(0.804, 0.0, 0.0, 0.957))
prev_item_highlight = item
else:
_unhover_previous()

func _unhover_previous() → void:
if prev_item_highlight != null and is_instance_valid(prev_item_highlight):
prev_item_highlight.set_custom_bg_color(0,Color(0.0, 0.0, 0.0, 0.0))