PopupMenu above/left of Vector2.ZERO

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

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?

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()

njamster | 2020-05-11 21:03

That works lovely, thank you!

Lawrence Wakefield | 2020-05-11 21:49