mouse_and_input_coordinates.rst 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. .. _doc_mouse_and_input_coordinates:
  2. Mouse & 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, make not
  13. as much sense outside of that scope.
  14. [STRIKEOUT:The only way to reliably obtain this information is by using
  15. functions such as:]
  16. **This method is no longer supported:** It was too confusing and caused
  17. errors for users making 2D games. Screen would stretch to different
  18. resolutions and input would stop making sense. Please use the
  19. ``_input`` function
  20. ::
  21. OS.get_video_mode_size()
  22. Input.get_mouse_pos()
  23. However, this is discouraged for pretty much any situation. Please do
  24. not use these functions unless you really know what you are doing.
  25. Viewport Display Coordinates
  26. ----------------------------
  27. Godot uses viewports to display content, and viewports can be scaled by
  28. several options (see :ref:`doc_multiple_resolutions` tutorial). Use, then, the
  29. functions in nodes to obtain the mouse coordinates and viewport size,
  30. for example:
  31. ::
  32. func _input(ev):
  33. # Mouse in viewport coordinates
  34. if (ev.type==InputEvent.MOUSE_BUTTON):
  35. print("Mouse Click/Unclick at: ",ev.pos)
  36. elif (ev.type==InputEvent.MOUSE_MOTION):
  37. print("Mouse Motion at: ",ev.pos)
  38. # Print the size of the viewport
  39. print("Viewport Resolution is: ",get_viewport_rect().size)
  40. func _ready():
  41. set_process_input(true)
  42. Alternatively it's possible to ask the viewport for the mouse position
  43. ::
  44. get_viewport().get_mouse_pos()