How to use mouse event, then pass to another control?

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

I have a few controls on top of each other. I want to trigger all the click events of each layer. The problem is, when the top layer gets the click event, it marks it as “handled”. I want to use the click event and process some things, then have the next control under the top control also get the mouse event and process it. Then pass it again.

How do I do this?

:bust_in_silhouette: Reply From: njamster

You can set the mouse_filter-property of your Control-nodes to “Pass”. InputEvents will be propoagated further up the tree then, unless explicitly stopped by you calling get_tree().set_input_as_handled() or accept_event().

So if your tree looks like this:

- Main
  - Control1
    - Control2

and Control1 and Control2 overlap in a certain area, clicking that area will first send an InputEvent to Control2, then Control1, then Main (“up the tree”).

However, if your tree looks like this:

- Main
  - Control1
  - Control2

then clicking the overlap area will only send an InputEvent to Control2 and Main, but not to Control2 (as this would be up the tree and then down again). So if you want the latter case to work, you (unfortunately) have to code that yourself.

THat is my question. How do you “code it” yourself?

ondesic | 2020-06-15 13:45

Well, that depends a great deal on what exactly you’re trying to do. The most simple solution would be attaching this script to each of your Controls:

extends Control

func _input(event):
    if event is InputEventMouseButton and event.pressed:
        if get_rect().has_point(event.global_position):
            print("Click")

njamster | 2020-06-15 16:16