2023-09-19 20:17:05 +00:00
|
|
|
//! 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.
|
|
|
|
//!
|
2023-11-28 23:43:40 +00:00
|
|
|
//! See the [`World::run_system`](World::run_system) or
|
|
|
|
//! [`World::run_system_once`](World#method.run_system_once_with)
|
Fix intra-doc link warnings (#10445)
When `cargo doc -Zunstable-options -Zrustdoc-scrape-examples` (trying to
figure out why it doesn't work with bevy), I had the following warnings:
```
warning: unresolved link to `Quad`
--> examples/2d/mesh2d.rs:1:66
|
1 | //! Shows how to render a polygonal [`Mesh`], generated from a [`Quad`] primitive, in a 2D scene.
| ^^^^ no item named `Quad` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
warning: `bevy` (example "mesh2d") generated 1 warning
warning: unresolved link to `update_weights`
--> examples/animation/morph_targets.rs:6:17
|
6 | //! See the [`update_weights`] system for details.
| ^^^^^^^^^^^^^^ no item named `update_weights` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
warning: public documentation for `morph_targets` links to private item `name_morphs`
--> examples/animation/morph_targets.rs:7:43
|
7 | //! - How to read morph target names in [`name_morphs`].
| ^^^^^^^^^^^ this item is private
|
= note: this link will resolve properly if you pass `--document-private-items`
= note: `#[warn(rustdoc::private_intra_doc_links)]` on by default
warning: public documentation for `morph_targets` links to private item `setup_animations`
--> examples/animation/morph_targets.rs:8:48
|
8 | //! - How to play morph target animations in [`setup_animations`].
| ^^^^^^^^^^^^^^^^ this item is private
|
= note: this link will resolve properly if you pass `--document-private-items`
warning: `bevy` (example "morph_targets") generated 3 warnings
warning: unresolved link to `Quad`
--> examples/2d/mesh2d_vertex_color_texture.rs:1:66
|
1 | //! Shows how to render a polygonal [`Mesh`], generated from a [`Quad`] primitive, in a 2D scene.
| ^^^^ no item named `Quad` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
warning: `bevy` (example "mesh2d_vertex_color_texture") generated 1 warning
warning: unresolved link to `UIScale`
--> examples/ui/ui_scaling.rs:1:36
|
1 | //! This example illustrates the [`UIScale`] resource from `bevy_ui`.
| ^^^^^^^ no item named `UIScale` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
warning: `bevy` (example "ui_scaling") generated 1 warning
warning: unresolved link to `dependencies`
--> examples/app/headless.rs:5:6
|
5 | //! [dependencies]
| ^^^^^^^^^^^^ no item named `dependencies` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
warning: `bevy` (example "headless") generated 1 warning
warning: unresolved link to `Material2d`
--> examples/2d/mesh2d_manual.rs:3:26
|
3 | //! It doesn't use the [`Material2d`] abstraction, but changes the vertex buffer to include verte...
| ^^^^^^^^^^ no item named `Material2d` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
= note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
warning: `bevy` (example "mesh2d_manual") generated 1 warning
```
2023-11-08 14:33:46 +00:00
|
|
|
//! docs for more details.
|
2023-09-19 20:17:05 +00:00
|
|
|
|
|
|
|
use bevy::{
|
|
|
|
ecs::system::{RunSystemOnce, SystemId},
|
|
|
|
prelude::*,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
2024-05-03 13:00:56 +00:00
|
|
|
.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())
|
2023-09-19 20:17:05 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Callback(SystemId);
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Triggered;
|
|
|
|
|
2024-05-03 13:00:56 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
struct A;
|
|
|
|
#[derive(Component)]
|
|
|
|
struct B;
|
|
|
|
|
|
|
|
fn setup_with_commands(mut commands: Commands) {
|
2024-08-25 14:12:13 +00:00
|
|
|
let system_id = commands.register_system(system_a);
|
2024-05-03 13:00:56 +00:00
|
|
|
commands.spawn((Callback(system_id), A));
|
2023-09-19 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 13:00:56 +00:00
|
|
|
fn setup_with_world(world: &mut World) {
|
|
|
|
// We can run it once manually
|
2024-09-30 01:00:39 +00:00
|
|
|
world.run_system_once(system_b).unwrap();
|
2024-05-03 13:00:56 +00:00
|
|
|
// Or with a Callback
|
|
|
|
let system_id = world.register_system(system_b);
|
|
|
|
world.spawn((Callback(system_id), B));
|
2023-09-19 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 13:00:56 +00:00
|
|
|
/// 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);
|
|
|
|
}
|
2023-09-19 20:17:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 13:00:56 +00:00
|
|
|
/// Runs the systems associated with each `Callback` component if the entity also has a `Triggered` component.
|
2023-09-19 20:17:05 +00:00
|
|
|
///
|
|
|
|
/// This could be done in an exclusive system rather than using `Commands` if preferred.
|
2024-05-03 13:00:56 +00:00
|
|
|
fn evaluate_callbacks(query: Query<(Entity, &Callback), With<Triggered>>, mut commands: Commands) {
|
|
|
|
for (entity, callback) in query.iter() {
|
2023-09-19 20:17:05 +00:00
|
|
|
commands.run_system(callback.0);
|
2024-05-03 13:00:56 +00:00
|
|
|
commands.entity(entity).remove::<Triggered>();
|
2023-09-19 20:17:05 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-03 13:00:56 +00:00
|
|
|
|
Text rework (#15591)
**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>
2024-10-09 18:35:36 +00:00
|
|
|
fn system_a(query: Query<Entity, With<Text>>, mut writer: UiTextWriter) {
|
|
|
|
*writer.text(query.single(), 3) = String::from("A");
|
2024-05-03 13:00:56 +00:00
|
|
|
info!("A: One shot system registered with Commands was triggered");
|
|
|
|
}
|
|
|
|
|
Text rework (#15591)
**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>
2024-10-09 18:35:36 +00:00
|
|
|
fn system_b(query: Query<Entity, With<Text>>, mut writer: UiTextWriter) {
|
|
|
|
*writer.text(query.single(), 3) = String::from("B");
|
2024-05-03 13:00:56 +00:00
|
|
|
info!("B: One shot system registered with World was triggered");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup_ui(mut commands: Commands) {
|
2024-10-05 01:59:52 +00:00
|
|
|
commands.spawn(Camera2d);
|
Text rework (#15591)
**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>
2024-10-09 18:35:36 +00:00
|
|
|
commands
|
|
|
|
.spawn((
|
|
|
|
Text::default(),
|
2024-10-09 20:58:27 +00:00
|
|
|
TextLayout::new_with_justify(JustifyText::Center),
|
Text rework (#15591)
**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>
2024-10-09 18:35:36 +00:00
|
|
|
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("-"),
|
2024-05-03 13:00:56 +00:00
|
|
|
TextStyle {
|
|
|
|
color: bevy::color::palettes::css::ORANGE.into(),
|
|
|
|
..default()
|
|
|
|
},
|
Text rework (#15591)
**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>
2024-10-09 18:35:36 +00:00
|
|
|
));
|
|
|
|
});
|
2024-05-03 13:00:56 +00:00
|
|
|
}
|