mouse_and_input_coordinates.rst 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. .. _doc_mouse_and_input_coordinates:
  2. Mouse and input coordinates
  3. ===========================
  4. About
  5. -----
  6. The reason for this small tutorial is to clear up many common mistakes
  7. about input coordinates, obtaining mouse position and screen resolution,
  8. etc.
  9. Hardware display coordinates
  10. ----------------------------
  11. Using hardware coordinates makes sense in the case of writing complex
  12. UIs meant to run on PC, such as editors, MMOs, tools, etc. Yet, it does
  13. not make as much sense outside of that scope.
  14. Viewport display coordinates
  15. ----------------------------
  16. Godot uses viewports to display content, and viewports can be scaled by
  17. several options (see :ref:`doc_multiple_resolutions` tutorial). Use, then, the
  18. functions in nodes to obtain the mouse coordinates and viewport size,
  19. for example:
  20. ::
  21. func _input(ev):
  22. # Mouse in viewport coordinates
  23. if (ev.type==InputEvent.MOUSE_BUTTON):
  24. print("Mouse Click/Unclick at: ",ev.pos)
  25. elif (ev.type==InputEvent.MOUSE_MOTION):
  26. print("Mouse Motion at: ",ev.pos)
  27. # Print the size of the viewport
  28. print("Viewport Resolution is: ",get_viewport_rect().size)
  29. func _ready():
  30. set_process_input(true)
  31. Alternatively it's possible to ask the viewport for the mouse position:
  32. ::
  33. get_viewport().get_mouse_pos()