Newer
Older
mod circle;
mod mandelbrot;
mod rectangle;
use crate::component::code::{pseudocode, Code};
use crate::component::grid::Grid;
use crate::egui::{Color32, Context, Frame, Ui};
use crate::slide::s5_fractals::circle::circle;
use crate::slide::s5_fractals::mandelbrot::mandelbrot;
use crate::slide::s5_fractals::rectangle::rectangle;
#[derive(Default)]
#[allow(unused)]
enum FractalsState {
/// TODO(finnb): How many pixels of the grid we've filled in.
/// TODO(finnb): How many pixels of the grid we've filled in.
/// TODO(finnb): How many pixels of the grid we've filled in.
impl Default for Fractals {
fn default() -> Self {
const HORIZONTAL_OFFSET: f32 = 275.0;
code.name = "fractal_code";
code.offset_x = -HORIZONTAL_OFFSET;
let mut grid = Grid::default();
grid.name = "fractal_grid";
grid.offset_x = HORIZONTAL_OFFSET;
// Grid cell stroke aliases too much at this resolution.
grid.stroke_width = 0.0;
Self {
state: FractalsState::default(),
code,
grid,
}
}
impl Slide for Fractals {
fn transition(&mut self, ctx: &Context) -> bool {
// Increment state by one.
match self.state {
FractalsState::Before => {
self.state = FractalsState::Grid {
fade_start: ctx.input().time,
}
}
FractalsState::Grid { .. } => {
// Make sure we finished fading in.
self.code.alpha = 1.0;
self.grid.alpha = 1.0;
self.state = FractalsState::Rectangle { pixel: 0 }
}
FractalsState::Rectangle { .. } => self.state = FractalsState::Circle { pixel: 0 },
FractalsState::Circle { .. } => self.state = FractalsState::Mandelbrot { pixel: 0 },
FractalsState::Mandelbrot { .. } => return true,
}
false
}
fn show(&mut self, ui: &mut Ui) {
Frame::none().margin(Margin::same(20.0)).show(ui, |ui| {
ui.vertical_centered(|ui| {
ui.heading("Fractals");
});
});
let algo: Box<dyn Fn(f32, f32) -> bool>;
match self.state {
FractalsState::Before => {
return;
}
FractalsState::Grid { fade_start } => {
let elapsed = ui.ctx().input().time - fade_start;
let alpha = (elapsed * 2.0).min(1.0) as f32;
self.code.alpha = alpha;
self.grid.alpha = alpha;
algo = Box::new(|_, _| false);
}
FractalsState::Rectangle { .. } => {
self.code.code = pseudocode(include_str!("s5_fractals/rectangle.rs"));
algo = Box::new(rectangle);
}
FractalsState::Circle { .. } => {
self.code.code = pseudocode(include_str!("s5_fractals/circle.rs"));
algo = Box::new(circle);
}
FractalsState::Mandelbrot { .. } => {
self.code.code = pseudocode(include_str!("s5_fractals/mandelbrot.rs"));
algo = Box::new(mandelbrot);
}
}
// TODO: Different demos may need different resolutions.
// Paint the grid.
// TODO(finnb): Paint pixel by pixel.
// 0 to 1.
let nx = (gx as f32 + 0.5) / RESOLUTION as f32;
let ny = (gy as f32 + 0.5) / RESOLUTION as f32;
// -4 to 4.
let x = nx * 4.0 - 2.0;
let y = ny * 4.0 - 2.0;
Color32::BLACK
} else {
Color32::TRANSPARENT
};
}
self.code.show(ui.ctx());
self.grid.show(ui.ctx());