Skip to content

Commit

Permalink
Clean up the simple_picking example (#15633)
Browse files Browse the repository at this point in the history
## Solution

- Removed superfluous `Pickable` components
- Slightly simplified the code for updating the text color
- Removed the `Pointer<Click>` observer from the mesh entirely since
that doesn't support picking yet
  • Loading branch information
tim-blackbird authored Oct 4, 2024
1 parent 0b9a461 commit 2526410
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions examples/picking/simple_picking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands
.spawn((
TextBundle {
text: Text::from_section("Click Me to get a box", TextStyle::default()),
style: Style {
position_type: PositionType::Absolute,
top: Val::Percent(12.0),
left: Val::Percent(12.0),
..default()
},
..Default::default()
.spawn(TextBundle {
text: Text::from_section("Click Me to get a box", TextStyle::default()),
style: Style {
position_type: PositionType::Absolute,
top: Val::Percent(12.0),
left: Val::Percent(12.0),
..default()
},
Pickable::default(),
))
..Default::default()
})
.observe(
|_click: Trigger<Pointer<Click>>,
mut commands: Commands,
Expand All @@ -47,26 +44,18 @@ fn setup(
)
.observe(|evt: Trigger<Pointer<Out>>, mut texts: Query<&mut Text>| {
let mut text = texts.get_mut(evt.entity()).unwrap();
let first = text.sections.first_mut().unwrap();
first.style.color = WHITE.into();
text.sections[0].style.color = WHITE.into();
})
.observe(|evt: Trigger<Pointer<Over>>, mut texts: Query<&mut Text>| {
let mut text = texts.get_mut(evt.entity()).unwrap();
let first = text.sections.first_mut().unwrap();
first.style.color = BLUE.into();
text.sections[0].style.color = BLUE.into();
});
// circular base
commands
.spawn((
Mesh3d(meshes.add(Circle::new(4.0))),
MeshMaterial3d(materials.add(Color::WHITE)),
Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
Pickable::default(),
))
.observe(|click: Trigger<Pointer<Click>>| {
let click = click.event();
println!("{click:?}");
});
commands.spawn((
Mesh3d(meshes.add(Circle::new(4.0))),
MeshMaterial3d(materials.add(Color::WHITE)),
Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
));
// light
commands.spawn((
PointLight {
Expand Down

0 comments on commit 2526410

Please sign in to comment.