2022-05-16 13:53:20 +00:00
|
|
|
//! This example displays each contributor to the bevy source code as a bouncing bevy-ball.
|
|
|
|
|
2024-03-07 02:30:15 +00:00
|
|
|
use bevy::{math::bounding::Aabb2d, prelude::*, utils::HashMap};
|
2020-11-06 22:35:18 +00:00
|
|
|
use rand::{prelude::SliceRandom, Rng};
|
|
|
|
use std::{
|
2021-12-08 20:09:53 +00:00
|
|
|
env::VarError,
|
2024-03-01 14:53:48 +00:00
|
|
|
hash::{DefaultHasher, Hash, Hasher},
|
2021-12-08 20:09:53 +00:00
|
|
|
io::{self, BufRead, BufReader},
|
2020-11-06 22:35:18 +00:00
|
|
|
process::Stdio,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
2021-07-27 20:21:06 +00:00
|
|
|
App::new()
|
2020-11-06 22:35:18 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2024-03-01 14:53:48 +00:00
|
|
|
.init_resource::<SelectionTimer>()
|
2023-03-18 01:45:34 +00:00
|
|
|
.add_systems(Startup, (setup_contributor_selection, setup))
|
2024-03-01 14:53:48 +00:00
|
|
|
.add_systems(Update, (gravity, movement, collisions, selection))
|
2020-11-06 22:35:18 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2024-03-01 14:53:48 +00:00
|
|
|
// Store contributors with their commit count in a collection that preserves the uniqueness
|
|
|
|
type Contributors = HashMap<String, usize>;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
Make `Resource` trait opt-in, requiring `#[derive(Resource)]` V2 (#5577)
*This PR description is an edited copy of #5007, written by @alice-i-cecile.*
# Objective
Follow-up to https://github.com/bevyengine/bevy/pull/2254. The `Resource` trait currently has a blanket implementation for all types that meet its bounds.
While ergonomic, this results in several drawbacks:
* it is possible to make confusing, silent mistakes such as inserting a function pointer (Foo) rather than a value (Foo::Bar) as a resource
* it is challenging to discover if a type is intended to be used as a resource
* we cannot later add customization options (see the [RFC](https://github.com/bevyengine/rfcs/blob/main/rfcs/27-derive-component.md) for the equivalent choice for Component).
* dependencies can use the same Rust type as a resource in invisibly conflicting ways
* raw Rust types used as resources cannot preserve privacy appropriately, as anyone able to access that type can read and write to internal values
* we cannot capture a definitive list of possible resources to display to users in an editor
## Notes to reviewers
* Review this commit-by-commit; there's effectively no back-tracking and there's a lot of churn in some of these commits.
*ira: My commits are not as well organized :')*
* I've relaxed the bound on Local to Send + Sync + 'static: I don't think these concerns apply there, so this can keep things simple. Storing e.g. a u32 in a Local is fine, because there's a variable name attached explaining what it does.
* I think this is a bad place for the Resource trait to live, but I've left it in place to make reviewing easier. IMO that's best tackled with https://github.com/bevyengine/bevy/issues/4981.
## Changelog
`Resource` is no longer automatically implemented for all matching types. Instead, use the new `#[derive(Resource)]` macro.
## Migration Guide
Add `#[derive(Resource)]` to all types you are using as a resource.
If you are using a third party type as a resource, wrap it in a tuple struct to bypass orphan rules. Consider deriving `Deref` and `DerefMut` to improve ergonomics.
`ClearColor` no longer implements `Component`. Using `ClearColor` as a component in 0.8 did nothing.
Use the `ClearColorConfig` in the `Camera3d` and `Camera2d` components instead.
Co-authored-by: Alice <alice.i.cecile@gmail.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: devil-ira <justthecooldude@gmail.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2022-08-08 21:36:35 +00:00
|
|
|
#[derive(Resource)]
|
2020-11-06 22:35:18 +00:00
|
|
|
struct ContributorSelection {
|
2022-04-14 19:30:36 +00:00
|
|
|
order: Vec<Entity>,
|
2020-11-06 22:35:18 +00:00
|
|
|
idx: usize,
|
|
|
|
}
|
|
|
|
|
Make `Resource` trait opt-in, requiring `#[derive(Resource)]` V2 (#5577)
*This PR description is an edited copy of #5007, written by @alice-i-cecile.*
# Objective
Follow-up to https://github.com/bevyengine/bevy/pull/2254. The `Resource` trait currently has a blanket implementation for all types that meet its bounds.
While ergonomic, this results in several drawbacks:
* it is possible to make confusing, silent mistakes such as inserting a function pointer (Foo) rather than a value (Foo::Bar) as a resource
* it is challenging to discover if a type is intended to be used as a resource
* we cannot later add customization options (see the [RFC](https://github.com/bevyengine/rfcs/blob/main/rfcs/27-derive-component.md) for the equivalent choice for Component).
* dependencies can use the same Rust type as a resource in invisibly conflicting ways
* raw Rust types used as resources cannot preserve privacy appropriately, as anyone able to access that type can read and write to internal values
* we cannot capture a definitive list of possible resources to display to users in an editor
## Notes to reviewers
* Review this commit-by-commit; there's effectively no back-tracking and there's a lot of churn in some of these commits.
*ira: My commits are not as well organized :')*
* I've relaxed the bound on Local to Send + Sync + 'static: I don't think these concerns apply there, so this can keep things simple. Storing e.g. a u32 in a Local is fine, because there's a variable name attached explaining what it does.
* I think this is a bad place for the Resource trait to live, but I've left it in place to make reviewing easier. IMO that's best tackled with https://github.com/bevyengine/bevy/issues/4981.
## Changelog
`Resource` is no longer automatically implemented for all matching types. Instead, use the new `#[derive(Resource)]` macro.
## Migration Guide
Add `#[derive(Resource)]` to all types you are using as a resource.
If you are using a third party type as a resource, wrap it in a tuple struct to bypass orphan rules. Consider deriving `Deref` and `DerefMut` to improve ergonomics.
`ClearColor` no longer implements `Component`. Using `ClearColor` as a component in 0.8 did nothing.
Use the `ClearColorConfig` in the `Camera3d` and `Camera2d` components instead.
Co-authored-by: Alice <alice.i.cecile@gmail.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: devil-ira <justthecooldude@gmail.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2022-08-08 21:36:35 +00:00
|
|
|
#[derive(Resource)]
|
2024-03-01 14:53:48 +00:00
|
|
|
struct SelectionTimer(Timer);
|
2022-04-08 04:02:14 +00:00
|
|
|
|
2024-03-01 14:53:48 +00:00
|
|
|
impl Default for SelectionTimer {
|
2022-04-08 04:02:14 +00:00
|
|
|
fn default() -> Self {
|
2024-03-01 14:53:48 +00:00
|
|
|
Self(Timer::from_seconds(
|
|
|
|
SHOWCASE_TIMER_SECS,
|
|
|
|
TimerMode::Repeating,
|
|
|
|
))
|
2022-04-08 04:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-10-03 19:23:44 +00:00
|
|
|
#[derive(Component)]
|
2020-11-06 22:35:18 +00:00
|
|
|
struct ContributorDisplay;
|
|
|
|
|
2021-10-03 19:23:44 +00:00
|
|
|
#[derive(Component)]
|
2020-11-06 22:35:18 +00:00
|
|
|
struct Contributor {
|
2022-04-14 19:30:36 +00:00
|
|
|
name: String,
|
2024-03-01 14:53:48 +00:00
|
|
|
num_commits: usize,
|
2021-03-17 23:59:51 +00:00
|
|
|
hue: f32,
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 19:23:44 +00:00
|
|
|
#[derive(Component)]
|
2020-11-06 22:35:18 +00:00
|
|
|
struct Velocity {
|
|
|
|
translation: Vec3,
|
|
|
|
rotation: f32,
|
|
|
|
}
|
|
|
|
|
2022-07-17 12:34:31 +00:00
|
|
|
const GRAVITY: f32 = 9.821 * 100.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
const SPRITE_SIZE: f32 = 75.0;
|
|
|
|
|
2024-03-01 14:53:48 +00:00
|
|
|
const SELECTED: Hsla = Hsla::hsl(0.0, 0.9, 0.7);
|
|
|
|
const DESELECTED: Hsla = Hsla::new(0.0, 0.3, 0.2, 0.92);
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
const SHOWCASE_TIMER_SECS: f32 = 3.0;
|
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
const CONTRIBUTORS_LIST: &[&str] = &["Carter Anderson", "And Many More"];
|
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
fn setup_contributor_selection(mut commands: Commands, asset_server: Res<AssetServer>) {
|
2021-12-08 20:09:53 +00:00
|
|
|
// Load contributors from the git history log or use default values from
|
2024-03-01 14:53:48 +00:00
|
|
|
// the constant array. Contributors are stored in a HashMap with their
|
|
|
|
// commit count.
|
2021-12-08 20:09:53 +00:00
|
|
|
let contribs = contributors().unwrap_or_else(|_| {
|
|
|
|
CONTRIBUTORS_LIST
|
|
|
|
.iter()
|
2024-03-01 14:53:48 +00:00
|
|
|
.map(|name| (name.to_string(), 1))
|
2021-12-08 20:09:53 +00:00
|
|
|
.collect()
|
|
|
|
});
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
let texture_handle = asset_server.load("branding/icon.png");
|
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
let mut contributor_selection = ContributorSelection {
|
2022-04-08 04:02:14 +00:00
|
|
|
order: Vec::with_capacity(contribs.len()),
|
2020-11-06 22:35:18 +00:00
|
|
|
idx: 0,
|
|
|
|
};
|
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
let mut rng = rand::thread_rng();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2024-03-01 14:53:48 +00:00
|
|
|
for (name, num_commits) in contribs {
|
|
|
|
let transform =
|
|
|
|
Transform::from_xyz(rng.gen_range(-400.0..400.0), rng.gen_range(0.0..400.0), 0.0);
|
2022-04-14 19:30:36 +00:00
|
|
|
let dir = rng.gen_range(-1.0..1.0);
|
2020-11-06 22:35:18 +00:00
|
|
|
let velocity = Vec3::new(dir * 500.0, 0.0, 0.0);
|
2024-03-01 14:53:48 +00:00
|
|
|
let hue = name_to_hue(&name);
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2024-03-01 14:53:48 +00:00
|
|
|
// Some sprites should be flipped for variety
|
|
|
|
let flipped = rng.gen();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
let entity = commands
|
Spawn now takes a Bundle (#6054)
# Objective
Now that we can consolidate Bundles and Components under a single insert (thanks to #2975 and #6039), almost 100% of world spawns now look like `world.spawn().insert((Some, Tuple, Here))`. Spawning an entity without any components is an extremely uncommon pattern, so it makes sense to give spawn the "first class" ergonomic api. This consolidated api should be made consistent across all spawn apis (such as World and Commands).
## Solution
All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input:
```rust
// before:
commands
.spawn()
.insert((A, B, C));
world
.spawn()
.insert((A, B, C);
// after
commands.spawn((A, B, C));
world.spawn((A, B, C));
```
All existing instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api. A new `spawn_empty` has been added, replacing the old `spawn` api.
By allowing `world.spawn(some_bundle)` to replace `world.spawn().insert(some_bundle)`, this opened the door to removing the initial entity allocation in the "empty" archetype / table done in `spawn()` (and subsequent move to the actual archetype in `.insert(some_bundle)`).
This improves spawn performance by over 10%:
![image](https://user-images.githubusercontent.com/2694663/191627587-4ab2f949-4ccd-4231-80eb-80dd4d9ad6b9.png)
To take this measurement, I added a new `world_spawn` benchmark.
Unfortunately, optimizing `Commands::spawn` is slightly less trivial, as Commands expose the Entity id of spawned entities prior to actually spawning. Doing the optimization would (naively) require assurances that the `spawn(some_bundle)` command is applied before all other commands involving the entity (which would not necessarily be true, if memory serves). Optimizing `Commands::spawn` this way does feel possible, but it will require careful thought (and maybe some additional checks), which deserves its own PR. For now, it has the same performance characteristics of the current `Commands::spawn_bundle` on main.
**Note that 99% of this PR is simple renames and refactors. The only code that needs careful scrutiny is the new `World::spawn()` impl, which is relatively straightforward, but it has some new unsafe code (which re-uses battle tested BundlerSpawner code path).**
---
## Changelog
- All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input
- All instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api
- World and Commands now have `spawn_empty()`, which is equivalent to the old `spawn()` behavior.
## Migration Guide
```rust
// Old (0.8):
commands
.spawn()
.insert_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
commands.spawn_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
let entity = commands.spawn().id();
// New (0.9)
let entity = commands.spawn_empty().id();
// Old (0.8)
let entity = world.spawn().id();
// New (0.9)
let entity = world.spawn_empty();
```
2022-09-23 19:55:54 +00:00
|
|
|
.spawn((
|
2024-03-01 14:53:48 +00:00
|
|
|
Contributor {
|
|
|
|
name,
|
|
|
|
num_commits,
|
|
|
|
hue,
|
|
|
|
},
|
2021-03-23 00:23:40 +00:00
|
|
|
Velocity {
|
|
|
|
translation: velocity,
|
|
|
|
rotation: -dir * 5.0,
|
|
|
|
},
|
2024-10-09 16:17:26 +00:00
|
|
|
Sprite {
|
|
|
|
image: texture_handle.clone(),
|
|
|
|
custom_size: Some(Vec2::splat(SPRITE_SIZE)),
|
|
|
|
color: DESELECTED.with_hue(hue).into(),
|
|
|
|
flip_x: flipped,
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-11-06 22:35:18 +00:00
|
|
|
},
|
2024-10-09 16:17:26 +00:00
|
|
|
transform,
|
2022-09-21 21:47:53 +00:00
|
|
|
))
|
2021-03-23 00:23:40 +00:00
|
|
|
.id();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
contributor_selection.order.push(entity);
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
contributor_selection.order.shuffle(&mut rng);
|
2021-12-08 20:09:53 +00:00
|
|
|
|
|
|
|
commands.insert_resource(contributor_selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
2024-10-05 01:59:52 +00:00
|
|
|
commands.spawn(Camera2d);
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2024-10-13 17:06:22 +00:00
|
|
|
let text_style = TextFont {
|
2024-03-01 14:53:48 +00:00
|
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
|
|
font_size: 60.0,
|
|
|
|
..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
|
|
|
commands
|
|
|
|
.spawn((
|
|
|
|
Text::new("Contributor showcase"),
|
|
|
|
text_style.clone(),
|
|
|
|
ContributorDisplay,
|
|
|
|
Style {
|
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
top: Val::Px(12.),
|
|
|
|
left: Val::Px(12.),
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
.with_child((
|
|
|
|
TextSpan::default(),
|
2024-10-13 17:06:22 +00:00
|
|
|
TextFont {
|
2024-03-01 14:53:48 +00:00
|
|
|
font_size: 30.,
|
|
|
|
..text_style
|
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
|
|
|
},
|
|
|
|
));
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds the next contributor to display and selects the entity
|
2024-03-01 14:53:48 +00:00
|
|
|
fn selection(
|
|
|
|
mut timer: ResMut<SelectionTimer>,
|
2021-12-08 20:09:53 +00:00
|
|
|
mut contributor_selection: ResMut<ContributorSelection>,
|
2024-10-13 20:32:06 +00:00
|
|
|
contributor_root: Single<Entity, (With<ContributorDisplay>, With<Text>)>,
|
2021-12-14 03:58:23 +00:00
|
|
|
mut query: Query<(&Contributor, &mut Sprite, &mut Transform)>,
|
2024-10-15 02:32:34 +00:00
|
|
|
mut writer: TextUiWriter,
|
2020-12-13 19:31:50 +00:00
|
|
|
time: Res<Time>,
|
2020-11-06 22:35:18 +00:00
|
|
|
) {
|
2024-03-01 14:53:48 +00:00
|
|
|
if !timer.0.tick(time.delta()).just_finished() {
|
2020-11-06 22:35:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-01 14:53:48 +00:00
|
|
|
// Deselect the previous contributor
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
let entity = contributor_selection.order[contributor_selection.idx];
|
|
|
|
if let Ok((contributor, mut sprite, mut transform)) = query.get_mut(entity) {
|
|
|
|
deselect(&mut sprite, contributor, &mut transform);
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 14:53:48 +00:00
|
|
|
// Select the next contributor
|
|
|
|
|
2022-04-08 04:02:14 +00:00
|
|
|
if (contributor_selection.idx + 1) < contributor_selection.order.len() {
|
|
|
|
contributor_selection.idx += 1;
|
|
|
|
} else {
|
|
|
|
contributor_selection.idx = 0;
|
|
|
|
}
|
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
let entity = contributor_selection.order[contributor_selection.idx];
|
2021-12-08 20:09:53 +00:00
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
if let Ok((contributor, mut sprite, mut transform)) = query.get_mut(entity) {
|
2024-10-13 20:32:06 +00:00
|
|
|
let entity = *contributor_root;
|
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
|
|
|
select(
|
|
|
|
&mut sprite,
|
|
|
|
contributor,
|
|
|
|
&mut transform,
|
|
|
|
entity,
|
|
|
|
&mut writer,
|
|
|
|
);
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 19:30:36 +00:00
|
|
|
/// Change the tint color to the "selected" color, bring the object to the front
|
|
|
|
/// and display the name.
|
2020-11-06 22:35:18 +00:00
|
|
|
fn select(
|
2021-12-14 03:58:23 +00:00
|
|
|
sprite: &mut Sprite,
|
2021-12-08 20:09:53 +00:00
|
|
|
contributor: &Contributor,
|
|
|
|
transform: &mut Transform,
|
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
|
|
|
entity: Entity,
|
2024-10-15 02:32:34 +00:00
|
|
|
writer: &mut TextUiWriter,
|
2021-12-14 03:58:23 +00:00
|
|
|
) {
|
2024-03-01 14:53:48 +00:00
|
|
|
sprite.color = SELECTED.with_hue(contributor.hue).into();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
transform.translation.z = 100.0;
|
2020-11-06 22:35:18 +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
|
|
|
writer.text(entity, 0).clone_from(&contributor.name);
|
|
|
|
*writer.text(entity, 1) = format!(
|
2024-03-01 14:53:48 +00:00
|
|
|
"\n{} commit{}",
|
|
|
|
contributor.num_commits,
|
|
|
|
if contributor.num_commits > 1 { "s" } else { "" }
|
|
|
|
);
|
2024-10-13 17:06:22 +00:00
|
|
|
writer.color(entity, 0).0 = sprite.color;
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 14:53:48 +00:00
|
|
|
/// Change the tint color to the "deselected" color and push
|
2020-11-06 22:35:18 +00:00
|
|
|
/// the object to the back.
|
2021-12-14 03:58:23 +00:00
|
|
|
fn deselect(sprite: &mut Sprite, contributor: &Contributor, transform: &mut Transform) {
|
2024-03-01 14:53:48 +00:00
|
|
|
sprite.color = DESELECTED.with_hue(contributor.hue).into();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
transform.translation.z = 0.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 14:53:48 +00:00
|
|
|
/// Applies gravity to all entities with a velocity.
|
|
|
|
fn gravity(time: Res<Time>, mut velocity_query: Query<&mut Velocity>) {
|
2020-11-28 21:08:31 +00:00
|
|
|
let delta = time.delta_seconds();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2022-07-11 15:28:50 +00:00
|
|
|
for mut velocity in &mut velocity_query {
|
2022-07-17 12:34:31 +00:00
|
|
|
velocity.translation.y -= GRAVITY * delta;
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-01 14:53:48 +00:00
|
|
|
/// Checks for collisions of contributor-birbs.
|
2020-11-06 22:35:18 +00:00
|
|
|
///
|
|
|
|
/// On collision with left-or-right wall it resets the horizontal
|
|
|
|
/// velocity. On collision with the ground it applies an upwards
|
|
|
|
/// force.
|
2024-03-01 14:53:48 +00:00
|
|
|
fn collisions(
|
2024-10-13 20:32:06 +00:00
|
|
|
window: Single<&Window>,
|
2021-12-08 20:09:53 +00:00
|
|
|
mut query: Query<(&mut Velocity, &mut Transform), With<Contributor>>,
|
2020-11-06 22:35:18 +00:00
|
|
|
) {
|
2024-03-01 22:28:37 +00:00
|
|
|
let window_size = window.size();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2024-03-01 14:53:48 +00:00
|
|
|
let collision_area = Aabb2d::new(Vec2::ZERO, (window_size - SPRITE_SIZE) / 2.);
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2022-07-17 12:34:31 +00:00
|
|
|
// The maximum height the birbs should try to reach is one birb below the top of the window.
|
2024-03-01 14:53:48 +00:00
|
|
|
let max_bounce_height = (window_size.y - SPRITE_SIZE * 2.0).max(0.0);
|
|
|
|
let min_bounce_height = max_bounce_height * 0.4;
|
2022-07-17 12:34:31 +00:00
|
|
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
2022-07-11 15:28:50 +00:00
|
|
|
for (mut velocity, mut transform) in &mut query {
|
2024-03-01 14:53:48 +00:00
|
|
|
// Clamp the translation to not go out of the bounds
|
|
|
|
if transform.translation.y < collision_area.min.y {
|
|
|
|
transform.translation.y = collision_area.min.y;
|
2022-07-17 12:34:31 +00:00
|
|
|
|
|
|
|
// How high this birb will bounce.
|
2024-03-01 14:53:48 +00:00
|
|
|
let bounce_height = rng.gen_range(min_bounce_height..=max_bounce_height);
|
2022-07-17 12:34:31 +00:00
|
|
|
|
|
|
|
// Apply the velocity that would bounce the birb up to bounce_height.
|
|
|
|
velocity.translation.y = (bounce_height * GRAVITY * 2.).sqrt();
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
2024-03-01 14:53:48 +00:00
|
|
|
|
|
|
|
// Birbs might hit the ceiling if the window is resized.
|
|
|
|
// If they do, bounce them.
|
|
|
|
if transform.translation.y > collision_area.max.y {
|
|
|
|
transform.translation.y = collision_area.max.y;
|
2022-07-17 12:34:31 +00:00
|
|
|
velocity.translation.y *= -1.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
2024-03-01 14:53:48 +00:00
|
|
|
|
|
|
|
// On side walls flip the horizontal velocity
|
|
|
|
if transform.translation.x < collision_area.min.x {
|
|
|
|
transform.translation.x = collision_area.min.x;
|
2021-12-08 20:09:53 +00:00
|
|
|
velocity.translation.x *= -1.0;
|
|
|
|
velocity.rotation *= -1.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
2024-03-01 14:53:48 +00:00
|
|
|
if transform.translation.x > collision_area.max.x {
|
|
|
|
transform.translation.x = collision_area.max.x;
|
2021-12-08 20:09:53 +00:00
|
|
|
velocity.translation.x *= -1.0;
|
|
|
|
velocity.rotation *= -1.0;
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply velocity to positions and rotations.
|
2024-03-01 14:53:48 +00:00
|
|
|
fn movement(time: Res<Time>, mut query: Query<(&Velocity, &mut Transform)>) {
|
2020-11-28 21:08:31 +00:00
|
|
|
let delta = time.delta_seconds();
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2022-07-11 15:28:50 +00:00
|
|
|
for (velocity, mut transform) in &mut query {
|
2021-12-08 20:09:53 +00:00
|
|
|
transform.translation += delta * velocity.translation;
|
2022-07-01 03:58:54 +00:00
|
|
|
transform.rotate_z(velocity.rotation * delta);
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 22:20:37 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
2021-12-08 20:09:53 +00:00
|
|
|
enum LoadContributorsError {
|
2023-10-28 22:20:37 +00:00
|
|
|
#[error("An IO error occurred while reading the git log.")]
|
|
|
|
Io(#[from] io::Error),
|
|
|
|
#[error("The CARGO_MANIFEST_DIR environment variable was not set.")]
|
|
|
|
Var(#[from] VarError),
|
|
|
|
#[error("The git process did not return a stdout handle.")]
|
2021-12-08 20:09:53 +00:00
|
|
|
Stdout,
|
|
|
|
}
|
|
|
|
|
2024-03-01 14:53:48 +00:00
|
|
|
/// Get the names and commit counts of all contributors from the git log.
|
2020-11-06 22:35:18 +00:00
|
|
|
///
|
|
|
|
/// This function only works if `git` is installed and
|
|
|
|
/// the program is run through `cargo`.
|
2021-12-08 20:09:53 +00:00
|
|
|
fn contributors() -> Result<Contributors, LoadContributorsError> {
|
2023-10-28 22:20:37 +00:00
|
|
|
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")?;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
|
|
|
let mut cmd = std::process::Command::new("git")
|
2022-10-26 19:15:15 +00:00
|
|
|
.args(["--no-pager", "log", "--pretty=format:%an"])
|
2020-11-06 22:35:18 +00:00
|
|
|
.current_dir(manifest_dir)
|
|
|
|
.stdout(Stdio::piped())
|
2023-10-28 22:20:37 +00:00
|
|
|
.spawn()?;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2021-12-08 20:09:53 +00:00
|
|
|
let stdout = cmd.stdout.take().ok_or(LoadContributorsError::Stdout)?;
|
2020-11-06 22:35:18 +00:00
|
|
|
|
2024-03-01 14:53:48 +00:00
|
|
|
// Take the list of commit author names and collect them into a HashMap,
|
|
|
|
// keeping a count of how many commits they authored.
|
|
|
|
let contributors = BufReader::new(stdout).lines().map_while(Result::ok).fold(
|
|
|
|
HashMap::new(),
|
|
|
|
|mut acc, word| {
|
|
|
|
*acc.entry(word).or_insert(0) += 1;
|
|
|
|
acc
|
|
|
|
},
|
|
|
|
);
|
2021-12-08 20:09:53 +00:00
|
|
|
|
|
|
|
Ok(contributors)
|
2020-11-06 22:35:18 +00:00
|
|
|
}
|
2024-03-01 14:53:48 +00:00
|
|
|
|
|
|
|
/// Give each unique contributor name a particular hue that is stable between runs.
|
|
|
|
fn name_to_hue(s: &str) -> f32 {
|
|
|
|
let mut hasher = DefaultHasher::new();
|
|
|
|
s.hash(&mut hasher);
|
|
|
|
hasher.finish() as f32 / u64::MAX as f32 * 360.
|
|
|
|
}
|