How can I select a node by clicking the rectangle it draws in the editor?

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

So, I’m currently making a plugin, where there’s is a custom node that is a tool and has a simple function to draw a rectangle using the _draw() function.

What I want to know is how can I make so that the node gets selected when I click the rectangle in the editor?

I probably have to use EditorPlugin and EditorInterface, but there aren’t any good tutorials about how to use them.

I don’t know anything about plugins, but You will not be able to select anything with just draw method alone. You will have to resort to Area2D or Rect2 check anyway.

Inces | 2022-02-27 08:58

:bust_in_silhouette: Reply From: Zylann

I am curious about this as well. Internally, all nodes inheriting CanvasItem have a bunch of methods they can override to specify their selection rectangle. Sadly there doesnt seem to be a particular API exposed for this. And the fact Godot doesn’t just support it from the drawn shapes suggests it is a missing feature.

Actually, after I asked this question here, I started to search about this rectangle selection some nodes has (Sprite, CollisionShape2D, etc), and ended up reading the source code.
I’m going to skip parts of my story, but I did discover the _edit_use_rect() and _edit_get_rect() functions. In fact gdscrypt has those method (you can verify with get_method_list()) but sadly they doesn’t seem to funtion, at least yet.
There’s a pull request in github that talks exacly about this, and I hope for this feature to get implemented in Godot 4.
You probably knew about this stuff but I just wanted to share my discovery, and my frustation of wasting a whole day trying to use those functions not knowing that they do nothing.

Yuu | 2022-03-06 04:04