Skip to content

Commit cb64a4a

Browse files
Refactor 3d_viewport_to_world example with let chains (#20071)
# Objective Make use of let chains to reduce LoC where we previously used let else to cut down on indentation. Best of both worlds.
1 parent c9c8964 commit cb64a4a

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

examples/3d/3d_viewport_to_world.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,30 @@ fn main() {
1313
fn draw_cursor(
1414
camera_query: Single<(&Camera, &GlobalTransform)>,
1515
ground: Single<&GlobalTransform, With<Ground>>,
16-
windows: Query<&Window>,
16+
window: Single<&Window>,
1717
mut gizmos: Gizmos,
1818
) {
19-
let Ok(windows) = windows.single() else {
20-
return;
21-
};
22-
2319
let (camera, camera_transform) = *camera_query;
2420

25-
let Some(cursor_position) = windows.cursor_position() else {
26-
return;
27-
};
28-
29-
// Calculate a ray pointing from the camera into the world based on the cursor's position.
30-
let Ok(ray) = camera.viewport_to_world(camera_transform, cursor_position) else {
31-
return;
32-
};
33-
34-
// Calculate if and where the ray is hitting the ground plane.
35-
let Some(distance) =
36-
ray.intersect_plane(ground.translation(), InfinitePlane3d::new(ground.up()))
37-
else {
38-
return;
39-
};
40-
let point = ray.get_point(distance);
21+
if let Some(cursor_position) = window.cursor_position()
22+
// Calculate a ray pointing from the camera into the world based on the cursor's position.
23+
&& let Ok(ray) = camera.viewport_to_world(camera_transform, cursor_position)
24+
// Calculate if and at what distance the ray is hitting the ground plane.
25+
&& let Some(distance) =
26+
ray.intersect_plane(ground.translation(), InfinitePlane3d::new(ground.up()))
27+
{
28+
let point = ray.get_point(distance);
4129

42-
// Draw a circle just above the ground plane at that position.
43-
gizmos.circle(
44-
Isometry3d::new(
45-
point + ground.up() * 0.01,
46-
Quat::from_rotation_arc(Vec3::Z, ground.up().as_vec3()),
47-
),
48-
0.2,
49-
Color::WHITE,
50-
);
30+
// Draw a circle just above the ground plane at that position.
31+
gizmos.circle(
32+
Isometry3d::new(
33+
point + ground.up() * 0.01,
34+
Quat::from_rotation_arc(Vec3::Z, ground.up().as_vec3()),
35+
),
36+
0.2,
37+
Color::WHITE,
38+
);
39+
}
5140
}
5241

5342
#[derive(Component)]

0 commit comments

Comments
 (0)