Up to date
This page is up to date for Godot 4.2
.
If you still find outdated information, please open an issue.
Мышь и входные координаты¶
О Godot Engine¶
Причина этого небольшого урока состоит в том, чтобы прояснить многие распространенные ошибки относительно входных координат, получения положения мыши, разрешения экрана и т. д.
Отображение hardware-координат¶
Использование hardware-координат имеет смысл в случае написания комплекса, предназначенного для работы на PC, например редакторов, MMO, инструментов и т. д. Однако это не имеет такого большого смысла за пределами этой области.
Отображение viewport-координат¶
Godot использует viewports (видовые экраны) для отображения контента, и видовые экраны можно масштабировать несколькими опциями (см. туториал Multiple resolutions). Используйте, например, функции в узлах для получения координат мыши и размера видового экрана:
func _input(event):
# Mouse in viewport coordinates.
if event is InputEventMouseButton:
print("Mouse Click/Unclick at: ", event.position)
elif event is InputEventMouseMotion:
print("Mouse Motion at: ", event.position)
# Print the size of the viewport.
print("Viewport Resolution is: ", get_viewport().get_visible_rect().size)
public override void _Input(InputEvent @event)
{
// Mouse in viewport coordinates.
if (@event is InputEventMouseButton eventMouseButton)
GD.Print("Mouse Click/Unclick at: ", eventMouseButton.Position);
else if (@event is InputEventMouseMotion eventMouseMotion)
GD.Print("Mouse Motion at: ", eventMouseMotion.Position);
// Print the size of the viewport.
GD.Print("Viewport Resolution is: ", GetViewport().GetVisibleRect().Size);
}
Кроме того, можно запросить в видовом окне положение мыши:
get_viewport().get_mouse_position()
GetViewport().GetMousePosition();
Примечание
When the mouse mode is set to Input.MOUSE_MODE_CAPTURED
, the event.position
value from InputEventMouseMotion
is the center of the screen. Use event.relative
instead of event.position
and event.velocity
to process mouse movement and position changes.