mouse_and_input_coordinates.rst 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. Mouse & Input Coordinates
  2. =========================
  3. About
  4. -----
  5. The reason for this small tutorial is to clear up many common mistakes
  6. about input coordinates, obtaining mouse position and screen resolution,
  7. etc.
  8. Hardware Display Coordinates
  9. ----------------------------
  10. Using hardware coordinates makes sense in the case of writing complex
  11. UIs meant to run on PC, such as editors, MMOs, tools, etc. Yet, make not
  12. as much sense outside of that scope.
  13. [STRIKEOUT:The only way to reliably obtain this information is by using
  14. functions such as:]
  15. **This method is no longer supported:** It was too confusing and caused
  16. errors for users making 2D games. Screen would stretch to different
  17. resolutions and input would stop making sense. Please use the
  18. \`\`\_input\`\` function
  19. ::
  20. OS.get_video_mode_size()
  21. Input.get_mouse_pos()
  22. However, this is discouraged for pretty much any situation. Please do
  23. not use these functions unless you really know what you are doing.
  24. Viewport Display Coordinates
  25. ----------------------------
  26. Godot uses viewports to display content, and viewports can be scaled by
  27. several options (see [[tutorial\_multires]] tutorial). Use, then, the
  28. functions in nodes to obtain the mouse coordinates and viewport size,
  29. for example:
  30. ::
  31. func _input(ev):
  32. # Mouse in viewport coordinates
  33. if (ev.type==InputEvent.MOUSE_BUTTON):
  34. print("Mouse Click/Unclick at: ",ev.pos)
  35. elif (ev.type==InputEvent.MOUSE_MOTION):
  36. print("Mouse Motion at: ",ev.pos)
  37. # Print the size of the viewport
  38. print("Viewport Resolution is: ",get_viewport_rect().size)
  39. func _ready():
  40. set_process_input(true)
  41. Alternatively it's possible to ask the viewport for the mouse position
  42. ::
  43. get_viewport().get_mouse_pos()
  44. *Juan Linietsky, Ariel Manzur, Distributed under the terms of the `CC
  45. By <https://creativecommons.org/licenses/by/3.0/legalcode>`__ license.*