From 64933b72137baa614dea3106243769f650af29e4 Mon Sep 17 00:00:00 2001 From: Nicolas <> Date: Fri, 8 Apr 2022 20:05:00 +0200 Subject: [PATCH] Add sampler --- src/main.rs | 1 + src/sampler.rs | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/sampler.rs diff --git a/src/main.rs b/src/main.rs index bdc8f6a..fd69c12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod vec; mod array; mod surface; mod animation; +mod sampler; #[derive(Parser)] #[clap(author = "Rico Riedel", version = "0.1.0", about = "Wipe your terminal with a random animation.")] diff --git a/src/sampler.rs b/src/sampler.rs new file mode 100644 index 0000000..3643fff --- /dev/null +++ b/src/sampler.rs @@ -0,0 +1,100 @@ +use crossterm::style::Color; +use crate::animation::Animation; +use crate::char::CharSampler; +use crate::color::ColorSampler; +use crate::fill::FillMode; +use crate::vec::Vector; + +pub enum Sample { + Keep, + Draw { char: char, color: Color }, + Clear, +} + +pub trait Sampler { + fn sample(&self, step: f32, pos: Vector) -> Sample; +} + +pub struct ComposedSampler { + animation: Box, + fill: Box, + color: Box, + char: Box, +} + +impl ComposedSampler { + pub fn new(animation: Box, + fill: Box, + color: Box, + char: Box) -> Self { + Self { animation, fill, color, char } + } +} + +impl Sampler for ComposedSampler { + fn sample(&self, step: f32, pos: Vector) -> Sample { + let level = self.animation.sample(step, pos); + + if level > 1.0 { + Sample::Keep + } else if level > 0.0 { + let char = self.char.sample(level); + let fill = self.fill.sample(level, pos); + let color = self.color.sample(fill); + + Sample::Draw { char, color } + } else { + Sample::Clear + } + } +} + +#[cfg(test)] +mod test { + use crate::char::SimpleCharSampler; + use super::*; + + struct MockAnimation; + struct MockFillMode; + struct MockColorSampler; + + impl Animation for MockAnimation { + fn sample(&self, step: f32, pos: Vector) -> f32 { + step + pos.x + pos.y + } + } + impl FillMode for MockFillMode { + fn sample(&self, level: f32, pos: Vector) -> f32 { + level + pos.x + pos.y + } + } + impl ColorSampler for MockColorSampler { + fn sample(&self, _: f32) -> Color { + Color::Green + } + } + + fn create_sampler() -> ComposedSampler { + let anim = Box::new(MockAnimation { }); + let fill = Box::new(MockFillMode { }); + let color = Box::new(MockColorSampler { }); + let char = Box::new(SimpleCharSampler::new("0123456789".to_string())); + + ComposedSampler::new(anim, fill, color, char) + } + + #[test] + fn sample_keep() { + assert!(matches!(create_sampler().sample(0.7, Vector::new(0.3, 0.1)), Sample::Keep)); + } + + #[test] + fn sample_draw() { + assert!(matches!(create_sampler().sample(0.4, Vector::new(0.1, 0.1)), Sample::Draw { char: '6', color: Color::Green })); + } + + #[test] + fn sample_clear() { + assert!(matches!(create_sampler().sample(-1.0, Vector::new(0.4, 0.5)), Sample::Clear)); + } +} \ No newline at end of file