godot-docs/tutorials/engine/mouse_and_input_coordinates...

50 lines
1.3 KiB
ReStructuredText
Raw Normal View History

2016-02-08 23:45:57 +01:00
.. _doc_mouse_and_input_coordinates:
Mouse and input coordinates
===========================
2016-02-06 01:54:33 +01:00
About
-----
The reason for this small tutorial is to clear up many common mistakes
about input coordinates, obtaining mouse position and screen resolution,
etc.
Hardware display coordinates
2016-02-06 01:54:33 +01:00
----------------------------
Using hardware coordinates makes sense in the case of writing complex
UIs meant to run on PC, such as editors, MMOs, tools, etc. Yet, it makes
not as much sense outside of that scope.
2016-02-06 01:54:33 +01:00
Viewport display coordinates
2016-02-06 01:54:33 +01:00
----------------------------
Godot uses viewports to display content, and viewports can be scaled by
2016-02-08 23:45:57 +01:00
several options (see :ref:`doc_multiple_resolutions` tutorial). Use, then, the
2016-02-06 01:54:33 +01:00
functions in nodes to obtain the mouse coordinates and viewport size,
for example:
::
func _input(ev):
# Mouse in viewport coordinates
if (ev.type==InputEvent.MOUSE_BUTTON):
print("Mouse Click/Unclick at: ",ev.pos)
elif (ev.type==InputEvent.MOUSE_MOTION):
print("Mouse Motion at: ",ev.pos)
# Print the size of the viewport
print("Viewport Resolution is: ",get_viewport_rect().size)
func _ready():
set_process_input(true)
2016-02-06 01:54:33 +01:00
Alternatively it's possible to ask the viewport for the mouse position:
2016-02-06 01:54:33 +01:00
::
get_viewport().get_mouse_pos()