+1 vote

I want to create a popupmenu at the location of a mouse click, so I have something like:

func _input(event):
  if event is InputEventMouseButton == false: return
  if event.button_index != 1: return
  if event.pressed: return

  var popup = PopupMenu.new()
  get_viewport().add_child(popup)
  popup.rect_global_position = get_global_mouse_position()
  popup.rect_size = Vector2(100, 100)
  popup.popup()

The popup appears as expected when the mouse coordinates are at x > 0 and y > 0, however, when the mouse coordinates are x < 0 and y < 0, they get clamped to zero.

I found this answer which seems to have a similar issue, as well as this issue, however neither helped resolve the issue.

I feel like I am fundamentally misunderstanding something. Can you see what I am doing wrong?

in Engine by (13 points)

when the mouse coordinates are x < 0 and y < 0, they get clamped to zero

That is precisely what's happening:

void Popup::_fix_size() {
    // ...
    if (pos.x < 0)
        pos.x = 0;
    // ...
    if (pos.y < 0)
        pos.y = 0;
}

However, I cannot tell you if that's a bug or intentional. You can use the following as a workaround, but maybe it will break under certain circumstances, dunno:

  popup.popup() # call popup() first, _then_ reposition
  popup.rect_global_position = get_global_mouse_position()

That works lovely, thank you!

Please log in or register to answer this question.

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.