mirror of
https://github.com/bevyengine/bevy
synced 2024-12-02 09:29:12 +00:00
9eefd7c022
# Objective Remove the `VerticalAlign` enum. Text's alignment field should only affect the text's internal text alignment, not its position. The only way to control a `TextBundle`'s position and bounds should be through the manipulation of the constraints in the `Style` components of the nodes in the Bevy UI's layout tree. `Text2dBundle` should have a separate `Anchor` component that sets its position relative to its transform. Related issues: #676, #1490, #5502, #5513, #5834, #6717, #6724, #6741, #6748 ## Changelog * Changed `TextAlignment` into an enum with `Left`, `Center`, and `Right` variants. * Removed the `HorizontalAlign` and `VerticalAlign` types. * Added an `Anchor` component to `Text2dBundle` * Added `Component` derive to `Anchor` * Use `f32::INFINITY` instead of `f32::MAX` to represent unbounded text in Text2dBounds ## Migration Guide The `alignment` field of `Text` now only affects the text's internal alignment. ### Change `TextAlignment` to TextAlignment` which is now an enum. Replace: * `TextAlignment::TOP_LEFT`, `TextAlignment::CENTER_LEFT`, `TextAlignment::BOTTOM_LEFT` with `TextAlignment::Left` * `TextAlignment::TOP_CENTER`, `TextAlignment::CENTER_LEFT`, `TextAlignment::BOTTOM_CENTER` with `TextAlignment::Center` * `TextAlignment::TOP_RIGHT`, `TextAlignment::CENTER_RIGHT`, `TextAlignment::BOTTOM_RIGHT` with `TextAlignment::Right` ### Changes for `Text2dBundle` `Text2dBundle` has a new field 'text_anchor' that takes an `Anchor` component that controls its position relative to its transform.
91 lines
2.7 KiB
Rust
91 lines
2.7 KiB
Rust
//! How to use an external thread to run an infinite task and communicate with a channel.
|
|
|
|
use bevy::prelude::*;
|
|
// Using crossbeam_channel instead of std as std `Receiver` is `!Sync`
|
|
use crossbeam_channel::{bounded, Receiver};
|
|
use rand::Rng;
|
|
use std::time::{Duration, Instant};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_event::<StreamEvent>()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_startup_system(setup)
|
|
.add_system(read_stream)
|
|
.add_system(spawn_text)
|
|
.add_system(move_text)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Resource, Deref)]
|
|
struct StreamReceiver(Receiver<u32>);
|
|
struct StreamEvent(u32);
|
|
|
|
#[derive(Resource, Deref)]
|
|
struct LoadedFont(Handle<Font>);
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
commands.spawn(Camera2dBundle::default());
|
|
|
|
let (tx, rx) = bounded::<u32>(10);
|
|
std::thread::spawn(move || loop {
|
|
// Everything here happens in another thread
|
|
// This is where you could connect to an external data source
|
|
let mut rng = rand::thread_rng();
|
|
let start_time = Instant::now();
|
|
let duration = Duration::from_secs_f32(rng.gen_range(0.0..0.2));
|
|
while start_time.elapsed() < duration {
|
|
// Spinning for 'duration', simulating doing hard work!
|
|
}
|
|
|
|
tx.send(rng.gen_range(0..2000)).unwrap();
|
|
});
|
|
|
|
commands.insert_resource(StreamReceiver(rx));
|
|
commands.insert_resource(LoadedFont(asset_server.load("fonts/FiraSans-Bold.ttf")));
|
|
}
|
|
|
|
// This system reads from the receiver and sends events to Bevy
|
|
fn read_stream(receiver: Res<StreamReceiver>, mut events: EventWriter<StreamEvent>) {
|
|
for from_stream in receiver.try_iter() {
|
|
events.send(StreamEvent(from_stream));
|
|
}
|
|
}
|
|
|
|
fn spawn_text(
|
|
mut commands: Commands,
|
|
mut reader: EventReader<StreamEvent>,
|
|
loaded_font: Res<LoadedFont>,
|
|
) {
|
|
let text_style = TextStyle {
|
|
font: loaded_font.clone(),
|
|
font_size: 20.0,
|
|
color: Color::WHITE,
|
|
};
|
|
|
|
for (per_frame, event) in reader.iter().enumerate() {
|
|
commands.spawn(Text2dBundle {
|
|
text: Text::from_section(event.0.to_string(), text_style.clone())
|
|
.with_alignment(TextAlignment::Center),
|
|
transform: Transform::from_xyz(
|
|
per_frame as f32 * 100.0 + rand::thread_rng().gen_range(-40.0..40.0),
|
|
300.0,
|
|
0.0,
|
|
),
|
|
..default()
|
|
});
|
|
}
|
|
}
|
|
|
|
fn move_text(
|
|
mut commands: Commands,
|
|
mut texts: Query<(Entity, &mut Transform), With<Text>>,
|
|
time: Res<Time>,
|
|
) {
|
|
for (entity, mut position) in &mut texts {
|
|
position.translation -= Vec3::new(0.0, 100.0 * time.delta_seconds(), 0.0);
|
|
if position.translation.y < -300.0 {
|
|
commands.entity(entity).despawn();
|
|
}
|
|
}
|
|
}
|