mirror of
https://github.com/ricoriedel/wipe.git
synced 2024-11-22 16:06:38 +00:00
Add shift transform
This commit is contained in:
parent
32cd72552e
commit
74e83cd543
116
src/main.rs
116
src/main.rs
@ -1,5 +1,6 @@
|
||||
pub mod convert;
|
||||
pub mod pattern;
|
||||
pub mod transform;
|
||||
|
||||
mod error;
|
||||
mod printer;
|
||||
@ -17,6 +18,7 @@ pub use vec::*;
|
||||
|
||||
use crate::convert::*;
|
||||
use crate::pattern::*;
|
||||
use crate::transform::*;
|
||||
use clap::{Parser, ValueEnum};
|
||||
use crossterm::style::Color;
|
||||
use crossterm::style::Color::*;
|
||||
@ -43,6 +45,8 @@ struct Args {
|
||||
colors: Vec<PalletEnum>,
|
||||
#[clap(long, value_enum)]
|
||||
color_pattern: Vec<PatternEnum>,
|
||||
#[clap(long)]
|
||||
color_shift: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(ValueEnum, Clone)]
|
||||
@ -81,53 +85,23 @@ enum PatternEnum {
|
||||
Wheel,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
let args = Args::parse();
|
||||
let rand = &mut thread_rng();
|
||||
|
||||
let char: Box<dyn PatternFactory> = create_pattern(choose(args.char_pattern, rand));
|
||||
let color: Box<dyn PatternFactory> = create_pattern(choose(args.color_pattern, rand));
|
||||
let pallet = create_pallet(choose(args.colors, rand));
|
||||
|
||||
let sampler = SamplerFactoryImpl::new(char, color);
|
||||
let char_converter = CharConverterImpl::new(args.chars);
|
||||
let color_converter = ColorConverterImpl::new(pallet);
|
||||
let converter = ConverterImpl::new(char_converter, color_converter);
|
||||
let term = TerminalImpl::new(stdout());
|
||||
let printer = PrinterImpl::new(term)?;
|
||||
let renderer = RendererImpl::new(sampler, converter, printer);
|
||||
|
||||
let clock = ClockImpl::default();
|
||||
let duration = Duration::from_millis(args.duration);
|
||||
let delay = Duration::from_nanos(1_000_000_000 / args.fps);
|
||||
let timer = Timer::new(clock, duration, delay);
|
||||
|
||||
timer.run(renderer)
|
||||
#[derive(derive_more::Constructor)]
|
||||
struct PatternConfig<'a> {
|
||||
patterns: &'a Vec<PatternEnum>,
|
||||
shift: Option<bool>,
|
||||
}
|
||||
|
||||
fn choose<TValue: ValueEnum + Clone, TRand: Rng>(options: Vec<TValue>, rand: &mut TRand) -> TValue {
|
||||
if options.is_empty() {
|
||||
TValue::value_variants()
|
||||
.iter()
|
||||
.choose(rand)
|
||||
.unwrap()
|
||||
.clone()
|
||||
} else {
|
||||
options.iter().choose(rand).unwrap().clone()
|
||||
impl Args {
|
||||
fn char_config(&self) -> PatternConfig {
|
||||
PatternConfig::new(&self.char_pattern, Some(true))
|
||||
}
|
||||
}
|
||||
|
||||
fn create_pattern(pattern: PatternEnum) -> Box<dyn PatternFactory> {
|
||||
match pattern {
|
||||
PatternEnum::Circle => Box::new(CircleFactory::default()),
|
||||
PatternEnum::Line => Box::new(LineFactory::default()),
|
||||
PatternEnum::Rhombus => Box::new(RhombusFactory::default()),
|
||||
PatternEnum::Wheel => Box::new(WheelFactory::default()),
|
||||
fn color_config(&self) -> PatternConfig {
|
||||
PatternConfig::new(&self.color_pattern, self.color_shift)
|
||||
}
|
||||
}
|
||||
|
||||
fn create_pallet(colors: PalletEnum) -> Vec<Color> {
|
||||
match colors {
|
||||
fn pallet(&self, rand: &mut impl Rng) -> Vec<Color> {
|
||||
match choose(&self.colors, rand) {
|
||||
PalletEnum::Red => vec![DarkRed, Red, White],
|
||||
PalletEnum::Yellow => vec![DarkYellow, Yellow, White],
|
||||
PalletEnum::Green => vec![DarkGreen, Green, White],
|
||||
@ -160,4 +134,64 @@ fn create_pallet(colors: PalletEnum) -> Vec<Color> {
|
||||
|
||||
PalletEnum::Gray => vec![Black, DarkGrey, Grey, White],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> PatternConfig<'a> {
|
||||
fn create_base(&self, rand: &mut impl Rng) -> Box<dyn PatternFactory> {
|
||||
match choose(self.patterns, rand) {
|
||||
PatternEnum::Circle => Box::new(CircleFactory::default()),
|
||||
PatternEnum::Line => Box::new(LineFactory::default()),
|
||||
PatternEnum::Rhombus => Box::new(RhombusFactory::default()),
|
||||
PatternEnum::Wheel => Box::new(WheelFactory::default()),
|
||||
}
|
||||
}
|
||||
|
||||
fn create(&self, rand: &mut impl Rng) -> Box<dyn PatternFactory> {
|
||||
let mut pattern = self.create_base(rand);
|
||||
|
||||
if self.shift.unwrap_or(rand.gen()) {
|
||||
pattern = Box::new(ShiftFactory::new(pattern))
|
||||
}
|
||||
pattern
|
||||
}
|
||||
}
|
||||
|
||||
fn choose<TValue: ValueEnum + Clone, TRand: Rng>(
|
||||
options: &Vec<TValue>,
|
||||
rand: &mut TRand,
|
||||
) -> TValue {
|
||||
if options.is_empty() {
|
||||
TValue::value_variants()
|
||||
.iter()
|
||||
.choose(rand)
|
||||
.unwrap()
|
||||
.clone()
|
||||
} else {
|
||||
options.iter().choose(rand).unwrap().clone()
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
let args = Args::parse();
|
||||
let rand = &mut thread_rng();
|
||||
|
||||
let char = args.char_config().create(rand);
|
||||
let color = args.color_config().create(rand);
|
||||
let pallet = args.pallet(rand);
|
||||
|
||||
let sampler = SamplerFactoryImpl::new(char, color);
|
||||
let char_converter = CharConverterImpl::new(args.chars);
|
||||
let color_converter = ColorConverterImpl::new(pallet);
|
||||
let converter = ConverterImpl::new(char_converter, color_converter);
|
||||
let term = TerminalImpl::new(stdout());
|
||||
let printer = PrinterImpl::new(term)?;
|
||||
let renderer = RendererImpl::new(sampler, converter, printer);
|
||||
|
||||
let clock = ClockImpl::default();
|
||||
let duration = Duration::from_millis(args.duration);
|
||||
let delay = Duration::from_nanos(1_000_000_000 / args.fps);
|
||||
let timer = Timer::new(clock, duration, delay);
|
||||
|
||||
timer.run(renderer)
|
||||
}
|
||||
|
3
src/transform/mod.rs
Normal file
3
src/transform/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
mod shift;
|
||||
|
||||
pub use shift::*;
|
25
src/transform/shift.rs
Normal file
25
src/transform/shift.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use crate::pattern::*;
|
||||
use crate::Vector;
|
||||
|
||||
#[derive(derive_more::Constructor)]
|
||||
pub struct ShiftFactory {
|
||||
child: Box<dyn PatternFactory>,
|
||||
}
|
||||
|
||||
#[derive(derive_more::Constructor)]
|
||||
pub struct Shift {
|
||||
child: Box<dyn Pattern>,
|
||||
shift: f32,
|
||||
}
|
||||
|
||||
impl PatternFactory for ShiftFactory {
|
||||
fn create(&self, config: &Config) -> Box<dyn Pattern> {
|
||||
Box::new(Shift::new(self.child.create(config), config.step))
|
||||
}
|
||||
}
|
||||
|
||||
impl Pattern for Shift {
|
||||
fn sample(&self, pos: Vector) -> f32 {
|
||||
self.child.sample(pos) + 1.0 - 2.0 * self.shift
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user