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

Add some documentation.

parent f068f4ef
No related branches found
No related tags found
No related merge requests found
......@@ -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,
......
/// See component directory.
pub mod code;
pub mod grid;
......@@ -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")
}
......@@ -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,
......
/// 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;
......
/// 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> {
......
......@@ -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);
});
});
......
......@@ -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.
......
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