Skip to content
22 changes: 8 additions & 14 deletions examples/2d/sprite_scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,22 @@ fn setup_sprites(mut commands: Commands, asset_server: Res<AssetServer>) {
];

for rect in rects {
let mut cmd = commands.spawn((
commands.spawn((
Sprite {
image: rect.texture,
custom_size: Some(rect.size),
image_mode: rect.image_mode,
..default()
},
rect.transform,
));

cmd.with_children(|builder| {
builder.spawn((
children![(
Text2d::new(rect.text),
TextLayout::new_with_justify(Justify::Center),
TextFont::from_font_size(15.),
Transform::from_xyz(0., -0.5 * rect.size.y - 10., 0.),
bevy::sprite::Anchor::TOP_CENTER,
));
});
)],
));
}
}

Expand Down Expand Up @@ -257,7 +254,7 @@ fn setup_texture_atlas(
];

for sprite_sheet in sprite_sheets {
let mut cmd = commands.spawn((
commands.spawn((
Sprite {
image_mode: sprite_sheet.image_mode,
custom_size: Some(sprite_sheet.size),
Expand All @@ -266,17 +263,14 @@ fn setup_texture_atlas(
sprite_sheet.indices,
sprite_sheet.timer,
sprite_sheet.transform,
));

cmd.with_children(|builder| {
builder.spawn((
children![(
Text2d::new(sprite_sheet.text),
TextLayout::new_with_justify(Justify::Center),
TextFont::from_font_size(15.),
Transform::from_xyz(0., -0.5 * sprite_sheet.size.y - 10., 0.),
bevy::sprite::Anchor::TOP_CENTER,
));
});
)],
));
}
}

Expand Down
76 changes: 38 additions & 38 deletions examples/2d/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,44 +132,44 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
Text2dShadow::default(),
));

commands
.spawn((
Sprite {
color: Color::Srgba(LIGHT_CYAN),
custom_size: Some(Vec2::new(10., 10.)),
..Default::default()
},
Transform::from_translation(250. * Vec3::Y),
))
.with_children(|commands| {
for (text_anchor, color) in [
(Anchor::TOP_LEFT, Color::Srgba(LIGHT_SALMON)),
(Anchor::TOP_RIGHT, Color::Srgba(LIGHT_GREEN)),
(Anchor::BOTTOM_RIGHT, Color::Srgba(LIGHT_BLUE)),
(Anchor::BOTTOM_LEFT, Color::Srgba(LIGHT_YELLOW)),
] {
commands
.spawn((
Text2d::new(" Anchor".to_string()),
slightly_smaller_text_font.clone(),
text_anchor,
TextBackgroundColor(Color::WHITE.darker(0.8)),
Transform::from_translation(-1. * Vec3::Z),
))
.with_child((
TextSpan("::".to_string()),
slightly_smaller_text_font.clone(),
TextColor(LIGHT_GREY.into()),
TextBackgroundColor(DARK_BLUE.into()),
))
.with_child((
TextSpan(format!("{text_anchor:?} ")),
slightly_smaller_text_font.clone(),
TextColor(color),
TextBackgroundColor(color.darker(0.3)),
));
}
});
let make_child = move |(text_anchor, color): (Anchor, Color)| {
(
Text2d::new(" Anchor".to_string()),
slightly_smaller_text_font.clone(),
text_anchor,
TextBackgroundColor(Color::WHITE.darker(0.8)),
Transform::from_translation(-1. * Vec3::Z),
children![
(
TextSpan("::".to_string()),
slightly_smaller_text_font.clone(),
TextColor(LIGHT_GREY.into()),
TextBackgroundColor(DARK_BLUE.into()),
),
(
TextSpan(format!("{text_anchor:?} ")),
slightly_smaller_text_font.clone(),
TextColor(color),
TextBackgroundColor(color.darker(0.3)),
)
],
)
};

commands.spawn((
Sprite {
color: Color::Srgba(LIGHT_CYAN),
custom_size: Some(Vec2::new(10., 10.)),
..Default::default()
},
Transform::from_translation(250. * Vec3::Y),
children![
make_child((Anchor::TOP_LEFT, Color::Srgba(LIGHT_SALMON))),
make_child((Anchor::TOP_RIGHT, Color::Srgba(LIGHT_GREEN))),
make_child((Anchor::BOTTOM_RIGHT, Color::Srgba(LIGHT_BLUE))),
make_child((Anchor::BOTTOM_LEFT, Color::Srgba(LIGHT_YELLOW))),
],
));
}

fn animate_translation(
Expand Down
46 changes: 20 additions & 26 deletions examples/animation/animated_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,35 +151,29 @@ fn setup(
player,
))
.id();
commands
.entity(planet_entity)
.insert(AnimationTarget {
commands.entity(planet_entity).insert((
AnimationTarget {
id: planet_animation_target_id,
player: planet_entity,
})
.with_children(|p| {
// This entity is just used for animation, but doesn't display anything
p.spawn((
Transform::default(),
Visibility::default(),
orbit_controller,
},
children![(
Transform::default(),
Visibility::default(),
orbit_controller,
AnimationTarget {
id: orbit_controller_animation_target_id,
player: planet_entity,
},
children![(
Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.9, 0.3))),
Transform::from_xyz(1.5, 0.0, 0.0),
AnimationTarget {
id: orbit_controller_animation_target_id,
id: satellite_animation_target_id,
player: planet_entity,
},
))
.with_children(|p| {
// The satellite, placed at a distance of the planet
p.spawn((
Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.9, 0.3))),
Transform::from_xyz(1.5, 0.0, 0.0),
AnimationTarget {
id: satellite_animation_target_id,
player: planet_entity,
},
satellite,
));
});
});
satellite,
)],
)],
));
}
69 changes: 32 additions & 37 deletions examples/animation/animated_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,43 +123,38 @@ fn setup(
// Build the UI. We have a parent node that covers the whole screen and
// contains the `AnimationPlayer`, as well as a child node that contains the
// text to be animated.
commands
.spawn((
// Cover the whole screen, and center contents.
Node {
position_type: PositionType::Absolute,
top: px(0),
left: px(0),
right: px(0),
bottom: px(0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
animation_player,
AnimationGraphHandle(animation_graph),
))
.with_children(|builder| {
// Build the text node.
let player = builder.target_entity();
builder
.spawn((
Text::new("Bevy"),
TextFont {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 24.0,
..default()
},
TextColor(Color::Srgba(Srgba::RED)),
TextLayout::new_with_justify(Justify::Center),
))
// Mark as an animation target.
.insert(AnimationTarget {
id: animation_target_id,
player,
})
.insert(animation_target_name);
});
let mut entity = commands.spawn((
// Cover the whole screen, and center contents.
Node {
position_type: PositionType::Absolute,
top: px(0),
left: px(0),
right: px(0),
bottom: px(0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
animation_player,
AnimationGraphHandle(animation_graph),
));

let player = entity.id();
entity.insert(children![(
Text::new("Bevy"),
TextFont {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 24.0,
..default()
},
TextColor(Color::Srgba(Srgba::RED)),
TextLayout::new_with_justify(Justify::Center),
AnimationTarget {
id: animation_target_id,
player,
},
animation_target_name,
)]);
}

// A type that represents the color of the first text section.
Expand Down
Loading
Loading