|
|
|
@ -29,13 +29,14 @@ fn capture_mouse( |
|
|
|
|
key: Res<ButtonInput<KeyCode>>, |
|
|
|
|
) { |
|
|
|
|
if mouse.just_pressed(MouseButton::Left) { |
|
|
|
|
window.cursor_options.visible = false; |
|
|
|
|
window.cursor_options.grab_mode = CursorGrabMode::Locked; |
|
|
|
|
set_mouse_grabbed(&mut window, true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if key.just_pressed(KeyCode::Escape) { |
|
|
|
|
window.cursor_options.visible = true; |
|
|
|
|
window.cursor_options.grab_mode = CursorGrabMode::None; |
|
|
|
|
set_mouse_grabbed(&mut window, false); |
|
|
|
|
} |
|
|
|
|
// Ungrab the mouse if window doesn't have focus.
|
|
|
|
|
if !window.focused { |
|
|
|
|
set_mouse_grabbed(&mut window, false); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -45,7 +46,7 @@ fn camera_mouse_rotation( |
|
|
|
|
mouse_motion: Res<AccumulatedMouseMotion>, |
|
|
|
|
) { |
|
|
|
|
// Mouse must be grabbed by the window for this system to run.
|
|
|
|
|
if window.cursor_options.grab_mode != CursorGrabMode::Locked { |
|
|
|
|
if !mouse_grabbed(&window) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -71,7 +72,7 @@ fn camera_keyboard_translation( |
|
|
|
|
key: Res<ButtonInput<KeyCode>>, |
|
|
|
|
) { |
|
|
|
|
// Mouse must be grabbed by the window for this system to run.
|
|
|
|
|
if window.cursor_options.grab_mode != CursorGrabMode::Locked { |
|
|
|
|
if !mouse_grabbed(&window) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -108,3 +109,16 @@ fn camera_keyboard_translation( |
|
|
|
|
transform.translation += translation; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn mouse_grabbed(window: &Window) -> bool { |
|
|
|
|
window.cursor_options.grab_mode == CursorGrabMode::Locked |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn set_mouse_grabbed(window: &mut Window, value: bool) { |
|
|
|
|
window.cursor_options.visible = !value; |
|
|
|
|
window.cursor_options.grab_mode = if value { |
|
|
|
|
CursorGrabMode::Locked |
|
|
|
|
} else { |
|
|
|
|
CursorGrabMode::None |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|