| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
use std::time::Duration; |
| 18 |
|
| 19 |
use bevy::{color::palettes, prelude::*}; |
| 20 |
|
| 21 |
fn main() { |
| 22 |
let (sender, receiver) = crossbeam_channel::unbounded::<()>(); |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
start_thread(receiver); |
| 27 |
|
| 28 |
App::new() |
| 29 |
.add_plugins(DefaultPlugins) |
| 30 |
.insert_resource(TaskSender(sender)) |
| 31 |
.add_systems(Startup, setup) |
| 32 |
.add_systems(Update, update_text) |
| 33 |
.run(); |
| 34 |
} |
| 35 |
|
| 36 |
fn update_text(mut text: Single<&mut Text>) { |
| 37 |
|
| 38 |
|
| 39 |
text.0 = "before".to_string(); |
| 40 |
} |
| 41 |
|
| 42 |
fn on_click( |
| 43 |
_click: On<PointerClick>, |
| 44 |
mut color: Single<&mut TextColor>, |
| 45 |
task_sender: Res<TaskSender>, |
| 46 |
) { |
| 47 |
|
| 48 |
|
| 49 |
color.0 = palettes::tailwind::RED_600.into(); |
| 50 |
|
| 51 |
let _ = task_sender.0.send(()); |
| 52 |
} |
| 53 |
|
| 54 |
#[derive(Resource)] |
| 55 |
struct TaskSender(crossbeam_channel::Sender<()>); |
| 56 |
|
| 57 |
fn setup(mut commands: Commands) { |
| 58 |
commands.spawn(Camera2d); |
| 59 |
|
| 60 |
commands |
| 61 |
.spawn(( |
| 62 |
Node { |
| 63 |
width: percent(100), |
| 64 |
height: percent(100), |
| 65 |
align_items: AlignItems::Center, |
| 66 |
justify_content: JustifyContent::Center, |
| 67 |
flex_direction: FlexDirection::Column, |
| 68 |
..default() |
| 69 |
}, |
| 70 |
children![( |
| 71 |
Text::default(), |
| 72 |
TextFont { |
| 73 |
font_size: FontSize::Px(100.0), |
| 74 |
..default() |
| 75 |
}, |
| 76 |
)], |
| 77 |
)) |
| 78 |
.observe(on_click); |
| 79 |
} |
| 80 |
|
| 81 |
fn start_thread(receiver: crossbeam_channel::Receiver<()>) { |
| 82 |
std::thread::spawn(move || { |
| 83 |
while receiver.recv().is_ok() { |
| 84 |
let start = bevy::platform::time::Instant::now(); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
let duration = bevy::app::hotpatch::call(|| Duration::from_secs(2)); |
| 89 |
|
| 90 |
std::thread::sleep(duration); |
| 91 |
info!("done after {:?}", start.elapsed()); |
| 92 |
} |
| 93 |
}); |
| 94 |
} |