pause simulation, instead of resetting (#2502)

This commit is contained in:
WorldSEnder 2022-03-18 14:30:04 +01:00 committed by GitHub
parent ab9fadf2e2
commit dd5d5b5ccc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,15 +24,17 @@ pub struct Props {
pub struct Simulation {
boids: Vec<Boid>,
interval: Interval,
settings: Settings,
generation: usize,
}
impl Component for Simulation {
type Message = Msg;
type Properties = Props;
fn create(ctx: &Context<Self>) -> Self {
let settings = &ctx.props().settings;
let settings = ctx.props().settings.clone();
let boids = (0..settings.boids)
.map(|_| Boid::new_random(settings))
.map(|_| Boid::new_random(&settings))
.collect();
let interval = {
@ -42,7 +44,13 @@ impl Component for Simulation {
})
};
Self { boids, interval }
let generation = ctx.props().generation;
Self {
boids,
interval,
settings,
generation,
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
@ -65,22 +73,30 @@ impl Component for Simulation {
}
fn changed(&mut self, ctx: &Context<Self>) -> bool {
self.boids.clear();
let props = ctx.props();
let should_reset = self.settings != props.settings || self.generation != props.generation;
self.settings = props.settings.clone();
self.generation = props.generation;
if should_reset {
self.boids.clear();
let settings = &ctx.props().settings;
self.boids
.resize_with(settings.boids, || Boid::new_random(settings));
let settings = &props.settings;
self.boids
.resize_with(settings.boids, || Boid::new_random(settings));
// as soon as the previous task is dropped it is cancelled.
// We don't need to worry about manually stopping it.
self.interval = {
let link = ctx.link().clone();
Interval::new(settings.tick_interval_ms as u32, move || {
link.send_message(Msg::Tick)
})
};
// as soon as the previous task is dropped it is cancelled.
// We don't need to worry about manually stopping it.
self.interval = {
let link = ctx.link().clone();
Interval::new(settings.tick_interval_ms as u32, move || {
link.send_message(Msg::Tick)
})
};
true
true
} else {
false
}
}
fn view(&self, _ctx: &Context<Self>) -> Html {