From 17e504812b5ab50015704b2eeb0fb93969a01274 Mon Sep 17 00:00:00 2001 From: ZoOL Date: Sat, 2 Nov 2024 02:25:42 +0800 Subject: [PATCH] simplify example, replace get_single to Single Query (#16187) # Objective clean up example get_single method, make code clean; ## Solution - replace `Query` with `Single` Query - remove `get_single` or `get_single_mut` condition block --- examples/2d/2d_viewport_to_world.rs | 6 +- examples/3d/camera_sub_view.rs | 6 +- examples/3d/pbr.rs | 22 +-- examples/asset/alter_mesh.rs | 16 +- examples/asset/alter_sprite.rs | 16 +- examples/audio/audio_control.rs | 30 +-- examples/camera/2d_top_down_camera.rs | 18 +- examples/camera/first_person_view_model.rs | 14 +- examples/helpers/camera_controller.rs | 205 ++++++++++----------- examples/math/cubic_splines.rs | 13 +- examples/mobile/src/lib.rs | 6 +- examples/window/screenshot.rs | 9 +- 12 files changed, 147 insertions(+), 214 deletions(-) diff --git a/examples/2d/2d_viewport_to_world.rs b/examples/2d/2d_viewport_to_world.rs index 5aa3db91e0..44a16d99a2 100644 --- a/examples/2d/2d_viewport_to_world.rs +++ b/examples/2d/2d_viewport_to_world.rs @@ -12,15 +12,11 @@ fn main() { fn draw_cursor( camera_query: Single<(&Camera, &GlobalTransform)>, - windows: Query<&Window>, + window: Single<&Window>, mut gizmos: Gizmos, ) { let (camera, camera_transform) = *camera_query; - let Ok(window) = windows.get_single() else { - return; - }; - let Some(cursor_position) = window.cursor_position() else { return; }; diff --git a/examples/3d/camera_sub_view.rs b/examples/3d/camera_sub_view.rs index 0e7ba74ff7..0fea1633d0 100644 --- a/examples/3d/camera_sub_view.rs +++ b/examples/3d/camera_sub_view.rs @@ -253,13 +253,9 @@ fn move_camera_view( // To ensure viewports remain the same at any window size fn resize_viewports( - windows: Query<&Window, With>, + window: Single<&Window, With>, mut viewports: Query<(&mut Camera, &ExampleViewports)>, ) { - let Ok(window) = windows.get_single() else { - return; - }; - let window_size = window.physical_size(); let small_height = window_size.y / 5; diff --git a/examples/3d/pbr.rs b/examples/3d/pbr.rs index 46f82bcc65..e4889f33c2 100644 --- a/examples/3d/pbr.rs +++ b/examples/3d/pbr.rs @@ -127,21 +127,17 @@ fn setup( fn environment_map_load_finish( mut commands: Commands, asset_server: Res, - environment_maps: Query<&EnvironmentMapLight>, - label_query: Query>, + environment_map: Single<&EnvironmentMapLight>, + label_entity: Single>, ) { - if let Ok(environment_map) = environment_maps.get_single() { - if asset_server - .load_state(&environment_map.diffuse_map) + if asset_server + .load_state(&environment_map.diffuse_map) + .is_loaded() + && asset_server + .load_state(&environment_map.specular_map) .is_loaded() - && asset_server - .load_state(&environment_map.specular_map) - .is_loaded() - { - if let Ok(label_entity) = label_query.get_single() { - commands.entity(label_entity).despawn(); - } - } + { + commands.entity(*label_entity).despawn(); } } diff --git a/examples/asset/alter_mesh.rs b/examples/asset/alter_mesh.rs index 72f425fab1..7462c47ae1 100644 --- a/examples/asset/alter_mesh.rs +++ b/examples/asset/alter_mesh.rs @@ -149,13 +149,11 @@ fn spawn_text(mut commands: Commands) { fn alter_handle( asset_server: Res, - mut right_shape: Query<(&mut Mesh3d, &mut Shape), Without>, + right_shape: Single<(&mut Mesh3d, &mut Shape), Without>, ) { // Mesh handles, like other parts of the ECS, can be queried as mutable and modified at // runtime. We only spawned one shape without the `Left` marker component. - let Ok((mut mesh, mut shape)) = right_shape.get_single_mut() else { - return; - }; + let (mut mesh, mut shape) = right_shape.into_inner(); // Switch to a new Shape variant shape.set_next_variant(); @@ -174,17 +172,11 @@ fn alter_handle( fn alter_mesh( mut is_mesh_scaled: Local, - left_shape: Query<&Mesh3d, With>, + left_shape: Single<&Mesh3d, With>, mut meshes: ResMut>, ) { - // It's convenient to retrieve the asset handle stored with the shape on the left. However, - // we could just as easily have retained this in a resource or a dedicated component. - let Ok(handle) = left_shape.get_single() else { - return; - }; - // Obtain a mutable reference to the Mesh asset. - let Some(mesh) = meshes.get_mut(handle) else { + let Some(mesh) = meshes.get_mut(*left_shape) else { return; }; diff --git a/examples/asset/alter_sprite.rs b/examples/asset/alter_sprite.rs index 76a1d88a4a..807013b675 100644 --- a/examples/asset/alter_sprite.rs +++ b/examples/asset/alter_sprite.rs @@ -107,13 +107,11 @@ fn spawn_text(mut commands: Commands) { fn alter_handle( asset_server: Res, - mut right_bird: Query<(&mut Bird, &mut Sprite), Without>, + right_bird: Single<(&mut Bird, &mut Sprite), Without>, ) { // Image handles, like other parts of the ECS, can be queried as mutable and modified at // runtime. We only spawned one bird without the `Left` marker component. - let Ok((mut bird, mut sprite)) = right_bird.get_single_mut() else { - return; - }; + let (mut bird, mut sprite) = right_bird.into_inner(); // Switch to a new Bird variant bird.set_next_variant(); @@ -124,15 +122,9 @@ fn alter_handle( sprite.image = asset_server.load(bird.get_texture_path()); } -fn alter_asset(mut images: ResMut>, left_bird: Query<&Sprite, With>) { - // It's convenient to retrieve the asset handle stored with the bird on the left. However, - // we could just as easily have retained this in a resource or a dedicated component. - let Ok(sprite) = left_bird.get_single() else { - return; - }; - +fn alter_asset(mut images: ResMut>, left_bird: Single<&Sprite, With>) { // Obtain a mutable reference to the Image asset. - let Some(image) = images.get_mut(&sprite.image) else { + let Some(image) = images.get_mut(&left_bird.image) else { return; }; diff --git a/examples/audio/audio_control.rs b/examples/audio/audio_control.rs index a5512099c5..f81f7ea89b 100644 --- a/examples/audio/audio_control.rs +++ b/examples/audio/audio_control.rs @@ -20,32 +20,20 @@ fn setup(mut commands: Commands, asset_server: Res) { #[derive(Component)] struct MyMusic; -fn update_speed(music_controller: Query<&AudioSink, With>, time: Res