Fix window close in example cause panic (#17533)

# Objective

Fixes #17532 

## Solution

- check window valide
This commit is contained in:
jiang heng 2025-01-28 13:37:23 +08:00 committed by GitHub
parent 51bb4f08a9
commit dfac3b9bfd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 39 additions and 8 deletions

View File

@ -12,9 +12,13 @@ fn main() {
fn draw_cursor(
camera_query: Single<(&Camera, &GlobalTransform)>,
window: Single<&Window>,
window: Query<&Window>,
mut gizmos: Gizmos,
) {
let Ok(window) = window.get_single() else {
return;
};
let (camera, camera_transform) = *camera_query;
let Some(cursor_position) = window.cursor_position() else {

View File

@ -13,9 +13,13 @@ fn main() {
fn draw_cursor(
camera_query: Single<(&Camera, &GlobalTransform)>,
ground: Single<&GlobalTransform, With<Ground>>,
windows: Single<&Window>,
windows: Query<&Window>,
mut gizmos: Gizmos,
) {
let Ok(windows) = windows.get_single() else {
return;
};
let (camera, camera_transform) = *camera_query;
let Some(cursor_position) = windows.cursor_position() else {

View File

@ -172,9 +172,13 @@ fn draw_shapes(mut gizmos: Gizmos, mines: Query<&Mine>) {
fn handle_click(
mouse_button_input: Res<ButtonInput<MouseButton>>,
camera: Single<(&Camera, &GlobalTransform)>,
windows: Single<&Window>,
windows: Query<&Window>,
mut commands: Commands,
) {
let Ok(windows) = windows.get_single() else {
return;
};
let (camera, camera_transform) = *camera;
if let Some(pos) = windows
.cursor_position()

View File

@ -42,7 +42,10 @@ fn move_system(mut sprites: Query<(&mut Transform, &Velocity)>) {
}
// Bounce sprites outside the window
fn bounce_system(window: Single<&Window>, mut sprites: Query<(&Transform, &mut Velocity)>) {
fn bounce_system(window: Query<&Window>, mut sprites: Query<(&Transform, &mut Velocity)>) {
let Ok(window) = window.get_single() else {
return;
};
let width = window.width();
let height = window.height();
let left = width / -2.0;

View File

@ -252,10 +252,14 @@ fn gravity(time: Res<Time>, mut velocity_query: Query<&mut Velocity>) {
/// velocity. On collision with the ground it applies an upwards
/// force.
fn collisions(
window: Single<&Window>,
window: Query<&Window>,
mut query: Query<(&mut Velocity, &mut Transform), With<Contributor>>,
mut rng: ResMut<SharedRng>,
) {
let Ok(window) = window.get_single() else {
return;
};
let window_size = window.size();
let collision_area = Aabb2d::new(Vec2::ZERO, (window_size - SPRITE_SIZE) / 2.);

View File

@ -46,12 +46,16 @@ fn main() {
}
fn touch_camera(
window: Single<&Window>,
window: Query<&Window>,
mut touches: EventReader<TouchInput>,
mut camera_transform: Single<&mut Transform, With<Camera3d>>,
mut last_position: Local<Option<Vec2>>,
mut rotations: EventReader<RotationGesture>,
) {
let Ok(window) = window.get_single() else {
return;
};
for touch in touches.read() {
if touch.phase == TouchPhase::Started {
*last_position = None;

View File

@ -327,12 +327,16 @@ fn mouse_handler(
args: Res<Args>,
time: Res<Time>,
mouse_button_input: Res<ButtonInput<MouseButton>>,
window: Single<&Window>,
window: Query<&Window>,
bird_resources: ResMut<BirdResources>,
mut counter: ResMut<BevyCounter>,
mut rng: Local<Option<ChaCha8Rng>>,
mut wave: Local<usize>,
) {
let Ok(window) = window.get_single() else {
return;
};
if rng.is_none() {
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
@ -529,7 +533,11 @@ fn handle_collision(half_extents: Vec2, translation: &Vec3, velocity: &mut Vec3)
velocity.y = 0.0;
}
}
fn collision_system(window: Single<&Window>, mut bird_query: Query<(&mut Bird, &Transform)>) {
fn collision_system(window: Query<&Window>, mut bird_query: Query<(&mut Bird, &Transform)>) {
let Ok(window) = window.get_single() else {
return;
};
let half_extents = 0.5 * window.size();
for (mut bird, transform) in &mut bird_query {