0 votes

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

in Engine by (46 points)

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

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.

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..

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

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

(this comment is moved to an answer)

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

1 Answer

0 votes
Best answer

(moved from comment to answer)
I did write a bit about Events and Control coordinates in the forum here:
https://godotdevelopers.org/forum/discussion/20520/some-info-if-you-get-in-gui-input-coordinate-mayhem-pivot-scale-rotation-input

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:

 _input(event):
  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.

by (3,258 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.