Skip to content

Commit

Permalink
SweepIn generic over Leading and Trailing edge behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
junkdog committed Sep 21, 2024
1 parent 272341d commit 4a9359b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
6 changes: 4 additions & 2 deletions src/fx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ pub fn sweep_in<T: Into<EffectTimer>, C: Into<Color>>(
faded_color: C,
timer: T,
) -> Effect {
SweepIn::new(direction, gradient_length, randomness, faded_color.into(), timer.into())
SweepIn::<SolidColor, NoOpEdgeBehavior>::new(direction, gradient_length, randomness, faded_color.into(), timer.into())
.into_effect()
}

Expand Down Expand Up @@ -786,6 +786,7 @@ macro_rules! invoke_fn {
}

pub (crate) use invoke_fn;
use crate::fx::sliding_window_alpha::{NoOpEdgeBehavior, SolidColor};

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -892,6 +893,7 @@ mod tests {
// just to track the size of the shader structs somewhere
mod size_assertions {
use crate::fx::offscreen_buffer::OffscreenBuffer;
use crate::fx::sliding_window_alpha::{NoOpEdgeBehavior, SolidColor};
use crate::fx::translate::Translate;
use super::*;

Expand Down Expand Up @@ -921,7 +923,7 @@ mod size_assertions {
verify_size!(ShaderFn<()>, 104);
verify_size!(Sleep, 12);
verify_size!(SlideCell, 72);
verify_size!(SweepIn, 72);
verify_size!(SweepIn<SolidColor, NoOpEdgeBehavior>, 72);
verify_size!(TemporaryEffect, 32);
verify_size!(Translate, 72);
verify_size!(TranslateBuffer, 32);
Expand Down
34 changes: 34 additions & 0 deletions src/fx/sliding_window_alpha.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::ops::Range;
use ratatui::buffer::Cell;
use ratatui::layout::{Position, Rect};
use ratatui::style::Color;
use crate::fx::Direction;

pub struct SlidingWindowAlpha {
Expand Down Expand Up @@ -89,3 +91,35 @@ fn slide_left(
) -> f32 {
1.0 - slide_right(position, gradient)
}

pub(crate) trait EdgeBehavior {
fn apply(cell: &mut Cell) {
Self::apply_with_color(cell, Color::default());
}

fn apply_with_color(cell: &mut Cell, color_override: Color) {}
}

#[derive(Clone)]
pub(crate) struct NoOpEdgeBehavior;
#[derive(Clone)]
pub(crate) struct BgToFgColor;
#[derive(Clone)]
pub(crate) struct SolidColor;

impl EdgeBehavior for NoOpEdgeBehavior {}

impl EdgeBehavior for BgToFgColor {
fn apply_with_color(cell: &mut Cell, color_override: Color) {
cell.set_char(' ');
cell.fg = cell.bg;
cell.bg = color_override;
}
}

impl EdgeBehavior for SolidColor {
fn apply_with_color(cell: &mut Cell, color_override: Color) {
cell.fg = color_override;
cell.bg = color_override;
}
}
33 changes: 18 additions & 15 deletions src/fx/sweep_in.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
use std::marker::PhantomData;
use ratatui::buffer::Buffer;
use ratatui::layout::{Position, Rect};
use ratatui::prelude::Color;

use Interpolation::CircOut;

use crate::effect_timer::EffectTimer;
use crate::fx::sliding_window_alpha::SlidingWindowAlpha;
use crate::fx::sliding_window_alpha::{EdgeBehavior, SlidingWindowAlpha};
use crate::fx::{Direction, DirectionalVariance};
use crate::interpolation::{Interpolatable, Interpolation};
use crate::shader::Shader;
use crate::CellFilter;
use crate::{CellIterator, ColorMapper, Duration};

#[derive(Clone)]
pub struct SweepIn {
pub struct SweepIn<Leading: EdgeBehavior, Trailing: EdgeBehavior> {
gradient_length: u16,
randomness_extent: u16,
faded_color: Color,
timer: EffectTimer,
direction: Direction,
area: Option<Rect>,
cell_filter: CellFilter,
phantom_leading: PhantomData<Leading>,
phantom_trailing: PhantomData<Trailing>,
}


impl SweepIn {
impl<Leading: EdgeBehavior, Trailing: EdgeBehavior> SweepIn<Leading, Trailing> {
pub fn new(
direction: Direction,
gradient_length: u16,
Expand All @@ -40,11 +43,17 @@ impl SweepIn {
timer: if direction.flips_timer() { lifetime.reversed() } else { lifetime },
area: None,
cell_filter: CellFilter::All,
phantom_leading: PhantomData,
phantom_trailing: PhantomData,
}
}
}

impl Shader for SweepIn {
impl<Leading, Trailing> Shader for SweepIn<Leading, Trailing>
where
Leading: EdgeBehavior + Clone + 'static,
Trailing: EdgeBehavior + Clone + 'static
{
fn name(&self) -> &'static str {
if self.timer.is_reversed() ^ self.direction.flips_timer() {
"sweep_out"
Expand Down Expand Up @@ -80,11 +89,8 @@ impl Shader for SweepIn {
let cell = buf.cell_mut(pos).unwrap();

match window_alpha.alpha(offset(pos, row_variance)) {
0.0 => {
cell.set_fg(self.faded_color);
cell.set_bg(self.faded_color);
},
1.0 => {} // nothing to do
0.0 => Leading::apply_with_color(cell, self.faded_color),
1.0 => Trailing::apply(cell),
a => {
let fg = fg_mapper
.map(cell.fg, a, |c| self.faded_color.tween(&c, a, CircOut));
Expand All @@ -109,11 +115,8 @@ impl Shader for SweepIn {
let col_variance = (0, col_variances[(x - area.x) as usize]);

match window_alpha.alpha(offset(pos, col_variance)) {
0.0 => {
cell.set_fg(self.faded_color);
cell.set_bg(self.faded_color);
},
1.0 => {} // nothing to do
0.0 => Leading::apply_with_color(cell, self.faded_color),
1.0 => Trailing::apply(cell),
a => {
let fg = fg_mapper
.map(cell.fg, a, |c| self.faded_color.tween(&c, a, CircOut));
Expand Down Expand Up @@ -170,4 +173,4 @@ fn offset(p: Position, translate: (i16, i16)) -> Position {
x: (p.x as i16 + translate.0).max(0) as _,
y: (p.y as i16 + translate.1).max(0) as _,
}
}
}

0 comments on commit 4a9359b

Please sign in to comment.