Skip to content

Commit 2c22bc1

Browse files
authored
Update computed_states example to use children macro (#18290)
# Objective Contributes to #18238 Updates the `computed_states`, example to use the `children!` macro. Note that this example requires `--features bevy_dev_tools` to run ## Solution Updates examples to use the Improved Spawning API merged in #17521 ## Testing - Did you test these changes? If so, how? - Opened the examples before and after and verified the same behavior was observed. I did this on Ubuntu 24.04.2 LTS using `--features wayland`. - Are there any parts that need more testing? - Other OS's and features can't hurt, but this is such a small change it shouldn't be a problem. - How can other people (reviewers) test your changes? Is there anything specific they need to know? - Run the examples yourself with and without these changes. - If relevant, what platforms did you test these changes on, and are there any important ones you can't test? - see above --- ## Showcase n/a ## Migration Guide n/a
1 parent d70c469 commit 2c22bc1

File tree

1 file changed

+116
-135
lines changed

1 file changed

+116
-135
lines changed

examples/state/computed_states.rs

Lines changed: 116 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -336,19 +336,19 @@ mod ui {
336336

337337
pub fn setup_menu(mut commands: Commands, tutorial_state: Res<State<TutorialState>>) {
338338
let button_entity = commands
339-
.spawn(Node {
340-
// center button
341-
width: Val::Percent(100.),
342-
height: Val::Percent(100.),
343-
justify_content: JustifyContent::Center,
344-
align_items: AlignItems::Center,
345-
flex_direction: FlexDirection::Column,
346-
row_gap: Val::Px(10.),
347-
..default()
348-
})
349-
.with_children(|parent| {
350-
parent
351-
.spawn((
339+
.spawn((
340+
Node {
341+
// center button
342+
width: Val::Percent(100.),
343+
height: Val::Percent(100.),
344+
justify_content: JustifyContent::Center,
345+
align_items: AlignItems::Center,
346+
flex_direction: FlexDirection::Column,
347+
row_gap: Val::Px(10.),
348+
..default()
349+
},
350+
children![
351+
(
352352
Button,
353353
Node {
354354
width: Val::Px(200.),
@@ -361,20 +361,16 @@ mod ui {
361361
},
362362
BackgroundColor(NORMAL_BUTTON),
363363
MenuButton::Play,
364-
))
365-
.with_children(|parent| {
366-
parent.spawn((
364+
children![(
367365
Text::new("Play"),
368366
TextFont {
369367
font_size: 33.0,
370368
..default()
371369
},
372370
TextColor(Color::srgb(0.9, 0.9, 0.9)),
373-
));
374-
});
375-
376-
parent
377-
.spawn((
371+
)],
372+
),
373+
(
378374
Button,
379375
Node {
380376
width: Val::Px(200.),
@@ -390,18 +386,17 @@ mod ui {
390386
TutorialState::Inactive => NORMAL_BUTTON,
391387
}),
392388
MenuButton::Tutorial,
393-
))
394-
.with_children(|parent| {
395-
parent.spawn((
389+
children![(
396390
Text::new("Tutorial"),
397391
TextFont {
398392
font_size: 33.0,
399393
..default()
400394
},
401395
TextColor(Color::srgb(0.9, 0.9, 0.9)),
402-
));
403-
});
404-
})
396+
)]
397+
),
398+
],
399+
))
405400
.id();
406401
commands.insert_resource(MenuData {
407402
root_entity: button_entity,
@@ -453,75 +448,66 @@ mod ui {
453448

454449
pub fn setup_paused_screen(mut commands: Commands) {
455450
info!("Printing Pause");
456-
commands
457-
.spawn((
458-
StateScoped(IsPaused::Paused),
451+
commands.spawn((
452+
StateScoped(IsPaused::Paused),
453+
Node {
454+
// center button
455+
width: Val::Percent(100.),
456+
height: Val::Percent(100.),
457+
justify_content: JustifyContent::Center,
458+
align_items: AlignItems::Center,
459+
flex_direction: FlexDirection::Column,
460+
row_gap: Val::Px(10.),
461+
position_type: PositionType::Absolute,
462+
..default()
463+
},
464+
children![(
459465
Node {
460-
// center button
461-
width: Val::Percent(100.),
462-
height: Val::Percent(100.),
466+
width: Val::Px(400.),
467+
height: Val::Px(400.),
468+
// horizontally center child text
463469
justify_content: JustifyContent::Center,
470+
// vertically center child text
464471
align_items: AlignItems::Center,
465-
flex_direction: FlexDirection::Column,
466-
row_gap: Val::Px(10.),
467-
position_type: PositionType::Absolute,
468472
..default()
469473
},
470-
))
471-
.with_children(|parent| {
472-
parent
473-
.spawn((
474-
Node {
475-
width: Val::Px(400.),
476-
height: Val::Px(400.),
477-
// horizontally center child text
478-
justify_content: JustifyContent::Center,
479-
// vertically center child text
480-
align_items: AlignItems::Center,
481-
..default()
482-
},
483-
BackgroundColor(NORMAL_BUTTON),
484-
MenuButton::Play,
485-
))
486-
.with_children(|parent| {
487-
parent.spawn((
488-
Text::new("Paused"),
489-
TextFont {
490-
font_size: 33.0,
491-
..default()
492-
},
493-
TextColor(Color::srgb(0.9, 0.9, 0.9)),
494-
));
495-
});
496-
});
474+
BackgroundColor(NORMAL_BUTTON),
475+
MenuButton::Play,
476+
children![(
477+
Text::new("Paused"),
478+
TextFont {
479+
font_size: 33.0,
480+
..default()
481+
},
482+
TextColor(Color::srgb(0.9, 0.9, 0.9)),
483+
)],
484+
),],
485+
));
497486
}
498487

499488
pub fn setup_turbo_text(mut commands: Commands) {
500-
commands
501-
.spawn((
502-
StateScoped(TurboMode),
503-
Node {
504-
// center button
505-
width: Val::Percent(100.),
506-
height: Val::Percent(100.),
507-
justify_content: JustifyContent::Start,
508-
align_items: AlignItems::Center,
509-
flex_direction: FlexDirection::Column,
510-
row_gap: Val::Px(10.),
511-
position_type: PositionType::Absolute,
489+
commands.spawn((
490+
StateScoped(TurboMode),
491+
Node {
492+
// center button
493+
width: Val::Percent(100.),
494+
height: Val::Percent(100.),
495+
justify_content: JustifyContent::Start,
496+
align_items: AlignItems::Center,
497+
flex_direction: FlexDirection::Column,
498+
row_gap: Val::Px(10.),
499+
position_type: PositionType::Absolute,
500+
..default()
501+
},
502+
children![(
503+
Text::new("TURBO MODE"),
504+
TextFont {
505+
font_size: 33.0,
512506
..default()
513507
},
514-
))
515-
.with_children(|parent| {
516-
parent.spawn((
517-
Text::new("TURBO MODE"),
518-
TextFont {
519-
font_size: 33.0,
520-
..default()
521-
},
522-
TextColor(Color::srgb(0.9, 0.3, 0.1)),
523-
));
524-
});
508+
TextColor(Color::srgb(0.9, 0.3, 0.1)),
509+
)],
510+
));
525511
}
526512

527513
pub fn change_color(time: Res<Time>, mut query: Query<&mut Sprite>) {
@@ -536,93 +522,88 @@ mod ui {
536522
}
537523

538524
pub fn movement_instructions(mut commands: Commands) {
539-
commands
540-
.spawn((
541-
StateScoped(Tutorial::MovementInstructions),
542-
Node {
543-
// center button
544-
width: Val::Percent(100.),
545-
height: Val::Percent(100.),
546-
justify_content: JustifyContent::End,
547-
align_items: AlignItems::Center,
548-
flex_direction: FlexDirection::Column,
549-
row_gap: Val::Px(10.),
550-
position_type: PositionType::Absolute,
551-
..default()
552-
},
553-
))
554-
.with_children(|parent| {
555-
parent.spawn((
525+
commands.spawn((
526+
StateScoped(Tutorial::MovementInstructions),
527+
Node {
528+
// center button
529+
width: Val::Percent(100.),
530+
height: Val::Percent(100.),
531+
justify_content: JustifyContent::End,
532+
align_items: AlignItems::Center,
533+
flex_direction: FlexDirection::Column,
534+
row_gap: Val::Px(10.),
535+
position_type: PositionType::Absolute,
536+
..default()
537+
},
538+
children![
539+
(
556540
Text::new("Move the bevy logo with the arrow keys"),
557541
TextFont {
558542
font_size: 33.0,
559543
..default()
560544
},
561545
TextColor(Color::srgb(0.3, 0.3, 0.7)),
562-
));
563-
parent.spawn((
546+
),
547+
(
564548
Text::new("Press T to enter TURBO MODE"),
565549
TextFont {
566550
font_size: 33.0,
567551
..default()
568552
},
569553
TextColor(Color::srgb(0.3, 0.3, 0.7)),
570-
));
571-
572-
parent.spawn((
554+
),
555+
(
573556
Text::new("Press SPACE to pause"),
574557
TextFont {
575558
font_size: 33.0,
576559
..default()
577560
},
578561
TextColor(Color::srgb(0.3, 0.3, 0.7)),
579-
));
580-
581-
parent.spawn((
562+
),
563+
(
582564
Text::new("Press ESCAPE to return to the menu"),
583565
TextFont {
584566
font_size: 33.0,
585567
..default()
586568
},
587569
TextColor(Color::srgb(0.3, 0.3, 0.7)),
588-
));
589-
});
570+
),
571+
],
572+
));
590573
}
591574

592575
pub fn pause_instructions(mut commands: Commands) {
593-
commands
594-
.spawn((
595-
StateScoped(Tutorial::PauseInstructions),
596-
Node {
597-
// center button
598-
width: Val::Percent(100.),
599-
height: Val::Percent(100.),
600-
justify_content: JustifyContent::End,
601-
align_items: AlignItems::Center,
602-
flex_direction: FlexDirection::Column,
603-
row_gap: Val::Px(10.),
604-
position_type: PositionType::Absolute,
605-
..default()
606-
},
607-
))
608-
.with_children(|parent| {
609-
parent.spawn((
576+
commands.spawn((
577+
StateScoped(Tutorial::PauseInstructions),
578+
Node {
579+
// center button
580+
width: Val::Percent(100.),
581+
height: Val::Percent(100.),
582+
justify_content: JustifyContent::End,
583+
align_items: AlignItems::Center,
584+
flex_direction: FlexDirection::Column,
585+
row_gap: Val::Px(10.),
586+
position_type: PositionType::Absolute,
587+
..default()
588+
},
589+
children![
590+
(
610591
Text::new("Press SPACE to resume"),
611592
TextFont {
612593
font_size: 33.0,
613594
..default()
614595
},
615596
TextColor(Color::srgb(0.3, 0.3, 0.7)),
616-
));
617-
618-
parent.spawn((
597+
),
598+
(
619599
Text::new("Press ESCAPE to return to the menu"),
620600
TextFont {
621601
font_size: 33.0,
622602
..default()
623603
},
624604
TextColor(Color::srgb(0.3, 0.3, 0.7)),
625-
));
626-
});
605+
),
606+
],
607+
));
627608
}
628609
}

0 commit comments

Comments
 (0)