Up to date
This page is up to date for Godot 4.2
.
If you still find outdated information, please open an issue.
Мишка і координати введення¶
Відомості¶
Причина цього невеликого уроку полягає в тому, щоб прояснити багато поширених помилок щодо координат введення, отримання положення мишки та роздільної здатності екрана, тощо.
Координати відображення устаткування¶
Використання апаратних координат має сенс у випадку написання складних UIs, призначених для запуску на ПК, таких як редактори, ММО, інструменти тощо. Однак це не має такого сенсу за межами цієї сфери.
Координати відображення вікна перегляду¶
Godot використовує вікна перегляду для відображення вмісту, а перегляди можуть бути масштабовані за кількома параметрами (див. 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.