
**Ready for review. Examples migration progress: 100%.** # Objective - Implement https://github.com/bevyengine/bevy/discussions/15014 ## Solution This implements [cart's proposal](https://github.com/bevyengine/bevy/discussions/15014#discussioncomment-10574459) faithfully except for one change. I separated `TextSpan` from `TextSpan2d` because `TextSpan` needs to require the `GhostNode` component, which is a `bevy_ui` component only usable by UI. Extra changes: - Added `EntityCommands::commands_mut` that returns a mutable reference. This is a blocker for extension methods that return something other than `self`. Note that `sickle_ui`'s `UiBuilder::commands` returns a mutable reference for this reason. ## Testing - [x] Text examples all work. --- ## Showcase TODO: showcase-worthy ## Migration Guide TODO: very breaking ### Accessing text spans by index Text sections are now text sections on different entities in a hierarchy, Use the new `TextReader` and `TextWriter` system parameters to access spans by index. Before: ```rust fn refresh_text(mut query: Query<&mut Text, With<TimeText>>, time: Res<Time>) { let text = query.single_mut(); text.sections[1].value = format_time(time.elapsed()); } ``` After: ```rust fn refresh_text( query: Query<Entity, With<TimeText>>, mut writer: UiTextWriter, time: Res<Time> ) { let entity = query.single(); *writer.text(entity, 1) = format_time(time.elapsed()); } ``` ### Iterating text spans Text spans are now entities in a hierarchy, so the new `UiTextReader` and `UiTextWriter` system parameters provide ways to iterate that hierarchy. The `UiTextReader::iter` method will give you a normal iterator over spans, and `UiTextWriter::for_each` lets you visit each of the spans. --------- Co-authored-by: ickshonpe <david.curthoys@googlemail.com> Co-authored-by: Carter Anderson <mcanders1@gmail.com>
116 lines
3.5 KiB
Rust
116 lines
3.5 KiB
Rust
//! Demonstrates the use of "one-shot systems", which run once when triggered.
|
|
//!
|
|
//! These can be useful to help structure your logic in a push-based fashion,
|
|
//! reducing the overhead of running extremely rarely run systems
|
|
//! and improving schedule flexibility.
|
|
//!
|
|
//! See the [`World::run_system`](World::run_system) or
|
|
//! [`World::run_system_once`](World#method.run_system_once_with)
|
|
//! docs for more details.
|
|
|
|
use bevy::{
|
|
ecs::system::{RunSystemOnce, SystemId},
|
|
prelude::*,
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_systems(
|
|
Startup,
|
|
(
|
|
setup_ui,
|
|
setup_with_commands,
|
|
setup_with_world.after(setup_ui), // since we run `system_b` once in world it needs to run after `setup_ui`
|
|
),
|
|
)
|
|
.add_systems(Update, (trigger_system, evaluate_callbacks).chain())
|
|
.run();
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct Callback(SystemId);
|
|
|
|
#[derive(Component)]
|
|
struct Triggered;
|
|
|
|
#[derive(Component)]
|
|
struct A;
|
|
#[derive(Component)]
|
|
struct B;
|
|
|
|
fn setup_with_commands(mut commands: Commands) {
|
|
let system_id = commands.register_system(system_a);
|
|
commands.spawn((Callback(system_id), A));
|
|
}
|
|
|
|
fn setup_with_world(world: &mut World) {
|
|
// We can run it once manually
|
|
world.run_system_once(system_b).unwrap();
|
|
// Or with a Callback
|
|
let system_id = world.register_system(system_b);
|
|
world.spawn((Callback(system_id), B));
|
|
}
|
|
|
|
/// Tag entities that have callbacks we want to run with the `Triggered` component.
|
|
fn trigger_system(
|
|
mut commands: Commands,
|
|
query_a: Query<Entity, With<A>>,
|
|
query_b: Query<Entity, With<B>>,
|
|
input: Res<ButtonInput<KeyCode>>,
|
|
) {
|
|
if input.just_pressed(KeyCode::KeyA) {
|
|
let entity = query_a.single();
|
|
commands.entity(entity).insert(Triggered);
|
|
}
|
|
if input.just_pressed(KeyCode::KeyB) {
|
|
let entity = query_b.single();
|
|
commands.entity(entity).insert(Triggered);
|
|
}
|
|
}
|
|
|
|
/// Runs the systems associated with each `Callback` component if the entity also has a `Triggered` component.
|
|
///
|
|
/// This could be done in an exclusive system rather than using `Commands` if preferred.
|
|
fn evaluate_callbacks(query: Query<(Entity, &Callback), With<Triggered>>, mut commands: Commands) {
|
|
for (entity, callback) in query.iter() {
|
|
commands.run_system(callback.0);
|
|
commands.entity(entity).remove::<Triggered>();
|
|
}
|
|
}
|
|
|
|
fn system_a(query: Query<Entity, With<Text>>, mut writer: UiTextWriter) {
|
|
*writer.text(query.single(), 3) = String::from("A");
|
|
info!("A: One shot system registered with Commands was triggered");
|
|
}
|
|
|
|
fn system_b(query: Query<Entity, With<Text>>, mut writer: UiTextWriter) {
|
|
*writer.text(query.single(), 3) = String::from("B");
|
|
info!("B: One shot system registered with World was triggered");
|
|
}
|
|
|
|
fn setup_ui(mut commands: Commands) {
|
|
commands.spawn(Camera2d);
|
|
commands
|
|
.spawn((
|
|
Text::default(),
|
|
TextBlock::new_with_justify(JustifyText::Center),
|
|
Style {
|
|
align_self: AlignSelf::Center,
|
|
justify_self: JustifySelf::Center,
|
|
..default()
|
|
},
|
|
))
|
|
.with_children(|p| {
|
|
p.spawn(TextSpan::new("Press A or B to trigger a one-shot system\n"));
|
|
p.spawn(TextSpan::new("Last Triggered: "));
|
|
p.spawn((
|
|
TextSpan::new("-"),
|
|
TextStyle {
|
|
color: bevy::color::palettes::css::ORANGE.into(),
|
|
..default()
|
|
},
|
|
));
|
|
});
|
|
}
|