From 2b1c00ea89e0d0aff003b2c86572deb6083b395f Mon Sep 17 00:00:00 2001 From: Finn Bear <finnbearlabs@gmail.com> Date: Wed, 27 Apr 2022 20:24:15 -0700 Subject: [PATCH] Add some documentation. --- src/color.rs | 1 + src/component.rs | 1 + src/component/code.rs | 3 ++- src/fade_in.rs | 1 + src/governor.rs | 2 ++ src/image.rs | 3 +++ src/main.rs | 31 +++++++++++++++++++++++++++++++ src/slide.rs | 1 + 8 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/color.rs b/src/color.rs index 54b6d31..6e037ee 100644 --- a/src/color.rs +++ b/src/color.rs @@ -56,6 +56,7 @@ pub fn set_style_alpha_in_place(style: &mut Style, factor: f32) { //style.visuals.widgets.noninteractive.bg_fill = Color32::TRANSPARENT; } +/// Render some children with the specified alpha from 0 to 1. pub fn with_alpha<R>( ui: &mut Ui, alpha: f32, diff --git a/src/component.rs b/src/component.rs index 5dfb4bb..cf13fbc 100644 --- a/src/component.rs +++ b/src/component.rs @@ -1,2 +1,3 @@ +/// See component directory. pub mod code; pub mod grid; diff --git a/src/component/code.rs b/src/component/code.rs index dc830e1..056e436 100644 --- a/src/component/code.rs +++ b/src/component/code.rs @@ -29,7 +29,7 @@ impl Default for Code { size_pixels: 400.0, background: Color32::from_rgb(230, 230, 230), alpha: 1.0, - code: String::from(r#"println!("Hello world!");"#), + code: String::from(r#"println("Hello world!");"#), } } } @@ -83,5 +83,6 @@ pub fn pseudocode(code: &str) -> String { .replace("mut ", "") .replace(": f32", "") .replace(" -> bool", "") + .replace("println!", "println") .replace(";\n", "\n") } diff --git a/src/fade_in.rs b/src/fade_in.rs index a515e5c..f028cab 100644 --- a/src/fade_in.rs +++ b/src/fade_in.rs @@ -3,6 +3,7 @@ use crate::egui::{InnerResponse, Ui}; const FADE_DURATION: f64 = 0.6; +/// Fade in some children, assuming the fading in started at [`fade_start`]. pub fn fade_in<R>( ui: &mut Ui, fade_start: f64, diff --git a/src/governor.rs b/src/governor.rs index e9b8c62..d8078f6 100644 --- a/src/governor.rs +++ b/src/governor.rs @@ -1,9 +1,11 @@ +/// Good for rate-limiting certain things. #[derive(Default)] pub struct Governor { last: f64, } impl Governor { + /// Is the rate limit ready to allow another action? pub fn ready(&mut self, time: f64, limit: f64) -> bool { if time > self.last + limit { self.last = time; diff --git a/src/image.rs b/src/image.rs index 494bea1..35c4819 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,3 +1,4 @@ +/// Embed an image in the binary and load it at runtime. #[macro_export] macro_rules! img { ($name: literal) => {{ @@ -10,6 +11,7 @@ macro_rules! img { }}; } +/// Embed an image in the binary and load it into the [`Context`] at runtime. #[macro_export] macro_rules! ctx_img { ($ctx: expr, $name: literal) => {{ @@ -17,6 +19,7 @@ macro_rules! ctx_img { }}; } +/// Decodes image data. pub fn load_image_from_memory( image_data: &[u8], ) -> Result<eframe::egui::ColorImage, image::ImageError> { diff --git a/src/main.rs b/src/main.rs index 82c522b..13ed9d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,9 +23,11 @@ use eframe::egui::{Key, Style, TextStyle, Visuals}; use eframe::epi::{Frame, Storage}; use eframe::{egui, epi}; +// Entry point. fn main() { let app = Cartoon::default(); + // 16:9 aspect ratio. let size = egui::Vec2::new(1280f32, 720f32); let native_options = eframe::NativeOptions { @@ -45,9 +47,13 @@ fn main() { eframe::run_native(Box::new(app), native_options); } +/// Top-level state. pub struct Cartoon { + /// All slides (including their state). slides: Vec<Box<dyn Slide>>, + /// Current index into [`slides`]. slide_index: usize, + /// When we started fading in the current slide, in seconds (with respect to `ctx.input().time`). transition_time: f64, } @@ -61,6 +67,10 @@ impl Default for Cartoon { } } +/// Creates all the slides from default values. This will reset any and all animations and +/// transitions built into the slides. +/// +/// This is also how the chronology of the slideshow is determined. fn create_slides() -> Vec<Box<dyn Slide>> { vec![ Box::new(Title::default()) as Box<dyn Slide>, @@ -75,15 +85,20 @@ fn create_slides() -> Vec<Box<dyn Slide>> { } impl epi::App for Cartoon { + /// The title of the window, which is mostly irrelevant. fn name(&self) -> &str { "Generative Art Cartoon" } + /// Called once at window initialization. fn setup(&mut self, ctx: &egui::Context, _frame: &Frame, _storage: Option<&dyn Storage>) { + // Top level style overrides. let mut style = Style::default(); style.animation_time = 0.5; style.visuals = Visuals::light(); style.spacing.window_margin = Margin::same(24.0); + + // Increase font sizes a lot. { let header_style = style.text_styles.get_mut(&TextStyle::Heading).unwrap(); header_style.size = 48.0; @@ -96,6 +111,7 @@ impl epi::App for Cartoon { let small_style = style.text_styles.get_mut(&TextStyle::Small).unwrap(); small_style.size = 22.0; } + // TODO(finnb): This doesn't work. May have to fork syntax coloring code to fix it. /* { let monospaced_style = style.text_styles.get_mut(&TextStyle::Monospace).unwrap(); @@ -104,6 +120,7 @@ impl epi::App for Cartoon { */ ctx.set_style(style); + // Start fading in title slide now. self.transition_time = ctx.input().time; } @@ -123,9 +140,20 @@ impl epi::App for Cartoon { }); */ + // Go forward one slide (don't wait for intra-slide transitions). + // + // This will wrap around to the beginning. let force_advance = ctx.input().key_pressed(Key::ArrowRight) || ctx.input().key_pressed(Key::D); + + // Play the next intra-slide transition or, if there isn't one, then go forward one slide. + // + // This will wrap around to the beginning. let advance = force_advance || ctx.input().key_pressed(Key::Space); + + // Go backward one slide (don't wait for intra-slide transitions). + // + // This will wrap around to the end. let retreat = ctx.input().key_pressed(Key::ArrowLeft) || ctx.input().key_pressed(Key::A); if advance || retreat { @@ -133,6 +161,7 @@ impl epi::App for Cartoon { if force_advance || self.slides[self.slide_index].transition(ctx) { let new = (self.slide_index + 1) % self.slides.len(); if new == 0 { + // We wrapped around to the beginning, so create slides to reset transitions. self.slides = create_slides(); } self.transition_time = ctx.input().time; @@ -151,7 +180,9 @@ impl epi::App for Cartoon { } egui::CentralPanel::default().show(ctx, |ui| { + // The current slide may be fading in. fade_in(ui, self.transition_time, |ui| { + // Render the current slide. self.slides[self.slide_index].show(ui); }); }); diff --git a/src/slide.rs b/src/slide.rs index b2f7a81..bc69774 100644 --- a/src/slide.rs +++ b/src/slide.rs @@ -9,6 +9,7 @@ pub mod s6_computation; pub mod s7_mosaic; pub mod s8_conclusion; +/// An interface for all slides. pub trait Slide { /// Returns whether "done." /// Repaint automatically requested. -- GitLab