How to detect click outside the element?

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

How to detect the click outside the UI element. Say I have an ItemList which is 1/3 of the screen and appears on top of the screen in the center. I want to hide it when the user clicks on the empty space, but I can’t find a way to check if the click position intersects with the element

“but I can’t find a way to check if the click position intersects with the element”
Why? Give little more details pls.

LordViperion | 2019-02-27 23:00

I don’t know what details do you want to know. I told almost everything that it related to the question, also it is a 2D game.

jackash | 2019-02-27 23:07

I think you could use _unhandled_input function for this… as the item list is a gui element, I think that clicking on it would do normal gui handling and the event won’t reach the _unhandled_input function. Then, inside that function you check if event is the mouse click, and if it is you hide the item list…

p7f | 2019-02-27 23:14

Could try, but why is it that complicated, I mean why should we rely on the event not reaching some handler?

jackash | 2019-02-27 23:17

Actually, in my opinion is more complicated to detect wether you clicked specifically outside a gui element. _unhandled_input is the way you should handle non gui events… if you wait a couple hours, may be I can do an example

p7f | 2019-02-27 23:39

(this comment is moved to an answer)

wombatstampede | 2019-02-28 16:16

That’s actually helpful, can you send it as an answer so I can choose it as a solution

jackash | 2019-02-28 17:37

:bust_in_silhouette: Reply From: wombatstampede

(moved from comment to answer)
I did write a bit about Events and Control coordinates in the forum here:

Basically you should be able to use the _input() handler (can be defined inside a gdscript attached to the control or elsewhere). And then just check if the event coords are inside the controls rect:

  if (event is InputEventMouseButton) and event.pressed:
    var evLocal = make_input_local(event)
    if !Rect2(Vector2(0,0),rect_size).has_point(evLocal.position):
      do something, for outside click

make_input_local() converts the global event coordinates into coordinates local to the control (should also take rotating and scaling into account). Coordinates outside the control should then be <0 or >= the controls size. This is what is checked with the rect2().has_point() method.