Skip to content
Snippets Groups Projects
Commit a68eaa55 authored by Finn Bear's avatar Finn Bear
Browse files

Docs and cleanup.

parent bfae19f3
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,8 @@ use eframe::{egui, epi};
/// Top-level state.
pub struct Cartoon {
/// All slides (including their state).
///
/// Note: The slides are dynamically (`dyn`) typed.
slides: Vec<Box<dyn Slide>>,
/// Current index into [`slides`].
slide_index: usize,
......@@ -39,6 +41,8 @@ impl Default for Cartoon {
///
/// This is also how the chronology of the slideshow is determined.
fn create_slides() -> Vec<Box<dyn Slide>> {
// Slides are dynamically typed, so we use indirection (`Box`) to ensure the collection (`Vec`)
// has items whose sizes are the same and known at compile-time.
vec![
Box::new(Title::default()) as Box<dyn Slide>,
Box::new(Introduction::default()),
......@@ -98,18 +102,6 @@ impl epi::App for Cartoon {
fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) {
// For inspiration and more examples, go to https://emilk.github.io/egui
/*
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
egui::menu::bar(ui, |ui| {
ui.menu_button("File", |ui| {
if ui.button("Quit").clicked() {
frame.quit();
}
});
});
});
*/
// Go forward one slide (don't wait for intra-slide transitions).
//
// This will wrap around to the beginning.
......@@ -149,6 +141,9 @@ impl epi::App for Cartoon {
};
}
// The slide gets rendered in here.
// TODO(finnb): Figure out way to fade out old slide and fade in new slide at the same time,
// without breaking everything.
egui::CentralPanel::default().show(ctx, |ui| {
// The current slide may be fading in.
fade_in(ui, self.transition_time, |ui| {
......
......@@ -54,9 +54,11 @@ impl Slide for Automata {
// Need to continuously animate the grid, or at least poll the governor.
ui.ctx().request_repaint();
// Iterate grid ~5 times a second.
if self.governor.ready(ui.ctx().input().time, 0.2) {
self.life = conways_game_of_life(&self.life);
}
self.life.show(ui.ctx());
}
}
......
......@@ -106,7 +106,12 @@ impl Slide for Fractals {
ui.ctx().request_repaint();
// The function that will be used to color the grid.
let algo: Box<dyn Fn(f32, f32) -> bool>;
//
// It is dynamically typed (so that multiple distinct functions can be used).
//
// Note that we use indirection via a reference but not a heap-allocated [`Box`]. We can get
// away with that since the function doesn't leave our stack frame.
let algo: &dyn Fn(f32, f32) -> bool;
match self.state {
FractalsState::Before => {
......@@ -119,19 +124,19 @@ impl Slide for Fractals {
let alpha = (elapsed * 2.0).min(1.0) as f32;
self.code.alpha = alpha;
self.grid.alpha = alpha;
algo = Box::new(|_, _| false);
algo = &|_, _| false;
}
FractalsState::Rectangle { .. } => {
self.code.code = pseudocode(include_str!("s5_fractals/rectangle.rs"));
algo = Box::new(rectangle);
algo = &rectangle;
}
FractalsState::Circle { .. } => {
self.code.code = pseudocode(include_str!("s5_fractals/circle.rs"));
algo = Box::new(circle);
algo = &circle;
}
FractalsState::Mandelbrot { .. } => {
self.code.code = pseudocode(include_str!("s5_fractals/mandelbrot.rs"));
algo = Box::new(mandelbrot);
algo = &mandelbrot;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment